Rewrite NTS Framework.
[sim/o1-interface.git] / ntsimulator / regxstring / main.cpp
diff --git a/ntsimulator/regxstring/main.cpp b/ntsimulator/regxstring/main.cpp
new file mode 100644 (file)
index 0000000..bfc18e3
--- /dev/null
@@ -0,0 +1,149 @@
+/*************************************************************************\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*     http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+***************************************************************************/\r
+\r
+#include <iostream>\r
+#include <fstream>\r
+#include <string>\r
+#include <cstdlib>\r
+#include <cctype>\r
+\r
+#include "regxstring.h"\r
+\r
+using namespace std;\r
+\r
+bool debug;\r
+const char * filename;\r
+\r
+static std::string Trim(std::string str){\r
+    size_t i = 0,e = str.length();\r
+    for(;i < e && std::isspace(str[i]);++i);\r
+    size_t j = e;\r
+    for(;j > i && std::isspace(str[j - 1]);--j);\r
+    return (i < j ? str.substr(i,j - i) : "");\r
+}\r
+\r
+static bool ExtractArg(const char * argstr,const char * pattern,const char *& result)\r
+{\r
+    if(!argstr || !pattern)\r
+        return false;\r
+    for(;*pattern;++pattern,++argstr)\r
+        if(*pattern != *argstr)\r
+            return false;\r
+    result = *argstr ? argstr : 0;\r
+    return true;\r
+}\r
+\r
+static const char * ProgramName(const char * argstr)\r
+{\r
+    const char * ret = argstr;\r
+    for(const char * cur = argstr;cur && *cur;++cur)\r
+        if(*cur == '/')\r
+            ret = cur + 1;\r
+    return ret;\r
+}\r
+\r
+static std::string pre_handle(const std::string & str)\r
+{\r
+    std::string ret = Trim(str);\r
+    if(!ret.empty()){\r
+        if(ret[0] != '^')\r
+            ret.insert(ret.begin(),'^');\r
+        if(ret[ret.size() - 1] != '$')\r
+            ret.push_back('$');\r
+    }\r
+    return ret;\r
+}\r
+\r
+#ifdef TEST\r
+#   include "test.h"\r
+#endif\r
+\r
+static void printUsage(const char * exe)\r
+{\r
+    cout<<"Usage: "<<exe<<" [N] [-t] [-d] [-f=FILE] [-h|?|--help]\n"\r
+        <<"  N          generate N random strings, default 1\n"\r
+#ifdef TEST\r
+        <<"  -t         batch test\n"\r
+#endif\r
+        <<"  -d         output debug info\n"\r
+        <<"  -f         use FILE as input\n"\r
+        <<"  -h\n"\r
+        <<"  ?\n"\r
+        <<"  --help     print this message\n"\r
+        <<endl;\r
+}\r
+\r
+static void use(int c)\r
+{\r
+    CRegxString regxstr;\r
+    std::string regx;\r
+    std::istream * in = &cin;\r
+    if(filename){\r
+        std::ifstream * file = new std::ifstream(filename);\r
+        if(!file->is_open()){\r
+            delete file;\r
+            cerr<<"cannot open file "<<filename<<endl;\r
+            return;\r
+        }\r
+        in = file;\r
+    }\r
+    while(std::getline(*in,regx)){\r
+        regxstr.ParseRegx(pre_handle(regx).c_str());\r
+        if(debug)\r
+            regxstr.Debug(cout);\r
+        for(int i = 0;i < c;++i)\r
+            cout<<regxstr.RandString()<<endl;\r
+        cout<<endl;\r
+    }\r
+    if(filename)\r
+        delete in;\r
+}\r
+\r
+int main(int argc,const char ** argv)\r
+{\r
+    int c = 1;\r
+    bool test = false;\r
+    for(int i = 1;i < argc;++i){\r
+        const char * ret = 0;\r
+        if((ExtractArg(argv[i],"-h",ret) ||\r
+            ExtractArg(argv[i],"?",ret) ||\r
+            ExtractArg(argv[i],"--help",ret)) && !ret)\r
+        {\r
+            printUsage(ProgramName(argv[0]));\r
+            return 1;\r
+        }else if((ExtractArg(argv[i],"-t",ret) || ExtractArg(argv[i],"-test",ret)) && !ret){\r
+            test = true;\r
+        }else if((ExtractArg(argv[i],"-d",ret) || ExtractArg(argv[i],"-debug",ret)) && !ret){\r
+            debug = true;\r
+        }else if(ExtractArg(argv[i],"-f=",ret) && ret){\r
+            filename = ret;\r
+        }else\r
+            if((c = atoi(argv[i])) <= 0){\r
+                printUsage(ProgramName(argv[0]));\r
+                return 1;\r
+            }\r
+    }\r
+#ifdef TEST\r
+    if(test)\r
+        test_pcre(c);\r
+    else\r
+#endif\r
+        use(c);\r
+    //test_01();\r
+#if _MEM_LEAK\r
+    cerr<<"__NodeBase::ref = "<<__NodeBase::ref<<endl;\r
+#endif\r
+    return 0;\r
+}\r