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
7 http://www.apache.org/licenses/LICENSE-2.0
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 ------------------------------------------- */
20 #include <schemaparser.h>
30 #define MAX_GDAT_HEADER 10000
32 gs_int8_t buf[2*MAXTUPLESZ];
33 gs_int8_t schema_buf[MAX_GDAT_HEADER];
34 gs_int8_t header_buf[MAX_GDAT_HEADER];
37 gs_retval_t read_tuple(gs_uint32_t * sz, gs_sp_t data, gs_int32_t maxlen) {
39 static gs_uint32_t read=0;
41 if (fread(&nsz,sizeof(gs_uint32_t),1,ifd)!=1) {
45 read=read+sizeof(gs_uint32_t);
48 fprintf(stderr,"INTERNAL ERROR tuple to long for fixed buffer. Tuple sz %u\n",
53 if (*sz==0) goto again;
54 if (fread(data,(*sz),1,ifd)!=1) {
55 fprintf(stderr,"UNEXPECTED END OF FILE. Tried to read tuple of size %u\n",
63 int main(int argc, char** argv) {
64 gs_schemahandle_t schema;
67 gs_int8_t rbuf[2*MAXTUPLESZ];
68 gs_int8_t wbuf[2*MAXTUPLESZ];
70 gs_int32_t numberoffields;
73 gs_int32_t parserversion;
74 gs_uint32_t schemalen;
82 "%s::usage: %s -v -x <schema_file_name> <input_file_name> <output_file_name>\n",
87 while ((ch = getopt(argc, argv, "vx")) != -1) {
98 fprintf(stderr,"%s::usage: %s -v -x <schema_file_name> <input_file_name> <output_file_name>\n",
104 if ((sfd=fopen(argv[0],"r"))==0) {
105 fprintf(stderr,"%s::error:could not open schema file %s\n",
110 schemalen = fread(schema_buf, 1, MAX_GDAT_HEADER, sfd);
111 schema_buf[schemalen] = 0;
112 schema = ftaschema_parse_string_prot(schema_buf);
114 fprintf(stderr,"%s::error:unable to parse schema file %s\n",
122 if ((strcmp(argv[0],"-")!=0)&&(strcmp(argv[0],"stdin")!=0)) {
123 if ((ifd=fopen(argv[0],"r"))==0) {
124 fprintf(stderr,"%s::error:could not open input file %s\n",
134 if ((ofd=fopen(argv[0],"wb"))==0) {
135 fprintf(stderr,"%s::error:could not open output file %s\n",
140 parserversion = get_schemaparser_version();
143 sprintf(header_buf,"GDAT\nVERSION:%u\nSCHEMALENGTH:%lu\n",
144 parserversion,schemalen+1);
145 // need to get ASCII version of schema
146 fwrite(header_buf,strlen(header_buf),1,ofd);
147 fwrite(schema_buf,schemalen+1,1,ofd);
149 /////////////////////////////////////////////
152 if ((numberoffields=ftaschema_tuple_len(schema))<0) {
153 fprintf(stderr,"%s::error:could not get number of fields in schema\n",
158 for(y=0; y<numberoffields;y++) {
159 printf("%s",ftaschema_field_name(schema,y));
160 if (y<numberoffields-1) printf("|");
165 unsigned long long tup_cnt = 0;
168 fgets(rbuf,2*MAXTUPLESZ,ifd);
170 gs_uint32_t tuple_pos = ftaschema_get_tuple_metadata_offset(schema) + 1; // position to copy string payload
172 char* field = strtok(rbuf, ",");
175 gs_int32_t field_type = ftaschema_get_field_type_by_index(schema, y);
176 gs_int32_t field_offset = ftaschema_get_field_offset_by_index(schema, y);
182 gs_float_t f,intpart,fractpart;
185 struct hfta_ipv6_str ip6;
187 unsigned ip1,ip2,ip3,ip4;
189 switch (field_type) {
191 ui=(strncasecmp("TRUE",field,4)==0);
192 memcpy(wbuf+field_offset,&ui,sizeof(ui));
195 sscanf(field,"%d",&i);
196 memcpy(wbuf+field_offset,&i,sizeof(i));
200 sscanf(field,"%u",&ui);
201 memcpy(wbuf+field_offset,&ui,sizeof(ui));
204 sscanf(field,"%llu",&ul);
205 memcpy(wbuf+field_offset,&ul,sizeof(ul));
208 sscanf(field,"%ldu",&l);
209 memcpy(wbuf+field_offset,&l,sizeof(l));
212 sscanf(field,"%f",&f);
213 memcpy(wbuf+field_offset,&f,sizeof(f));
217 if (!strcmp(field, " "))
219 vs.length = strlen(field);
220 vs.offset = tuple_pos;
222 memcpy(wbuf+vs.offset, field, vs.length);
223 memcpy(wbuf+field_offset,&vs,sizeof(vs));
224 tuple_pos += vs.length;
228 sscanf(field,"%u.%u.%u.%u",&ip1,&ip2,&ip3,&ip4);
229 ui=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
230 memcpy(wbuf+field_offset,&ui,sizeof(ui));
233 sscanf(field,"%x:%x:%x:%x:%x:%x:%x:%x",&v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
234 ip6.v[0]=htonl(v[0]<<16|v[1]);
235 ip6.v[1]=htonl(v[2]<<16|v[3]);
236 ip6.v[2]=htonl(v[4]<<16|v[5]);
237 ip6.v[3]=htonl(v[6]<<16|v[7]);
238 memcpy(wbuf+field_offset,&ip6,sizeof(ip6));
241 sscanf(field,"%lf sec",&f);
242 fractpart=modf(f,&intpart);
244 t.tv_usec = fractpart * 1000000;
245 memcpy(wbuf+field_offset,&t,sizeof(t));
251 field = strtok(NULL, ",");
252 } while (y < numberoffields);
253 gs_uint32_t tup_len = htonl(tuple_pos);
254 fwrite(&tup_len,sizeof(gs_uint32_t),1,ofd);
255 fwrite(wbuf,tuple_pos,1,ofd);
258 if (tup_cnt % 1000000 == 0)
259 printf("dumped %llu tuples\n", tup_cnt);
264 if (verbose!=0) fflush(stdout);