37#include "match_string.h"
45static char *option[N_OPTIONS] = {
49static const char *USAGE =
50 "Usage: TFS2sdds [<inputfile> <outputfile>] [-pipe[=input][,output]]\n"
52 "Converts LEP TFS format files (used by MAD) to SDDS.\n"
54 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
").";
63static char *typeName[TYPENAMES] = {
71#define SDDS_MAXLINE 1024
73long identifyType(
char *format);
75int main(
int argc,
char **argv) {
77 long i_arg, type, inHeader = 1;
79 unsigned long pipeFlags;
81 char s1[SDDS_MAXLINE], s2[SDDS_MAXLINE];
82 char *name, *format, *value;
85 argc =
scanargs(&scanned, argc, argv);
87 fprintf(stderr,
"%s\n", USAGE);
91 input = output = format = value = NULL;
93 for (i_arg = 1; i_arg < argc; i_arg++) {
94 if (scanned[i_arg].arg_type == OPTION) {
95 switch (
match_string(scanned[i_arg].list[0], option, N_OPTIONS, 0)) {
97 if (!
processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipeFlags)) {
98 fprintf(stderr,
"Error: Invalid -pipe syntax.\n");
103 fprintf(stderr,
"Error: Unknown option '%s'.\n", scanned[i_arg].list[0]);
109 input = scanned[i_arg].list[0];
110 }
else if (!output) {
111 output = scanned[i_arg].list[0];
113 fprintf(stderr,
"Error: Too many filenames provided.\n");
123 else if (!(fpi = fopen(input,
"r"))) {
124 fprintf(stderr,
"Error: Unable to open input file '%s'.\n", input);
130 else if (!(fpo = fopen(output,
"w"))) {
131 fprintf(stderr,
"Error: Unable to open output file '%s'.\n", output);
135 fprintf(fpo,
"SDDS1\n");
136 if (!fgets(s1, SDDS_MAXLINE, fpi)) {
137 SDDS_Bomb(
"Input file ends prematurely");
140 while (s1[0] ==
'@') {
145 if ((type = identifyType(format)) < 0) {
146 fprintf(stderr,
"Error (TFS2sdds): Unknown format string: %s\n", format);
149 fprintf(fpo,
"¶meter name=%s, type=%s, fixed_value=\"%s\" &end\n", name, typeName[type], value);
151 if (!fgets(s1, SDDS_MAXLINE, fpi)) {
152 SDDS_Bomb(
"Input file ends prematurely");
157 if (!fgets(s2, SDDS_MAXLINE, fpi)) {
158 SDDS_Bomb(
"Input file ends prematurely");
165 SDDS_Bomb(
"Column format line not seen");
173 fprintf(fpo,
"&column name=%s, type=", name);
174 if ((type = identifyType(format)) < 0) {
175 fprintf(stderr,
"Error (TFS2sdds): Unknown format string: %s\n", format);
178 fprintf(fpo,
"%s &end\n", typeName[type]);
182 fputs(
"&data mode=ascii, no_row_counts=1 &end\n", fpo);
185 while (fgets(s1, SDDS_MAXLINE, fpi)) {
192 if ((type = identifyType(format)) < 0) {
193 fprintf(stderr,
"Error (TFS2sdds): Unknown format string: %s\n", format);
196 fprintf(fpo,
"¶meter name=%s, type=%s, fixed_value=\"%s\" &end\n", name, typeName[type], value);
200 fputs(
"&data mode=ascii, no_row_counts=1 &end\n", fpo);
205 fputs(
"&data mode=ascii, no_row_counts=1 &end\n\n", fpo);
211long identifyType(
char *format) {
218 length = strlen(format);
219 if (format[0] !=
'%') {
224 if (strcmp(format + length - 2,
"le") == 0 || strcmp(format + length - 2,
"lf") == 0) {
227 if (strcmp(format + length - 2,
"ld") == 0) {
230 if (strcmp(format + length - 2,
"hd") == 0) {
236 if (format[length - 1] ==
'e' || format[length - 1] ==
'f') {
239 if (format[length - 1] ==
'd') {
242 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.
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.