Initial commit
[com/gs-lite.git] / src / ftacmp / nic_def.h
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 #ifndef _FTA_NIC_DEF_H_INCLUDED__
16 #define _FTA_NIC_DEF_H_INCLUDED__
17
18 #include<vector>
19 #include<string>
20 #include<map>
21
22 class nic_property{
23 public:
24         std::vector<std::string> opta;  // option args
25         std::vector<std::string> optv;  // val of option args
26         std::vector<std::string> typea; // legal types 
27         std::vector<std::string> opba;  // legal binary ops
28         std::vector<std::string> opua;  // legal unary ops
29         std::vector<std::string> fieldsa; // illegal fields
30         std::vector<std::string> funca;  // legal functions
31
32         nic_property(){};
33
34         bool legal_type(std::string t);
35         bool legal_unary_op(std::string t);
36         bool legal_binary_op(std::string t);
37         bool illegal_field(std::string t);
38         bool option_exists(std::string t);
39         std::string option_value(std::string t);
40 };
41
42
43 nic_property *load_nic_property(std::string dir, std::string name);
44
45
46 class nic_prop_db{
47 public:
48         std::string dir;
49         std::map<std::string, nic_property *> ndb;
50
51         nic_prop_db(std::string d){dir=d;};
52
53         
54         nic_property *get_nic_property(std::string nics, int &err_no){
55                 if(ndb.count(nics))
56                         return ndb[nics];
57                 nic_property *ret = load_nic_property(dir, nics);
58                 if(ret)
59                         ndb[nics] = ret;
60                 return ret;
61         };
62
63
64         std::string get_nic_prop_val(std::string nics, std::string prop,int &err_no){
65                 nic_property *nicp = this->get_nic_property(nics, err_no);
66                 if(!nicp){
67                         fprintf(stderr,"ERROR cannot load nic properties %s for nic type %s in get_nic_prop_val\n",nics.c_str(), prop.c_str());
68                                 exit(1);
69                 }
70                 if(nicp->option_exists(prop)){
71                         return nicp->option_value("Return");
72                 }else{
73                         return "";
74                 }
75
76         }
77 };
78
79
80 void NicParser_setfileinput(FILE *f);
81
82
83
84 #endif