55char *option[N_OPTIONS] =
62 "sddsbinarystring [<source-file>] [<target-file>]\n"
63 " [-pipe=[input][,output]]\n"
64 " -column=<list of column names>\n"
66 "sddsbinarystring converts integer columns into binary string representations.\n"
67 "Binary string columns are appended as <oldColumnName>BinaryString.\n\n"
69 "-column List of columns to convert. Wildcards are accepted.\n"
70 "-pipe Use pipes for input and/or output.\n\n"
71 "Author: Hairong (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
73int main(
int argc,
char **argv) {
77 long tmpfile_used, no_warnings, integer_columns, digits;
78 int32_t column_matches;
84 char *input, *output, **column_name, **column_match, **integer_column;
85 char buff[1024], **binary_string;
86 unsigned long pipe_flags;
91 input = output = NULL;
98 columns = column_matches = integer_columns = 0;
99 column_name = column_match = integer_column = NULL;
104 argc =
scanargs(&s_arg, argc, argv);
109 for (i_arg = 1; i_arg < argc; i_arg++) {
110 if (s_arg[i_arg].arg_type == OPTION) {
112 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
114 if (s_arg[i_arg].n_items < 2)
117 tmalloc(
sizeof(*column_match) *
118 (column_matches = s_arg[i_arg].n_items - 1));
119 for (i = 0; i < column_matches; i++)
120 column_match[i] = s_arg[i_arg].list[i + 1];
124 s_arg[i_arg].n_items - 1, &pipe_flags))
128 fprintf(stderr,
"Error (%s): unknown switch: %s\n",
129 argv[0], s_arg[i_arg].list[0]);
135 input = s_arg[i_arg].list[0];
136 else if (output == NULL)
137 output = s_arg[i_arg].list[0];
145 no_warnings, &tmpfile_used);
148 fprintf(stderr,
"Initializing input and output files.\n");
162 &columns, SDDS_MATCH_COLUMN);
167 for (i = 0; i < columns; i++) {
173 sizeof(*integer_column) * (integer_columns + 1));
176 sizeof(*integer_type) * (integer_columns + 1));
177 integer_column[integer_columns] = NULL;
180 integer_type[integer_columns] = type;
188 if (!integer_columns) {
189 fprintf(stderr,
"There are no integer columns in %s for converting.\n",
201 for (i = 0; i < integer_columns; i++) {
202 sprintf(buff,
"%sBinaryString", integer_column[i]);
205 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
209 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
212 fprintf(stderr,
"Reading integer data from input file.\n");
218 binary_string = malloc(
sizeof(*binary_string) * rows);
221 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
224 for (i = 0; i < integer_columns; i++) {
227 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
238 for (i_row = 0; i_row < rows; i_row++) {
239 binary_string[i_row] = NULL;
240 binary_string[i_row] =
241 (
char *)malloc(
sizeof(**binary_string) * (digits + 1));
243 for (j = 0; j < digits; j++) {
245 binary_string[i_row][digits - 1 - j] = (((
short*)data)[i_row] >> j & 0x1) ?
'1' :
'0';
247 binary_string[i_row][digits - 1 - j] = (((
unsigned short*)data)[i_row] >> j & 0x1) ?
'1' :
'0';
249 binary_string[i_row][digits - 1 - j] = (((int32_t*)data)[i_row] >> j & 0x1) ?
'1' :
'0';
251 binary_string[i_row][digits - 1 - j] = (((uint32_t*)data)[i_row] >> j & 0x1) ?
'1' :
'0';
253 binary_string[i_row][digits - 1 - j] = (((int64_t*)data)[i_row] >> j & 0x1) ?
'1' :
'0';
255 binary_string[i_row][digits - 1 - j] = (((uint64_t*)data)[i_row] >> j & 0x1) ?
'1' :
'0';
257 binary_string[i_row][digits] = 0;
261 sprintf(buff,
"%sBinaryString", integer_column[i]);
265 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
269 binary_string = NULL;
274 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
280 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
284 free(integer_column);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the 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.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
char ** getMatchingSDDSNames(SDDS_DATASET *dataset, char **matchName, int32_t matches, int32_t *names, short type)
Retrieves an array of matching SDDS entity names based on specified criteria.
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.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
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.
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_ULONG
Identifier for the unsigned 32-bit integer data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer 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_USHORT
Identifier for the unsigned short integer data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
Utility functions for SDDS dataset manipulation and string array operations.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
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.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
void free_scanargs(SCANNED_ARG **scanned, int argc)