Fixed newline characters throughout the code
[com/gs-lite.git] / src / tools / gdat2ascii.c
1 /* ------------------------------------------------
2  Copyright 2014 AT&T Intellectual Property
3  Licensed under the Apache License, Version 2.0 (the "License");
4  you may not use this file except in compliance with the License.
5  You may obtain a copy of the License at
6  
7  http://www.apache.org/licenses/LICENSE-2.0
8  
9  Unless required by applicable law or agreed to in writing, software
10  distributed under the License is distributed on an "AS IS" BASIS,
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  See the License for the specific language governing permissions and
13  limitations under the License.
14  ------------------------------------------- */
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <schemaparser.h>
19 #include "gsconfig.h"
20 #include "gstypes.h"
21
22 FILE * ifd;
23
24 gs_retval_t read_tuple(gs_uint32_t * sz, gs_sp_t data, gs_int32_t maxlen) {
25     gs_uint32_t nsz;
26     static gs_uint32_t read=0;
27 again:
28     if (fread(&nsz,sizeof(gs_uint32_t),1,ifd)!=1) {
29         
30         exit(0);
31     }
32     read=read+sizeof(gs_uint32_t);
33     *sz=ntohl(nsz);
34     if ((*sz)>maxlen) {
35         fprintf(stderr,"INTERNAL ERROR tuple to long for fixed buffer. Tuple sz %u\n",
36                 (*sz));
37         *sz=0;
38         return 0;
39     }
40     if (*sz==0) goto again;
41     if (fread(data,(*sz),1,ifd)!=1) {
42         fprintf(stderr,"UNEXPECTED END OF FILE. Tryed to read tuple of size %u\n",
43                 (*sz));
44         exit(0);
45     }
46     read=read+*sz;
47     return 0;
48 }
49
50 int main(int argc, char** argv) {
51     gs_uint32_t streamid;
52     gs_schemahandle_t schema;
53     
54     gs_uint32_t rstreamid;
55     gs_uint32_t rsize;
56     gs_int8_t rbuf[2*MAXTUPLESZ];
57     
58     gs_int32_t numberoffields;
59     gs_int32_t verbose=0;
60     gs_int32_t truncate=-1;
61     gs_int32_t y;
62     gs_int32_t parserversion;
63     gs_uint32_t schemalen;
64     gs_sp_t asciischema;
65     gs_sp_t me;
66     gs_int32_t ch;
67     gs_uint32_t scramble =0;
68     gs_uint32_t schema_only =0;
69     
70     /* initialize host library and the sgroup  */
71     
72     me=argv[0];
73     
74     if (argc<2) {
75         fprintf(stderr,
76                 "%s::usage: %s -v -x -t <int> -s <int> <uncompressed_file_name>\n",
77                 me,me);
78         exit(1);
79     }
80     
81     while ((ch = getopt(argc, argv, "ps:t:vx")) != -1) {
82         switch(ch) {
83             case 'p':
84                 schema_only=1;
85                 break;
86             case 'v':
87                 verbose=1;
88                 break;
89             case 'x':
90                 verbose=2;
91                 break;
92             case 't':
93                 truncate=atoi(optarg);
94                 break;
95             case 's':
96                 sscanf(optarg,"%u",&scramble);
97         }
98     }
99     argc -= optind;
100     if (argc < 1) {
101         fprintf(stderr,"%s::usage: %s -v -x -t <int> <uncompressed_file_name>\n",
102                 me,me);
103         exit(1);
104     }
105     argv += optind;
106     
107     if ((strcmp(argv[0],"-")!=0)&&(strcmp(argv[0],"stdin")!=0)) {
108         if ((ifd=fopen(argv[0],"r"))==0) {
109             fprintf(stderr,"%s::error:could not open input file %s\n",
110                     me,argv[0]);
111             exit(1);
112         }
113     } else {
114         ifd = stdin;
115     }
116     
117     if (fscanf(ifd,"GDAT\nVERSION:%u\nSCHEMALENGTH:%u\n",&parserversion,&schemalen)!=2) {
118         fprintf(stderr,"%s::error:unknown file type for file %s\n",
119                 me,argv[0]);
120         exit(1);
121     }
122     
123     if (schemaparser_accepts_version(parserversion)!=1) {
124         fprintf(stderr,"%s::error: wrong parser version %u for file %s\n",
125                 me,parserversion,argv[0]);
126         exit(1);
127     }
128     
129     if ((asciischema=malloc(schemalen))==0) {
130         fprintf(stderr,"%s::error: could not allocate schema buffer of sz %u "
131                 "for file %s\n",
132                 me,schemalen,argv[0]);
133         exit(1);
134     }
135     
136     if (fread(asciischema,schemalen,1,ifd)!=1) {
137         fprintf(stderr,"%s::error: could not read schema buffer of sz %u "
138                 "for file %s\n",
139                 me,schemalen,argv[0]);
140         exit(1);
141     }
142     if (schema_only==1) {
143         fprintf(stdout,"%s\n",asciischema);
144     }
145     if (verbose==2) {
146         fprintf(stderr,"%s\n",asciischema);
147     }
148     if ((schema=ftaschema_parse_string(asciischema))<0) {
149         fprintf(stderr,"%s::error: could not parse schema  "
150                 "for file %s\n",
151                 me,argv[0]);
152         exit(1);
153     }
154     
155     if ((numberoffields=ftaschema_tuple_len(schema))<0) {
156         fprintf(stderr,"%s::error:could not get number of fields in schema\n",
157                 me);
158         exit(1);
159     }
160     if (verbose==1) {
161         for(y=0; y<numberoffields;y++) {
162             printf("%s",ftaschema_field_name(schema,y));
163             if (y<numberoffields-1) printf("|");
164         }
165         printf("\n");
166     }
167     while(read_tuple(&rsize,rbuf,2*MAXTUPLESZ)==0) {
168         if ((!rsize) || (schema_only==1))
169             continue;
170         for(y=0; y<numberoffields;y++) {
171             struct access_result ar;
172             if (verbose==2)
173                 printf("%s->",ftaschema_field_name(schema,y));
174             ar=ftaschema_get_field_by_index(schema,y,rbuf,rsize);
175             switch (ar.field_data_type) {
176                 case INT_TYPE:
177                     printf("%d",ar.r.i);
178                     break;
179                 case UINT_TYPE:
180                     printf("%u",ar.r.ui);
181                     break;
182                 case IP_TYPE:
183                     if (scramble!=0) {
184                         ar.r.ui=gscpbswap32(ar.r.ui^scramble);
185                     }
186                     printf("%u.%u.%u.%u",ar.r.ui>>24&0xff,
187                            ar.r.ui>>16&0xff,
188                            ar.r.ui>>8&0xff,
189                            ar.r.ui&0xff);
190                     break;
191                 case IPV6_TYPE:
192                 {
193                     unsigned x;
194                     unsigned zc=0;
195                     for(x=0;x<4;x++) { if (ar.r.ip6.v[x]==0) zc++;}
196                     if (zc!=4) {
197                         for(x=0;x<8;x++) {
198                             unsigned char * a = (unsigned char *)  &(ar.r.ip6.v[0]);
199                             unsigned y;
200                             y=((unsigned)a[2*x])<<8|((unsigned) a[2*x+1]);
201                             printf("%04x",y);
202                             if (x<7) printf(":");
203                         }
204                     } else {
205                         printf("::");
206                     }
207                 }
208                     break;
209                 case USHORT_TYPE:
210                     printf("%u",ar.r.ui);
211                     break;
212                 case BOOL_TYPE:
213                     if (ar.r.ui==0) {
214                         printf("FALSE");
215                     } else {
216                         printf("TRUE");
217                     }
218                     break;
219                 case ULLONG_TYPE:
220                 {
221                     gs_uint64_t ul;
222                     gs_uint64_t t1;
223                     gs_uint64_t t2;
224                     ul=ar.r.ul;
225                     printf("%llu",ul);
226                 }
227                     break;
228                 case LLONG_TYPE:
229                     printf("%lld",ar.r.l);
230                     break;
231                 case FLOAT_TYPE:
232                     printf("%f",ar.r.f);
233                     break;
234                 case TIMEVAL_TYPE:
235                 {
236                     gs_float_t t;
237                     t= ar.r.t.tv_usec;
238                     t=t/1000000;
239                     t=t+ar.r.t.tv_sec;
240                     printf("%lf sec",t);
241                 }
242                     break;
243                 case VSTR_TYPE:
244                 {
245                     int x;
246                     int c;
247                     char * src;
248                     src=(gs_sp_t)ar.r.vs.offset;
249                     if ((truncate>=0) && (ar.r.vs.length>truncate)) {
250                         ar.r.vs.length=truncate;
251                     }
252                     if ((ar.r.vs.length>0) && (src[ar.r.vs.length-1]==0)) {
253                         ar.r.vs.length = ar.r.vs.length-1;
254                     }
255                     for(x=0;x<ar.r.vs.length;x++) {
256                         c=src[x];
257                         if (((c<='~') && (c>=' '))&&(c!='|')) {
258                             printf("%c",c);
259                         } else {
260                             printf("(0x%x)",(gs_uint8_t)c);
261                         }
262                     }
263                 }
264                     break;
265                 default:
266                     break;
267             }
268             if (y<numberoffields-1) printf("|");
269         }
270         printf("\n");
271         if (verbose!=0) fflush(stdout);
272     }
273     exit(0);
274 }
275