Parse command line and some utility functions
Posted on | August 9, 2007 |
I was assigned to write a dictionary module for a huge MFC project. The library as well as the executable has to be fully compliance with standard c++.
Now, I have to write a parser. I know I don’t really have to write one, as there is already many out in the net. Why to make the wheel again ? Well known answer — as I can
//Declare
wstring CLGetKeyValue(wstring cl ,wstring key, wstring::size_type pos=0);
void ParseCommandLine(wstring cl,wstring::size_type pos=0);
std::map <wstring,wstring> tb;
//this is for parsing
void ParseCommandLine(wstring cl,wstring::size_type pos)
{
wstring::size_type p1 = cl.find(L“-”,pos);
if((int)p1
return;
wstring::size_type p2 = cl.find(L” -”,p1+1);
if((int)p2
p2 = cl.length();
wstring pair = cl.substr(p1,(p2-p1));
int mid = (int)pair.find(L” “);
tb.insert(make_pair(pair.substr(1,mid-1),pair.substr(mid+1,pair.length()))) ;
ParseCommandLine(cl,p2);
}
//This checks the presence of a key
inline int CLHasKey(wstring cl,wstring key)
{
if((int)cl.find(L“-”+key+L” “)
return 0;
return 1;
}
//It returns the key value, assumed that the key is present
wstring CLGetKeyValue(wstring cl, wstring key, wstring::size_type pos)
{
//should be checked CLHasKey(cl,key);
wstring::size_type p1 = cl.find(L“-”,pos);
if((int)p1
return L“”;
wstring::size_type p2 = cl.find(L” -”,p1+1);
if((int)p2
p2 = cl.length();
wstring pair = cl.substr(p1,(p2-p1));
int mid = (int)pair.find(L” “);
if( key.compare(pair.substr(1,mid-1)) ==0 )
{
return pair.substr(mid+1,pair.length());
}
return CLGetKeyValue(cl,key,p2);
}
Comments
2 Responses to “Parse command line and some utility functions”
Leave a Reply
April 6th, 2008 @ 11:14 am
stop saying grose things about this! It’s your terrible way of thinking! Normal people don’t care about stuff like that
April 9th, 2008 @ 2:27 am
Very well! Are you dudes agree with me? When I read it I was like: oh man!Sounds great! This is just what I was searching for! All is true, and checked, I’m sure.