SDDSlib
Loading...
Searching...
No Matches
sddscast.c File Reference

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

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

Go to the source code of this file.

Classes

struct  CAST_NAME
 
struct  CAST_REQUEST
 

Macros

#define COLUMN_MODE   0
 
#define PARAMETER_MODE   1
 
#define ARRAY_MODE   2
 
#define MODES   3
 
#define CLO_SHORT   0
 
#define CLO_USHORT   1
 
#define CLO_LONG   2
 
#define CLO_ULONG   3
 
#define CLO_LONG64   4
 
#define CLO_ULONG64   5
 
#define CLO_FLOAT   6
 
#define CLO_DOUBLE   7
 
#define CLO_LONGDOUBLE   8
 
#define TYPES   9
 

Enumerations

enum  option_type {
  SET_CAST , SET_NOWARNINGS , SET_PIPE , SET_MAJOR_ORDER ,
  N_OPTIONS
}
 

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)
 

Variables

static char * mode_name [MODES]
 
char * option [N_OPTIONS]
 
char * types [TYPES]
 
char * USAGE
 

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
-cast=<mode>,<columnNames>,<typeNames>,<newType>

Arguments:

  • <source-file>: Path to the input SDDS file. If omitted, standard input is used.
  • <target-file>: Path to the output SDDS file. If omitted, standard output is used.
  • -pipe=<input>,<output>: Specify input and/or output pipes.
  • -noWarnings: Suppress warning messages.
  • -cast=<mode>,<columnNames>,<typeNames>,<newType>:
    • <mode>: One of column, parameter, or array.
    • <columnNames>: Comma-separated list of column/parameter/array names, optionally with wildcards or enclosed in parentheses.
    • <typeNames>: Comma-separated list of current types corresponding to <columnNames>. Supports types like long, ulong, long64, ulong64, short, ushort, longdouble, double, float, or * for any type.
    • <newType>: The target datatype. Must be one of long, ulong, long64, ulong64, short, ushort, longdouble, double, or float.

Examples

  • Cast specific columns to double:
    sddscast input.sdds output.sdds -cast=column,col1,col2,long,double
  • Suppress warnings and use pipes for input/output:
    sddscast -pipe=stdin,stdout -noWarnings -cast=parameter,param1,double,float
Note
Ensure that the specified <newType> is compatible with the original datatype to prevent data loss or corruption.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
H. Shang, R. Soliday

Definition in file sddscast.c.

Macro Definition Documentation

◆ ARRAY_MODE

#define ARRAY_MODE   2

Definition at line 57 of file sddscast.c.

◆ CLO_DOUBLE

#define CLO_DOUBLE   7

Definition at line 89 of file sddscast.c.

◆ CLO_FLOAT

#define CLO_FLOAT   6

Definition at line 88 of file sddscast.c.

◆ CLO_LONG

#define CLO_LONG   2

Definition at line 84 of file sddscast.c.

◆ CLO_LONG64

#define CLO_LONG64   4

Definition at line 86 of file sddscast.c.

◆ CLO_LONGDOUBLE

#define CLO_LONGDOUBLE   8

Definition at line 90 of file sddscast.c.

◆ CLO_SHORT

#define CLO_SHORT   0

Definition at line 82 of file sddscast.c.

◆ CLO_ULONG

#define CLO_ULONG   3

Definition at line 85 of file sddscast.c.

◆ CLO_ULONG64

#define CLO_ULONG64   5

Definition at line 87 of file sddscast.c.

◆ CLO_USHORT

#define CLO_USHORT   1

Definition at line 83 of file sddscast.c.

◆ COLUMN_MODE

#define COLUMN_MODE   0

Definition at line 55 of file sddscast.c.

◆ MODES

#define MODES   3

Definition at line 58 of file sddscast.c.

◆ PARAMETER_MODE

#define PARAMETER_MODE   1

Definition at line 56 of file sddscast.c.

◆ TYPES

#define TYPES   9

Definition at line 91 of file sddscast.c.

Enumeration Type Documentation

◆ option_type

enum option_type

Definition at line 67 of file sddscast.c.

67 {
68 SET_CAST,
69 SET_NOWARNINGS,
70 SET_PIPE,
71 SET_MAJOR_ORDER,
72 N_OPTIONS
73};

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 552 of file sddscast.c.

552 {
553 long add = 0;
554
555 if (!(SDDS_NUMERIC_TYPE(sdds_type))) {
556 if (!noWarnings)
557 fprintf(stderr, "Warning: The type of '%s' is not numeric and cannot be cast to a numeric type.\n", add_name);
558 return 0;
559 }
560 if (strcmp(old_type, "*") == 0)
561 add = 1;
562 else {
563 switch (sdds_type) {
564 case SDDS_LONGDOUBLE:
565 if (strcmp(old_type, "longdouble") == 0)
566 add = 1;
567 break;
568 case SDDS_DOUBLE:
569 if (strcmp(old_type, "double") == 0)
570 add = 1;
571 break;
572 case SDDS_FLOAT:
573 if (strcmp(old_type, "float") == 0)
574 add = 1;
575 break;
576 case SDDS_LONG64:
577 if (strcmp(old_type, "long64") == 0)
578 add = 1;
579 break;
580 case SDDS_ULONG64:
581 if (strcmp(old_type, "ulong64") == 0)
582 add = 1;
583 break;
584 case SDDS_LONG:
585 if (strcmp(old_type, "long") == 0)
586 add = 1;
587 break;
588 case SDDS_ULONG:
589 if (strcmp(old_type, "ulong") == 0)
590 add = 1;
591 break;
592 case SDDS_SHORT:
593 if (strcmp(old_type, "short") == 0)
594 add = 1;
595 break;
596 case SDDS_USHORT:
597 if (strcmp(old_type, "ushort") == 0)
598 add = 1;
599 break;
600 default:
601 break;
602 }
603 }
604 if (!add) {
605 if (!noWarnings)
606 fprintf(stderr, "Warning: The type of '%s' does not match '%s'.\n", add_name, old_type);
607 return 0;
608 }
609 cast_name->name = SDDS_Realloc(cast_name->name, sizeof(char *) * (cast_name->n + 1));
610 cast_name->new_type = SDDS_Realloc(cast_name->new_type, sizeof(char *) * (cast_name->n + 1));
611 SDDS_CopyString(&cast_name->name[cast_name->n], add_name);
612 SDDS_CopyString(&cast_name->new_type[cast_name->n], new_type);
613 cast_name->index = SDDS_Realloc(cast_name->index, sizeof(long) * (cast_name->n + 1));
614 cast_name->index[cast_name->n] = index;
615 cast_name->n++;
616 return 1;
617}
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 316 of file sddscast.c.

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

◆ main()

int main ( int argc,
char ** argv )

Definition at line 144 of file sddscast.c.

144 {
145 SDDS_DATASET SDDS_input, SDDS_output;
146 SCANNED_ARG *s_arg;
147 char *inputfile, *outputfile;
148 long tmpfile_used, noWarnings, i, i_arg;
149 CAST_NAME column_cast, parameter_cast, array_cast;
150 CAST_REQUEST *col_request, *par_request, *array_request;
151 long col_requests, par_requests, array_requests, rows;
152 unsigned long pipeFlags, majorOrderFlag;
153 short columnMajorOrder = -1;
154
155 column_cast.n = parameter_cast.n = array_cast.n = 0;
156 column_cast.name = column_cast.new_type = NULL;
157 column_cast.index = parameter_cast.index = array_cast.index = 0;
158 parameter_cast.name = parameter_cast.new_type = NULL;
159 array_cast.name = array_cast.new_type = NULL;
160 col_request = par_request = array_request = NULL;
161 col_requests = par_requests = array_requests = rows = 0;
162
164 argc = scanargs(&s_arg, argc, argv);
165 if (argc < 3)
166 bomb(NULL, USAGE);
167
168 inputfile = outputfile = NULL;
169 noWarnings = tmpfile_used = pipeFlags = 0;
170
171 for (i_arg = 1; i_arg < argc; i_arg++) {
172 if (s_arg[i_arg].arg_type == OPTION) {
173 delete_chars(s_arg[i_arg].list[0], "_");
174 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
175 case SET_MAJOR_ORDER:
176 majorOrderFlag = 0;
177 s_arg[i_arg].n_items -= 1;
178 if (s_arg[i_arg].n_items > 0 &&
179 (!scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
180 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
181 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
182 SDDS_Bomb("invalid -majorOrder syntax/values");
183 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
184 columnMajorOrder = 1;
185 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
186 columnMajorOrder = 0;
187 break;
188 case SET_NOWARNINGS:
189 noWarnings = 1;
190 break;
191 case SET_PIPE:
192 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
193 SDDS_Bomb("invalid -pipe syntax");
194 break;
195 case SET_CAST:
196 if (s_arg[i_arg].n_items < 4)
197 SDDS_Bomb("invalid -cast syntax");
198 if (match_string(s_arg[i_arg].list[4], types, TYPES, MATCH_WHOLE_STRING) < 0) {
199 SDDS_Bomb("The new type to cast has to be a numeric type!");
200 }
201 switch (match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
202 case COLUMN_MODE:
203 col_request = SDDS_Realloc(col_request, sizeof(*col_request) * (col_requests + 1));
204 col_request[col_requests].match_string = s_arg[i_arg].list[2];
205 col_request[col_requests].type_string = s_arg[i_arg].list[3];
206 col_request[col_requests].new_type = s_arg[i_arg].list[4];
207 col_requests++;
208 break;
209 case PARAMETER_MODE:
210 par_request = SDDS_Realloc(par_request, sizeof(*par_request) * (par_requests + 1));
211 par_request[par_requests].match_string = s_arg[i_arg].list[2];
212 par_request[par_requests].type_string = s_arg[i_arg].list[3];
213 par_request[par_requests].new_type = s_arg[i_arg].list[4];
214 par_requests++;
215 break;
216 case ARRAY_MODE:
217 array_request = SDDS_Realloc(array_request, sizeof(*array_request) * (array_requests + 1));
218 array_request[array_requests].match_string = s_arg[i_arg].list[2];
219 array_request[array_requests].type_string = s_arg[i_arg].list[3];
220 array_request[array_requests].new_type = s_arg[i_arg].list[4];
221 array_requests++;
222 break;
223 default:
224 SDDS_Bomb("unknown cast mode.");
225 break;
226 }
227 break;
228 default:
229 SDDS_Bomb("unknown option syntax.");
230 break;
231 }
232 } else {
233 if (inputfile == NULL)
234 inputfile = s_arg[i_arg].list[0];
235 else if (outputfile == NULL)
236 outputfile = s_arg[i_arg].list[0];
237 else {
238 fprintf(stderr, "Error: Too many filenames provided (sddscast).\n");
239 exit(EXIT_FAILURE);
240 }
241 }
242 }
243
244 processFilenames("sddscast", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used);
245
246 if (!SDDS_InitializeInput(&SDDS_input, inputfile)) {
247 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
248 exit(EXIT_FAILURE);
249 }
250 if (!SDDS_InitializeCopy(&SDDS_output, &SDDS_input, outputfile, "w")) {
251 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
252 exit(EXIT_FAILURE);
253 }
254 if (columnMajorOrder != -1)
255 SDDS_output.layout.data_mode.column_major = columnMajorOrder;
256 else
257 SDDS_output.layout.data_mode.column_major = SDDS_input.layout.data_mode.column_major;
258
259 process_cast_columns(&SDDS_input, &column_cast, col_request, col_requests, noWarnings);
260 process_cast_parameters(&SDDS_input, &parameter_cast, par_request, par_requests, noWarnings);
261 process_cast_arrays(&SDDS_input, &array_cast, array_request, array_requests, noWarnings);
262
263 for (i = 0; i < column_cast.n; i++) {
264 if (!SDDS_ChangeColumnInformation(&SDDS_output, "type", column_cast.new_type[i], SDDS_PASS_BY_STRING | SDDS_SET_BY_NAME, column_cast.name[i])) {
265 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
266 exit(EXIT_FAILURE);
267 }
268 }
269 for (i = 0; i < parameter_cast.n; i++) {
270 if (!SDDS_ChangeParameterInformation(&SDDS_output, "type", parameter_cast.new_type[i], SDDS_PASS_BY_STRING | SDDS_SET_BY_NAME, parameter_cast.name[i])) {
271 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
272 exit(EXIT_FAILURE);
273 }
274 }
275
276 for (i = 0; i < array_cast.n; i++) {
277 if (!SDDS_ChangeArrayInformation(&SDDS_output, "type", array_cast.new_type[i], SDDS_PASS_BY_STRING | SDDS_SET_BY_NAME, array_cast.name[i])) {
278 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
279 exit(EXIT_FAILURE);
280 }
281 }
282
283 if (!SDDS_WriteLayout(&SDDS_output))
284 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
285
286 while (SDDS_ReadPage(&SDDS_input) > 0) {
287 if (!SDDS_CopyPage(&SDDS_output, &SDDS_input) || !SDDS_WritePage(&SDDS_output)) {
288 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
289 exit(EXIT_FAILURE);
290 }
291 }
292
293 if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) {
294 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
295 exit(EXIT_FAILURE);
296 }
297
298 if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile))
299 exit(EXIT_FAILURE);
300
301 /* Free allocated memory */
302 cleanUpCast(&column_cast);
303 cleanUpCast(&parameter_cast);
304 cleanUpCast(&array_cast);
305 if (array_request)
306 free(array_request);
307 if (par_request)
308 free(par_request);
309 if (col_request)
310 free(col_request);
311
312 free_scanargs(&s_arg, argc);
313 return EXIT_SUCCESS;
314}
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 481 of file sddscast.c.

481 {
482 char *ptr, **Names, **Match_string, **type_string;
483 long i, index, j, k, type, s1, s2;
484 int32_t names;
485
486 if (!array_requests)
487 return;
488 ptr = NULL;
489 names = s1 = s2 = 0;
490 Names = NULL;
491 Match_string = type_string = NULL;
492
493 if (!(Names = (char **)SDDS_GetArrayNames(SDDS_input, &names))) {
494 if (!noWarnings)
495 fprintf(stderr, "Warning: Unable to retrieve array names from the input file.\n");
496 return;
497 }
498 for (i = 0; i < array_requests; i++) {
499 s1 = s2 = 0;
500 ptr = strtok(array_request[i].match_string, ",");
501 while (ptr != NULL) {
502 Match_string = SDDS_Realloc(Match_string, sizeof(char *) * (s1 + 1));
503 SDDS_CopyString(&Match_string[s1], ptr);
504 s1++;
505 ptr = strtok(NULL, ",");
506 }
507 ptr = strtok(array_request[i].type_string, ",");
508 while (ptr != NULL) {
509 type_string = SDDS_Realloc(type_string, sizeof(char *) * (s2 + 1));
510 SDDS_CopyString(&type_string[s2], ptr);
511 s2++;
512 ptr = strtok(NULL, ",");
513 }
514 if (s2 < s1) {
515 for (j = s2; j < s1; j++) {
516 type_string = SDDS_Realloc(type_string, sizeof(char *) * (j + 1));
517 SDDS_CopyString(&type_string[j], type_string[s2 - 1]);
518 s2++;
519 }
520 }
521 for (k = 0; k < s1; k++) {
522 if (!has_wildcards(Match_string[k])) {
523 index = SDDS_GetArrayIndex(SDDS_input, Match_string[k]);
524 if (index < 0) {
525 if (!noWarnings)
526 fprintf(stderr, "Warning: Array '%s' does not exist in the input file.\n", Match_string[k]);
527 continue;
528 }
529 type = SDDS_GetArrayType(SDDS_input, index);
530 add_casts(type_string[k], type, array_cast, Match_string[k], array_request[i].new_type, index, noWarnings);
531 } else {
532 for (j = 0; j < names; j++) {
533 if (wild_match(Names[j], Match_string[k])) {
534 index = SDDS_GetArrayIndex(SDDS_input, Names[j]);
535 type = SDDS_GetArrayType(SDDS_input, index);
536 add_casts(type_string[k], type, array_cast, Names[j], array_request[i].new_type, index, noWarnings);
537 }
538 }
539 }
540 }
541 if (Match_string)
542 SDDS_FreeStringArray(Match_string, s1);
543 if (type_string)
544 SDDS_FreeStringArray(type_string, s2);
545 Match_string = type_string = NULL;
546 }
547 for (i = 0; i < names; i++)
548 free(Names[i]);
549 free(Names);
550}
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 332 of file sddscast.c.

332 {
333 char *ptr, **Names, **Match_string, **type_string;
334 long i, index, j, k, type, s1, s2;
335 int32_t names;
336
337 if (!col_requests)
338 return;
339 ptr = NULL;
340 names = s1 = s2 = 0;
341 Names = NULL;
342 Match_string = type_string = NULL;
343
344 for (i = 0; i < col_requests; i++) {
345 s1 = s2 = 0;
346 ptr = strtok(col_request[i].match_string, ",");
347 while (ptr != NULL) {
348 Match_string = SDDS_Realloc(Match_string, sizeof(char *) * (s1 + 1));
349 SDDS_CopyString(&Match_string[s1], ptr);
350 s1++;
351 ptr = strtok(NULL, ",");
352 }
353 ptr = strtok(col_request[i].type_string, ",");
354 while (ptr != NULL) {
355 type_string = SDDS_Realloc(type_string, sizeof(char *) * (s2 + 1));
356 SDDS_CopyString(&type_string[s2], ptr);
357 s2++;
358 ptr = strtok(NULL, ",");
359 }
360 if (s2 < s1) {
361 for (j = s2; j < s1; j++) {
362 type_string = SDDS_Realloc(type_string, sizeof(char *) * (j + 1));
363 SDDS_CopyString(&type_string[j], type_string[s2 - 1]);
364 s2++;
365 }
366 }
367 for (k = 0; k < s1; k++) {
368 if (!has_wildcards(Match_string[k])) {
369 index = SDDS_GetColumnIndex(SDDS_input, Match_string[k]);
370 if (index < 0) {
371 if (!noWarnings)
372 fprintf(stderr, "Warning: Column '%s' does not exist.\n", Match_string[k]);
373 continue;
374 }
375 type = SDDS_GetColumnType(SDDS_input, index);
376 add_casts(type_string[k], type, column_cast, Match_string[k], col_request[i].new_type, index, noWarnings);
377 } else {
378 SDDS_SetColumnFlags(SDDS_input, 0);
379 if (!SDDS_SetColumnsOfInterest(SDDS_input, SDDS_MATCH_STRING, Match_string[k], SDDS_OR))
380 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
381 if (!(Names = (char **)SDDS_GetColumnNames(SDDS_input, &names))) {
382 if (!noWarnings)
383 fprintf(stderr, "Warning: No columns match the pattern '%s'.\n", Match_string[k]);
384 } else {
385 for (j = 0; j < names; j++) {
386 index = SDDS_GetColumnIndex(SDDS_input, Names[j]);
387 type = SDDS_GetColumnType(SDDS_input, index);
388 add_casts(type_string[k], type, column_cast, Names[j], col_request[i].new_type, index, noWarnings);
389 free(Names[j]);
390 }
391 free(Names);
392 Names = NULL;
393 }
394 }
395 }
396 if (Match_string)
397 SDDS_FreeStringArray(Match_string, s1);
398 if (type_string)
399 SDDS_FreeStringArray(type_string, s2);
400 Match_string = type_string = NULL;
401 }
402}
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 404 of file sddscast.c.

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

Variable Documentation

◆ mode_name

char* mode_name[MODES]
static
Initial value:
= {
"column",
"parameter",
"array",
}

Definition at line 60 of file sddscast.c.

60 {
61 "column",
62 "parameter",
63 "array",
64};

◆ option

char* option[N_OPTIONS]
Initial value:
= {
"cast",
"nowarnings",
"pipe",
"majorOrder",
}

Definition at line 75 of file sddscast.c.

75 {
76 "cast",
77 "nowarnings",
78 "pipe",
79 "majorOrder",
80};

◆ types

char* types[TYPES]
Initial value:
= {
"short",
"ushort",
"long",
"ulong",
"long64",
"ulong64",
"float",
"double",
"longdouble",
}

Definition at line 93 of file sddscast.c.

93 {
94 "short",
95 "ushort",
96 "long",
97 "ulong",
98 "long64",
99 "ulong64",
100 "float",
101 "double",
102 "longdouble",
103};

◆ USAGE

char* USAGE
Initial value:
=
"Usage: sddscast [<source-file>] [<target-file>] \n"
" [-pipe=<input>,<output>] \n"
" -noWarnings \n"
" -cast=<mode>,<columnNames>,<typeNames>,<newType>\n\n"
"Options:\n"
" -pipe=<input>,<output> Specify input and/or output pipes.\n"
" -noWarnings Suppress warning messages.\n"
" -cast=<mode>,<names>,<types>,<newType>\n"
" Cast datatypes of specified columns, parameters, or arrays.\n"
" <mode>: one of 'column', 'parameter', or 'array'.\n"
" <names>: Comma-separated list of names (supports wildcards).\n"
" <types>: Comma-separated list of current types.\n"
" <newType>: Target datatype (long, ulong, long64, ulong64, short, ushort,\n"
" longdouble, double, float).\n\n"
"Description:\n"
" sddscast converts the numeric columns, parameters, or arrays from one datatype to another within an SDDS file.\n\n"
"Author: Hairong Shang\n"
"Compiled on " __DATE__ " at " __TIME__ ", SVN revision: " SVN_VERSION "\n"

Definition at line 118 of file sddscast.c.