28#include "match_string.h"
30#define DEFAULT_HSIZE 484
31#define DEFAULT_VSIZE 512
33#define SET_DEFINITION 0
35#define SET_MAJOR_ORDER 2
38char *option[N_OPTIONS] = {
44char *USAGE =
"raw2sdds <inputfile> <outputfile>\n\
45 -definition=<name>,<definition-entries>\n\
46 [-size=<horiz-pixels>,<vert-pixels>] [-majorOrder=row|column]\n\n\
47raw2sdds converts a binary data stream to SDDS format. The definition entries\
48are of the form <keyword>=<value>, where the keyword is any valid field name for\
50Program by Michael Borland (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
52char *process_column_definition(
char **argv,
long argc);
54int main(
int argc,
char **argv) {
57 unsigned long majorOrderFlag;
59 char *input, *output, *definition;
61 char *data, *data_name;
62 char ts1[100], ts2[100];
64 short columnMajorOrder = 0;
66 argc =
scanargs(&scanned, argc, argv);
70 input = output = data_name = NULL;
71 hsize = DEFAULT_HSIZE;
72 vsize = DEFAULT_VSIZE;
75 for (i_arg = 1; i_arg < argc; i_arg++) {
76 if (scanned[i_arg].arg_type == OPTION) {
78 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
81 scanned[i_arg].n_items--;
82 if (scanned[i_arg].n_items > 0 && (!
scanItemList(&majorOrderFlag, scanned[i_arg].list + 1, &scanned[i_arg].n_items, 0,
"row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
"column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
83 SDDS_Bomb(
"invalid -majorOrder syntax/values");
84 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
86 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
90 data_name = scanned[i_arg].list[1];
91 definition = process_column_definition(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1);
92 if (!strstr(definition,
"type=character"))
93 SDDS_Bomb(
"data type must be character for now");
96 if (scanned[i_arg].n_items != 3 || sscanf(scanned[i_arg].list[1],
"%ld", &hsize) != 1 || hsize <= 0 || sscanf(scanned[i_arg].list[2],
"%ld", &vsize) != 1 || vsize <= 0)
97 bomb(
"invalid -size syntax", USAGE);
100 bomb(
"invalid option seen", USAGE);
105 input = scanned[i_arg].list[0];
107 output = scanned[i_arg].list[0];
109 bomb(
"too many filenames", USAGE);
119 sprintf(ts1,
"%ld", hsize);
120 sprintf(ts2,
"%ld", vsize);
122 "screen image from raw file",
"screen image",
128 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
129 if (!
SDDS_WriteLayout(&SDDS_table) || !SDDS_StartTable(&SDDS_table, hsize * vsize))
132 data =
tmalloc(
sizeof(*data) * hsize * vsize);
134 if (fread(data,
sizeof(*data), hsize * vsize, fpi) != hsize * vsize)
135 SDDS_Bomb(
"unable to read (all) data from input file");
137 if (!
SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, data, hsize * vsize, data_name) ||
138 !SDDS_WriteTable(&SDDS_table) ||
144char *process_column_definition(
char **argv,
long argc) {
145 char buffer[SDDS_MAXLINE], *ptr;
150 sprintf(buffer,
"&column name=%s, ", argv[0]);
151 for (i = 1; i < argc; i++) {
152 if (!strchr(argv[i],
'='))
154 strcat(buffer, argv[i]);
155 strcat(buffer,
", ");
157 if (!strstr(buffer,
"type="))
158 strcat(buffer,
"type=character ");
159 strcat(buffer,
"&end");
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
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_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
int32_t SDDS_ProcessColumnString(SDDS_DATASET *SDDS_dataset, char *string, int32_t mode)
Process a column definition string.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
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.
FILE * fopen_e(char *file, char *open_mode, long mode)
Opens a file with error checking, messages, and aborts.
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 scanargs(SCANNED_ARG **scanned, int argc, char **argv)
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.