commit
848cdf9c03
@ -0,0 +1,21 @@
|
||||
# EditorConfig
|
||||
# https://editorconfig.org/
|
||||
# https://editorconfig-specification.readthedocs.io/
|
||||
|
||||
# top-most EditorConfig file
|
||||
root = true
|
||||
|
||||
# Unix-style newlines with a newline ending every file
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
|
||||
# 4 space indentation
|
||||
[*.{h,cpp,html,css,js,conf,txt}]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
|
||||
# Tab indentation (no size specified)
|
||||
[Makefile]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
@ -1,76 +1,58 @@
|
||||
# Makefile for GNU make
|
||||
|
||||
ifndef CXX
|
||||
CXX = clang++
|
||||
endif
|
||||
|
||||
ifndef CXXFLAGS
|
||||
CXXFLAGS = -Wall -O2 -I/usr/local/include
|
||||
endif
|
||||
export CXX
|
||||
export CXXFLAGS
|
||||
export AR
|
||||
|
||||
ifndef LDFLAGS
|
||||
LDFLAGS = -L/usr/local/lib
|
||||
endif
|
||||
|
||||
ifndef AR
|
||||
AR = ar
|
||||
endif
|
||||
all: src
|
||||
|
||||
#CXX=g++5
|
||||
#CXXFLAGS=-Wall -O0 -g3 -gdwarf-2 -std=c++14
|
||||
|
||||
export CXX
|
||||
export CXXFLAGS
|
||||
export LDFLAGS
|
||||
export AR
|
||||
src: FORCE
|
||||
$(MAKE) -C src
|
||||
|
||||
|
||||
all: space mainparser mainspaceparser utf8 date convert log
|
||||
tests: FORCE
|
||||
$(MAKE) -C src
|
||||
$(MAKE) -C tests
|
||||
|
||||
|
||||
tests-gcc10: FORCE
|
||||
env CXX=g++10 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc10/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C src
|
||||
env CXX=g++10 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc10/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C tests
|
||||
|
||||
|
||||
space: FORCE
|
||||
@cd space ; $(MAKE) -e
|
||||
tests-clang: FORCE
|
||||
env CXX=clang++ CXXFLAGS="-Wall -pedantic -O0 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C src
|
||||
env CXX=clang++ CXXFLAGS="-Wall -pedantic -O0 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C tests
|
||||
|
||||
mainparser: FORCE
|
||||
@cd mainparser ; $(MAKE) -e
|
||||
|
||||
mainspaceparser: FORCE
|
||||
@cd mainspaceparser ; $(MAKE) -e
|
||||
tests-clang-sa: FORCE
|
||||
env CXX=clang++ CXXFLAGS="-fsanitize=address -Wall -pedantic -O0 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C src
|
||||
env CXX=clang++ CXXFLAGS="-fsanitize=address -Wall -pedantic -O0 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C tests
|
||||
|
||||
utf8: FORCE
|
||||
@cd utf8 ; $(MAKE) -e
|
||||
|
||||
date: FORCE
|
||||
@cd date ; $(MAKE) -e
|
||||
tests-clang-sm: FORCE
|
||||
env CXX=clang++ CXXFLAGS="-fsanitize=memory -Wall -pedantic -O3 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C src
|
||||
env CXX=clang++ CXXFLAGS="-fsanitize=memory -Wall -pedantic -O3 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C tests
|
||||
|
||||
convert: FORCE
|
||||
@cd convert ; $(MAKE) -e
|
||||
|
||||
log: FORCE
|
||||
@cd log ; $(MAKE) -e
|
||||
tests-clang-su: FORCE
|
||||
env CXX=clang++ CXXFLAGS="-fsanitize=undefined -Wall -pedantic -O3 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C src
|
||||
env CXX=clang++ CXXFLAGS="-fsanitize=undefined -Wall -pedantic -O3 -g3 -std=c++20 -I../src -I/usr/local/include" $(MAKE) -C tests
|
||||
|
||||
|
||||
clean: FORCE
|
||||
$(MAKE) -C src clean
|
||||
$(MAKE) -C tests clean
|
||||
|
||||
|
||||
depend: FORCE
|
||||
$(MAKE) -C src depend
|
||||
$(MAKE) -C tests depend
|
||||
|
||||
|
||||
FORCE:
|
||||
|
||||
|
||||
clean:
|
||||
@cd space ; $(MAKE) -e clean
|
||||
@cd mainparser ; $(MAKE) -e clean
|
||||
@cd mainspaceparser ; $(MAKE) -e clean
|
||||
@cd utf8 ; $(MAKE) -e clean
|
||||
@cd date ; $(MAKE) -e clean
|
||||
@cd convert ; $(MAKE) -e clean
|
||||
@cd log ; $(MAKE) -e clean
|
||||
|
||||
depend:
|
||||
@cd space ; $(MAKE) -e depend
|
||||
@cd mainparser ; $(MAKE) -e depend
|
||||
@cd mainspaceparser ; $(MAKE) -e depend
|
||||
@cd utf8 ; $(MAKE) -e depend
|
||||
@cd date ; $(MAKE) -e depend
|
||||
@cd convert ; $(MAKE) -e depend
|
||||
@cd log ; $(MAKE) -e depend
|
||||
FORCE:
|
||||
|
||||
|
@ -1,27 +0,0 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
libname=convert.a
|
||||
|
||||
all: $(libname)
|
||||
|
||||
$(libname): $(o)
|
||||
$(AR) rcs $(libname) $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) -I.. $<
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. -I.. -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
|
@ -1,5 +0,0 @@
|
||||
# DO NOT DELETE
|
||||
|
||||
inttostr.o: inttostr.h
|
||||
misc.o: misc.h text.h
|
||||
text.o: text.h
|
@ -1 +0,0 @@
|
||||
o = inttostr.o misc.o text.o
|
@ -1,196 +0,0 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017-2018, 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 <cstddef>
|
||||
#include "text.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
// white_chars table should be sorted (a binary search algorithm is used to find a character)
|
||||
// we do not treat a new line character (10) as a white character here
|
||||
// also space (32) and tab (9) are not inserted here
|
||||
static const wchar_t white_chars_table[] = {
|
||||
0x000B, // LINE TABULATION (vertical tabulation)
|
||||
0x000C, // FORM FEED (FF)
|
||||
0x000D, // CARRIAGE RETURN (CR) - a character at the end in a dos text file
|
||||
0x0085, // NEXT LINE (NEL)
|
||||
0x00A0, // NO-BREAK SPACE (old name: NON-BREAKING SPACE)
|
||||
0x1680, // OGHAM SPACE MARK
|
||||
0x180E, // MONGOLIAN VOWEL SEPARATOR
|
||||
0x2000, // EN QUAD
|
||||
0x2001, // EM QUAD
|
||||
0x2002, // EN SPACE
|
||||
0x2003, // EM SPACE
|
||||
0x2004, // THREE-PER-EM SPACE
|
||||
0x2005, // FOUR-PER-EM SPACE
|
||||
0x2006, // SIX-PER-EM SPACE
|
||||
0x2007, // FIGURE SPACE
|
||||
0x2008, // PUNCTUATION SPACE
|
||||
0x2009, // THIN SPACE
|
||||
0x200A, // HAIR SPACE
|
||||
0x2028, // LINE SEPARATOR
|
||||
0x2029, // PARAGRAPH SEPARATOR
|
||||
0x202F, // NARROW NO-BREAK SPACE
|
||||
0x205F, // MEDIUM MATHEMATICAL SPACE
|
||||
0x3000, // IDEOGRAPHIC SPACE
|
||||
0xFEFF, // ZERO WIDTH NO-BREAK SPACE
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
if check_additional_chars is false then we are testing only a space (32), tab (9) and a new line (10) (if treat_new_line_as_white is true)
|
||||
*/
|
||||
bool IsWhite(wchar_t c, bool check_additional_chars, bool treat_new_line_as_white)
|
||||
{
|
||||
// space (32) and tab (9) are the most common white chars
|
||||
// so we check them at the beginning (optimisation)
|
||||
if( c == 32 || c == 9 )
|
||||
return true;
|
||||
|
||||
std::size_t len = sizeof(white_chars_table) / sizeof(wchar_t);
|
||||
std::size_t o1 = 0;
|
||||
std::size_t o2 = len - 1;
|
||||
|
||||
if( c == 10 )
|
||||
return treat_new_line_as_white ? true : false;
|
||||
|
||||
if( !check_additional_chars )
|
||||
return false;
|
||||
|
||||
if( c < white_chars_table[o1] || c > white_chars_table[o2] )
|
||||
return false;
|
||||
|
||||
if( c == white_chars_table[o1] || c == white_chars_table[o2] )
|
||||
return true;
|
||||
|
||||
while( o1 + 1 < o2 )
|
||||
{
|
||||
std::size_t o = (o2 - o1)/2 + o1;
|
||||
|
||||
if( c == white_chars_table[o] )
|
||||
return true;
|
||||
|
||||
if( c > white_chars_table[o] )
|
||||
o1 = o;
|
||||
else
|
||||
o2 = o;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool IsDigit(wchar_t c, int base, int * digit)
|
||||
{
|
||||
int d = 0;
|
||||
|
||||
if( c >= '0' && c <= '9' )
|
||||
{
|
||||
d = c - '0';
|
||||
}
|
||||
else
|
||||
if( c >= 'a' && c <= 'f' )
|
||||
{
|
||||
d = c - 'a' + 10;
|
||||
}
|
||||
else
|
||||
if( c >= 'A' && c <= 'F' )
|
||||
{
|
||||
d = c - 'A' + 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( digit )
|
||||
*digit = d;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if( digit )
|
||||
*digit = d;
|
||||
|
||||
return d < base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
wchar_t ToLower(wchar_t c)
|
||||
{
|
||||
if( c >= 'A' && c <= 'Z' )
|
||||
return c - 'A' + 'a';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
wchar_t ToUpper(wchar_t c)
|
||||
{
|
||||
if( c >= 'a' && c <= 'z' )
|
||||
return c - 'a' + 'A';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
void ToLower(std::wstring & s)
|
||||
{
|
||||
std::wstring::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToLower(s[i]);
|
||||
}
|
||||
|
||||
|
||||
void ToUpper(std::wstring & s)
|
||||
{
|
||||
std::wstring::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToUpper(s[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,269 +0,0 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017-2018, 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_convert_text
|
||||
#define headerfile_picotools_convert_text
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
bool IsWhite(wchar_t c, bool check_additional_chars = true, bool treat_new_line_as_white = true);
|
||||
|
||||
|
||||
bool IsDigit(wchar_t c, int base = 10, int * digit = 0);
|
||||
|
||||
|
||||
template<class CharType>
|
||||
CharType * SkipWhite(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true)
|
||||
{
|
||||
while( IsWhite(static_cast<wchar_t>(*str), check_additional_chars, treat_new_line_as_white) )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* str_end is pointing at the end of the string (the last item + one)
|
||||
*
|
||||
* return value is a pointer to the first white character after a non-white character at the end
|
||||
* or to the last+one if there is no any white characters
|
||||
*
|
||||
*/
|
||||
template<class CharType>
|
||||
CharType * SkipWhiteFromBack(CharType * str_begin, CharType * str_end, bool check_additional_chars = true, bool treat_new_line_as_white = true)
|
||||
{
|
||||
while( str_end > str_begin && IsWhite(static_cast<wchar_t>(*(str_end-1)), check_additional_chars, treat_new_line_as_white) )
|
||||
{
|
||||
str_end -= 1;
|
||||
}
|
||||
|
||||
return str_end;
|
||||
}
|
||||
|
||||
|
||||
template<class CharType>
|
||||
CharType * SkipWhiteFromBack(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true)
|
||||
{
|
||||
CharType * str_begin = str;
|
||||
|
||||
while( *str != 0 )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
|
||||
return SkipWhiteFromBack(str_begin, str, check_additional_chars, treat_new_line_as_white);
|
||||
}
|
||||
|
||||
|
||||
wchar_t ToLower(wchar_t c);
|
||||
wchar_t ToUpper(wchar_t c);
|
||||
|
||||
|
||||
// change to a template
|
||||
void ToLower(std::wstring & s);
|
||||
void ToUpper(std::wstring & s);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCase(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
while( *str1 && *str2 && ToLower(*str1) == ToLower(*str2) )
|
||||
{
|
||||
++str1;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( *str1 == 0 && *str2 == 0 )
|
||||
return 0;
|
||||
|
||||
return (int)ToLower(*str1) - (int)ToLower(*str2);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCase(const StringType1 & str1, const StringType2 & str2)
|
||||
{
|
||||
return CompareNoCase(str1.c_str(), str2.c_str());
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCasep(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
return CompareNoCase(str1, str2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCase(const StringType1 * str1_begin, const StringType1 * str1_end, const StringType2 * str2)
|
||||
{
|
||||
while( str1_begin < str1_end && *str2 && ToLower(*str1_begin) == ToLower(*str2) )
|
||||
{
|
||||
++str1_begin;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( str1_begin == str1_end && *str2 == 0 )
|
||||
return 0;
|
||||
|
||||
wchar_t str1_char = 0;
|
||||
|
||||
if( str1_begin < str1_end )
|
||||
str1_char = *str1_begin;
|
||||
|
||||
return (int)ToLower(str1_char) - (int)ToLower(*str2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
return CompareNoCase(str1, str2) == 0;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 & str1, const StringType2 & str2)
|
||||
{
|
||||
return EqualNoCase(str1.c_str(), str2.c_str());
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCasep(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
return EqualNoCase(str1, str2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 * str1_begin, const StringType1 * str1_end, const StringType2 * str2)
|
||||
{
|
||||
return CompareNoCase(str1_begin, str1_end, str2) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringp(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
while( *short_str && *long_str && wchar_t(*short_str) == wchar_t(*long_str) )
|
||||
{
|
||||
++short_str;
|
||||
++long_str;
|
||||
}
|
||||
|
||||
if( *short_str == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubString(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
return IsSubStringp(short_str, long_str);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubString(const StringType1 & short_str, const StringType2 & long_str)
|
||||
{
|
||||
return IsSubStringp(short_str.c_str(), long_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringNoCasep(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
while( *short_str && *long_str && ToLower(*short_str) == ToLower(*long_str) )
|
||||
{
|
||||
++short_str;
|
||||
++long_str;
|
||||
}
|
||||
|
||||
if( *short_str == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringNoCase(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
return IsSubStringNoCasep(short_str, long_str);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringNoCase(const StringType1 & short_str, const StringType2 & long_str)
|
||||
{
|
||||
return IsSubStringNoCasep(short_str.c_str(), long_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
@ -1,27 +0,0 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
libname=date.a
|
||||
|
||||
all: $(libname)
|
||||
|
||||
$(libname): $(o)
|
||||
$(AR) rcs $(libname) $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) -I.. $<
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. -I.. -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
|
@ -1,3 +0,0 @@
|
||||
# DO NOT DELETE
|
||||
|
||||
date.o: date.h ../convert/inttostr.h
|
@ -1 +0,0 @@
|
||||
o = date.o
|
@ -1,27 +0,0 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
libname=log.a
|
||||
|
||||
all: $(libname)
|
||||
|
||||
$(libname): $(o)
|
||||
$(AR) rcs $(libname) $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) -I.. $<
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. -I.. -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
|
@ -1,9 +0,0 @@
|
||||
# DO NOT DELETE
|
||||
|
||||
filelog.o: filelog.h ../textstream/textstream.h ../space/space.h
|
||||
filelog.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h
|
||||
filelog.o: ../membuffer/membuffer.h ../textstream/types.h ../utf8/utf8.h
|
||||
log.o: log.h ../textstream/textstream.h ../space/space.h
|
||||
log.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h
|
||||
log.o: ../membuffer/membuffer.h ../textstream/types.h filelog.h
|
||||
log.o: ../utf8/utf8.h
|
@ -1 +0,0 @@
|
||||
o = filelog.o log.o
|
@ -1,27 +0,0 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
libname=mainparser.a
|
||||
|
||||
all: $(libname)
|
||||
|
||||
$(libname): $(o)
|
||||
$(AR) rcs $(libname) $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) -I.. $<
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. -I.. -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
|
@ -1,3 +0,0 @@
|
||||
# DO NOT DELETE
|
||||
|
||||
mainparser.o: mainparser.h
|
@ -1 +0,0 @@
|
||||
o = mainparser.o
|
@ -1,237 +0,0 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#include "mainparser.h"
|
||||
#include <string.h>
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
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 ∅
|
||||
|
||||
if( last_double_param != &empty )
|
||||
return last_double_param;
|
||||
|
||||
Advance();
|
||||
|
||||
if( argindex >= argsize )
|
||||
return ∅
|
||||
|
||||
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 ∅
|
||||
|
||||
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 = 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;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
@ -1,132 +0,0 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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
|
@ -1,10 +0,0 @@
|
||||
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
|
@ -1,27 +0,0 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
libname=mainspaceparser.a
|
||||
|
||||
all: $(libname)
|
||||
|
||||
$(libname): $(o)
|
||||
$(AR) rcs $(libname) $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) -I.. $<
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. -I.. -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
|
@ -1,6 +0,0 @@
|
||||
# DO NOT DELETE
|
||||
|
||||
mainspaceparser.o: mainspaceparser.h ../space/space.h ../textstream/types.h
|
||||
mainspaceparser.o: ../utf8/utf8.h ../textstream/textstream.h ../date/date.h
|
||||
mainspaceparser.o: ../convert/inttostr.h ../membuffer/membuffer.h
|
||||
mainspaceparser.o: ../textstream/types.h
|
@ -1 +0,0 @@
|
||||
o = mainspaceparser.o
|
@ -1,297 +0,0 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016-2017, 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 "mainspaceparser.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include <string.h>
|
||||
|
||||
// REMOVE ME
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
MainSpaceParser::MainSpaceParser()
|
||||
{
|
||||
space = 0;
|
||||
options_space = 0;
|
||||
use_utf8 = true;
|
||||
last_status = status_ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
MainSpaceParser::~MainSpaceParser()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MainSpaceParser::UTF8(bool utf8)
|
||||
{
|
||||
use_utf8 = utf8;
|
||||
}
|
||||
|
||||
|
||||
void MainSpaceParser::SetSpace(Space & space_ref)
|
||||
{
|
||||
space = &space_ref;
|
||||
options_space = 0;
|
||||
}
|
||||
|
||||
|
||||
std::wstring & MainSpaceParser::GetErrorToken()
|
||||
{
|
||||
return last_error_token;
|
||||
}
|
||||
|
||||
|
||||
MainSpaceParser::Status MainSpaceParser::Parse(int argc, const char ** argv)
|
||||
{
|
||||
if( !space )
|
||||
{
|
||||
return status_space_not_assigned;
|
||||
}
|
||||
|
||||
options_space = space->FindSpace(L"options");
|
||||
last_status = status_ok;
|
||||
last_error_token.clear();
|
||||
|
||||
for(size_t i=1 ; i < (size_t)argc && last_status == status_ok ; )
|
||||
{
|
||||
Parse((size_t)argc, argv, i);
|
||||
}
|
||||
|
||||
return last_status;
|
||||
}
|
||||
|
||||
|
||||
void MainSpaceParser::Parse(size_t argc, const char ** argv, size_t & argv_index)
|
||||
{
|
||||
const char * pchar = argv[argv_index];
|
||||
|
||||
if( *pchar == '-' )
|
||||
{
|
||||
if( *(pchar+1) == '-' )
|
||||
{
|
||||
ParseMultiArgument(argc, argv, argv_index);
|
||||
}
|
||||
else
|
||||
{
|
||||
ParseSingleArgument(argc, argv, argv_index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
last_status = status_syntax_error;
|
||||
ConvertStr(pchar, last_error_token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainSpaceParser::ConvertStr(const char * src, std::wstring & dst)
|
||||
{
|
||||
if( use_utf8 )
|
||||
{
|
||||
PT::UTF8ToWide(src,dst);
|
||||
}
|
||||
else
|
||||
{
|
||||
dst.clear();
|
||||
|
||||
for( ; *src ; ++src )
|
||||
dst += (wchar_t)(unsigned char)*src;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainSpaceParser::ParseSingleArgument(size_t argc, const char ** argv, size_t & argv_index)
|
||||
{
|
||||
ConvertStr(argv[argv_index] + 1, wide_arg);
|
||||
const wchar_t * wide_pchar = wide_arg.c_str();
|
||||
|
||||
temp_list_val.clear();
|
||||
bool was_option = false;
|
||||
argv_index += 1;
|
||||
|
||||
for( ; *wide_pchar && !was_option ; ++wide_pchar )
|
||||
{
|
||||
temp_arg = *wide_pchar;
|
||||
size_t opt_size = RequireOption(temp_arg);
|
||||
|
||||
if( opt_size > 0 )
|
||||
{
|
||||
was_option = true;
|
||||
|
||||
if( *(wide_pchar+1) )
|
||||
{
|
||||
temp_val = wide_pchar + 1;
|
||||
temp_list_val.push_back(temp_val);
|
||||
opt_size -= 1;
|
||||
}
|
||||
|
||||
for( ; opt_size > 0 && argv_index < argc ; --opt_size, ++argv_index)
|
||||
{
|
||||
ConvertStr(argv[argv_index], temp_val);
|
||||
temp_list_val.push_back(temp_val);
|
||||
}
|
||||
|
||||
if( opt_size > 0 )
|
||||
{
|
||||
last_status = status_reading_eof;
|
||||
last_error_token.clear();
|
||||
}
|
||||
}
|
||||
|
||||
temp_val.clear();
|
||||
AddValueToItem(temp_arg, temp_val, temp_list_val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainSpaceParser::ParseMultiArgument(size_t argc, const char ** argv, size_t & argv_index)
|
||||
{
|
||||
ConvertStr(argv[argv_index] + 2, temp_arg);
|
||||
argv_index += 1;
|
||||
|
||||
size_t opt_size = RequireOption(temp_arg);
|
||||
temp_list_val.clear();
|
||||
|
||||
if( opt_size > 0 )
|
||||
{
|
||||
for( ; opt_size > 0 && argv_index < argc ; --opt_size, ++argv_index)
|
||||
{
|
||||
ConvertStr(argv[argv_index], temp_val);
|
||||
temp_list_val.push_back(temp_val);
|
||||
}
|
||||
|
||||
if( opt_size > 0 )
|
||||
{
|
||||
last_status = status_reading_eof;
|
||||
last_error_token.clear();
|
||||
}
|
||||
}
|
||||
|
||||
temp_val.clear();
|
||||
AddValueToItem(temp_arg, temp_val, temp_list_val);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void MainSpaceParser::AddValueToItem(const std::wstring & name, const std::wstring & empty_value, const std::vector<std::wstring> & list)
|
||||
{
|
||||
std::wstring * val = space->GetFirstValue(name);
|
||||
|
||||
if( !val )
|
||||
{
|
||||
if( list.empty() )
|
||||
space->Add(name, empty_value);
|
||||
else
|
||||
if( list.size() == 1 )
|
||||
space->Add(name, list[0]);
|
||||
else
|
||||
space->table[name] = list; // !! IMPROVE ME there'll be a new api in space
|
||||
}
|
||||
else
|
||||
{
|
||||
PT::Space::Table::iterator i = space->table.find(name);
|
||||
PT::Space::Value * table_value;
|
||||
|
||||
if( i == space->table.end() )
|
||||
{
|
||||
table_value = &space->table[name];
|
||||
table_value->push_back(*val);
|
||||
//space->table_single.erase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
table_value = &i->second;
|
||||
}
|
||||
|
||||
if( list.empty() )
|
||||
{
|
||||
table_value->push_back(empty_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(const auto & list_item : list)
|
||||
table_value->push_back(list_item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t MainSpaceParser::RequireOption(const std::wstring & arg)
|
||||
{
|
||||
size_t res = 0;
|
||||
|
||||
if( options_space )
|
||||
{
|
||||
std::wstring * val = options_space->GetFirstValue(arg);
|
||||
|
||||
if( val )
|
||||
{
|
||||
/*
|
||||
* IMPLEMENT ME
|
||||
* add a converter to convert/inttostr.h
|
||||
*
|
||||
*/
|
||||
|
||||
long res_long = wcstol(val->c_str(), 0, 10);
|
||||
|
||||
if( res_long < 0 )
|
||||
res_long = 0;
|
||||
|
||||
res = (size_t)res_long;
|
||||
|
||||
//std::wcout << L"argument " << arg << L" needs " << res << L" options" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
@ -1,27 +0,0 @@
|
||||
include Makefile.o.dep
|
||||
|
||||
libname=space.a
|
||||
|
||||
all: $(libname)
|
||||
|
||||
$(libname): $(o)
|
||||
$(AR) rcs $(libname) $(o)
|
||||
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) -c $(CXXFLAGS) -I.. $<
|
||||
|
||||
|
||||
|
||||
depend:
|
||||
makedepend -Y. -I.. -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
|
||||