1 /*************************************************************************
\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
7 * http://www.apache.org/licenses/LICENSE-2.0
\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
22 #include "regxstring.h"
\r
24 using namespace std;
\r
27 const char * filename;
\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
33 for(;j > i && std::isspace(str[j - 1]);--j);
\r
34 return (i < j ? str.substr(i,j - i) : "");
\r
37 static bool ExtractArg(const char * argstr,const char * pattern,const char *& result)
\r
39 if(!argstr || !pattern)
\r
41 for(;*pattern;++pattern,++argstr)
\r
42 if(*pattern != *argstr)
\r
44 result = *argstr ? argstr : 0;
\r
48 static const char * ProgramName(const char * argstr)
\r
50 const char * ret = argstr;
\r
51 for(const char * cur = argstr;cur && *cur;++cur)
\r
57 static std::string pre_handle(const std::string & str)
\r
59 std::string ret = Trim(str);
\r
62 ret.insert(ret.begin(),'^');
\r
63 if(ret[ret.size() - 1] != '$')
\r
73 static void printUsage(const char * exe)
\r
75 cout<<"Usage: "<<exe<<" [N] [-t] [-d] [-f=FILE] [-h|?|--help]\n"
\r
76 <<" N generate N random strings, default 1\n"
\r
78 <<" -t batch test\n"
\r
80 <<" -d output debug info\n"
\r
81 <<" -f use FILE as input\n"
\r
84 <<" --help print this message\n"
\r
88 static void use(int c)
\r
90 CRegxString regxstr;
\r
92 std::istream * in = &cin;
\r
94 std::ifstream * file = new std::ifstream(filename);
\r
95 if(!file->is_open()){
\r
97 cerr<<"cannot open file "<<filename<<endl;
\r
102 while(std::getline(*in,regx)){
\r
103 regxstr.ParseRegx(pre_handle(regx).c_str());
\r
105 regxstr.Debug(cout);
\r
106 for(int i = 0;i < c;++i)
\r
107 cout<<regxstr.RandString()<<endl;
\r
114 int main(int argc,const char ** argv)
\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
124 printUsage(ProgramName(argv[0]));
\r
126 }else if((ExtractArg(argv[i],"-t",ret) || ExtractArg(argv[i],"-test",ret)) && !ret){
\r
128 }else if((ExtractArg(argv[i],"-d",ret) || ExtractArg(argv[i],"-debug",ret)) && !ret){
\r
130 }else if(ExtractArg(argv[i],"-f=",ret) && ret){
\r
133 if((c = atoi(argv[i])) <= 0){
\r
134 printUsage(ProgramName(argv[0]));
\r
146 cerr<<"__NodeBase::ref = "<<__NodeBase::ref<<endl;
\r