/* * This file is a part of PikoTools * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2011-2012, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name Tomasz Sowa nor the names of contributors to this * project may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef headerfile_picotools_mainparser_mainparser #define headerfile_picotools_mainparser_mainparser namespace PT { /* a very little parser for main(int argc, char ** argv) parameters look in sample/sample.cpp how to use the parser */ class MainParser { public: MainParser(); MainParser(int argc, const char ** argv); // setting arguments passed to main(int argc, char ** argv) function void Set(int argc, const char ** argv); // reseting the current state of parsing // now you can start parsing from the beginning // you don't have to call it for the first time // (is automatically called by the Set method) void Reset(); // checking if there is a next single or double parameter // this method represents the main loop of checking parameters bool NextParam(); // returning a single parameter (if exists) or '\0' otherwise // single parameter means a parameter with '-' at the beginning e.g. "-a" // next call to this method (without calling NextParam) returns the same value // this method should be called after NextParam() char GetSingleParam(); // calling GetSingleParam() and comparign with 'c' bool IsSingleParam(char c); // returning a string for a double parameter or an empty string if there is no such a parameter // double parameter means a parameter with '--' at the beginning e.g. "--output" // next call to this method (without calling NextParam) returns the same value // GetDoubleParam() should be called after NextParam() // this method never returns a null pointer -- if there is no a param name (end of the string) // a pointer to en empty string will be returned const char * GetDoubleParam(); // calling GetDoubleParam() and comparing with 'param' // so you don't have to call strcmp directly bool IsDoubleParam(const char * param); // returning a string representing a value // you have to know which parameter requires a value // and if such a parameter is found then use this method to obtain the value // the method advances the current pointer so next call to this method return a next value // you can call GetValue() even when NextParam() has returned false // in such a case this gets you the last values (those at the end of the parameter list) // this method never returns a null pointer -- if there is no a value (end of the string) // a pointer to en empty string will be returned const char * GetValue(); // returning true if the input string is finished // there are no more parameters or values bool IsEnd(); // returning true if there is a single parameter // should be called after NextParam() bool HasSingleParam(); // returning true if there is a double parameter // should be called after NextParam() bool HasDoubleParam(); private: bool Advance(); int argindex; int offset; int argsize; const char ** arg; char empty; bool has_single_param; bool has_double_param; char last_single_param; const char * last_double_param; }; } // namespace #endif