71# if defined(__BORLANDC__)
72# define _setmode(handle, amode) setmode(handle, amode)
79static char *mode_name[MODES] = {
87static char *order_names[ORDERS] = {
106char *option[N_OPTIONS] = {
119 "Usage: sdds2plaindata <input> <output>\n"
120 " [-pipe=[input][,output]]\n"
121 " [-outputMode={ascii|binary}]\n"
122 " [-separator=<string>]\n"
124 " [-order={rowMajor|columnMajor}]\n"
125 " [-parameter=<name>[,format=<string>]...]\n"
126 " [-column=<name>[,format=<string>]...]\n"
130 " -outputMode Specify output format: ascii or binary.\n"
131 " -separator Define the column separator string in ASCII mode.\n"
132 " -noRowCount Exclude the number of rows from the output file.\n"
133 " (Note: Binary mode always includes row count.)\n"
134 " -order Set data ordering: rowMajor (default) or columnMajor.\n"
135 " -parameter Include specified parameters in the output. Optionally specify a format.\n"
136 " -column Include specified columns in the output. Supports wildcards and optional format.\n"
137 " -labeled Add labels for each parameter or column in ASCII mode.\n"
138 " -nowarnings Suppress warning messages.\n\n"
139 "Program by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
143int main(
int argc,
char **argv) {
150 long i_arg, retval, page_number = 0, size, columnOrder = 0;
154 char *input, *output;
155 unsigned long pipeFlags = 0, flags = 0;
156 long noWarnings = 0, tmpfile_used = 0;
159 long *parameterType, *columnType, *parameterIndex;
160 int32_t *columnIndex;
161 char **parameterUnits;
164 static char printBuffer[SDDS_MAXLINE * 16];
165 static char formatbuffer[100], formatbuffer2[100];
167 long binary = 0, noRowCount = 0;
168 char *separator, *ptr = NULL;
169 char **parameter, **parameterFormat;
170 char **column, **columnFormat, **columnUnits, **columnMatch, **columnMatchFormat, **columnName;
171 long parameters = 0, columns = 0, columnMatches = 0;
172 int32_t columnNames = 0;
174 input = output = NULL;
175 parameter = column = parameterFormat = columnFormat = columnMatch = columnMatchFormat = columnName = NULL;
178 parameterType = columnType = parameterIndex = NULL;
181 parameterUnits = columnUnits = NULL;
183 buffer =
tmalloc(
sizeof(
char) * 16);
186 argc =
scanargs(&s_arg, argc, argv);
190 for (i_arg = 1; i_arg < argc; i_arg++) {
191 if (s_arg[i_arg].arg_type == OPTION) {
192 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
194 if (s_arg[i_arg].n_items != 2)
196 switch (
match_string(s_arg[i_arg].list[1], mode_name, MODES, 0)) {
209 if (s_arg[i_arg].n_items != 2)
211 separator = s_arg[i_arg].list[1];
214 if (s_arg[i_arg].n_items != 1)
219 if (s_arg[i_arg].n_items != 2)
221 switch (
match_string(s_arg[i_arg].list[1], order_names, ORDERS, 0)) {
234 if ((s_arg[i_arg].n_items != 2) && (s_arg[i_arg].n_items != 4))
236 parameter =
trealloc(parameter,
sizeof(*parameter) * (++parameters));
237 parameterFormat =
trealloc(parameterFormat,
sizeof(*parameterFormat) * (parameters));
238 parameter[parameters - 1] = s_arg[i_arg].list[1];
239 parameterFormat[parameters - 1] = NULL;
240 s_arg[i_arg].n_items -= 2;
241 if (!
scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
"format",
SDDS_STRING, &(parameterFormat[parameters - 1]), 1, 0, NULL))
243 if (parameterFormat[parameters - 1]) {
244 replaceString(formatbuffer, parameterFormat[parameters - 1],
"ld", PRId32, -1, 0);
245 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
246 parameterFormat[parameters - 1] = malloc(
sizeof(*(parameterFormat[parameters - 1])) * (strlen(formatbuffer2) + 1));
247 sprintf(parameterFormat[parameters - 1],
"%s", formatbuffer2);
251 if ((s_arg[i_arg].n_items < 2))
254 columnMatch =
trealloc(columnMatch,
sizeof(*columnMatch) * (++columnMatches));
255 columnMatchFormat =
trealloc(columnMatchFormat,
sizeof(*columnMatchFormat) * (columnMatches));
256 SDDS_CopyString(&columnMatch[columnMatches - 1], s_arg[i_arg].list[1]);
257 columnMatchFormat[columnMatches - 1] = NULL;
258 s_arg[i_arg].n_items -= 2;
259 if (!
scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
"format",
SDDS_STRING, &(columnMatchFormat[columnMatches - 1]), 1, 0, NULL))
261 if (columnMatchFormat[columnMatches - 1]) {
262 replaceString(formatbuffer, columnMatchFormat[columnMatches - 1],
"ld", PRId32, -1, 0);
263 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
264 columnMatchFormat[columnMatches - 1] = malloc(
sizeof(*(columnMatchFormat[columnMatches - 1])) * (strlen(formatbuffer2) + 1));
265 sprintf(columnMatchFormat[columnMatches - 1],
"%s", formatbuffer2);
268 column =
trealloc(column,
sizeof(*column) * (++columns));
269 columnFormat =
trealloc(columnFormat,
sizeof(*columnFormat) * (columns));
271 columnFormat[columns - 1] = NULL;
272 s_arg[i_arg].n_items -= 2;
273 if (!
scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
"format",
SDDS_STRING, &(columnFormat[columns - 1]), 1, 0, NULL))
275 if (columnFormat[columns - 1]) {
276 replaceString(formatbuffer, columnFormat[columns - 1],
"ld", PRId32, -1, 0);
277 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
278 columnFormat[columns - 1] = malloc(
sizeof(*(columnFormat[columns - 1])) * (strlen(formatbuffer2) + 1));
279 sprintf(columnFormat[columns - 1],
"%s", formatbuffer2);
284 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
288 if (s_arg[i_arg].n_items != 1)
296 fprintf(stderr,
"error: unknown switch: %s\n", s_arg[i_arg].list[0]);
302 input = s_arg[i_arg].list[0];
303 }
else if (output == NULL) {
304 output = s_arg[i_arg].list[0];
306 fprintf(stderr,
"error: too many filenames provided.\n");
312 processFilenames(
"sdds2plaindata", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
314 if (!columns && !columnMatches && !parameters)
315 SDDS_Bomb(
"error: you must specify at least one of the -column or -parameter options.");
327 for (i = 0; i < columnMatches; i++) {
329 for (j = 0; j < columnNames; j++) {
332 if (
match_string(columnName[j], column, columns, EXACT_MATCH) < 0) {
333 column =
trealloc(column,
sizeof(*column) * (columns + 1));
335 columnFormat =
trealloc(columnFormat,
sizeof(*columnFormat) * (columns + 1));
336 columnFormat[columns] = NULL;
337 if (columnMatchFormat[i])
342 column =
trealloc(column,
sizeof(*column) * (columns + 1));
344 columnFormat =
trealloc(columnFormat,
sizeof(*columnFormat) * (columns + 1));
345 columnFormat[columns] = NULL;
346 if (columnMatchFormat[i])
358 free(columnMatchFormat);
362 parameterType =
tmalloc(
sizeof(*parameterType) * parameters);
363 parameterIndex =
tmalloc(
sizeof(*parameterIndex) * parameters);
364 parameterUnits =
tmalloc(
sizeof(*parameterUnits) * parameters);
365 for (i = 0; i < parameters; i++) {
367 fprintf(stderr,
"error: parameter '%s' does not exist.\n", parameter[i]);
378 columnType =
tmalloc(
sizeof(*columnType) * columns);
379 columnIndex =
tmalloc(
sizeof(*columnIndex) * columns);
380 columnData =
tmalloc(
sizeof(*columnData) * columns);
381 columnUnits =
tmalloc(
sizeof(*columnUnits) * columns);
382 for (i = 0; i < columns; i++) {
384 fprintf(stderr,
"error: column '%s' does not exist.\n", column[i]);
398 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
399 fprintf(stderr,
"error: unable to set stdout to binary mode.\n");
407 fileID = fopen(output,
"wb");
409 fileID = fopen(output,
"w");
412 if (fileID == NULL) {
413 fprintf(stderr,
"error: unable to open output file for writing.\n");
418 layout = &SDDS_dataset.layout;
419 fBuffer = &SDDS_dummy.fBuffer;
420 fBuffer->buffer = NULL;
421 if (!fBuffer->buffer) {
422 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * SDDS_FILEBUFFER_SIZE))) {
423 fprintf(stderr,
"error: unable to allocate buffer for binary output.\n");
426 fBuffer->bufferSize = SDDS_FILEBUFFER_SIZE;
427 fBuffer->bytesLeft = SDDS_FILEBUFFER_SIZE;
432 while (retval != page_number && (retval =
SDDS_ReadPage(&SDDS_dataset)) > 0) {
433 if (page_number && retval != page_number)
441 if ((binary) && (!noRowCount)) {
442 if (rows > INT32_MAX) {
445 fprintf(stderr,
"error: failed to write row count (overflow).\n");
449 fprintf(stderr,
"error: failed to write row count.\n");
455 fprintf(stderr,
"error: failed to write row count.\n");
461 for (i = 0; i < parameters; i++) {
469 fprintf(stderr,
"error: failed to write string parameter.\n");
472 }
else if (!
SDDS_BufferedWrite(SDDS_dataset.parameter[parameterIndex[i]],
SDDS_type_size[layout->parameter_definition[parameterIndex[i]].type - 1], fileID, fBuffer)) {
473 fprintf(stderr,
"error: failed to write parameter value.\n");
483 fputs(parameter[i], fileID);
484 fputs(separator, fileID);
485 if (parameterUnits[i])
486 fputs(parameterUnits[i], fileID);
487 fputs(separator, fileID);
489 fputs(printBuffer, fileID);
496 fprintf(fileID,
"\t%" PRId64
"\n", rows);
497 if (labeled && columns) {
498 for (j = 0; j < columns; j++) {
499 fputs(column[j], fileID);
500 if (j != (columns - 1))
501 fputs(separator, fileID);
504 for (j = 0; j < columns; j++) {
506 fputs(columnUnits[j], fileID);
507 if (j != (columns - 1))
508 fputs(separator, fileID);
518 for (j = 0; j < columns; j++) {
520 for (i = 0; i < rows; i++) {
522 fprintf(stderr,
"error: failed to write string data for column '%s'.\n", column[j]);
528 for (i = 0; i < rows; i++) {
529 if (!
SDDS_BufferedWrite((
char *)SDDS_dataset.data[columnIndex[j]] + i * size, size, fileID, fBuffer)) {
530 fprintf(stderr,
"error: failed to write data for column '%s'.\n", column[j]);
537 for (i = 0; i < rows; i++) {
538 for (j = 0; j < columns; j++) {
541 fprintf(stderr,
"error: failed to write string data for column '%s'.\n", column[j]);
546 if (!
SDDS_BufferedWrite((
char *)SDDS_dataset.data[columnIndex[j]] + i * size, size, fileID, fBuffer)) {
547 fprintf(stderr,
"error: failed to write data for column '%s'.\n", column[j]);
555 for (i = 0; i < columns; i++) {
562 for (i = 0; i < columns; i++) {
563 for (j = 0; j < rows; j++) {
565 fputs(printBuffer, fileID);
567 fprintf(fileID,
"%s", separator);
572 for (j = 0; j < rows; j++) {
573 for (i = 0; i < columns; i++) {
575 fputs(printBuffer, fileID);
576 if (i != columns - 1)
577 fprintf(fileID,
"%s", separator);
594 SDDS_SetError(
"Unable to write page--buffer flushing problem (SDDS_WriteBinaryPage)");
613 free(parameterFormat);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_BufferedWrite(void *target, int64_t targetSize, FILE *fp, SDDS_FILEBUFFER *fBuffer)
int32_t SDDS_WriteBinaryString(char *string, FILE *fp, SDDS_FILEBUFFER *fBuffer)
Writes a binary string to a file with buffering.
int32_t SDDS_FlushBuffer(FILE *fp, SDDS_FILEBUFFER *fBuffer)
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
int32_t SDDS_GetParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified parameter in the SDDS dataset.
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_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_SprintTypedValue(void *data, int64_t index, int32_t type, const char *format, char *buffer, uint32_t mode)
Formats a data value of a specified type into a string buffer using an optional printf format string.
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_Malloc(size_t size)
Allocates memory of a specified size.
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.
#define SDDS_STRING
Identifier for the string data type.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
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 * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
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.
int replaceString(char *t, char *s, char *orig, char *repl, long count_limit, long here)
Replace occurrences of one string with another string with additional options.
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)
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.
int has_wildcards(char *template)
Check if a template string contains any wildcard characters.
char * expand_ranges(char *template)
Expand range specifiers in a wildcard template into explicit character lists.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.