Fixed newline characters throughout the code
[com/gs-lite.git] / src / tools / gdat2ascii.c
index 8f7e85c..8381ed0 100644 (file)
-/* ------------------------------------------------\r
- Copyright 2014 AT&T Intellectual Property\r
- Licensed under the Apache License, Version 2.0 (the "License");\r
- you may not use this file except in compliance with the License.\r
- You may obtain a copy of the License at\r
\r
- http://www.apache.org/licenses/LICENSE-2.0\r
\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
- ------------------------------------------- */\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include <unistd.h>\r
-#include <schemaparser.h>\r
-#include "gsconfig.h"\r
-#include "gstypes.h"\r
-\r
-FILE * ifd;\r
-\r
-gs_retval_t read_tuple(gs_uint32_t * sz, gs_sp_t data, gs_int32_t maxlen) {\r
-    gs_uint32_t nsz;\r
-    static gs_uint32_t read=0;\r
-again:\r
-    if (fread(&nsz,sizeof(gs_uint32_t),1,ifd)!=1) {\r
-        \r
-        exit(0);\r
-    }\r
-    read=read+sizeof(gs_uint32_t);\r
-    *sz=ntohl(nsz);\r
-    if ((*sz)>maxlen) {\r
-        fprintf(stderr,"INTERNAL ERROR tuple to long for fixed buffer. Tuple sz %u\n",\r
-                (*sz));\r
-        *sz=0;\r
-        return 0;\r
-    }\r
-    if (*sz==0) goto again;\r
-    if (fread(data,(*sz),1,ifd)!=1) {\r
-        fprintf(stderr,"UNEXPECTED END OF FILE. Tryed to read tuple of size %u\n",\r
-                (*sz));\r
-        exit(0);\r
-    }\r
-    read=read+*sz;\r
-    return 0;\r
-}\r
-\r
-int main(int argc, char** argv) {\r
-    gs_uint32_t streamid;\r
-    gs_schemahandle_t schema;\r
-    \r
-    gs_uint32_t rstreamid;\r
-    gs_uint32_t rsize;\r
-    gs_int8_t rbuf[2*MAXTUPLESZ];\r
-    \r
-    gs_int32_t numberoffields;\r
-    gs_int32_t verbose=0;\r
-    gs_int32_t truncate=-1;\r
-    gs_int32_t y;\r
-    gs_int32_t parserversion;\r
-    gs_uint32_t schemalen;\r
-    gs_sp_t asciischema;\r
-    gs_sp_t me;\r
-    gs_int32_t ch;\r
-    gs_uint32_t scramble =0;\r
-    gs_uint32_t schema_only =0;\r
-    \r
-    /* initialize host library and the sgroup  */\r
-    \r
-    me=argv[0];\r
-    \r
-    if (argc<2) {\r
-        fprintf(stderr,\r
-                "%s::usage: %s -v -x -t <int> -s <int> <uncompressed_file_name>\n",\r
-                me,me);\r
-        exit(1);\r
-    }\r
-    \r
-    while ((ch = getopt(argc, argv, "ps:t:vx")) != -1) {\r
-        switch(ch) {\r
-            case 'p':\r
-                schema_only=1;\r
-                break;\r
-            case 'v':\r
-                verbose=1;\r
-                break;\r
-            case 'x':\r
-                verbose=2;\r
-                break;\r
-            case 't':\r
-                truncate=atoi(optarg);\r
-                break;\r
-            case 's':\r
-                sscanf(optarg,"%u",&scramble);\r
-        }\r
-    }\r
-    argc -= optind;\r
-    if (argc < 1) {\r
-        fprintf(stderr,"%s::usage: %s -v -x -t <int> <uncompressed_file_name>\n",\r
-                me,me);\r
-        exit(1);\r
-    }\r
-    argv += optind;\r
-    \r
-    if ((strcmp(argv[0],"-")!=0)&&(strcmp(argv[0],"stdin")!=0)) {\r
-        if ((ifd=fopen(argv[0],"r"))==0) {\r
-            fprintf(stderr,"%s::error:could not open input file %s\n",\r
-                    me,argv[0]);\r
-            exit(1);\r
-        }\r
-    } else {\r
-        ifd = stdin;\r
-    }\r
-    \r
-    if (fscanf(ifd,"GDAT\nVERSION:%u\nSCHEMALENGTH:%u\n",&parserversion,&schemalen)!=2) {\r
-        fprintf(stderr,"%s::error:unknown file type for file %s\n",\r
-                me,argv[0]);\r
-        exit(1);\r
-    }\r
-    \r
-    if (schemaparser_accepts_version(parserversion)!=1) {\r
-        fprintf(stderr,"%s::error: wrong parser version %u for file %s\n",\r
-                me,parserversion,argv[0]);\r
-        exit(1);\r
-    }\r
-    \r
-    if ((asciischema=malloc(schemalen))==0) {\r
-        fprintf(stderr,"%s::error: could not allocate schema buffer of sz %u "\r
-                "for file %s\n",\r
-                me,schemalen,argv[0]);\r
-        exit(1);\r
-    }\r
-    \r
-    if (fread(asciischema,schemalen,1,ifd)!=1) {\r
-        fprintf(stderr,"%s::error: could not read schema buffer of sz %u "\r
-                "for file %s\n",\r
-                me,schemalen,argv[0]);\r
-        exit(1);\r
-    }\r
-    if (schema_only==1) {\r
-        fprintf(stdout,"%s\n",asciischema);\r
-    }\r
-    if (verbose==2) {\r
-        fprintf(stderr,"%s\n",asciischema);\r
-    }\r
-    if ((schema=ftaschema_parse_string(asciischema))<0) {\r
-        fprintf(stderr,"%s::error: could not parse schema  "\r
-                "for file %s\n",\r
-                me,argv[0]);\r
-        exit(1);\r
-    }\r
-    \r
-    if ((numberoffields=ftaschema_tuple_len(schema))<0) {\r
-        fprintf(stderr,"%s::error:could not get number of fields in schema\n",\r
-                me);\r
-        exit(1);\r
-    }\r
-    if (verbose==1) {\r
-        for(y=0; y<numberoffields;y++) {\r
-            printf("%s",ftaschema_field_name(schema,y));\r
-            if (y<numberoffields-1) printf("|");\r
-        }\r
-        printf("\n");\r
-    }\r
-    while(read_tuple(&rsize,rbuf,2*MAXTUPLESZ)==0) {\r
-        if ((!rsize) || (schema_only==1))\r
-            continue;\r
-        for(y=0; y<numberoffields;y++) {\r
-            struct access_result ar;\r
-            if (verbose==2)\r
-                printf("%s->",ftaschema_field_name(schema,y));\r
-            ar=ftaschema_get_field_by_index(schema,y,rbuf,rsize);\r
-            switch (ar.field_data_type) {\r
-                case INT_TYPE:\r
-                    printf("%d",ar.r.i);\r
-                    break;\r
-                case UINT_TYPE:\r
-                    printf("%u",ar.r.ui);\r
-                    break;\r
-                case IP_TYPE:\r
-                    if (scramble!=0) {\r
-                        ar.r.ui=gscpbswap32(ar.r.ui^scramble);\r
-                    }\r
-                    printf("%u.%u.%u.%u",ar.r.ui>>24&0xff,\r
-                           ar.r.ui>>16&0xff,\r
-                           ar.r.ui>>8&0xff,\r
-                           ar.r.ui&0xff);\r
-                    break;\r
-                case IPV6_TYPE:\r
-                {\r
-                    unsigned x;\r
-                    unsigned zc=0;\r
-                    for(x=0;x<4;x++) { if (ar.r.ip6.v[x]==0) zc++;}\r
-                    if (zc!=4) {\r
-                        for(x=0;x<8;x++) {\r
-                            unsigned char * a = (unsigned char *)  &(ar.r.ip6.v[0]);\r
-                            unsigned y;\r
-                            y=((unsigned)a[2*x])<<8|((unsigned) a[2*x+1]);\r
-                            printf("%04x",y);\r
-                            if (x<7) printf(":");\r
-                        }\r
-                    } else {\r
-                        printf("::");\r
-                    }\r
-                }\r
-                    break;\r
-                case USHORT_TYPE:\r
-                    printf("%u",ar.r.ui);\r
-                    break;\r
-                case BOOL_TYPE:\r
-                    if (ar.r.ui==0) {\r
-                        printf("FALSE");\r
-                    } else {\r
-                        printf("TRUE");\r
-                    }\r
-                    break;\r
-                case ULLONG_TYPE:\r
-                {\r
-                    gs_uint64_t ul;\r
-                    gs_uint64_t t1;\r
-                    gs_uint64_t t2;\r
-                    ul=ar.r.ul;\r
-                    printf("%llu",ul);\r
-                }\r
-                    break;\r
-                case LLONG_TYPE:\r
-                    printf("%lld",ar.r.l);\r
-                    break;\r
-                case FLOAT_TYPE:\r
-                    printf("%f",ar.r.f);\r
-                    break;\r
-                case TIMEVAL_TYPE:\r
-                {\r
-                    gs_float_t t;\r
-                    t= ar.r.t.tv_usec;\r
-                    t=t/1000000;\r
-                    t=t+ar.r.t.tv_sec;\r
-                    printf("%lf sec",t);\r
-                }\r
-                    break;\r
-                case VSTR_TYPE:\r
-                {\r
-                    int x;\r
-                    int c;\r
-                    char * src;\r
-                    src=(gs_sp_t)ar.r.vs.offset;\r
-                    if ((truncate>=0) && (ar.r.vs.length>truncate)) {\r
-                        ar.r.vs.length=truncate;\r
-                    }\r
-                    if ((ar.r.vs.length>0) && (src[ar.r.vs.length-1]==0)) {\r
-                        ar.r.vs.length = ar.r.vs.length-1;\r
-                    }\r
-                    for(x=0;x<ar.r.vs.length;x++) {\r
-                        c=src[x];\r
-                        if (((c<='~') && (c>=' '))&&(c!='|')) {\r
-                            printf("%c",c);\r
-                        } else {\r
-                            printf("(0x%x)",(gs_uint8_t)c);\r
-                        }\r
-                    }\r
-                }\r
-                    break;\r
-                default:\r
-                    break;\r
-            }\r
-            if (y<numberoffields-1) printf("|");\r
-        }\r
-        printf("\n");\r
-        if (verbose!=0) fflush(stdout);\r
-    }\r
-    exit(0);\r
-}\r
-\r
+/* ------------------------------------------------
+ Copyright 2014 AT&T Intellectual Property
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ ------------------------------------------- */
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <schemaparser.h>
+#include "gsconfig.h"
+#include "gstypes.h"
+
+FILE * ifd;
+
+gs_retval_t read_tuple(gs_uint32_t * sz, gs_sp_t data, gs_int32_t maxlen) {
+    gs_uint32_t nsz;
+    static gs_uint32_t read=0;
+again:
+    if (fread(&nsz,sizeof(gs_uint32_t),1,ifd)!=1) {
+        
+        exit(0);
+    }
+    read=read+sizeof(gs_uint32_t);
+    *sz=ntohl(nsz);
+    if ((*sz)>maxlen) {
+        fprintf(stderr,"INTERNAL ERROR tuple to long for fixed buffer. Tuple sz %u\n",
+                (*sz));
+        *sz=0;
+        return 0;
+    }
+    if (*sz==0) goto again;
+    if (fread(data,(*sz),1,ifd)!=1) {
+        fprintf(stderr,"UNEXPECTED END OF FILE. Tryed to read tuple of size %u\n",
+                (*sz));
+        exit(0);
+    }
+    read=read+*sz;
+    return 0;
+}
+
+int main(int argc, char** argv) {
+    gs_uint32_t streamid;
+    gs_schemahandle_t schema;
+    
+    gs_uint32_t rstreamid;
+    gs_uint32_t rsize;
+    gs_int8_t rbuf[2*MAXTUPLESZ];
+    
+    gs_int32_t numberoffields;
+    gs_int32_t verbose=0;
+    gs_int32_t truncate=-1;
+    gs_int32_t y;
+    gs_int32_t parserversion;
+    gs_uint32_t schemalen;
+    gs_sp_t asciischema;
+    gs_sp_t me;
+    gs_int32_t ch;
+    gs_uint32_t scramble =0;
+    gs_uint32_t schema_only =0;
+    
+    /* initialize host library and the sgroup  */
+    
+    me=argv[0];
+    
+    if (argc<2) {
+        fprintf(stderr,
+                "%s::usage: %s -v -x -t <int> -s <int> <uncompressed_file_name>\n",
+                me,me);
+        exit(1);
+    }
+    
+    while ((ch = getopt(argc, argv, "ps:t:vx")) != -1) {
+        switch(ch) {
+            case 'p':
+                schema_only=1;
+                break;
+            case 'v':
+                verbose=1;
+                break;
+            case 'x':
+                verbose=2;
+                break;
+            case 't':
+                truncate=atoi(optarg);
+                break;
+            case 's':
+                sscanf(optarg,"%u",&scramble);
+        }
+    }
+    argc -= optind;
+    if (argc < 1) {
+        fprintf(stderr,"%s::usage: %s -v -x -t <int> <uncompressed_file_name>\n",
+                me,me);
+        exit(1);
+    }
+    argv += optind;
+    
+    if ((strcmp(argv[0],"-")!=0)&&(strcmp(argv[0],"stdin")!=0)) {
+        if ((ifd=fopen(argv[0],"r"))==0) {
+            fprintf(stderr,"%s::error:could not open input file %s\n",
+                    me,argv[0]);
+            exit(1);
+        }
+    } else {
+        ifd = stdin;
+    }
+    
+    if (fscanf(ifd,"GDAT\nVERSION:%u\nSCHEMALENGTH:%u\n",&parserversion,&schemalen)!=2) {
+        fprintf(stderr,"%s::error:unknown file type for file %s\n",
+                me,argv[0]);
+        exit(1);
+    }
+    
+    if (schemaparser_accepts_version(parserversion)!=1) {
+        fprintf(stderr,"%s::error: wrong parser version %u for file %s\n",
+                me,parserversion,argv[0]);
+        exit(1);
+    }
+    
+    if ((asciischema=malloc(schemalen))==0) {
+        fprintf(stderr,"%s::error: could not allocate schema buffer of sz %u "
+                "for file %s\n",
+                me,schemalen,argv[0]);
+        exit(1);
+    }
+    
+    if (fread(asciischema,schemalen,1,ifd)!=1) {
+        fprintf(stderr,"%s::error: could not read schema buffer of sz %u "
+                "for file %s\n",
+                me,schemalen,argv[0]);
+        exit(1);
+    }
+    if (schema_only==1) {
+        fprintf(stdout,"%s\n",asciischema);
+    }
+    if (verbose==2) {
+        fprintf(stderr,"%s\n",asciischema);
+    }
+    if ((schema=ftaschema_parse_string(asciischema))<0) {
+        fprintf(stderr,"%s::error: could not parse schema  "
+                "for file %s\n",
+                me,argv[0]);
+        exit(1);
+    }
+    
+    if ((numberoffields=ftaschema_tuple_len(schema))<0) {
+        fprintf(stderr,"%s::error:could not get number of fields in schema\n",
+                me);
+        exit(1);
+    }
+    if (verbose==1) {
+        for(y=0; y<numberoffields;y++) {
+            printf("%s",ftaschema_field_name(schema,y));
+            if (y<numberoffields-1) printf("|");
+        }
+        printf("\n");
+    }
+    while(read_tuple(&rsize,rbuf,2*MAXTUPLESZ)==0) {
+        if ((!rsize) || (schema_only==1))
+            continue;
+        for(y=0; y<numberoffields;y++) {
+            struct access_result ar;
+            if (verbose==2)
+                printf("%s->",ftaschema_field_name(schema,y));
+            ar=ftaschema_get_field_by_index(schema,y,rbuf,rsize);
+            switch (ar.field_data_type) {
+                case INT_TYPE:
+                    printf("%d",ar.r.i);
+                    break;
+                case UINT_TYPE:
+                    printf("%u",ar.r.ui);
+                    break;
+                case IP_TYPE:
+                    if (scramble!=0) {
+                        ar.r.ui=gscpbswap32(ar.r.ui^scramble);
+                    }
+                    printf("%u.%u.%u.%u",ar.r.ui>>24&0xff,
+                           ar.r.ui>>16&0xff,
+                           ar.r.ui>>8&0xff,
+                           ar.r.ui&0xff);
+                    break;
+                case IPV6_TYPE:
+                {
+                    unsigned x;
+                    unsigned zc=0;
+                    for(x=0;x<4;x++) { if (ar.r.ip6.v[x]==0) zc++;}
+                    if (zc!=4) {
+                        for(x=0;x<8;x++) {
+                            unsigned char * a = (unsigned char *)  &(ar.r.ip6.v[0]);
+                            unsigned y;
+                            y=((unsigned)a[2*x])<<8|((unsigned) a[2*x+1]);
+                            printf("%04x",y);
+                            if (x<7) printf(":");
+                        }
+                    } else {
+                        printf("::");
+                    }
+                }
+                    break;
+                case USHORT_TYPE:
+                    printf("%u",ar.r.ui);
+                    break;
+                case BOOL_TYPE:
+                    if (ar.r.ui==0) {
+                        printf("FALSE");
+                    } else {
+                        printf("TRUE");
+                    }
+                    break;
+                case ULLONG_TYPE:
+                {
+                    gs_uint64_t ul;
+                    gs_uint64_t t1;
+                    gs_uint64_t t2;
+                    ul=ar.r.ul;
+                    printf("%llu",ul);
+                }
+                    break;
+                case LLONG_TYPE:
+                    printf("%lld",ar.r.l);
+                    break;
+                case FLOAT_TYPE:
+                    printf("%f",ar.r.f);
+                    break;
+                case TIMEVAL_TYPE:
+                {
+                    gs_float_t t;
+                    t= ar.r.t.tv_usec;
+                    t=t/1000000;
+                    t=t+ar.r.t.tv_sec;
+                    printf("%lf sec",t);
+                }
+                    break;
+                case VSTR_TYPE:
+                {
+                    int x;
+                    int c;
+                    char * src;
+                    src=(gs_sp_t)ar.r.vs.offset;
+                    if ((truncate>=0) && (ar.r.vs.length>truncate)) {
+                        ar.r.vs.length=truncate;
+                    }
+                    if ((ar.r.vs.length>0) && (src[ar.r.vs.length-1]==0)) {
+                        ar.r.vs.length = ar.r.vs.length-1;
+                    }
+                    for(x=0;x<ar.r.vs.length;x++) {
+                        c=src[x];
+                        if (((c<='~') && (c>=' '))&&(c!='|')) {
+                            printf("%c",c);
+                        } else {
+                            printf("(0x%x)",(gs_uint8_t)c);
+                        }
+                    }
+                }
+                    break;
+                default:
+                    break;
+            }
+            if (y<numberoffields-1) printf("|");
+        }
+        printf("\n");
+        if (verbose!=0) fflush(stdout);
+    }
+    exit(0);
+}
+