TPCCLIB
Loading...
Searching...
No Matches
asc2flat.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14#include <math.h>
15#include <string.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Reads numerical data from an ASCII file and writes those in a binary file",
23 "as 4-byte floats.",
24 "Numerical data can be stored on one or more lines, separated by",
25 "commas, tabs, semi-colons, or spaces.",
26 "Comment lines starting with '#' or '//' are allowed (and ignored) in",
27 "the beginning of the file.",
28 " ",
29 "Usage: @P [Options] ASCII_file [binary_file]",
30 " ",
31 "Options:",
32 " -stdoptions", // List standard options like --help, -v, etc
33 " ",
34 "See also: flat2nii, flat2img, simcirc, dft2img, simiart, imgadd, img2tif",
35 " ",
36 "Keywords: software testing, simulation, image, mask",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
55int main(int argc, char **argv)
56{
57 int ai, help=0, version=0, verbose=1;
58 unsigned int vi, isComment;
59 char *cptr, datfile[FILENAME_MAX], binfile[FILENAME_MAX], buf[1024];
60 FILE *fp1, *fp2=NULL;
61 float f;
62
63
64 /*
65 * Get arguments
66 */
67 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
68 datfile[0]=binfile[0]=(char)0;
69 /* Options */
70 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
71 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
72 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
73 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
74 return(1);
75 } else break;
76
77 /* Print help or version? */
78 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
79 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
80 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
81
82 /* Process other arguments, starting from the first non-option */
83 for(; ai<argc; ai++) {
84 if(!datfile[0]) {
85 strlcpy(datfile, argv[ai], FILENAME_MAX); continue;
86 } else if(!binfile[0]) {
87 strlcpy(binfile, argv[ai], FILENAME_MAX); continue;
88 }
89 /* We should not be here */
90 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
91 return(1);
92 }
93
94 /* Is something missing? */
95 if(!datfile[0]) {
96 fprintf(stderr, "Error: missing command-line argument; try %s --help\n",
97 argv[0]);
98 return(1);
99 }
100
101 /*
102 * If output filename was not specified, then create it
103 */
104 if(!binfile[0]) {
105 strlcpy(binfile, datfile, FILENAME_MAX); cptr=strrchr(binfile, '.');
106 if(cptr!=NULL) *cptr=(char)0;
107 strcat(binfile, ".bin");
108 }
109
110 /* In verbose mode print arguments and options */
111 if(verbose>1) {
112 printf("datfile := %s\n", datfile);
113 printf("binfile := %s\n", binfile);
114 }
115
116 /*
117 * Open ASCII file
118 */
119 if(verbose>0) fprintf(stdout, "opening ASCII file %s\n", datfile);
120 // Open in binary mode, otherwise ftell() will not function consistently
121 // in all platforms/compilers.
122 if((fp1=fopen(datfile, "rb"))==NULL) {
123 fprintf(stderr, "Error: cannot open %s\n", datfile);
124 return(2);
125 }
126
127 /*
128 * Consume all comment lines from the beginning
129 */
130 if(verbose>1) printf("checking for comment lines\n");
131 long int pos;
132 vi=0;
133 do {
134 isComment=0; vi++;
135 pos=ftell(fp1); //fgetpos(fp1, &pos);
136 if(fgets(buf, 1023, fp1)==NULL) break;
137 if(buf[0]=='\n') isComment=1;
138 if(buf[0]=='\r') isComment=1;
139 if(buf[0]=='#') isComment=1;
140 if(buf[0]=='/' && buf[1]=='*') isComment=1;
141 if(buf[0]=='/' && buf[1]=='/') isComment=1;
142 if(verbose>1 && isComment) printf("ignoring line %d\n", vi);
143 } while(isComment);
144 fseek(fp1, pos, SEEK_SET); //fsetpos(fp1, &pos);
145
146 /*
147 * Read the values in ASCII file and write them as binary floats
148 */
149 if(verbose>1) printf("reading data\n");
150 vi=0;
151 while(fscanf(fp1, "\t%f", &f)==1 || fscanf(fp1, " %f", &f)==1 ||
152 fscanf(fp1, ",%f", &f)==1 || fscanf(fp1, ";%f", &f)==1) {
153 if(verbose>10) printf("vi=%d : %g\n", vi+1, f);
154 /* Open output binary file, if this is the first value */
155 if(vi==0) {
156 if(verbose>0) fprintf(stdout, "opening binary file %s\n", binfile);
157 fp2=fopen(binfile, "wb"); if(fp2==NULL) {
158 fprintf(stderr, "Error: cannot open %s for write\n", binfile);
159 fclose(fp1); return(11);
160 }
161 }
162 /* Write */
163 if(fwrite(&f, sizeof(float), 1, fp2) != 1) {
164 fprintf(stderr, "Error: cannot write in %s\n", binfile);
165 fclose(fp1); fclose(fp2); return(12);
166 }
167 vi++;
168 }
169 fclose(fp1); fclose(fp2);
170 if(vi==0) {
171 fprintf(stderr, "Error: no values could be read from %s\n", datfile);
172 return(4);
173 }
174 if(verbose>=0)
175 fprintf(stdout, "%d float(s) were written in %s\n", vi, binfile);
176
177 return(0);
178}
179/*****************************************************************************/
180
181/*****************************************************************************/
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158