Fixed newline characters throughout the code
[com/gs-lite.git] / src / tools / gdatcat.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
16 #include "gsconfig.h"
17 #include "gstypes.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <time.h>
25 #include <unistd.h>
26
27
28
29 gs_sp_t me = 0;
30 gs_sp_t schematext = 0;
31 gs_int32_t schematextlen = 0;
32 gs_sp_t schematmp = 0;
33
34 static void do_file(gs_sp_t filename, gs_int32_t fnlen);
35
36 int main(int argc, char** argv) {
37     gs_int32_t x;
38     gs_int32_t s=1;
39     gs_int32_t debug=0;
40     if (strcmp(argv[s],"-d")==0) {
41         debug=1;
42         s++;
43     }
44     for(x=s;x<argc;x++) {
45         if (debug) {
46             fprintf(stderr,"%s\n",argv[x]);
47         }
48         do_file(argv[x], strlen(argv[x]));
49     }
50     return 0;
51 }
52
53
54 /*
55  * do_file: dump the file out
56  */
57
58 static void do_file(gs_sp_t filename, gs_int32_t fnlen) {
59     gs_int32_t pipe, parserversion, schemalen;
60     FILE *input;
61     gs_int8_t cmd2[4096 + 128];
62     static gs_int8_t *dbuf;
63     size_t sz;
64     
65     if (fnlen > 3 && filename[fnlen - 3] == '.' &&
66         filename[fnlen - 2] == 'g' &&
67         filename[fnlen - 1] == 'z') {
68         pipe = 1;
69         snprintf(cmd2, sizeof(cmd2), "gzcat %s", filename);
70         input = popen(cmd2, "r");
71     } else {
72         if (fnlen > 3 && filename[fnlen - 3] == 'b' &&
73             filename[fnlen - 2] == 'z' &&
74             filename[fnlen - 1] == '2') {
75             pipe = 1;
76             snprintf(cmd2, sizeof(cmd2), "bzcat %s", filename);
77             input = popen(cmd2, "r");
78         } else {
79             input = fopen(filename, "r");
80         }
81     }
82     
83     if (!input) {
84         perror("stream open");
85         fprintf(stderr, "%s: cannot open %s\n", me, filename);
86         return;
87     }
88     
89     if (fscanf(input, "GDAT\nVERSION:%u\nSCHEMALENGTH:%u\n",
90                &parserversion,&schemalen) != 2) {
91         fprintf(stderr,"%s: cannot parse GDAT file header in '%s'\n",
92                 me, filename);
93         exit(1);
94     }
95     
96     /* first time ? */
97     if (schematext == 0) {
98         schematextlen = schemalen;
99         schematext = malloc(schemalen);
100         dbuf = malloc(CATBLOCKSZ);
101         if (!schematext  || !dbuf) {
102             fprintf(stderr,"%s: malloc error reading GDAT file header in '%s'\n",
103                     me, filename);
104             exit(1);
105         }
106         if (fread(schematext, schemalen, 1, input) != 1) {
107             fprintf(stderr,"%s: cannot parse-read GDAT file header in '%s'\n",
108                     me, filename);
109             exit(1);
110         }
111         printf("GDAT\nVERSION:%u\nSCHEMALENGTH:%u\n", parserversion, schemalen);
112         fwrite(schematext, schemalen, 1, stdout);
113     } else {
114         schematmp = malloc(schemalen);
115         if (!schematmp ) {
116             fprintf(stderr,"%s: malloc error reading GDAT file header in '%s'\n",
117                     me, filename);
118             exit(1);
119         }
120         if (fread(schematmp, schemalen, 1, input) != 1) {
121             fprintf(stderr,"%s: cannot parse-read GDAT file header in '%s'\n",
122                     me, filename);
123             exit(1);
124         }
125         free(schematmp);
126         //   if (memcmp(schematext, schematmp, schematextlen)) {
127         //     fprintf(stderr,"%s: GDAT schema mis-match in file '%s'\n",
128         //             me, filename);
129         //     exit(1);
130         //   }
131     }
132     
133     while ((sz = fread(dbuf, 1, CATBLOCKSZ, input)) > 0) {
134         fwrite(dbuf, 1, sz, stdout);
135     }
136     
137     if (pipe) {
138         pclose(input);
139     } else {
140         fclose(input);
141     }
142     
143 }