comparison FXAnalyse.c @ 183:791ca26fee6a

Rewrite data writing to file Fmt() uses by default truncation to conver double arguments to their text representation. Rounding must be used. Rewrite using standard C functions to get rid of this problem (and probably make it more efficient). Extend to handle arbitrary number of channels and to report errors on opening the data files.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 21 Feb 2014 18:42:30 +0100
parents 8faada7e4faf
children 12df3a2b18de
comparison
equal deleted inserted replaced
182:4d1f9e222e9e 183:791ca26fee6a
1 #include <tcpsupp.h> 1 #include <tcpsupp.h>
2 #include <utility.h> 2 #include <utility.h>
3 #include <ansi_c.h> 3 #include <ansi_c.h>
4 #include <lowlvlio.h>
4 #include <cvirte.h> 5 #include <cvirte.h>
5 #include <userint.h> 6 #include <userint.h>
6 #include <formatio.h> 7 #include <formatio.h>
7 #include <inifile.h> 8 #include <inifile.h>
8 #include <string.h> 9 #include <string.h>
298 299
299 300
300 static void write_data(const char *folder, const char *name, const char *id, 301 static void write_data(const char *folder, const char *name, const char *id,
301 const char *timestr, double utc, double *v, int nchan) 302 const char *timestr, double utc, double *v, int nchan)
302 { 303 {
304 int i, fd, len;
303 char line[1024]; 305 char line[1024];
304 char filename[FILENAME_MAX]; 306 char filename[FILENAME_MAX];
305 307
306 // construct filename in the form folder\\id-name.txt 308 // construct filename in the form folder\\id-name.txt
307 snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name); 309 snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
308 310
309 int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII); 311 fd = open(filename, O_CREAT|O_WRONLY|O_APPEND);
310 switch (nchan) 312 if (fd < 0) {
311 { 313 logmessage(ERROR, "open data file %s: %s", filename, strerror(errno));
312 case 1: 314 return;
313 Fmt(line, "%s\t%f[p3]\t%f[p3]", 315 }
314 timestr, utc, v[0]); 316
315 break; 317 // timestamp
316 case 4: 318 len = snprintf(line, sizeof(line), "%s\t%.3f", timestr, utc);
317 Fmt(line, "%s\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]", 319
318 timestr, utc, v[0], v[1], v[2], v[3]); 320 // data channels
319 break; 321 for (i = 0; i < nchan; i++)
320 default: 322 len += snprintf(line + len, sizeof(line) - len, "\t%.3f", v[i]);
321 strcpy(line, "?"); 323
322 } 324 // newline
323 WriteLine(fd, line, -1); 325 line[len++] = '\r';
324 CloseFile(fd); 326 line[len++] = '\n';
327
328 // write to file
329 write(fd, line, len);
330
331 close(fd);
325 } 332 }
326 333
327 334
328 static inline void datafile_append(struct datafile *d, char *id, char *timestr) 335 static inline void datafile_append(struct datafile *d, char *id, char *timestr)
329 { 336 {