SDDSlib
Loading...
Searching...
No Matches
TFS2sdds.c
1/*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * This file is distributed subject to a Software License Agreement found
7 * in the file LICENSE that is included with this distribution.
8\*************************************************************************/
9
10/* program: TFS2sdds
11 * purpose: convert MAD TFS data to SDDS
12 *
13 * Michael Borland, 1995
14 $Log: not supported by cvs2svn $
15 Revision 1.5 2001/01/23 19:14:53 soliday
16 Standardized usage message.
17
18 Revision 1.4 1999/05/25 19:01:52 soliday
19 Removed compiler warning on linux.
20
21 Revision 1.3 1995/09/06 14:55:45 saunders
22 First test release of SDDS1.5
23
24*/
25#include "mdb.h"
26#include "scan.h"
27#include "SDDS.h"
28#include "match_string.h"
29
30#define SET_PIPE 0
31#define N_OPTIONS 1
32
33static char *option[N_OPTIONS] = {
34 "pipe",
35};
36
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 ").";
41
42#define SHORT_TYPE 0
43#define LONG_TYPE 1
44#define DOUBLE_TYPE 2
45#define FLOAT_TYPE 3
46#define STRING_TYPE 4
47#define TYPENAMES 5
48static char *typeName[TYPENAMES] = {
49 "short",
50 "long",
51 "double",
52 "float",
53 "string",
54};
55
56#define SDDS_MAXLINE 1024
57
58long identifyType(char *format);
59
60int main(int argc, char **argv) {
61 SCANNED_ARG *scanned;
62 long i_arg, type, inHeader = 1;
63 char *input, *output;
64 unsigned long pipeFlags;
65 FILE *fpi, *fpo;
66 char s1[SDDS_MAXLINE], s2[SDDS_MAXLINE];
67 char *name, *format, *value;
68
70 argc = scanargs(&scanned, argc, argv);
71 if (argc < 2)
72 bomb(NULL, USAGE);
73
74 input = output = format = value = NULL;
75 pipeFlags = 0;
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)) {
79 case SET_PIPE:
80 if (!processPipeOption(scanned[i_arg].list + 1, scanned[i_arg].n_items - 1, &pipeFlags))
81 SDDS_Bomb("invalid -pipe syntax");
82 break;
83 default:
84 SDDS_Bomb("unknown switch");
85 break;
86 }
87 } else {
88 if (!input)
89 input = scanned[i_arg].list[0];
90 else if (!output)
91 output = scanned[i_arg].list[0];
92 else
93 SDDS_Bomb("too many filenames");
94 }
95 }
96
97 processFilenames("TFS2sdds", &input, &output, pipeFlags, 0, NULL);
98
99 if (!input)
100 fpi = stdin;
101 else if (!(fpi = fopen(input, "r")))
102 SDDS_Bomb("Unable to open input file");
103 if (!output)
104 fpo = stdout;
105 else if (!(fpo = fopen(output, "w")))
106 SDDS_Bomb("Unable to open output file");
107
108 fprintf(fpo, "SDDS1\n");
109 if (!fgets(s1, SDDS_MAXLINE, fpi))
110 SDDS_Bomb("Input file ends prematurely");
111
112 while (s1[0] == '@') {
113 strcpy_ss(s1, s1 + 1);
114 if (!(name = get_token(s1)) || !(format = get_token(s1)) || !(value = get_token(s1)))
115 SDDS_Bomb("Missing data for parameter");
116 if ((type = identifyType(format)) < 0) {
117 fprintf(stderr, "Error (TFS2sdds): unknown format string: %s\n", format);
118 exit(1);
119 }
120 fprintf(fpo, "&parameter name = %s, type = %s, fixed_value = \"%s\" &end\n", name, typeName[type], value);
121
122 if (!fgets(s1, SDDS_MAXLINE, fpi))
123 SDDS_Bomb("Input file ends prematurely");
124 inHeader = 0;
125 }
126
127 if (!fgets(s2, SDDS_MAXLINE, fpi))
128 SDDS_Bomb("Input file ends prematurely");
129
130 if (s1[0] != '*')
131 SDDS_Bomb("Column name line not seen");
132 if (s2[0] != '$')
133 SDDS_Bomb("Column format line not seen");
134 strcpy_ss(s1, s1 + 1);
135 strcpy_ss(s2, s2 + 1);
136 while ((name = get_token(s1))) {
137 if (!(format = get_token(s2)))
138 SDDS_Bomb("Missing format for column");
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);
142 exit(1);
143 }
144 fprintf(fpo, "%s &end\n", typeName[type]);
145 }
146
147 if (inHeader == 0) {
148 fputs("&data mode=ascii, no_row_counts=1 &end\n", fpo);
149 }
150
151 while (fgets(s1, SDDS_MAXLINE, fpi)) {
152 if (inHeader) {
153 if (s1[0] == '@') {
154 strcpy_ss(s1, s1 + 1);
155 if (!(name = get_token(s1)) || !(format = get_token(s1)) || !(value = get_token(s1)))
156 SDDS_Bomb("Missing data for parameter");
157 if ((type = identifyType(format)) < 0) {
158 fprintf(stderr, "Error (TFS2sdds): unknown format string: %s\n", format);
159 exit(1);
160 }
161 fprintf(fpo, "&parameter name = %s, type = %s, fixed_value = \"%s\" &end\n", name, typeName[type], value);
162 continue;
163 }
164 inHeader = 0;
165 fputs("&data mode=ascii, no_row_counts=1 &end\n", fpo);
166 }
167 fputs(s1, fpo);
168 }
169 if (inHeader)
170 fputs("&data mode=ascii, no_row_counts=1 &end\n\n", fpo);
171
172 return (0);
173}
174
175long identifyType(char *format) {
176 long length;
177 length = strlen(format);
178 if (!format || format[0] != '%')
179 SDDS_Bomb("Bad format string seen");
180 if (strcmp(format + length - 2, "le") == 0 || strcmp(format + length - 2, "lf") == 0)
181 return DOUBLE_TYPE;
182 if (strcmp(format + length - 1, "e") == 0 || strcmp(format + length - 1, "f") == 0)
183 return FLOAT_TYPE;
184 if (strcmp(format + length - 2, "ld") == 0)
185 return LONG_TYPE;
186 if (strcmp(format + length - 2, "hd") == 0)
187 return SHORT_TYPE;
188 if (format[length - 1] == 'd')
189 return LONG_TYPE;
190 if (format[length - 1] == 's')
191 return STRING_TYPE;
192 return -1;
193}
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.
Definition SDDS_utils.c:288
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
char * get_token(char *s)
Extracts the next token from the input string.
Definition data_scan.c:413
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)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34