9#include "tpcclibConfig.h"
21static char *info[] = {
22 "Reads numerical data from an ASCII file and writes those in a binary file",
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.",
29 "Usage: @P [Options] ASCII_file [binary_file]",
34 "See also: flat2nii, flat2img, simcirc, dft2img, simiart, imgadd, img2tif",
36 "Keywords: software testing, simulation, image, mask",
55int main(
int argc,
char **argv)
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];
67 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
68 datfile[0]=binfile[0]=(char)0;
70 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
71 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
73 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
78 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
83 for(; ai<argc; ai++) {
85 strlcpy(datfile, argv[ai], FILENAME_MAX);
continue;
86 }
else if(!binfile[0]) {
87 strlcpy(binfile, argv[ai], FILENAME_MAX);
continue;
90 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
96 fprintf(stderr,
"Error: missing command-line argument; try %s --help\n",
105 strlcpy(binfile, datfile, FILENAME_MAX); cptr=strrchr(binfile,
'.');
106 if(cptr!=NULL) *cptr=(char)0;
107 strcat(binfile,
".bin");
112 printf(
"datfile := %s\n", datfile);
113 printf(
"binfile := %s\n", binfile);
119 if(verbose>0) fprintf(stdout,
"opening ASCII file %s\n", datfile);
122 if((fp1=fopen(datfile,
"rb"))==NULL) {
123 fprintf(stderr,
"Error: cannot open %s\n", datfile);
130 if(verbose>1) printf(
"checking for comment lines\n");
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);
144 fseek(fp1, pos, SEEK_SET);
149 if(verbose>1) printf(
"reading data\n");
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);
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);
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);
169 fclose(fp1); fclose(fp2);
171 fprintf(stderr,
"Error: no values could be read from %s\n", datfile);
175 fprintf(stdout,
"%d float(s) were written in %s\n", vi, binfile);
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)