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 < style="">
return;
wstring::size_type p2 = cl.find(L” -”,p1+1);
if((int)p2 < style="">
p2 = cl.length();
wstring pair = cl.substr(p1,(p2-p1));
int mid = (int)pair.find(L” “);
tb.insert(make_pair
ParseCommandLine(cl,p2);
}
//This checks the presence of a key
inline int CLHasKey(wstring cl,wstring key)
{
if((int)cl.find(L“-”+key+L” “) < style="">
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 < style="">
return L“”;
wstring::size_type p2 = cl.find(L” -”,p1+1);
if((int)p2 < style="">
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
Leave a Reply