59long appendToStringArray(
char ***item,
long items,
char *newItem) {
60 if (!(*item =
SDDS_Realloc(*item,
sizeof(**item) * (items + 1))))
61 SDDS_Bomb(
"allocation failure in appendToStringArray");
69long expandColumnPairNames(
SDDS_DATASET *SDDSin,
char ***name,
char ***errorName,
long names,
char **excludeName,
70 long excludeNames,
long typeMode,
long typeValue) {
71 long i, j, names1, errorNames1, names2;
72 char **name1, **errorName1, **name2, **errorName2;
76 name2 = errorName1 = errorName2 = NULL;
77 names2 = errorNames1 = 0;
78 for (i = 0; i < names; i++) {
81 case FIND_NUMERIC_TYPE:
82 case FIND_INTEGER_TYPE:
83 case FIND_FLOATING_TYPE:
84 names1 =
SDDS_MatchColumns(SDDSin, excludeNames ? NULL : &name1, SDDS_MATCH_STRING, typeMode,
85 (*name)[i], SDDS_0_PREVIOUS | SDDS_OR);
87 case FIND_SPECIFIED_TYPE:
89 SDDS_Bomb(
"invalid type value in expandColumnPairNames");
90 names1 =
SDDS_MatchColumns(SDDSin, excludeNames ? NULL : &name1, SDDS_MATCH_STRING, typeMode, typeValue,
91 (*name)[i], SDDS_0_PREVIOUS | SDDS_OR);
94 SDDS_Bomb(
"invalid typeMode in expandColumnPairNames");
102 SDDS_Bomb(
"unable to perform column name match in expandColumnPairNames");
106 for (j = 0; j < excludeNames; j++)
108 SDDS_MATCH_STRING, FIND_ANY_TYPE,
109 excludeName[j], SDDS_NEGATE_MATCH | SDDS_AND)))
114 moveToStringArray(&name2, &names2, name1, names1);
115 if (errorName && *errorName && (*errorName)[i]) {
116 if (strstr((*errorName)[i],
"%s")) {
117 if (!(errorName1 = (
char **)malloc(
sizeof(*errorName1) * names1)))
118 SDDS_Bomb(
"allocation failure in expandColumnPairNames");
119 errorNames1 = names1;
120 for (j = 0; j < names1; j++) {
121 if (!(errorName1[j] = (
char *)malloc(
sizeof(**errorName1) *
123 strlen((*errorName)[i]) + 1)))
124 SDDS_Bomb(
"allocation failure in expandColumnPairNames");
129 }
else if (names1 == 1) {
131 if (!(errorName1 = (
char **)malloc(
sizeof(*errorName1))))
132 SDDS_Bomb(
"allocation failure in expandColumnPairNames");
136 SDDS_Bomb(
"%s template must be employed with error names when primary name has wildcards");
138 moveToStringArray(&errorName2, &names2, errorName1, errorNames1);
150 *errorName = errorName2;
155void moveToStringArray(
char ***target,
long *targets,
char **source,
long sources) {
159 *target =
SDDS_Realloc(*target,
sizeof(**target) * (*targets + sources));
160 for (i = 0; i < sources; i++) {
161 for (j = 0; j < *targets; j++)
162 if (strcmp(source[i], (*target)[j]) == 0)
165 (*target)[j] = source[i];
171char *multiplyColumnUnits(
SDDS_DATASET *SDDSin,
char *name1,
char *name2) {
172 char buffer[SDDS_MAXLINE];
173 char *units1, *units2;
179 strcpy(buffer, units1);
184 strcat(buffer, units2);
189 strcpy(buffer, units2);
202char *divideColumnUnits(
SDDS_DATASET *SDDSin,
char *name1,
char *name2) {
203 char buffer[SDDS_MAXLINE];
204 char *units1, *units2;
210 strcpy(buffer, units1);
214 strcat(buffer,
"/(");
215 strcat(buffer, units2);
221 strcpy(buffer,
"1/(");
222 strcat(buffer, units2);
236long SDDS_CompareParameterValues(
void *param1,
void *param2,
long type) {
243 ddiff = *((
float *)param1) - *((
float *)param2);
244 return ddiff < 0 ? -1 : ddiff > 0 ? 1 : 0;
246 ddiff = *((
double *)param1) - *((
double *)param2);
247 return ddiff < 0 ? -1 : ddiff > 0 ? 1 : 0;
249 ldiff = *((int64_t *)param1) - *((int64_t *)param2);
250 return ldiff < 0 ? -1 : ldiff > 0 ? 1 : 0;
252 ldiff = *((int32_t *)param1) - *((int32_t *)param2);
253 return ldiff < 0 ? -1 : ldiff > 0 ? 1 : 0;
255 ldiff = *((
short *)param1) - *((
short *)param2);
256 return ldiff < 0 ? -1 : ldiff > 0 ? 1 : 0;
258 cdiff = (short)*((
char *)param1) - (short)*((
char *)param2);
259 return cdiff < 0 ? -1 : cdiff > 0 ? 1 : 0;
261 return strcmp(*(
char **)param1, *(
char **)param2);
263 SDDS_SetError(
"Problem doing data comparison--invalid data type (SDDS_CompareParameterValues)");
269char *makeFrequencyUnits(
SDDS_DATASET *SDDSin,
char *indepName) {
272 long reciprocal = 0, end;
278 end = strlen(timeUnits) - 1;
279 if (timeUnits[0] ==
'(' && timeUnits[end] ==
')') {
282 }
else if (timeUnits[0] ==
'1' && timeUnits[1] ==
'/' && timeUnits[2] ==
'(' && timeUnits[end] ==
')') {
285 reciprocal = !reciprocal;
291 units =
tmalloc(
sizeof(*units) * 1);
302 units =
tmalloc(
sizeof(*units) * (strlen(timeUnits) + 5));
303 if (strchr(timeUnits,
' '))
304 sprintf(units,
"1/(%s)", timeUnits);
306 sprintf(units,
"1/%s", timeUnits);
311int64_t greatestProductOfSmallPrimes(int64_t rows)
314 int64_t bestResult = 0, result, nPrimes;
315 static int64_t prime[MAXPRIMES] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
316 71, 73, 79, 83, 89, 97};
318 for (nPrimes = 1; nPrimes <= MAXPRIMES; nPrimes++) {
319 if ((result = greatestProductOfSmallPrimes1(rows, prime, nPrimes)) > bestResult &&
324 SDDS_Bomb(
"couldn't find acceptable number of rows for truncation/padding");
328int64_t greatestProductOfSmallPrimes1(int64_t rows, int64_t *primeList, int64_t nPrimes) {
329 int64_t iprime, product, remains, bestFactor = 0;
330 double remainder, smallestRemainder;
334 while (remains > 2) {
335 smallestRemainder = LONG_MAX;
336 for (iprime = 0; iprime < nPrimes; iprime++) {
337 remainder = remains - primeList[iprime] * ((long)(remains / primeList[iprime]));
338 if (remainder < smallestRemainder) {
339 smallestRemainder = remainder;
340 bestFactor = primeList[iprime];
345 remains /= bestFactor;
346 product *= bestFactor;
348 return product * remains;
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_GetColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified column in the SDDS dataset.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
int32_t SDDS_MatchColumns(SDDS_DATASET *SDDS_dataset, char ***nameReturn, int32_t matchMode, int32_t typeMode,...)
Matches and retrieves column names from an SDDS dataset based on specified criteria.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#define SDDS_VALID_TYPE(type)
Validates whether the given type identifier is within the defined range of SDDS types.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
int replace_stringn(char *t, char *s, char *orig, char *repl, long count_limit)
Replace a limited number of occurrences of one string with another string.
char * strslide(char *s, long distance)
Slides character data within a string by a specified distance.