Rewrite NTS Framework.
[sim/o1-interface.git] / ntsimulator / regxstring / gen_regex.cpp
diff --git a/ntsimulator/regxstring/gen_regex.cpp b/ntsimulator/regxstring/gen_regex.cpp
new file mode 100644 (file)
index 0000000..e0a4953
--- /dev/null
@@ -0,0 +1,86 @@
+/*************************************************************************\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 <string>\r
+#include <cstdlib>\r
+#include <iostream>\r
+\r
+#include "regxstring.h"\r
+\r
+using namespace std;\r
+\r
+static 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 string pre_handle(const string & str)\r
+{\r
+    string ret = trim(str);\r
+    if(!ret.empty()) {\r
+        if(ret[0] != '^') {\r
+            ret.insert(ret.begin(),'^');\r
+        }\r
+\r
+        if(ret[ret.size() - 1] != '$') {\r
+            ret.push_back('$');\r
+        }\r
+    }\r
+    return ret;\r
+}\r
+\r
+static void rand_init(void) {\r
+    unsigned int seed;\r
+    FILE* urandom = fopen("/dev/urandom", "r");\r
+    size_t ret = fread(&seed, sizeof(int), 1, urandom);\r
+    (void)ret;\r
+    fclose(urandom);\r
+    srand(seed);\r
+    srandom(seed);\r
+}\r
+\r
+int main(int argc, const char ** argv)\r
+{\r
+    CRegxString regxstr;\r
+    string regx = "";\r
+\r
+    switch(argc) {\r
+        case 2:\r
+            rand_init();\r
+            regx = argv[1];\r
+            break;\r
+\r
+        case 3:\r
+            int pseudo_seed = 0;\r
+            int i = 0;\r
+            while(argv[1][i]) {\r
+                pseudo_seed *= 10;\r
+                pseudo_seed += argv[1][i] - '0';\r
+                i++;\r
+            }\r
+            srand(pseudo_seed);\r
+            srandom(pseudo_seed);\r
+            regx = argv[2];\r
+            break;\r
+    }\r
+\r
+    regxstr.ParseRegx(pre_handle(regx).c_str());\r
+    cout << regxstr.RandString() << endl;\r
+    \r
+    return 0;\r
+}\r