SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddscast.c File Reference

Detailed Description

Converts numeric columns, parameters, or arrays in SDDS files from one datatype to another.

This program allows users to cast the data types of columns, parameters, or arrays within SDDS (Self Describing Data Sets) files. It supports various numeric types and provides options for handling warnings, piping input/output, and setting data ordering.

Usage

sddscast [<source-file>] [<target-file>]
[-pipe=[<input>][,<output>]]
[-noWarnings]
[-majorOrder=row|column]
-cast=<mode>,<columnNames>,<typeNames>,<newType>

Options

Required Description
-cast Cast data types of specified entities.
Optional Description
-pipe Specify input and/or output pipes.
-noWarnings Suppress warning messages. Useful for scripting or when warnings are not desired.
-majorOrder Set the column data ordering.

Option Constraints

  • The -cast option can be used multiple times to perform multiple casting operations.

Casting Details

  • <mode>: Specifies the type of entity to cast. Valid values are:
    • column: Casts data types of specified columns.
    • parameter: Casts data types of specified parameters.
    • array: Casts data types of specified arrays.
  • <columnNames>: Comma-separated list of entity names. Supports wildcards (*) for pattern matching.
  • <typeNames>: Comma-separated list of current data types corresponding to the entity names. If fewer types are provided than names, the last type is reused.
  • <newType>: The target data type for casting. Supported types are:
    • long, ulong, long64, ulong64, short, ushort, longdouble, double, float.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Authors
H. Shang, R. Soliday

Definition in file sddscast.c.

#include "mdb.h"
#include "SDDS.h"
#include "scan.h"

Go to the source code of this file.

Functions

void process_cast_columns (SDDS_DATASET *SDDS_input, CAST_NAME *column_cast, CAST_REQUEST *col_request, long col_requests, long noWarnings)
 
void process_cast_parameters (SDDS_DATASET *SDDS_input, CAST_NAME *parameter_cast, CAST_REQUEST *par_request, long par_requests, long noWarnings)
 
void process_cast_arrays (SDDS_DATASET *SDDS_input, CAST_NAME *array_cast, CAST_REQUEST *array_request, long array_requests, long noWarnings)
 
long add_casts (char *old_type, long sdds_type, CAST_NAME *cast_name, char *add_name, char *new_type, long index, long noWarnings)
 
void cleanUpCast (CAST_NAME *cast_name)
 
int main (int argc, char **argv)
 

Function Documentation

◆ add_casts()

long add_casts ( char * old_type,
long sdds_type,
CAST_NAME * cast_name,
char * add_name,
char * new_type,
long index,
long noWarnings )

Definition at line 556 of file sddscast.c.

556 {
557 long add = 0;
558
559 if (!(SDDS_NUMERIC_TYPE(sdds_type))) {
560 if (!noWarnings)
561 fprintf(stderr, "Warning: The type of '%s' is not numeric and cannot be cast to a numeric type.\n", add_name);
562 return 0;
563 }
564 if (strcmp(old_type, "*") == 0)
565 add = 1;
566 else {
567 switch (sdds_type) {
568 case SDDS_LONGDOUBLE:
569 if (strcmp(old_type, "longdouble") == 0)
570 add = 1;
571 break;
572 case SDDS_DOUBLE:
573 if (strcmp(old_type, "double") == 0)
574 add = 1;
575 break;
576 case SDDS_FLOAT:
577 if (strcmp(old_type, "float") == 0)
578 add = 1;
579 break;
580 case SDDS_LONG64:
581 if (strcmp(old_type, "long64") == 0)
582 add = 1;
583 break;
584 case SDDS_ULONG64:
585 if (strcmp(old_type, "ulong64") == 0)
586 add = 1;
587 break;
588 case SDDS_LONG:
589 if (strcmp(old_type, "long") == 0)
590 add = 1;
591 break;
592 case SDDS_ULONG:
593 if (strcmp(old_type, "ulong") == 0)
594 add = 1;
595 break;
596 case SDDS_SHORT:
597 if (strcmp(old_type, "short") == 0)
598 add = 1;
599 break;
600 case SDDS_USHORT:
601 if (strcmp(old_type, "ushort") == 0)
602 add = 1;
603 break;
604 default:
605 break;
606 }
607 }
608 if (!add) {
609 if (!noWarnings)
610 fprintf(stderr, "Warning: The type of '%s' does not match '%s'.\n", add_name, old_type);
611 return 0;
612 }
613 cast_name->name = SDDS_Realloc(cast_name->name, sizeof(char *) * (cast_name->n + 1));
614 cast_name->new_type = SDDS_Realloc(cast_name->new_type, sizeof(char *) * (cast_name->n + 1));
615 SDDS_CopyString(&cast_name->name[cast_name->n], add_name);
616 SDDS_CopyString(&cast_name->new_type[cast_name->n], new_type);
617 cast_name->index = SDDS_Realloc(cast_name->index, sizeof(long) * (cast_name->n + 1));
618 cast_name->index[cast_name->n] = index;
619 cast_name->n++;
620 return 1;
621}
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
Definition SDDS_utils.c:856
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
Definition SDDS_utils.c:677
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
Definition SDDStypes.h:67
#define SDDS_FLOAT
Identifier for the float data type.
Definition SDDStypes.h:43
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
Definition SDDStypes.h:55
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_SHORT
Identifier for the signed short integer data type.
Definition SDDStypes.h:73
#define SDDS_USHORT
Identifier for the unsigned short integer data type.
Definition SDDStypes.h:79
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
Definition SDDStypes.h:138
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
Definition SDDStypes.h:31
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
Definition SDDStypes.h:49

◆ cleanUpCast()

void cleanUpCast ( CAST_NAME * cast_name)

Definition at line 320 of file sddscast.c.

320 {
321 long i;
322 for (i = 0; i < cast_name->n; i++) {
323 free(cast_name->name[i]);
324 free(cast_name->new_type[i]);
325 cast_name->name[i] = NULL;
326 cast_name->new_type[i] = NULL;
327 }
328 free(cast_name->name);
329 free(cast_name->new_type);
330 free(cast_name->index);
331 cast_name->name = NULL;
332 cast_name->new_type = NULL;
333 cast_name->index = NULL;
334}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 148 of file sddscast.c.

148 {
149 SDDS_DATASET SDDS_input, SDDS_output;
150 SCANNED_ARG *s_arg;
151 char *inputfile, *outputfile;
152 long tmpfile_used, noWarnings, i, i_arg;
153 CAST_NAME column_cast, parameter_cast, array_cast;
154 CAST_REQUEST *col_request, *par_request, *array_request;
155 long col_requests, par_requests, array_requests, rows;
156 unsigned long pipeFlags, majorOrderFlag;
157 short columnMajorOrder = -1;
158
159 column_cast.n = parameter_cast.n = array_cast.n = 0;
160 column_cast.name = column_cast.new_type = NULL;
161 column_cast.index = parameter_cast.index = array_cast.index = 0;
162 parameter_cast.name = parameter_cast.new_type = NULL;
163 array_cast.name = array_cast.new_type = NULL;
164 col_request = par_request = array_request = NULL;
165 col_requests = par_requests = array_requests = rows = 0;
166
168 argc = scanargs(&s_arg, argc, argv);
169 if (argc < 3)
170 bomb(NULL, USAGE);
171
172 inputfile = outputfile = NULL;
173 noWarnings = tmpfile_used = pipeFlags = 0;
174
175 for (i_arg = 1; i_arg < argc; i_arg++) {
176 if (s_arg[i_arg].arg_type == OPTION) {
177 delete_chars(s_arg[i_arg].list[0], "_");
178 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
179 case SET_MAJOR_ORDER:
180 majorOrderFlag = 0;
181 s_arg[i_arg].n_items -= 1;
182 if (s_arg[i_arg].n_items > 0 &&
183 (!scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
184 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
185 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
186 SDDS_Bomb("invalid -majorOrder syntax/values");
187 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
188 columnMajorOrder = 1;
189 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
190 columnMajorOrder = 0;
191 break;
192 case SET_NOWARNINGS:
193 noWarnings = 1;
194 break;
195 case SET_PIPE:
196 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
197 SDDS_Bomb("invalid -pipe syntax");
198 break;
199 case SET_CAST:
200 if (s_arg[i_arg].n_items < 4)
201 SDDS_Bomb("invalid -cast syntax");
202 if (match_string(s_arg[i_arg].list[4], types, TYPES, MATCH_WHOLE_STRING) < 0) {
203 SDDS_Bomb("The new type to cast has to be a numeric type!");
204 }
205 switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
206 case COLUMN_MODE:
207 col_request = SDDS_Realloc(col_request, sizeof(*col_request) * (col_requests + 1));
208 col_request[col_requests].match_string = s_arg[i_arg].list[2];
209 col_request[col_requests].type_string = s_arg[i_arg].list[3];
210 col_request[col_requests].new_type = s_arg[i_arg].list[4];
211 col_requests++;
212 break;
213 case PARAMETER_MODE:
214 par_request = SDDS_Realloc(par_request, sizeof(*par_request) * (par_requests + 1));
215 par_request[par_requests].match_string = s_arg[i_arg].list[2];
216 par_request[par_requests].type_string = s_arg[i_arg].list[3];
217 par_request[par_requests].new_type = s_arg[i_arg].list[4];
218 par_requests++;
219 break;
220 case ARRAY_MODE:
221 array_request = SDDS_Realloc(array_request, sizeof(*array_request) * (array_requests + 1));
222 array_request[array_requests].match_string = s_arg[i_arg].list[2];
223 array_request[array_requests].type_string = s_arg[i_arg].list[3];
224 array_request[array_requests].new_type = s_arg[i_arg].list[4];
225 array_requests++;
226 break;
227 default:
228 SDDS_Bomb("unknown cast mode.");
229 break;
230 }
231 break;
232 default:
233 SDDS_Bomb("unknown option syntax.");
234 break;
235 }
236 } else {
237 if (inputfile == NULL)
238 inputfile = s_arg[i_arg].list[0];
239 else if (outputfile == NULL)
240 outputfile = s_arg[i_arg].list[0];
241 else {
242 fprintf(stderr, "Error: Too many filenames provided (sddscast).\n");
243 exit(EXIT_FAILURE);
244 }
245 }
246 }
247
248 processFilenames("sddscast", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used);
249
250 if (!SDDS_InitializeInput(&SDDS_input, inputfile)) {
251 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
252 exit(EXIT_FAILURE);
253 }
254 if (!SDDS_InitializeCopy(&SDDS_output, &SDDS_input, outputfile, "w")) {
255 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
256 exit(EXIT_FAILURE);
257 }
258 if (columnMajorOrder != -1)
259 SDDS_output.layout.data_mode.column_major = columnMajorOrder;
260 else
261 SDDS_output.layout.data_mode.column_major = SDDS_input.layout.data_mode.column_major;
262
263 process_cast_columns(&SDDS_input, &column_cast, col_request, col_requests, noWarnings);
264 process_cast_parameters(&SDDS_input, &parameter_cast, par_request, par_requests, noWarnings);
265 process_cast_arrays(&SDDS_input, &array_cast, array_request, array_requests, noWarnings);
266
267 for (i = 0; i < column_cast.n; i++) {
268 if (!SDDS_ChangeColumnInformation(&SDDS_output, "type", column_cast.new_type[i], SDDS_PASS_BY_STRING | SDDS_SET_BY_NAME, column_cast.name[i])) {
269 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
270 exit(EXIT_FAILURE);
271 }
272 }
273 for (i = 0; i < parameter_cast.n; i++) {
274 if (!SDDS_ChangeParameterInformation(&SDDS_output, "type", parameter_cast.new_type[i], SDDS_PASS_BY_STRING | SDDS_SET_BY_NAME, parameter_cast.name[i])) {
275 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
276 exit(EXIT_FAILURE);
277 }
278 }
279
280 for (i = 0; i < array_cast.n; i++) {
281 if (!SDDS_ChangeArrayInformation(&SDDS_output, "type", array_cast.new_type[i], SDDS_PASS_BY_STRING | SDDS_SET_BY_NAME, array_cast.name[i])) {
282 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
283 exit(EXIT_FAILURE);
284 }
285 }
286
287 if (!SDDS_WriteLayout(&SDDS_output))
288 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
289
290 while (SDDS_ReadPage(&SDDS_input) > 0) {
291 if (!SDDS_CopyPage(&SDDS_output, &SDDS_input) || !SDDS_WritePage(&SDDS_output)) {
292 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
293 exit(EXIT_FAILURE);
294 }
295 }
296
297 if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) {
298 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
299 exit(EXIT_FAILURE);
300 }
301
302 if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile))
303 exit(EXIT_FAILURE);
304
305 /* Free allocated memory */
306 cleanUpCast(&column_cast);
307 cleanUpCast(&parameter_cast);
308 cleanUpCast(&array_cast);
309 if (array_request)
310 free(array_request);
311 if (par_request)
312 free(par_request);
313 if (col_request)
314 free(col_request);
315
316 free_scanargs(&s_arg, argc);
317 return EXIT_SUCCESS;
318}
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
Definition SDDS_copy.c:40
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:578
int32_t SDDS_ChangeParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in a parameter definition within the SDDS dataset.
Definition SDDS_info.c:485
int32_t SDDS_ChangeArrayInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in an array definition within the SDDS dataset.
Definition SDDS_info.c:597
int32_t SDDS_ChangeColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in a column definition within the SDDS dataset.
Definition SDDS_info.c:364
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
char * delete_chars(char *s, char *t)
Removes all occurrences of characters found in string t from string s.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
Definition replacefile.c:75
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
void free_scanargs(SCANNED_ARG **scanned, int argc)
Definition scanargs.c:584
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.

◆ process_cast_arrays()

void process_cast_arrays ( SDDS_DATASET * SDDS_input,
CAST_NAME * array_cast,
CAST_REQUEST * array_request,
long array_requests,
long noWarnings )

Definition at line 485 of file sddscast.c.

485 {
486 char *ptr, **Names, **Match_string, **type_string;
487 long i, index, j, k, type, s1, s2;
488 int32_t names;
489
490 if (!array_requests)
491 return;
492 ptr = NULL;
493 names = s1 = s2 = 0;
494 Names = NULL;
495 Match_string = type_string = NULL;
496
497 if (!(Names = (char **)SDDS_GetArrayNames(SDDS_input, &names))) {
498 if (!noWarnings)
499 fprintf(stderr, "Warning: Unable to retrieve array names from the input file.\n");
500 return;
501 }
502 for (i = 0; i < array_requests; i++) {
503 s1 = s2 = 0;
504 ptr = strtok(array_request[i].match_string, ",");
505 while (ptr != NULL) {
506 Match_string = SDDS_Realloc(Match_string, sizeof(char *) * (s1 + 1));
507 SDDS_CopyString(&Match_string[s1], ptr);
508 s1++;
509 ptr = strtok(NULL, ",");
510 }
511 ptr = strtok(array_request[i].type_string, ",");
512 while (ptr != NULL) {
513 type_string = SDDS_Realloc(type_string, sizeof(char *) * (s2 + 1));
514 SDDS_CopyString(&type_string[s2], ptr);
515 s2++;
516 ptr = strtok(NULL, ",");
517 }
518 if (s2 < s1) {
519 for (j = s2; j < s1; j++) {
520 type_string = SDDS_Realloc(type_string, sizeof(char *) * (j + 1));
521 SDDS_CopyString(&type_string[j], type_string[s2 - 1]);
522 s2++;
523 }
524 }
525 for (k = 0; k < s1; k++) {
526 if (!has_wildcards(Match_string[k])) {
527 index = SDDS_GetArrayIndex(SDDS_input, Match_string[k]);
528 if (index < 0) {
529 if (!noWarnings)
530 fprintf(stderr, "Warning: Array '%s' does not exist in the input file.\n", Match_string[k]);
531 continue;
532 }
533 type = SDDS_GetArrayType(SDDS_input, index);
534 add_casts(type_string[k], type, array_cast, Match_string[k], array_request[i].new_type, index, noWarnings);
535 } else {
536 for (j = 0; j < names; j++) {
537 if (wild_match(Names[j], Match_string[k])) {
538 index = SDDS_GetArrayIndex(SDDS_input, Names[j]);
539 type = SDDS_GetArrayType(SDDS_input, index);
540 add_casts(type_string[k], type, array_cast, Names[j], array_request[i].new_type, index, noWarnings);
541 }
542 }
543 }
544 }
545 if (Match_string)
546 SDDS_FreeStringArray(Match_string, s1);
547 if (type_string)
548 SDDS_FreeStringArray(type_string, s2);
549 Match_string = type_string = NULL;
550 }
551 for (i = 0; i < names; i++)
552 free(Names[i]);
553 free(Names);
554}
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
int32_t SDDS_GetArrayIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named array in the SDDS dataset.
int32_t SDDS_GetArrayType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of an array in the SDDS dataset by its index.
char ** SDDS_GetArrayNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all arrays in the SDDS dataset.
int has_wildcards(char *template)
Check if a template string contains any wildcard characters.
Definition wild_match.c:498
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.
Definition wild_match.c:49

◆ process_cast_columns()

void process_cast_columns ( SDDS_DATASET * SDDS_input,
CAST_NAME * column_cast,
CAST_REQUEST * col_request,
long col_requests,
long noWarnings )

Definition at line 336 of file sddscast.c.

336 {
337 char *ptr, **Names, **Match_string, **type_string;
338 long i, index, j, k, type, s1, s2;
339 int32_t names;
340
341 if (!col_requests)
342 return;
343 ptr = NULL;
344 names = s1 = s2 = 0;
345 Names = NULL;
346 Match_string = type_string = NULL;
347
348 for (i = 0; i < col_requests; i++) {
349 s1 = s2 = 0;
350 ptr = strtok(col_request[i].match_string, ",");
351 while (ptr != NULL) {
352 Match_string = SDDS_Realloc(Match_string, sizeof(char *) * (s1 + 1));
353 SDDS_CopyString(&Match_string[s1], ptr);
354 s1++;
355 ptr = strtok(NULL, ",");
356 }
357 ptr = strtok(col_request[i].type_string, ",");
358 while (ptr != NULL) {
359 type_string = SDDS_Realloc(type_string, sizeof(char *) * (s2 + 1));
360 SDDS_CopyString(&type_string[s2], ptr);
361 s2++;
362 ptr = strtok(NULL, ",");
363 }
364 if (s2 < s1) {
365 for (j = s2; j < s1; j++) {
366 type_string = SDDS_Realloc(type_string, sizeof(char *) * (j + 1));
367 SDDS_CopyString(&type_string[j], type_string[s2 - 1]);
368 s2++;
369 }
370 }
371 for (k = 0; k < s1; k++) {
372 if (!has_wildcards(Match_string[k])) {
373 index = SDDS_GetColumnIndex(SDDS_input, Match_string[k]);
374 if (index < 0) {
375 if (!noWarnings)
376 fprintf(stderr, "Warning: Column '%s' does not exist.\n", Match_string[k]);
377 continue;
378 }
379 type = SDDS_GetColumnType(SDDS_input, index);
380 add_casts(type_string[k], type, column_cast, Match_string[k], col_request[i].new_type, index, noWarnings);
381 } else {
382 SDDS_SetColumnFlags(SDDS_input, 0);
383 if (!SDDS_SetColumnsOfInterest(SDDS_input, SDDS_MATCH_STRING, Match_string[k], SDDS_OR))
384 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
385 if (!(Names = (char **)SDDS_GetColumnNames(SDDS_input, &names))) {
386 if (!noWarnings)
387 fprintf(stderr, "Warning: No columns match the pattern '%s'.\n", Match_string[k]);
388 } else {
389 for (j = 0; j < names; j++) {
390 index = SDDS_GetColumnIndex(SDDS_input, Names[j]);
391 type = SDDS_GetColumnType(SDDS_input, index);
392 add_casts(type_string[k], type, column_cast, Names[j], col_request[i].new_type, index, noWarnings);
393 free(Names[j]);
394 }
395 free(Names);
396 Names = NULL;
397 }
398 }
399 }
400 if (Match_string)
401 SDDS_FreeStringArray(Match_string, s1);
402 if (type_string)
403 SDDS_FreeStringArray(type_string, s2);
404 Match_string = type_string = NULL;
405 }
406}
int32_t SDDS_SetColumnsOfInterest(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Sets the acceptance flags for columns based on specified naming criteria.
int32_t SDDS_SetColumnFlags(SDDS_DATASET *SDDS_dataset, int32_t column_flag_value)
Sets the acceptance flags for all columns in the current data table of a data set.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.

◆ process_cast_parameters()

void process_cast_parameters ( SDDS_DATASET * SDDS_input,
CAST_NAME * parameter_cast,
CAST_REQUEST * par_request,
long par_requests,
long noWarnings )

Definition at line 408 of file sddscast.c.

408 {
409 char *ptr, **Names, **Match_string, **type_string;
410 long i, index, j, k, type, s1, s2;
411 int32_t names;
412
413 if (!par_requests)
414 return;
415 ptr = NULL;
416 names = s1 = s2 = 0;
417 Names = NULL;
418 Match_string = type_string = NULL;
419
420 if (!(Names = (char **)SDDS_GetParameterNames(SDDS_input, &names))) {
421 if (!noWarnings)
422 fprintf(stderr, "Warning: No parameters found in the input file.\n");
423 return;
424 }
425 for (i = 0; i < par_requests; i++) {
426 s1 = s2 = 0;
427 ptr = strtok(par_request[i].match_string, ",");
428 while (ptr != NULL) {
429 Match_string = SDDS_Realloc(Match_string, sizeof(char *) * (s1 + 1));
430 SDDS_CopyString(&Match_string[s1], ptr);
431 s1++;
432 ptr = strtok(NULL, ",");
433 }
434 ptr = strtok(par_request[i].type_string, ",");
435 while (ptr != NULL) {
436 type_string = SDDS_Realloc(type_string, sizeof(char *) * (s2 + 1));
437 SDDS_CopyString(&type_string[s2], ptr);
438 s2++;
439 ptr = strtok(NULL, ",");
440 }
441 if (s2 < s1) {
442 for (j = s2; j < s1; j++) {
443 type_string = SDDS_Realloc(type_string, sizeof(char *) * (j + 1));
444 SDDS_CopyString(&type_string[j], type_string[s2 - 1]);
445 s2++;
446 }
447 }
448 for (k = 0; k < s1; k++) {
449 if (!has_wildcards(Match_string[k])) {
450 index = SDDS_GetParameterIndex(SDDS_input, Match_string[k]);
451 if (index < 0) {
452 if (!noWarnings)
453 fprintf(stderr, "Warning: Parameter '%s' does not exist in the input file.\n", Match_string[k]);
454 continue;
455 }
456 type = SDDS_GetParameterType(SDDS_input, index);
457 add_casts(type_string[k], type, parameter_cast, Match_string[k], par_request[i].new_type, index, noWarnings);
458 } else {
459 for (j = 0; j < names; j++) {
460 if (wild_match(Names[j], Match_string[k])) {
461 index = SDDS_GetParameterIndex(SDDS_input, Names[j]);
462 type = SDDS_GetParameterType(SDDS_input, index);
463 add_casts(type_string[k], type, parameter_cast, Names[j], par_request[i].new_type, index, noWarnings);
464 }
465 }
466 }
467 }
468 if (Match_string) {
469 for (j = 0; j < s1; j++)
470 free(Match_string[j]);
471 free(Match_string);
472 }
473 if (type_string) {
474 for (j = 0; j < s2; j++)
475 free(type_string[j]);
476 free(type_string);
477 }
478 Match_string = type_string = NULL;
479 }
480 for (i = 0; i < names; i++)
481 free(Names[i]);
482 free(Names);
483}
int32_t SDDS_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
char ** SDDS_GetParameterNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all parameters in the SDDS dataset.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.