28#include "match_string.h"
33static char *option[N_OPTIONS] = {
37char *USAGE =
"TFS2sdds <inputfile> <outputfile>\n\
38 [-pipe[=input][,output]]\n\n\
39Converts LEP TFS format files (used by MAD) to SDDS.\n\n\
40Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
").";
48static char *typeName[TYPENAMES] = {
56#define SDDS_MAXLINE 1024
58long identifyType(
char *format);
60int main(
int argc,
char **argv) {
62 long i_arg, type, inHeader = 1;
64 unsigned long pipeFlags;
66 char s1[SDDS_MAXLINE], s2[SDDS_MAXLINE];
67 char *name, *format, *value;
70 argc =
scanargs(&scanned, argc, argv);
74 input = output = format = value = NULL;
76 for (i_arg = 1; i_arg < argc; i_arg++) {
77 if (scanned[i_arg].arg_type == OPTION) {
78 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
80 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipeFlags))
89 input = scanned[i_arg].list[0];
91 output = scanned[i_arg].list[0];
101 else if (!(fpi = fopen(input,
"r")))
105 else if (!(fpo = fopen(output,
"w")))
108 fprintf(fpo,
"SDDS1\n");
109 if (!fgets(s1, SDDS_MAXLINE, fpi))
110 SDDS_Bomb(
"Input file ends prematurely");
112 while (s1[0] ==
'@') {
116 if ((type = identifyType(format)) < 0) {
117 fprintf(stderr,
"Error (TFS2sdds): unknown format string: %s\n", format);
120 fprintf(fpo,
"¶meter name = %s, type = %s, fixed_value = \"%s\" &end\n", name, typeName[type], value);
122 if (!fgets(s1, SDDS_MAXLINE, fpi))
123 SDDS_Bomb(
"Input file ends prematurely");
127 if (!fgets(s2, SDDS_MAXLINE, fpi))
128 SDDS_Bomb(
"Input file ends prematurely");
133 SDDS_Bomb(
"Column format line not seen");
139 fprintf(fpo,
"&column name=%s, type=", name);
140 if ((type = identifyType(format)) < 0) {
141 fprintf(stderr,
"Error (TFS2sdds): unknown format string: %s\n", format);
144 fprintf(fpo,
"%s &end\n", typeName[type]);
148 fputs(
"&data mode=ascii, no_row_counts=1 &end\n", fpo);
151 while (fgets(s1, SDDS_MAXLINE, fpi)) {
157 if ((type = identifyType(format)) < 0) {
158 fprintf(stderr,
"Error (TFS2sdds): unknown format string: %s\n", format);
161 fprintf(fpo,
"¶meter name = %s, type = %s, fixed_value = \"%s\" &end\n", name, typeName[type], value);
165 fputs(
"&data mode=ascii, no_row_counts=1 &end\n", fpo);
170 fputs(
"&data mode=ascii, no_row_counts=1 &end\n\n", fpo);
175long identifyType(
char *format) {
177 length = strlen(format);
178 if (!format || format[0] !=
'%')
180 if (strcmp(format + length - 2,
"le") == 0 || strcmp(format + length - 2,
"lf") == 0)
182 if (strcmp(format + length - 1,
"e") == 0 || strcmp(format + length - 1,
"f") == 0)
184 if (strcmp(format + length - 2,
"ld") == 0)
186 if (strcmp(format + length - 2,
"hd") == 0)
188 if (format[length - 1] ==
'd')
190 if (format[length - 1] ==
's')
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
char * get_token(char *s)
Extracts the next token from the input string.
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 processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.