C + + Implementation of common parameter resolution classes example _c language

Source: Internet
Author: User


Main.cpp

Copy Code code as follows:



#include <iostream>


#include <getopt.h>


#include "Parsingargs.h"


#include <string.h>


using namespace Std;


int main (int argc, char * argv[])
{

String Tmppara = "-P \" 4567\ "--out 1.log"; Ok


String Tmppara = "xxxx-p \" 4567\ "--out 1.log";//ok


String Tmppara = "-P \" 4567\ "--out 1.log 2.log"; Ok


String Tmppara = "";


for (int i=1;i <argc; i++)


{


cout << i << "=" << argv[i] << "---" << endl;


if (strlen (argv[i]) = = 0)//Handle empty string


{


cout << "Find NULL" << Endl;


Tmppara + + char (31);


}


Else


{


Tmppara + = Argv[i];


}


Tmppara + = "";


}


Std::map<std::string, std::vector<std::string> > result;


Parsingargs PA;


Pa. Addargtype (' l ', "getlist", Parsingargs::no_value);


Pa. Addargtype (' P ', "GetUser", Parsingargs::maybe_value);


Pa. Addargtype (' O ', "outfile", Parsingargs::must_value);


BOOL Bexit = false;


Todo


{


Result.clear ();


cout << "input is:" << Tmppara << "---size =" << tmppara.size () << Endl;


Std::string Errpos;


int iRet = Pa. Parse (Tmppara,result, Errpos);


if (0>iret)


{


cout << "parameter error" << iRet << errpos << Endl;


}


Else


{


Map<std::string, std::vector<std::string> >::iterator it = Result.begin ();


for (; it!= result.end (); ++it)


{


cout << "key=" << it->first<<endl;


for (int i=0; i<it->second.size (); ++i)


{


cout << "value =" << it->second[i] << "------" << Endl;


}


}


}


String str;


cout << ">>>";


Getline (CIN, Tmppara);


if (0 = Tmppara.compare ("Exit"))


{


Bexit = true;


}

}while (!bexit);
return 0;
}

Parsingargs.h

Copy Code code as follows:



#ifndef Parsingargs_h


#define Parsingargs_h


//Purpose @ Parse input parameters, first by Addargtype to add the required parameters and the allowed parameter key to the decision list


* Results are returned by result in parse, where the key of the result is a valid KEY,VECOTR for the argument list


* The parameter list supports the quotation marks and the escape of the quotes and \ Before and after the parameters are removed


*


* Special legal fields:


* Format actual store value


* \\value\ "\value"


* "\\\value\" "\value"


*


* Precautions:


* 1, the input parameter in the list of parameters separated by a space between


* 2,-followed by a single character keyword, followed by a long string keyword


* 3, the keyword can not be repeated, the length of the keyword can not appear in the argument list, otherwise the parse function will prompt parameter errors


*


Usage


* Parsingargs PA;


* Pa. Addargtype (' l ', "getlist", Parsingargs::no_value); Cannot have parameters after No_value keyword


* Pa. Addargtype (' P ', "GetUser", Parsingargs::maybe_value); There may be keywords after the Maybe_value keyword


* Pa. Addargtype (' O ', "outfile", Parsingargs::must_value); Must have parameters after Must_value keyword


* std::map<std::string, std::vector<std::string> > result;


* int iRet = Pa. Parse (Tmppara,result); Result is a sequence of values related to the input keyword for key storage


*


* Date @ 2014.02.19


* Author @ haibin.wang


*


*/

#include <map>
#include <vector>
#include <string>

Class Parsingargs
{
Public:
    Parsingargs ();
    ~parsingargs ();
    enum keyflag{invalid_key=-1, No_value, Maybe_value, must_value};
   /* Pur @ Add an explanation parameter, one parameter can be a long parameter, or an abbreviated segment parameter, a short argument can only be a single character, longname and ShortName must have at least one
      * para @ shortname short parameter name, 0 is not short parameter
     * para @ longname long parameter name, NULL is not long parameter
 &nbs p;   * Para @ flag requires parameters, 0 not required, 1 must, 2 can not
     * Return @ True Add success, false add failed
    */
    bool Addargtype (const char shortname, const char * longname = NULL, Keyflag FL Ag=no_value);

   /* Pur @ To interpret the Passed-in string based on the parameter type
     * Para @ paras need to interpret the string
      * Para @ results returns the parsed result
     * Para @ errpos Returns the approximate location of the error when the error
     * Return @ 0 explanation succeeded, negative explanation failed
     *         -1 Unknown parameter error
                2 options with no parameters have parameter errors
     *         -3 must have parameter options with no parameters
      *         -4 keyword not added to Addargtype
      *         -5 keyword repeat
    */
     int Parse (const STD::STRING & Paras, std::map<std::string, std::vector<std::string> > & result, std::string &errpos);

Private
/* Pur @ Determines whether the incoming parameter is a parameter type that has been added, if it is removed-or--, and returns
* Para @ Key to determine the parameters
* Return @-1 is not a valid argument type otherwise returns flag in option
*/
Keyflag Getkeyflag (std::string &key);

/* pur-@ Delete the keyword before-or-
*/
void Removekeyflag (Std::string & paras);

* Pur @ Get a WORD from the Paras, automatically filter out the quotes around the word, and implement \ Escape the spaces and quotes
* Para @ Paras returns all contents after the first word
* Para @ Word returns the first word
* Return @ successfully returned True,false failed
*/
BOOL Getword (Std::string & Paras, std::string & word);

/* Pur @ Check the keyword repeat
* Para @ key is checked for keyword
* Para @ result stored keyword sequence
* Return @ True is duplicate, false does not repeat
*/
BOOL Isduplicatekey (const std::string &key, const std::map<std::string, std::vector<std::string> > & result);

struct Option
{
Std::string M_longname;
Char M_shortname;
Keyflag M_flag;
};

Std::vector<option> M_args; Parameter Information list
};

#endif

Parsingargs.cpp

Copy Code code as follows:



#include "Parsingargs.h"


#include &lt;list&gt;

Parsingargs::P Arsingargs ()
{
}

Parsingargs::~parsingargs ()
{
}

BOOL Parsingargs::addargtype (char shortname, const char * longname, Keyflag flag)
{
if (NULL = = longname && 0 = shortname)
{
return false;
}
Option tmp;
Tmp.m_longname = longname;
Tmp.m_shortname = ShortName;
Tmp.m_flag = Flag;
M_args.push_back (TMP);
return true;
}

Parsingargs::keyflag Parsingargs::getkeyflag (std::string &amp;key)//return flag,


{


for (int i=0; i&lt;m_args.size (); ++i)


{


std::string shortname = "-";


std::string longname = "--";


ShortName + = M_args[i].m_shortname;


LongName + = M_args[i].m_longname;


if (0 = = Key.compare (shortname) | |


(0==key.compare (longname))


)


{


Removekeyflag (key);


return m_args[i].m_flag;


}


}


return invalid_key;


}

void Parsingargs::removekeyflag (std::string &amp; Word)


{


if (Word.size () &gt;=2)


{


if (word[1] = = '-')


{


Word.erase (1,1);


}


if (word[0] = = '-')


{


Word.erase (0,1);


}


}


}


* Pur @ Get a WORD from the Paras, automatically filter out the quotes around the word, and implement \ Escape the spaces and quotes


* Para @ Paras returns all contents after the first word


* Para @ Word returns the first word


* Return @ successfully returned True,false failed


*/


BOOL Parsingargs::getword (Std::string &amp; Paras, std::string &amp; Word)


{


size_t Inotspacepos = paras.find_first_not_of (', 0);//Find first non-space character position


if (Inotspacepos = = Std::string::npos)


{


Paras.clear ();


Word.clear ();


return true;


}


int length = Paras.size ();


Std::list&lt;char&gt; Specialchar;


int islashpos =-1;


for (int i=inotspacepos; i&lt;length; i++)


{


Char Cur=paras[i];


BOOL BOk = false;


Switch (cur)


{


Case ':


if (Specialchar.empty ())


{


if (i!= (length-1))


{


Paras = std::string (paras, i+1, length-i-1);


}


Else


{//The last one is a space


Paras.clear ();


}


BOk = true;


}


Else


{


if (specialchar.back () = = ' \ ')


{


Specialchar.pop_back ();


}


Word.append (1,cur);


}


Break


Case ' "':


if (Specialchar.empty ())


{


Specialchar.push_back (cur);


}


else if (specialchar.back () = = cur)


{//Find a matching bracket


Specialchar.pop_back ();


}


else if (specialchar.back () = = ' \ ')


{


Word.append (1,cur);


Specialchar.pop_back ();


}


Else


{


Word.clear ();


return false;


}


Break


Case ' \ ':


if (Specialchar.empty ())


{


Specialchar.push_back (cur);


Islashpos = i;


}


else if (specialchar.back () = = ' ")


{


if (i&lt; (length-1))


{


if (' "' ==paras[i+1] | | \ \ ' ==paras[i+1])


{


Specialchar.push_back (cur);


}


Else


{


Word.append (1,cur);


}


}


Else


{


Word.clear ();


return false;


}


}


else if (' \ = ' = Specialchar.back ())


{


Word.append (1,cur);


Specialchar.pop_back ();


}


Else


{


Word.clear ();


return false;


}


Break


Default


Word.append (1,paras[i]);


if (i== (length-1))


{


BOk = true;


Paras.clear ();


}


Break


}


if (bOk)


{


return true;


}


}//for End


if (Specialchar.empty ())


{


Paras.clear ();


return true;


}


Else


{


return false;


}


}

BOOL Parsingargs::isduplicatekey (const std::string &key, const std::map<std::string, STD::VECTOR<STD:: string> > & result)
{
    if (Result.find (key)!= result.end ())
    {
        return true;//keyword repeat
   }

    for (int i=0; i<m_args.size (); ++i)
    {
         if ((Key.compare (m_args[i].m_longname) = = 0 && result.find (std::string (1, m_args[i].m_ shortname))!= result.end ())
                 | | (Key.compare (std::string (1, m_args[i].m_shortname)) = = 0 && result.find (m_args[i].m_longname)!= result.end () )
         )
        {
            return true;
       }
   }
    return false;
}

int Parsingargs::P arse (const STD::STRING & Paras, std::map<std::string, std::vector<std::string> > & result, std::string &errpos)
{
std::string tmpstring = paras;
Keyflag Keyflag = Invalid_key; Parameter identification
std::string SKey = ""; Key keyword
BOOL Bfindvalue = false; Whether there is already a value
while (!tmpstring.empty ())
{
std::string word = "";

BOOL BRet = Getword (tmpstring, Word);

if (BRet = = False)


{


Errpos = tmpstring;


return-4;//parameter Parsing error


}


Else


{


Keyflag Tmpflag = Getkeyflag (word);


if (Isduplicatekey (word, result))


{


Errpos = tmpstring;


return-5;


}


if (Tmpflag!= invalid_key)


{


if (Tmpflag = = Must_value &amp;&amp; keyflag = must_value &amp;&amp;!bfindvalue)


{


Errpos = tmpstring;


return-3;


}


Keyflag = Tmpflag;


std::vector&lt;std::string&gt; tmp;


Result[word] = tmp;


SKey = Word;


Bfindvalue = false;


}


Else


{


Switch (Keyflag)


{


Case Maybe_value:


Case Must_value:


{


Std::map&lt;std::string, std::vector&lt;std::string&gt; &gt;::iterator it = Result.find (SKey);


if (it!= result.end ())


{


It-&gt;second.push_back (word);


Bfindvalue = true;


}


Else


{


Errpos = tmpstring;


return-1;//did not find the associated key


}


}


Break


Case No_value:


Errpos = tmpstring;


Return-2; Arguments cannot have parameters after an option


Default


Errpos = tmpstring;


return-1;//parameter Error


}//switch End


}


}


}//while End


return 0;


}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.