45#include "match_string.h"
47#define DEFAULT_HSIZE 484
48#define DEFAULT_VSIZE 512
57char *option[N_OPTIONS] = {
64 "Usage: raw2sdds <inputfile> <outputfile>\n"
65 " -definition=<name>,<definition-entries>\n"
66 " [-size=<horiz-pixels>,<vert-pixels>] \n"
67 " [-majorOrder=row|column]\n"
69 " -definition=<name>,<definition-entries>\n"
70 " Defines the SDDS columns. Each definition entry should be in the form <keyword>=<value>.\n"
71 " Example: -definition=Data,type=character\n\n"
72 " -size=<horiz-pixels>,<vert-pixels>\n"
73 " Specifies the horizontal and vertical size of the data in pixels.\n"
74 " Defaults are 484 horizontally and 512 vertically if not specified.\n"
75 " Example: -size=800,600\n\n"
76 " -majorOrder=row|column\n"
77 " Sets the major order of the output file data. Choose 'row' for row-major order or 'column' for column-major order.\n"
78 " Defaults to row-major if not specified.\n"
79 " Example: -majorOrder=column\n\n"
80 "raw2sdds converts a binary data stream to SDDS format. The definition entries are of the form <keyword>=<value>,\n"
81 "where the keyword is any valid field name for an SDDS column.\n\n"
82 "Program by Michael Borland (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
84char *process_column_definition(
char **argv,
long argc);
86int main(
int argc,
char **argv) {
89 unsigned long majorOrderFlag;
91 char *input, *output, *definition;
93 char *data, *data_name;
94 char ts1[100], ts2[100];
96 short columnMajorOrder = 0;
98 argc =
scanargs(&scanned, argc, argv);
100 fprintf(stderr,
"%s", USAGE);
104 input = output = data_name = NULL;
105 hsize = DEFAULT_HSIZE;
106 vsize = DEFAULT_VSIZE;
109 for (i_arg = 1; i_arg < argc; i_arg++) {
110 if (scanned[i_arg].arg_type == OPTION) {
112 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
113 case SET_MAJOR_ORDER:
115 scanned[i_arg].n_items--;
116 if (scanned[i_arg].n_items > 0 &&
117 (!
scanItemList(&majorOrderFlag, scanned[i_arg].list + 1, &scanned[i_arg].n_items,
118 0,
"row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
119 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL))) {
120 fprintf(stderr,
"Error: Invalid -majorOrder syntax or values.\n");
123 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
124 columnMajorOrder = 1;
125 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
126 columnMajorOrder = 0;
130 if (scanned[i_arg].n_items < 2) {
131 fprintf(stderr,
"Error: -definition requires at least a name and one definition entry.\n");
132 fprintf(stderr,
"%s", USAGE);
135 data_name = scanned[i_arg].list[1];
136 definition = process_column_definition(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1);
138 fprintf(stderr,
"Error: Invalid column definition.\n");
141 if (!strstr(definition,
"type=character")) {
142 fprintf(stderr,
"Error: Data type must be 'character' for now.\n");
148 if (scanned[i_arg].n_items != 3 ||
149 sscanf(scanned[i_arg].list[1],
"%ld", &hsize) != 1 || hsize <= 0 ||
150 sscanf(scanned[i_arg].list[2],
"%ld", &vsize) != 1 || vsize <= 0) {
151 fprintf(stderr,
"Error: Invalid -size syntax.\n");
152 fprintf(stderr,
"%s", USAGE);
158 fprintf(stderr,
"Error: Invalid option '%s'.\n", scanned[i_arg].list[0]);
159 fprintf(stderr,
"%s", USAGE);
164 input = scanned[i_arg].list[0];
166 output = scanned[i_arg].list[0];
168 fprintf(stderr,
"Error: Too many filenames provided.\n");
169 fprintf(stderr,
"%s", USAGE);
176 fprintf(stderr,
"Error: Input file not specified.\n");
177 fprintf(stderr,
"%s", USAGE);
182 fprintf(stderr,
"Error: Output file not specified.\n");
183 fprintf(stderr,
"%s", USAGE);
188 fprintf(stderr,
"Error: Column definition not specified.\n");
189 fprintf(stderr,
"%s", USAGE);
193 snprintf(ts1,
sizeof(ts1),
"%ld", hsize);
194 snprintf(ts2,
sizeof(ts2),
"%ld", vsize);
197 "Screen image from raw file",
"screen image",
206 SDDS_table.layout.data_mode.column_major = columnMajorOrder;
208 if (!
SDDS_WriteLayout(&SDDS_table) || !SDDS_StartTable(&SDDS_table, hsize * vsize)) {
213 data =
tmalloc(
sizeof(*data) * hsize * vsize);
215 if (fread(data,
sizeof(*data), hsize * vsize, fpi) != (
size_t)(hsize * vsize)) {
217 fprintf(stderr,
"Error: Unable to read all data from input file '%s'.\n", input);
222 if (!
SDDS_SetColumn(&SDDS_table, SDDS_SET_BY_NAME, data, hsize * vsize, data_name) ||
223 !SDDS_WriteTable(&SDDS_table) ||
233char *process_column_definition(
char **argv,
long argc) {
234 char buffer[SDDS_MAXLINE] =
"&column name=";
241 strcat(buffer, argv[0]);
242 strcat(buffer,
", ");
244 for (i = 1; i < argc; i++) {
245 if (!strchr(argv[i],
'=')) {
248 strcat(buffer, argv[i]);
249 strcat(buffer,
", ");
252 if (!strstr(buffer,
"type=")) {
253 strcat(buffer,
"type=character, ");
256 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.
#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.
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.