TPCCLIB
Loading...
Searching...
No Matches
convend.c
Go to the documentation of this file.
1
9/*****************************************************************************/
10#include "tpcclibConfig.h"
11/*****************************************************************************/
12#include <stdio.h>
13#include <stdlib.h>
14#include <math.h>
15#include <string.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Byte order conversion of numerical values stored in flat binary files,",
23 "between big endian (Sun Sparc, Motorola, PowerPC) and little endian",
24 "(PC/Intel) computers.",
25 " ",
26 "Usage: @P [Options] basetype flatfile outputfile",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Accepted base types are: char, short, int, long, float, and double.",
32 " ",
33 "See also: bigend, img2flat, flat2img, anabyteo",
34 " ",
35 "Keywords: image, byte order, big endian, little endian",
36 0};
37/*****************************************************************************/
38
39/*****************************************************************************/
40/* Turn on the globbing of the command line, since it is disabled by default in
41 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
42 In Unix&Linux wildcard command line processing is enabled by default. */
43/*
44#undef _CRT_glob
45#define _CRT_glob -1
46*/
47int _dowildcard = -1;
48/*****************************************************************************/
49
50/*****************************************************************************/
54int main(int argc, char **argv)
55{
56 int ai, help=0, version=0, verbose=1;
57 int size=0, wn;
58 char infile[FILENAME_MAX], outfile[FILENAME_MAX];
59 char *cptr, *base_type=NULL;
60 long int n;
61 FILE *fp, *fp2;
62 unsigned short s[2];
63 unsigned long l[2];
64 double d[2];
65
66
67 /*
68 * Get arguments
69 */
70 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
71 infile[0]=outfile[0]=(char)0;
72 /* Options */
73 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
74 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
77 return(1);
78 } else break;
79
80 /* Print help or version? */
81 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
82 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
83 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
84
85 /* Process other arguments, starting from the first non-option */
86 for(; ai<argc; ai++) {
87 if(size<=0) {
88 base_type=argv[ai];
89 if(!strcasecmp(base_type, "int")) size=sizeof(int);
90 else if(!strcasecmp(base_type, "short")) size=sizeof(short int);
91 else if(!strcasecmp(base_type, "int")) size=sizeof(int);
92 else if(!strcasecmp(base_type, "long")) size=sizeof(long int);
93 else if(!strcasecmp(base_type, "float")) size=sizeof(float);
94 else if(!strcasecmp(base_type, "double")) size=sizeof(double);
95 else if(!strcasecmp(base_type, "char")) {
96 printf("No conversion is necessary for char type.\n"); return(0);
97 } else {
98 fprintf(stderr, "Error: illegal datatype %s.\n", base_type);
99 return(1);
100 }
101 continue;
102 } else if(!infile[0]) {
103 strlcpy(infile, argv[ai], FILENAME_MAX); continue;
104 } else if(!outfile[0]) {
105 strlcpy(outfile, argv[ai], FILENAME_MAX); continue;
106 }
107 fprintf(stderr, "Error: invalid argument '%s'\n", argv[ai]);
108 return(1);
109 }
110
111 /* Is something missing? */
112 if(!outfile[0]) {
113 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
114 return(1);
115 }
116 if(strcasecmp(infile, outfile)==0) {
117 fprintf(stderr, "Error: same name for input and output file.\n");
118 return(1);
119 }
120
121
122 /* In verbose mode print arguments and options */
123 if(verbose>1) {
124 printf("infile := %s\n", infile);
125 printf("outfile := %s\n", outfile);
126 printf("size := %d\n", size);
127 if(verbose>2)
128 printf("double=%d, float=%d, long=%d, int=%d, short=%d, char=%d\n",
129 (int)sizeof(double), (int)sizeof(float), (int)sizeof(long),
130 (int)sizeof(int), (int)sizeof(short int), (int)sizeof(char));
131 }
132
133 /* Open datafile for read */
134 if((fp=fopen(infile, "rb"))==NULL) {
135 fprintf(stderr, "Error: cannot open file %s\n", infile); return(2);
136 }
137 /* Open outputfile for write */
138 if((fp2=fopen(outfile, "wb"))==NULL) {
139 fprintf(stderr, "Error: cannot open file %s\n", outfile);
140 fclose(fp); return(11);
141 }
142 /* Read, convert and write one value at a time */
143 n=0; wn=1;
144 switch(size) {
145 case 2:
146 while(!feof(fp)) {
147 if(fread((char*)s, size, 1, fp)!=1) break;
148 swap((char*)s, (char*)(s+1), size);
149 wn=fwrite((char*)(s+1), size, 1, fp2); if(wn==0) break;
150 n++;
151 }
152 break;
153 case 4:
154 while(!feof(fp)) {
155 if(fread((char*)l, size, 1, fp)!=1) break;
156 swap((char*)l, (char*)(l+1), size);
157 wn=fwrite((char*)(l+1), size, 1, fp2); if(wn==0) break;
158 n++;
159 }
160 break;
161 case 8:
162 while(!feof(fp)) {
163 if(fread((char*)d, size, 1, fp)!=1) break;
164 swap((char*)d, (char*)(d+1), size);
165 wn=fwrite((char*)(d+1), size, 1, fp2); if(wn==0) break;
166 n++;
167 }
168 break;
169 }
170 fclose(fp); fclose(fp2);
171 if(wn==0) {
172 fprintf(stderr, "Error: cannot write data in %s\n", outfile);
173 return(12);
174 }
175 if(verbose>0) fprintf(stdout, " %ld %ss converted\n", n, base_type);
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
void swap(void *orig, void *new, int size)
Definition swap.c:31