Fixed newline characters throughout the code
[com/gs-lite.git] / src / ftacmp / field_list.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 #include<stdio.h>
16 #include<stdlib.h>
17
18 #include<vector>
19 #include<string>
20 #include<map>
21
22 #include"xml_t.h"
23 #include"field_list.h"
24
25 using namespace std;
26
27 struct type_map_t{
28         std::string from;
29         std::string to;
30 };
31
32 static struct type_map_t type_map[] = {
33         "BOOL",                         "BOOLEAN",
34         "Bool",                         "BOOLEAN",
35         "bool",                         "BOOLEAN",
36         "BOOLEAN",                      "BOOLEAN",
37         "INT",                          "INT",
38         "Int",                          "INT",
39         "int",                          "INT",
40         "INTEGER",                      "INT",
41         "INT(_huge_)",          "INT(_huge_)",
42         "LLONG",                        "INT(_huge_)",
43         "Llong",                        "INT(_huge_)",
44         "llong",                        "INT(_huge_)",
45         "INTEGER(_huge_)",      "INT(_huge_)",
46         "UINT",                         "UINT",
47         "Uint",                         "UINT",
48         "uint",                         "UINT",
49         "USHORT",                       "UINT",
50         "Ushort",                       "UINT",
51         "ushort",                       "UINT",
52         "UINT(_huge_)",         "UINT(_huge_)",
53         "ULLONG",                       "UINT(_huge_)",
54         "Ullong",                       "UINT(_huge_)",
55         "ullong",                       "UINT(_huge_)",
56         "STR",                          "STRING",
57         "STRING",                       "STRING",
58         "FLT",                          "FLOAT",
59         "FLOAT",                        "FLOAT",
60         "Float",                        "FLOAT",
61         "float",                        "FLOAT",
62         "DATE",                         "DATE",
63         "CLOCK",                        "CLOCK",
64         "DATE_CLOCK",           "DATE_CLOCK",
65         "TIME",                         "TIME",
66         "IP",                           "IP2",
67         "IP2",                          "IP2",
68         "STRING",                       "STRING",
69         "STR",                          "STRING",
70         "String",                       "STRING",
71         "string",                       "STRING",
72         "V_STR",                        "STRING",
73         "V_str",                        "STRING",
74         "v_str",                        "STRING",
75         "IPV6",                         "IP6z",
76         "IP6",                          "IP6z",
77         "IP6z",                         "IP6z",
78         "",                                     ""
79 };
80
81
82         
83 field_list::field_list(xml_t *x){
84         int i,j,pos;
85         for(i=0;type_map[i].from != ""; i++){
86                 type_verifier[type_map[i].from] = type_map[i].to;
87         }
88         type_verifier[""] = "";
89
90         for(i=0;i<x->leaves.size();i++){
91                 if(x->leaves[i]->name != "field"){
92                         fprintf(stderr,"WARNING, unrecognized leaf element %s in field list file, ignoring.\n",x->leaves[i]->name.c_str());
93                         continue;
94                 }
95
96                 std::string fname, ftype;
97                 for(j=0;j<x->leaves[i]->attrs.size();j++){
98                         if(x->leaves[i]->attrs[j] == "name"){
99                                 if(fname != ""){
100                                         fprintf(stderr,"WARNING, found duplicate name property for a field (#%d, %s) in the field list, ignoring second name %s.\n",i,fname.c_str(), x->leaves[i]->vals[j].c_str());
101                                 }else{
102                                         fname = x->leaves[i]->vals[j];
103                                 }
104                         }
105                 }
106                 if(fname == ""){
107                         fprintf(stderr,"WARNING, field %d has an empty name, ignoring\n",i);
108                         continue;
109                 }
110
111                 for(j=0;j<x->leaves[i]->attrs.size();j++){
112                         if(x->leaves[i]->attrs[j] == "type"){
113                                 if(ftype != ""){
114                                         fprintf(stderr,"WARNING, found duplicate type property for field %s in the field list, ignoring.\n",fname.c_str());
115                                 }else{
116                                         ftype = x->leaves[i]->vals[j];
117                                 }
118                         }
119                 }
120
121                 if(fields.count(fname)){
122                         fprintf(stderr,"WARNING, duplicate field list name %s, ignoring repeat entry.\n",fname.c_str());
123                         continue;
124                 }
125                 if(type_verifier.count(ftype) == 0){
126                         fprintf(stderr,"WARNING, field %s in the field list has unrecognized type %s, ignoring the type.\n",fname.c_str(),ftype.c_str());
127                         ftype = "";
128                 }
129
130                 string::size_type ix = fname.rfind("/");
131                 if(ix != string::npos){
132                         fname = fname.substr(ix+1);
133                 }
134
135                 fields[fname] = ftype;
136         }
137 }
138
139