Fixed newline characters throughout the code
[com/gs-lite.git] / src / ftacmp / test_interfacelib.cc
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 <string>
17
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include "schemaparser.h"
22 #include "schemaparser_impl.h"
23
24 //      Interface to FTA definition lexer and parser ...
25
26
27 //double htonf(double f){return f;}
28 //long long int htonll(long long int l){return l;}
29
30
31 using namespace std;
32 extern int errno;
33
34 // ./test_interfacelib test_param1.q_0.sch  test_param4.q_1.sch
35
36
37
38 int main(int argc, char **argv){
39         int i;
40         char tmpstr[1000];
41         void * pblock;
42         int block_size;
43         
44         if(argc < 2){
45                 fprintf(stderr,"Usage: %s schema_file [schema_file ... ]\n",argv[0]);
46                 exit(1);
47         }
48         
49         int nfiles = argc-1;
50         int f;
51         for(f=0;f<nfiles;++f){
52
53                 FILE *infl = fopen(argv[f+1],"r");
54                 if(infl == NULL){
55                         fprintf(stderr,"ERROR can't open schema file %s\n",argv[1]);
56                         exit(1);
57                 }
58
59                 int sch_handle;
60                 if(f%2==0){
61                         sch_handle = ftaschema_parse_file(infl);
62                 }else{
63                         string fstr;
64                         while(fgets(tmpstr, 1000, infl)){
65                                 fstr += tmpstr;
66                                 fstr += "\n";
67                         }
68                         sch_handle = ftaschema_parse_string( (char *)(fstr.c_str()) );
69                 }
70
71                 if(sch_handle < 0){
72                         fprintf(stderr,"ERROR parsing schema file.\n");
73                         exit(1);
74                 }
75
76                 ftaschema_debugdump(sch_handle);
77                 
78                 fclose(infl);
79         }
80         
81         printf("Testing access library.\n");
82         
83 //                      test_param1.q_0.sch
84
85         printf("Query %s has %d fields and %d parameters.\n",
86                 ftaschema_name(0), ftaschema_tuple_len(0), ftaschema_parameter_len(0));
87         printf("The parameters are named:\n");
88         for(i=0;i<ftaschema_parameter_len(0);++i){
89                 printf("%s  ",ftaschema_parameter_name(0,i));
90         }
91         printf("\n");
92         printf("Setting parameters by name.\n");
93         sprintf(tmpstr,"%d",1111); printf("min_hdr_length : %s\n",tmpstr);
94         ftaschema_setparam_by_name(0,"min_hdr_length",tmpstr,strlen(tmpstr));
95         sprintf(tmpstr,"%d",2222); printf("unused_param : %s\n",tmpstr);
96         ftaschema_setparam_by_name(0,"unused_param",tmpstr,strlen(tmpstr));
97         printf("Creating param block.\n");
98         ftaschema_create_param_block(0,&pblock, &block_size);
99         printf("Block created, size is %d. Accessing by name.\n",block_size);
100         printf("\t%s=%d (type is %d) %s=%lu (type is %d)\n",
101                 "time",
102                 (ftaschema_get_field_by_name(0,"time",pblock, block_size)).r.i,
103                 (ftaschema_get_field_by_name(0,"time",pblock, block_size)).field_data_type,
104                 "hdr_length",
105                 (ftaschema_get_field_by_name(0,"hdr_length",pblock, block_size)).r.ul,
106                 (ftaschema_get_field_by_name(0,"hdr_length",pblock, block_size)).field_data_type
107         );
108         
109         printf("Query %s has %d fields and %d parameters.\n",
110                 ftaschema_name(1), ftaschema_tuple_len(1), ftaschema_parameter_len(1));
111         printf("The parameters are named:\n");
112         for(i=0;i<ftaschema_parameter_len(1);++i){
113                 printf("%s  ",ftaschema_parameter_name(1,i));
114         }
115
116         printf("\n");
117         printf("Setting parameters by index.\n");
118         sprintf(tmpstr,"%d",3333); printf("an_ullong_parameter : %s\n",tmpstr);
119         ftaschema_setparam_by_index(1,0,tmpstr,strlen(tmpstr));
120         sprintf(tmpstr,"%d",2222); printf("ullong_parameter2 : %s\n",tmpstr);
121         ftaschema_setparam_by_index(1,1,tmpstr,strlen(tmpstr));
122         sprintf(tmpstr,"%f",4444.0); printf("min_prec : %s\n",tmpstr);
123         ftaschema_setparam_by_index(1,2,tmpstr,strlen(tmpstr)); 
124         sprintf(tmpstr,"%s","Foo"); printf("min_str : %s\n",tmpstr);
125         ftaschema_setparam_by_index(1,3,tmpstr,strlen(tmpstr)+1);       
126         sprintf(tmpstr,"%s","Bar"); printf("min_str : %s\n",tmpstr);
127         ftaschema_setparam_by_index(1,3,tmpstr,strlen(tmpstr)+1);       
128         ftaschema_create_param_block(1,&pblock, &block_size);
129         printf("Block created, size is %d. Accessing by index.\n",block_size);
130         printf("\t%s=%ld, type is %d\n",
131                 ftaschema_tuple_name(1,0),
132                 (ftaschema_get_field_by_index(1,0,pblock, block_size)).r.ul,
133                 (ftaschema_get_field_by_index(1,0,pblock, block_size)).field_data_type
134         );      
135         printf("\t%s=%ld, type is %d\n",
136                 ftaschema_tuple_name(1,1),
137                 (ftaschema_get_field_by_index(1,1,pblock, block_size)).r.ul,
138                 (ftaschema_get_field_by_index(1,1,pblock, block_size)).field_data_type
139         );      
140         printf("\t%s=%lf, type is %d\n",
141                 ftaschema_tuple_name(1,2),
142                 (ftaschema_get_field_by_index(1,2,pblock, block_size)).r.f,
143                 (ftaschema_get_field_by_index(1,2,pblock, block_size)).field_data_type
144         );      
145         printf("\t%s=%s (length=%d), type is %d\n",
146                 ftaschema_tuple_name(1,3),
147                 (ftaschema_get_field_by_index(1,3,pblock, block_size)).r.vs.offset,
148                 (ftaschema_get_field_by_index(1,3,pblock, block_size)).r.vs.length,
149                 (ftaschema_get_field_by_index(1,3,pblock, block_size)).field_data_type
150         );      
151         
152         
153         
154 }