moving mainparser to pikotools

git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@368 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2012-01-11 11:22:30 +00:00
parent 8c3d76ce13
commit 736e44bf47
7 changed files with 513 additions and 0 deletions

29
mainparser/Makefile Normal file
View File

@ -0,0 +1,29 @@
include Makefile.o.dep
libname=mainparser.a
all: $(libname)
$(libname): $(o)
ar rcs $(libname) $(o)
.SUFFIXES: .cpp .o
.cpp.o:
$(CXX) -c $(CXXFLAGS) $<
depend:
makedepend $(CXXFLAGS) -Y. -f- *.cpp > Makefile.dep
echo -n "o = " > Makefile.o.dep
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
clean:
rm -f *.o
rm -f $(libname)
include Makefile.dep

3
mainparser/Makefile.dep Normal file
View File

@ -0,0 +1,3 @@
# DO NOT DELETE
mainparser.o: mainparser.h

View File

@ -0,0 +1 @@
o = mainparser.o

229
mainparser/mainparser.cpp Normal file
View File

@ -0,0 +1,229 @@
/*
* This file is a part of MainParser -- simple parser for main() parameters
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2011, 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.
*/
#include "mainparser.h"
#include <string.h>
MainParser::MainParser()
{
argsize = 0;
arg = 0;
Reset();
}
MainParser::MainParser(int argc, const char ** argv)
{
Set(argc, argv);
Reset();
}
void MainParser::Set(int argc, const char ** argv)
{
argsize = argc;
arg = argv;
Reset();
}
void MainParser::Reset()
{
argindex = 1;
offset = 0;
has_single_param = false;
has_double_param = false;
}
char MainParser::GetSingleParam()
{
if( !has_single_param )
return 0;
if( last_single_param != 0 )
return last_single_param;
Advance();
if( argindex >= argsize )
return 0;
last_single_param = arg[argindex][offset];
offset += 1;
return last_single_param;
}
bool MainParser::IsSingleParam(char c)
{
return GetSingleParam() == c;
}
const char * MainParser::GetDoubleParam()
{
empty = 0;
if( !has_double_param )
return &empty;
if( last_double_param != &empty )
return last_double_param;
Advance();
if( argindex >= argsize )
return &empty;
last_double_param = &arg[argindex][offset];
offset = 0;
argindex += 1;
return last_double_param;
}
bool MainParser::IsDoubleParam(const char * param)
{
return strcmp(GetDoubleParam(), param) == 0;
}
const char * MainParser::GetValue()
{
empty = 0;
Advance();
if( argindex >= argsize )
return &empty;
const char * value = &arg[argindex][offset];
offset = 0;
argindex += 1;
has_single_param = false;
has_double_param = false;
return value;
}
bool MainParser::NextParam()
{
bool was_single_param = has_single_param;
has_single_param = false;
has_double_param = false;
last_single_param = 0;
last_double_param = &empty;
empty = 0;
if( Advance() )
was_single_param = false;
if( argindex >= argsize )
return false;
if( arg[argindex][offset]=='-' )
{
if( arg[argindex][offset+1]=='-' )
{
has_double_param = true;
offset += 2;
}
else
{
has_single_param = true;
offset += 1;
}
}
else
{
if( was_single_param )
has_single_param = true;
}
return has_single_param || has_double_param;
}
bool MainParser::IsEnd()
{
Advance();
return argindex >= argsize;
}
bool MainParser::HasSingleParam()
{
return has_single_param;
}
bool MainParser::HasDoubleParam()
{
return has_double_param;
}
bool MainParser::Advance()
{
bool was_incremented = false;
while( argindex < argsize && arg[argindex][offset] == 0 )
{
offset = 0;
argindex += 1;
was_incremented = true;
}
return was_incremented;
}

124
mainparser/mainparser.h Normal file
View File

@ -0,0 +1,124 @@
/*
* This file is a part of MainParser -- simple parser for main() parameters
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2011, 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_mainparser_mainparser
#define headerfile_mainparser_mainparser
/*
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;
};
#endif

View File

@ -0,0 +1,10 @@
output=sample
all: $(output)
$(output): sample.cpp ../mainparser.h ../mainparser.cpp
g++ -o $(output) sample.cpp ../mainparser.cpp
clean:
rm -f $(output)
rm -f $(output).exe

View File

@ -0,0 +1,117 @@
/*
* This file is a part of MainParser -- simple parser for main() parameters
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2011, 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.
*/
#include <iostream>
#include <string.h>
#include "../mainparser.h"
int main()
{
MainParser mp;
// suppose you call a 'programname' in such a way:
// $ programname -a -b - c --longparam -- otherlongparam -xyz paramwithvalue -x --longparam2 longwithvalue lastvalue1 lastvalue2 lastvalue3
// so the main() function get this table as input:
const char * tab[] = {
"programname",
"-a",
"-b",
"-",
"c",
"--longparam",
"--",
"otherlongparam",
"-xyz",
"paramwithvalue",
"-x",
"--longparam2",
"longwithvalue",
"lastvalue1", // some values left at the end
"lastvalue2", // you can get them by using GetValue() method
"lastvalue3",
};
mp.Set(sizeof(tab)/sizeof(const char*), tab);
while( mp.NextParam() )
{
if( mp.GetSingleParam() != 0 )
{
std::cout << "-" << mp.GetSingleParam() << std::endl;
// we know that 'z' requires a value
if( mp.GetSingleParam() == 'z' )
std::cout << "value for z: " << mp.GetValue() << std::endl;
}
if( *mp.GetDoubleParam() )
{
std::cout << "--" << mp.GetDoubleParam() << std::endl;
// we know that "longparam2" requires a value
if( strcmp(mp.GetDoubleParam(), "longparam2") == 0 )
std::cout << "value for longparam2: " << mp.GetValue() << std::endl;
}
}
while( !mp.IsEnd() )
std::cout << mp.GetValue() << std::endl;
}
/*
program output:
-a
-b
-c
--longparam
--otherlongparam
-x
-y
-z
value for z: paramwithvalue
-x
--longparam2
value for longparam2: longwithvalue
lastvalue1
lastvalue2
lastvalue3
*/