Rewrite NTS Framework.
[sim/o1-interface.git] / ntsimulator / regxstring / main.cpp
1 /*************************************************************************\r
2 *\r
3 * Licensed under the Apache License, Version 2.0 (the "License");\r
4 * you may not use this file except in compliance with the License.\r
5 * You may obtain a copy of the License at\r
6 *\r
7 *     http://www.apache.org/licenses/LICENSE-2.0\r
8 *\r
9 * Unless required by applicable law or agreed to in writing, software\r
10 * distributed under the License is distributed on an "AS IS" BASIS,\r
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 * See the License for the specific language governing permissions and\r
13 * limitations under the License.\r
14 ***************************************************************************/\r
15 \r
16 #include <iostream>\r
17 #include <fstream>\r
18 #include <string>\r
19 #include <cstdlib>\r
20 #include <cctype>\r
21 \r
22 #include "regxstring.h"\r
23 \r
24 using namespace std;\r
25 \r
26 bool debug;\r
27 const char * filename;\r
28 \r
29 static std::string Trim(std::string str){\r
30     size_t i = 0,e = str.length();\r
31     for(;i < e && std::isspace(str[i]);++i);\r
32     size_t j = e;\r
33     for(;j > i && std::isspace(str[j - 1]);--j);\r
34     return (i < j ? str.substr(i,j - i) : "");\r
35 }\r
36 \r
37 static bool ExtractArg(const char * argstr,const char * pattern,const char *& result)\r
38 {\r
39     if(!argstr || !pattern)\r
40         return false;\r
41     for(;*pattern;++pattern,++argstr)\r
42         if(*pattern != *argstr)\r
43             return false;\r
44     result = *argstr ? argstr : 0;\r
45     return true;\r
46 }\r
47 \r
48 static const char * ProgramName(const char * argstr)\r
49 {\r
50     const char * ret = argstr;\r
51     for(const char * cur = argstr;cur && *cur;++cur)\r
52         if(*cur == '/')\r
53             ret = cur + 1;\r
54     return ret;\r
55 }\r
56 \r
57 static std::string pre_handle(const std::string & str)\r
58 {\r
59     std::string ret = Trim(str);\r
60     if(!ret.empty()){\r
61         if(ret[0] != '^')\r
62             ret.insert(ret.begin(),'^');\r
63         if(ret[ret.size() - 1] != '$')\r
64             ret.push_back('$');\r
65     }\r
66     return ret;\r
67 }\r
68 \r
69 #ifdef TEST\r
70 #   include "test.h"\r
71 #endif\r
72 \r
73 static void printUsage(const char * exe)\r
74 {\r
75     cout<<"Usage: "<<exe<<" [N] [-t] [-d] [-f=FILE] [-h|?|--help]\n"\r
76         <<"  N          generate N random strings, default 1\n"\r
77 #ifdef TEST\r
78         <<"  -t         batch test\n"\r
79 #endif\r
80         <<"  -d         output debug info\n"\r
81         <<"  -f         use FILE as input\n"\r
82         <<"  -h\n"\r
83         <<"  ?\n"\r
84         <<"  --help     print this message\n"\r
85         <<endl;\r
86 }\r
87 \r
88 static void use(int c)\r
89 {\r
90     CRegxString regxstr;\r
91     std::string regx;\r
92     std::istream * in = &cin;\r
93     if(filename){\r
94         std::ifstream * file = new std::ifstream(filename);\r
95         if(!file->is_open()){\r
96             delete file;\r
97             cerr<<"cannot open file "<<filename<<endl;\r
98             return;\r
99         }\r
100         in = file;\r
101     }\r
102     while(std::getline(*in,regx)){\r
103         regxstr.ParseRegx(pre_handle(regx).c_str());\r
104         if(debug)\r
105             regxstr.Debug(cout);\r
106         for(int i = 0;i < c;++i)\r
107             cout<<regxstr.RandString()<<endl;\r
108         cout<<endl;\r
109     }\r
110     if(filename)\r
111         delete in;\r
112 }\r
113 \r
114 int main(int argc,const char ** argv)\r
115 {\r
116     int c = 1;\r
117     bool test = false;\r
118     for(int i = 1;i < argc;++i){\r
119         const char * ret = 0;\r
120         if((ExtractArg(argv[i],"-h",ret) ||\r
121             ExtractArg(argv[i],"?",ret) ||\r
122             ExtractArg(argv[i],"--help",ret)) && !ret)\r
123         {\r
124             printUsage(ProgramName(argv[0]));\r
125             return 1;\r
126         }else if((ExtractArg(argv[i],"-t",ret) || ExtractArg(argv[i],"-test",ret)) && !ret){\r
127             test = true;\r
128         }else if((ExtractArg(argv[i],"-d",ret) || ExtractArg(argv[i],"-debug",ret)) && !ret){\r
129             debug = true;\r
130         }else if(ExtractArg(argv[i],"-f=",ret) && ret){\r
131             filename = ret;\r
132         }else\r
133             if((c = atoi(argv[i])) <= 0){\r
134                 printUsage(ProgramName(argv[0]));\r
135                 return 1;\r
136             }\r
137     }\r
138 #ifdef TEST\r
139     if(test)\r
140         test_pcre(c);\r
141     else\r
142 #endif\r
143         use(c);\r
144     //test_01();\r
145 #if _MEM_LEAK\r
146     cerr<<"__NodeBase::ref = "<<__NodeBase::ref<<endl;\r
147 #endif\r
148     return 0;\r
149 }\r