Compare commits

..

1 Commits

Author SHA1 Message Date
Tomasz Sowa 03b159d9be add a listener callback when parsing xml tags 2022-01-21 01:09:49 +01:00
59 changed files with 1641 additions and 4437 deletions

9
.gitignore vendored
View File

@ -5,12 +5,3 @@
src/pikotools.a
tests/tests
m
.clangd
.qtc_clangd/
pikotools.cflags
pikotools.config
pikotools.creator
pikotools.creator.user
pikotools.cxxflags
pikotools.files
pikotools.includes

24
LICENSE
View File

@ -1,24 +0,0 @@
Copyright (c) 2008-2022, 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:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. 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.
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 HOLDER 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.

View File

@ -17,9 +17,9 @@ tests: FORCE
$(MAKE) -C tests
tests-gcc11: FORCE
env CXX=g++11 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc11/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C src
env CXX=g++11 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc11/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(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
tests-clang: FORCE

View File

@ -2,8 +2,11 @@
./convert/inttostr.o: ./convert/inttostr.h
./convert/misc.o: ./convert/misc.h ./convert/text.h textstream/stream.h
./convert/misc.o: textstream/types.h ./convert/inttostr.h utf8/utf8.h
./convert/misc.o: utf8/utf8_templates.h utf8/utf8_private.h
./convert/misc.o: textstream/types.h utf8/utf8_stream.h
./convert/misc.o: textstream/textstream.h textstream/stream.h space/space.h
./convert/misc.o: convert/inttostr.h utf8/utf8.h utf8/utf8_templates.h
./convert/misc.o: utf8/utf8_private.h date/date.h membuffer/membuffer.h
./convert/misc.o: textstream/types.h ./convert/inttostr.h
./convert/text.o: ./convert/text.h ./convert/text_private.h
./convert/double.o: ./convert/double.h textstream/textstream.h
./convert/double.o: textstream/stream.h space/space.h textstream/types.h
@ -15,6 +18,7 @@
./convert/baseparser.o: convert/inttostr.h utf8/utf8.h textstream/stream.h
./convert/baseparser.o: utf8/utf8_templates.h utf8/utf8_private.h date/date.h
./convert/baseparser.o: membuffer/membuffer.h textstream/types.h
./convert/baseparser.o: utf8/utf8_stream.h
./date/date.o: ./date/date.h convert/inttostr.h
./log/filelog.o: ./log/filelog.h textstream/textstream.h textstream/stream.h
./log/filelog.o: space/space.h textstream/types.h convert/inttostr.h
@ -32,7 +36,8 @@
./space/space.o: convert/patternreplacer.h textstream/textstream.h
./space/space.o: textstream/stream.h space/space.h date/date.h
./space/space.o: membuffer/membuffer.h textstream/types.h convert/strtoint.h
./space/space.o: ./convert/text.h ./convert/misc.h ./convert/double.h
./space/space.o: ./convert/text.h ./convert/misc.h utf8/utf8_stream.h
./space/space.o: ./convert/double.h
./space/spaceparser.o: ./space/spaceparser.h ./space/space.h
./space/spaceparser.o: textstream/types.h convert/inttostr.h utf8/utf8.h
./space/spaceparser.o: textstream/stream.h utf8/utf8_templates.h
@ -40,7 +45,7 @@
./space/spaceparser.o: textstream/textstream.h textstream/stream.h
./space/spaceparser.o: space/space.h date/date.h membuffer/membuffer.h
./space/spaceparser.o: textstream/types.h convert/strtoint.h ./convert/text.h
./space/spaceparser.o: ./convert/misc.h
./space/spaceparser.o: ./convert/misc.h utf8/utf8_stream.h
./utf8/utf8.o: ./utf8/utf8.h textstream/stream.h utf8/utf8_templates.h
./utf8/utf8.o: utf8/utf8_private.h
./utf8/utf8_private.o: utf8/utf8_private.h
@ -55,15 +60,15 @@
./mainoptions/mainoptionsparser.o: convert/inttostr.h utf8/utf8.h
./mainoptions/mainoptionsparser.o: textstream/stream.h utf8/utf8_templates.h
./mainoptions/mainoptionsparser.o: utf8/utf8_private.h
./html/bbcodeparser.o: ./html/bbcodeparser.h ./html/htmlparser.h
./html/bbcodeparser.o: convert/baseparser.h textstream/textstream.h
./html/bbcodeparser.o: textstream/stream.h space/space.h textstream/types.h
./html/bbcodeparser.o: convert/inttostr.h utf8/utf8.h textstream/stream.h
./html/bbcodeparser.o: utf8/utf8_templates.h utf8/utf8_private.h date/date.h
./html/bbcodeparser.o: membuffer/membuffer.h textstream/types.h
./html/htmlparser.o: ./html/htmlparser.h convert/baseparser.h
./html/htmlparser.o: textstream/textstream.h textstream/stream.h
./html/htmlparser.o: space/space.h textstream/types.h convert/inttostr.h
./html/htmlparser.o: utf8/utf8.h textstream/stream.h utf8/utf8_templates.h
./html/htmlparser.o: utf8/utf8_private.h date/date.h membuffer/membuffer.h
./html/htmlparser.o: textstream/types.h convert/text.h
./html/bbcodeparser.o: ./html/bbcodeparser.h ./html/htmlparser.h
./html/bbcodeparser.o: convert/baseparser.h textstream/textstream.h
./html/bbcodeparser.o: textstream/stream.h space/space.h textstream/types.h
./html/bbcodeparser.o: convert/inttostr.h utf8/utf8.h textstream/stream.h
./html/bbcodeparser.o: utf8/utf8_templates.h utf8/utf8_private.h date/date.h
./html/bbcodeparser.o: membuffer/membuffer.h textstream/types.h

View File

@ -1,39 +1,43 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2022, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "baseparser.h"
#include "utf8/utf8.h"
#include "utf8/utf8_stream.h"
namespace pt
@ -45,15 +49,9 @@ BaseParser::BaseParser()
}
BaseParser::~BaseParser()
{
}
void BaseParser::clear_input_flags()
{
line = 0;
column = 0;
reading_from_file = false;
pchar_ascii = nullptr;
pchar_unicode = nullptr;
@ -71,16 +69,6 @@ void BaseParser::clear_input_flags()
}
void BaseParser::check_new_line()
{
if( lastc == '\n' )
{
++line;
column = 0;
}
}
int BaseParser::read_utf8_char()
{
int c;
@ -98,7 +86,9 @@ bool correct;
while( !correct );
lastc = c;
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -107,7 +97,9 @@ return lastc;
int BaseParser::read_ascii_char()
{
lastc = file.get();
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -120,7 +112,8 @@ int BaseParser::read_char_from_wchar_string()
else
lastc = *(pchar_unicode++);
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -143,7 +136,8 @@ bool correct;
if( correct )
lastc = c;
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -156,7 +150,8 @@ int BaseParser::read_char_from_ascii_string()
else
lastc = *(pchar_ascii++);
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -174,7 +169,8 @@ int BaseParser::read_char_from_wtext_stream()
lastc = -1;
}
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -196,7 +192,8 @@ int BaseParser::read_char_from_utf8_text_stream()
if( correct )
lastc = c;
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}
@ -214,7 +211,8 @@ int BaseParser::read_char_from_ascii_text_stream()
lastc = -1;
}
check_new_line();
if( lastc == '\n' )
++line;
return lastc;
}

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2022, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_baseparser
#define headerfile_pikotools_src_convert_baseparser
#ifndef headerfile_picotools_convert_baseparser
#define headerfile_picotools_convert_baseparser
#include <string>
#include <fstream>
@ -48,11 +51,9 @@ class BaseParser
protected:
BaseParser();
virtual ~BaseParser();
virtual void clear_input_flags();
virtual void check_new_line();
virtual int read_utf8_char();
virtual int read_ascii_char();
virtual int read_char_from_wchar_string();
@ -71,11 +72,6 @@ protected:
*/
int line;
/*
a number of a column in which there is a syntax_error
*/
int column;
/*
true if parse() method was called

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2022, Tomasz Sowa
* Copyright (c) 2012-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_convert
#define headerfile_pikotools_src_convert_convert
#ifndef headerfile_picotools_membuffer_convert_convert
#define headerfile_picotools_membuffer_convert_convert
#include "inttostr.h"

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,25 +11,28 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdlib>

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,29 +11,32 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_double
#define headerfile_pikotools_src_convert_double
#ifndef headerfile_picotools_membuffer_convert_double
#define headerfile_picotools_membuffer_convert_double
#include <string>

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2022, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "inttostr.h"
@ -38,114 +41,114 @@
namespace pt
{
std::string to_str(unsigned long long value, int base, size_t min_width)
std::string to_str(unsigned long long value, int base)
{
std::string res;
Toa(value, res, false, base, min_width);
Toa(value, res, false, base);
return res;
}
std::string to_str(long long value, int base, size_t min_width)
std::string to_str(long long value, int base)
{
std::string res;
Toa(value, res, false, base, min_width);
Toa(value, res, false, base);
return res;
}
std::string to_str(unsigned long value, int base, size_t min_width)
std::string to_str(unsigned long value, int base)
{
return to_str(static_cast<unsigned long long>(value), base, min_width);
return to_str(static_cast<unsigned long long>(value), base);
}
std::string to_str(long value, int base, size_t min_width)
std::string to_str(long value, int base)
{
return to_str(static_cast<long long>(value), base, min_width);
return to_str(static_cast<long long>(value), base);
}
std::string to_str(unsigned int value, int base, size_t min_width)
std::string to_str(unsigned int value, int base)
{
return to_str(static_cast<unsigned long long>(value), base, min_width);
return to_str(static_cast<unsigned long long>(value), base);
}
std::string to_str(int value, int base, size_t min_width)
std::string to_str(int value, int base)
{
return to_str(static_cast<long long>(value), base, min_width);
return to_str(static_cast<long long>(value), base);
}
std::string to_str(unsigned short value, int base, size_t min_width)
std::string to_str(unsigned short value, int base)
{
return to_str(static_cast<unsigned long long>(value), base, min_width);
return to_str(static_cast<unsigned long long>(value), base);
}
std::string to_str(short value, int base, size_t min_width)
std::string to_str(short value, int base)
{
return to_str(static_cast<long long>(value), base, min_width);
return to_str(static_cast<long long>(value), base);
}
std::wstring to_wstr(unsigned long long value, int base, size_t min_width)
std::wstring to_wstr(unsigned long long value, int base)
{
std::wstring res;
Toa(value, res, false, base, min_width);
Toa(value, res, false, base);
return res;
}
std::wstring to_wstr(long long value, int base, size_t min_width)
std::wstring to_wstr(long long value, int base)
{
std::wstring res;
Toa(value, res, false, base, min_width);
Toa(value, res, false, base);
return res;
}
std::wstring to_wstr(unsigned long value, int base, size_t min_width)
std::wstring to_wstr(unsigned long value, int base)
{
return to_wstr(static_cast<unsigned long long>(value), base, min_width);
return to_wstr(static_cast<unsigned long long>(value), base);
}
std::wstring to_wstr(long value, int base, size_t min_width)
std::wstring to_wstr(long value, int base)
{
return to_wstr(static_cast<long long>(value), base, min_width);
return to_wstr(static_cast<long long>(value), base);
}
std::wstring to_wstr(unsigned int value, int base, size_t min_width)
std::wstring to_wstr(unsigned int value, int base)
{
return to_wstr(static_cast<unsigned long long>(value), base, min_width);
return to_wstr(static_cast<unsigned long long>(value), base);
}
std::wstring to_wstr(int value, int base, size_t min_width)
std::wstring to_wstr(int value, int base)
{
return to_wstr(static_cast<long long>(value), base, min_width);
return to_wstr(static_cast<long long>(value), base);
}
std::wstring to_wstr(unsigned short value, int base, size_t min_width)
std::wstring to_wstr(unsigned short value, int base)
{
return to_wstr(static_cast<unsigned long long>(value), base, min_width);
return to_wstr(static_cast<unsigned long long>(value), base);
}
std::wstring to_wstr(short value, int base, size_t min_width)
std::wstring to_wstr(short value, int base)
{
return to_wstr(static_cast<long long>(value), base, min_width);
return to_wstr(static_cast<long long>(value), base);
}

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2022, Tomasz Sowa
* Copyright (c) 2012-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_inttostr
#define headerfile_pikotools_src_convert_inttostr
#ifndef headerfile_picotools_convert_inttostr
#define headerfile_picotools_convert_inttostr
#include <string>
@ -49,9 +52,8 @@ namespace pt
// if the buffer is too small it will be terminated at the beginning (empty string)
// and the function returns false
// min_width - if greater than zero then it is used for zero padding
template<class CharType>
bool Toa(unsigned long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = nullptr, size_t min_width = 0)
bool Toa(unsigned long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
size_t i1, i2;
long rest;
@ -75,14 +77,6 @@ long rest;
}
while(value != 0 && i2 < buf_len);
if( min_width > 0 )
{
for( ; i2 < min_width && i2 < buf_len ; ++i2)
{
buffer[i2] = '0';
}
}
if( i2 >= buf_len )
{
buffer[0] = 0; // ops, the buffer was too small
@ -112,7 +106,7 @@ return true;
// if the buffer is too small it will be terminated at the beginning (empty string)
// and the function returns false
template<class CharType>
bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = nullptr, size_t min_width = 0)
bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
if( len_out )
*len_out = 0;
@ -132,7 +126,7 @@ bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size
is_sign = true;
}
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out, min_width);
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out);
if( res )
{
@ -152,44 +146,44 @@ return res;
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out, min_width);
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out, min_width);
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out, min_width);
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out, min_width);
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(unsigned short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
bool Toa(unsigned short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out, min_width);
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out, min_width);
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
}
@ -198,7 +192,7 @@ bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t *
template<class StringType>
void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10)
{
typename StringType::value_type buffer[50];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
@ -210,13 +204,13 @@ void Toa(unsigned long long value, StringType & res, bool clear_string = true, i
* the size of the buffer is sufficient so the status should always be true
*/
size_t len_out;
Toa(value, buffer, buffer_len, base, &len_out, min_width);
Toa(value, buffer, buffer_len, base, &len_out);
res.append(buffer, len_out);
}
template<class StringType>
void Toa(long long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(long long value, StringType & res, bool clear_string = true, int base = 10)
{
typename StringType::value_type buffer[50];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
@ -228,71 +222,71 @@ void Toa(long long value, StringType & res, bool clear_string = true, int base =
* the size of the buffer is sufficient so the status should always be true
*/
size_t len_out;
Toa(value, buffer, buffer_len, base, &len_out, min_width);
Toa(value, buffer, buffer_len, base, &len_out);
res.append(buffer, len_out);
}
template<class StringType>
void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<unsigned long long>(value), res, clear_string, base, min_width);
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(long value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<long long>(value), res, clear_string, base, min_width);
Toa(static_cast<long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<unsigned long long>(value), res, clear_string, base, min_width);
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(int value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(int value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<long long>(value), res, clear_string, base, min_width);
Toa(static_cast<long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<unsigned long long>(value), res, clear_string, base, min_width);
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(short value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
void Toa(short value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<long long>(value), res, clear_string, base, min_width);
Toa(static_cast<long long>(value), res, clear_string, base);
}
std::string to_str(unsigned long long value, int base = 10, size_t min_width = 0);
std::string to_str(long long value, int base = 10, size_t min_width = 0);
std::string to_str(unsigned long value, int base = 10, size_t min_width = 0);
std::string to_str(long value, int base = 10, size_t min_width = 0);
std::string to_str(unsigned int value, int base = 10, size_t min_width = 0);
std::string to_str(int value, int base = 10, size_t min_width = 0);
std::string to_str(unsigned short value, int base = 10, size_t min_width = 0);
std::string to_str(short value, int base = 10, size_t min_width = 0);
std::string to_str(unsigned long long value, int base = 10);
std::string to_str(long long value, int base = 10);
std::string to_str(unsigned long value, int base = 10);
std::string to_str(long value, int base = 10);
std::string to_str(unsigned int value, int base = 10);
std::string to_str(int value, int base = 10);
std::string to_str(unsigned short value, int base = 10);
std::string to_str(short value, int base = 10);
std::wstring to_wstr(unsigned long long value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(long long value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(unsigned long value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(long value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(unsigned int value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(int value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(unsigned short value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(short value, int base = 10, size_t min_width = 0);
std::wstring to_wstr(unsigned long long value, int base = 10);
std::wstring to_wstr(long long value, int base = 10);
std::wstring to_wstr(unsigned long value, int base = 10);
std::wstring to_wstr(long value, int base = 10);
std::wstring to_wstr(unsigned int value, int base = 10);
std::wstring to_wstr(int value, int base = 10);
std::wstring to_wstr(unsigned short value, int base = 10);
std::wstring to_wstr(short value, int base = 10);

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2017-2022, Tomasz Sowa
* Copyright (c) 2017-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "misc.h"
@ -48,108 +51,62 @@ void SetOverflow(bool * was_overflow, bool val)
}
void esc_to_json_uformat(wchar_t val, Stream & out)
void esc_to_json(char val, Stream & out)
{
char buf[10];
size_t len;
Toa((unsigned long)val, buf, sizeof(buf)/sizeof(char), 16, &len);
out << "\\u";
if( len < 4 )
if( (unsigned char)val < 32 )
{
for(size_t i=0 ; i < (4-len) ; ++i)
char buf[10];
size_t len;
Toa((unsigned char)val, buf, sizeof(buf)/sizeof(char), 16, &len);
out << "\\u";
if( len < 4 )
{
out << '0';
for(size_t i=0 ; i < (4-len) ; ++i)
{
out << '0';
}
}
out << buf;
}
else
{
// CHECKME
// \r \n \t are <32 and will be serialized os \u.... above
switch( val )
{
case 0: out << '\\'; out << '0'; break; // may to skip this character is better?
case '\r': out << '\\'; out << 'r'; break;
case '\n': out << '\\'; out << 'n'; break;
case '\t': out << '\\'; out << 't'; break;
case 0x08: out << '\\'; out << 'b'; break;
case 0x0c: out << '\\'; out << 'f'; break;
case '\\': out << '\\'; out << '\\'; break;
case '"': out << '\\'; out << '\"'; break;
default:
out << val;
}
}
out << buf;
}
/*
* return true if the val character was escaped and put to the out stream
* if the character is invalid for such a stream then only return true
* but not put it to the stream
*/
bool try_esc_to_json(wchar_t val, Stream & out)
{
bool status = false;
if( val == '\r' )
{
out << '\\' << 'r';
status = true;
}
else
if( val == '\n' )
{
out << '\\' << 'n';
status = true;
}
else
if( val == '\t' )
{
out << '\\' << 't';
status = true;
}
else
if( val == 0x08 )
{
out << '\\' << 'b';
status = true;
}
else
if( val == 0x0c )
{
out << '\\' << 'f';
status = true;
}
else
if( val == '\\' )
{
out << '\\' << '\\';
status = true;
}
else
if( val == '"' )
{
out << '\\' << '\"';
status = true;
}
else
if( val < 32 )
{
esc_to_json_uformat(val, out);
status = true;
}
return status;
}
void esc_to_json(wchar_t val, Stream & out)
{
if( !try_esc_to_json(val, out) )
char utf8_buf[10];
std::size_t utf8_buf_len = sizeof(utf8_buf) / sizeof(char);
size_t len = int_to_utf8(static_cast<int>(val), utf8_buf, utf8_buf_len);
for(size_t a = 0 ; a < len ; ++a)
{
out << val;
esc_to_json(utf8_buf[a], out);
}
}
void esc_to_json(char val, Stream & out)
{
if( !try_esc_to_json((wchar_t)(unsigned char)val, out) )
{
out << val;
}
}
void esc_to_json(const char * c, pt::Stream & out)
{
for(size_t i = 0 ; c[i] != 0 ; ++i)
@ -199,65 +156,47 @@ void esc_to_json(const std::wstring & in, Stream & out)
/*
* return true if the val character was escaped and put to the out stream
* if the character is invalid for such a stream then only return true
* but not put it to the stream
*/
bool try_esc_to_xml(wchar_t val, Stream & out)
{
bool status = false;
if( val == 0 )
{
// null character is invalid in XML 1.0 and 1.1
// https://en.wikipedia.org/wiki/Valid_characters_in_XML
// return true but not put the char to the out stream
status = true;
}
else
if( val == '<')
{
out << "&lt;";
status = true;
}
else
if( val == '>')
{
out << "&gt;";
status = true;
}
else
if( val == '&')
{
out << "&amp;";
status = true;
}
else
if( val == '"')
{
out << "&quot;";
status = true;
}
return status;
}
void esc_to_xml(wchar_t val, Stream & out)
{
if( !try_esc_to_xml(val, out) )
{
out << val;
}
}
void esc_to_xml(char val, Stream & out)
{
if( !try_esc_to_xml((wchar_t)(unsigned char)val, out) )
switch(val)
{
case '<':
out << "&lt;";
break;
case '>':
out << "&gt;";
break;
case '&':
out << "&amp;";
break;
case '"':
out << "&quot;";
break;
default:
out << val;
break;
// what about zero (null) character?
}
}
void esc_to_xml(wchar_t val, Stream & out)
{
char utf8_buf[10];
std::size_t utf8_buf_len = sizeof(utf8_buf) / sizeof(char);
size_t len = int_to_utf8(static_cast<int>(val), utf8_buf, utf8_buf_len);
for(size_t a = 0 ; a < len ; ++a)
{
esc_to_xml(utf8_buf[a], out);
}
}
@ -313,49 +252,37 @@ void esc_to_xml(const std::wstring & in, Stream & out)
/*
* return true if the val character was escaped and put to the out stream
* if the character is invalid for such a stream then only return true
* but not put it to the stream
*/
bool try_esc_to_csv(wchar_t val, pt::Stream & out)
void esc_to_csv(char c, pt::Stream & out)
{
bool status = false;
if( val == 0 )
{
// null characters are invalid in text files
// return true but not put to the out stream
status = true;
}
else
if( val == '"' )
switch(c)
{
case '"':
out << "\"\"";
status = true;
}
break;
return status;
default:
out << c;
break;
// what about zero (null) character?
}
}
void esc_to_csv(wchar_t val, pt::Stream & out)
void esc_to_csv(wchar_t val, Stream & out)
{
if( !try_esc_to_csv(val, out) )
char utf8_buf[10];
std::size_t utf8_buf_len = sizeof(utf8_buf) / sizeof(char);
size_t len = int_to_utf8(static_cast<int>(val), utf8_buf, utf8_buf_len);
for(size_t a = 0 ; a < len ; ++a)
{
out << val;
esc_to_csv(utf8_buf[a], out);
}
}
void esc_to_csv(char val, Stream & out)
{
if( !try_esc_to_csv((wchar_t)(unsigned char)val, out) )
{
out << val;
}
}
void esc_to_csv(const char * c, pt::Stream & out)
{

View File

@ -1,44 +1,48 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2017-2023, Tomasz Sowa
* Copyright (c) 2017-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_misc
#define headerfile_pikotools_src_convert_misc
#ifndef headerfile_picotools_convert_misc
#define headerfile_picotools_convert_misc
#include <limits>
#include "text.h"
#include "textstream/stream.h"
#include "textstream/types.h"
#include "utf8/utf8_stream.h"
namespace pt
@ -46,9 +50,8 @@ namespace pt
void SetOverflow(bool * was_overflow, bool val);
bool try_esc_to_json(wchar_t val, Stream & out);
void esc_to_json(wchar_t val, Stream & out);
void esc_to_json(char val, Stream & out);
void esc_to_json(wchar_t val, Stream & out);
void esc_to_json(const char * c, pt::Stream & out);
void esc_to_json(const char * c, std::size_t len, Stream & out);
void esc_to_json(const wchar_t * c, Stream & out);
@ -56,9 +59,8 @@ void esc_to_json(const wchar_t * c, size_t len, pt::Stream & out);
void esc_to_json(const std::string & in, Stream & out);
void esc_to_json(const std::wstring & in, Stream & out);
bool try_esc_to_xml(wchar_t val, Stream & out);
void esc_to_xml(wchar_t c, pt::Stream & out);
void esc_to_xml(char c, pt::Stream & out);
void esc_to_xml(wchar_t c, pt::Stream & out);
void esc_to_xml(const char * c, pt::Stream & out);
void esc_to_xml(const char * c, std::size_t len, pt::Stream & out);
void esc_to_xml(const wchar_t * c, pt::Stream & out);
@ -66,29 +68,48 @@ void esc_to_xml(const wchar_t * c, size_t len, pt::Stream & out);
void esc_to_xml(const std::string & in, Stream & out);
void esc_to_xml(const std::wstring & in, Stream & out);
bool try_esc_to_csv(wchar_t val, pt::Stream & out);
void esc_to_csv(wchar_t val, Stream & out);
void esc_to_csv(char c, pt::Stream & out);
void esc_to_csv(wchar_t val, Stream & out);
void esc_to_csv(const char * c, std::size_t len, Stream & out);
void esc_to_csv(const char * c, pt::Stream & out);
void esc_to_csv(const char * c, std::size_t len, pt::Stream & out);
void esc_to_csv(const wchar_t * c, pt::Stream & out);
void esc_to_csv(const wchar_t * c, size_t len, pt::Stream & out);
void esc_to_csv(const std::string & in, Stream & out);
void esc_to_csv(const std::wstring & in, Stream & out);
template<typename StreamType>
void esc_to_json(const StreamType & in, Stream & out)
{
char utf8_buf[10];
std::size_t utf8_buf_len = sizeof(utf8_buf) / sizeof(char);
typename StreamType::const_iterator i = in.begin();
typename StreamType::const_iterator end = in.end();
int res;
bool correct;
while( i != end )
for( ; i != end ; ++i)
{
wchar_t c = i.get_unicode_and_advance(end);
esc_to_json(c, out);
if( in.is_wchar_stream() && out.is_char_stream() )
{
std::size_t len = int_to_utf8(static_cast<int>(*i), utf8_buf, utf8_buf_len);
esc_to_json(utf8_buf, len, out);
}
else
if( in.is_char_stream() && out.is_wchar_stream() )
{
utf8_to_int(i, end, res, correct);
if( correct )
esc_to_json(static_cast<wchar_t>(res), out); // IMPROVEME no surrogate pair used here (if sizeof(wchar_t) == 2)
// put replacement char if not correct?
}
else
{
esc_to_json(static_cast<wchar_t>(*i), out);
}
}
}
@ -96,13 +117,34 @@ void esc_to_json(const StreamType & in, Stream & out)
template<typename StreamType>
void esc_to_xml(const StreamType & in, Stream & out)
{
char utf8_buf[10];
std::size_t utf8_buf_len = sizeof(utf8_buf) / sizeof(char);
typename StreamType::const_iterator i = in.begin();
typename StreamType::const_iterator end = in.end();
int res;
bool correct;
while( i != end )
for( ; i != end ; ++i)
{
wchar_t c = i.get_unicode_and_advance(end);
esc_to_xml(c, out);
if( in.is_wchar_stream() && out.is_char_stream() )
{
std::size_t len = int_to_utf8(static_cast<int>(*i), utf8_buf, utf8_buf_len);
esc_to_xml(utf8_buf, len, out);
}
else
if( in.is_char_stream() && out.is_wchar_stream() )
{
utf8_to_int(i, end, res, correct);
if( correct )
esc_to_xml(static_cast<wchar_t>(res), out); // IMPROVEME no surrogate pair used here (if sizeof(wchar_t) == 2)
// put replacement char if not correct?
}
else
{
esc_to_xml(static_cast<wchar_t>(*i), out);
}
}
}
@ -110,13 +152,34 @@ void esc_to_xml(const StreamType & in, Stream & out)
template<typename StreamType>
void esc_to_csv(const StreamType & in, Stream & out)
{
char utf8_buf[10];
std::size_t utf8_buf_len = sizeof(utf8_buf) / sizeof(char);
typename StreamType::const_iterator i = in.begin();
typename StreamType::const_iterator end = in.end();
int res;
bool correct;
while( i != end )
for( ; i != end ; ++i)
{
wchar_t c = i.get_unicode_and_advance(end);
esc_to_csv(c, out);
if( in.is_wchar_stream() && out.is_char_stream() )
{
std::size_t len = int_to_utf8(static_cast<int>(*i), utf8_buf, utf8_buf_len);
esc_to_csv(utf8_buf, len, out);
}
else
if( in.is_char_stream() && out.is_wchar_stream() )
{
utf8_to_int(i, end, res, correct);
if( correct )
esc_to_csv(static_cast<wchar_t>(res), out); // IMPROVEME no surrogate pair used here (if sizeof(wchar_t) == 2)
// put replacement char if not correct?
}
else
{
esc_to_csv(static_cast<wchar_t>(*i), out);
}
}
}

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_patternreplacer
#define headerfile_pikotools_src_convert_patternreplacer
#ifndef headerfile_picotools_convert_patternreplacer
#define headerfile_picotools_convert_patternreplacer
#include <string>
#include "textstream/textstream.h"
@ -132,7 +135,7 @@ private:
const CharType * index_start_str = &pattern[i+1];
const CharType * after_str;
bool was_overflow;
unsigned long index = to_ul(index_start_str, 10, &after_str, &was_overflow, false);
unsigned long index = Toul(index_start_str, 10, &after_str, &was_overflow, false);
if( !was_overflow && after_str > index_start_str && (size_t)index < params.size() )
{

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2017-2022, Tomasz Sowa
* Copyright (c) 2017-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_strtoint
#define headerfile_pikotools_src_convert_strtoint
#ifndef headerfile_picotools_convert_strtoint
#define headerfile_picotools_convert_strtoint
#include <limits>
#include "text.h"
@ -44,8 +47,10 @@ namespace pt
{
template<typename CharType>
unsigned long long to_ull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
unsigned long long res = 0;
bool carry = false;
@ -94,16 +99,8 @@ unsigned long long to_ull(const CharType * str, int base = 10, const CharType **
}
template<class StringType>
unsigned long long to_ull(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_ull(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
template<typename CharType>
long long to_ll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
bool was_sign = false;
bool was_overflow_u = false;
@ -119,7 +116,7 @@ long long to_ll(const CharType * str, int base = 10, const CharType ** after_str
str += 1;
}
unsigned long long uval = to_ull(str, base, after_str, &was_overflow_u, false);
unsigned long long uval = Toull(str, base, after_str, &was_overflow_u, false);
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
if( was_overflow_u )
@ -144,21 +141,14 @@ long long to_ll(const CharType * str, int base = 10, const CharType ** after_str
template<class StringType>
long long to_ll(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_ll(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
template<typename CharType, typename IntegerType>
IntegerType to_unsigned_integer_type(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
bool was_overflow_ll = false;
SetOverflow(was_overflow, false);
unsigned long long val = to_ull(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
unsigned long long val = Toull(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
if( was_overflow_ll || val > static_cast<unsigned long long>(std::numeric_limits<IntegerType>::max()) )
{
@ -171,39 +161,28 @@ IntegerType to_unsigned_integer_type(const CharType * str, int base = 10, const
template<class CharType>
unsigned long to_ul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type<CharType, unsigned long>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
unsigned long to_ul(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type<typename StringType::value_type, unsigned long>(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
return ToUnsignedIntegerType<CharType, unsigned long>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class CharType>
unsigned int to_ui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type<CharType, unsigned int>(str, base, after_str, was_overflow, allow_skip_whitechars);
return ToUnsignedIntegerType<CharType, unsigned int>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
unsigned int to_ui(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type<typename StringType::value_type, unsigned int>(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
template<typename CharType, typename IntegerType>
IntegerType to_integer_type(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
bool was_overflow_ll = false;
SetOverflow(was_overflow, false);
long long val = to_ll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
long long val = Toll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
if( was_overflow_ll ||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
@ -219,28 +198,22 @@ IntegerType to_integer_type(const CharType * str, int base = 10, const CharType
template<class CharType>
long to_l(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type<CharType, long>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
long to_l(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type<typename StringType::value_type, long>(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
return ToIntegerType<CharType, long>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class CharType>
int to_i(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type<CharType, int>(str, base, after_str, was_overflow, allow_skip_whitechars);
return ToIntegerType<CharType, int>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
int to_i(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type<typename StringType::value_type, int>(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
/*
@ -254,7 +227,7 @@ int to_i(const StringType & str, int base = 10, const typename StringType::value
*
*/
template<typename CharType>
unsigned long long to_ull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned long long Toull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
if( allow_skip_whitechars )
str = skip_white(str);
@ -279,13 +252,13 @@ unsigned long long to_ull_b(const CharType * str, const CharType ** after_str =
str += 1;
}
return to_ull(str, base, after_str, was_overflow, false);
return Toull(str, base, after_str, was_overflow, false);
}
template<typename CharType>
long long to_ll_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
bool was_sign = false;
bool was_overflow_u = false;
@ -301,7 +274,7 @@ long long to_ll_b(const CharType * str, const CharType ** after_str = 0, bool *
str += 1;
}
unsigned long long uval = to_ull_b(str, after_str, &was_overflow_u, false);
unsigned long long uval = Toull_b(str, after_str, &was_overflow_u, false);
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
if( was_overflow_u )
@ -332,12 +305,12 @@ long long to_ll_b(const CharType * str, const CharType ** after_str = 0, bool *
template<typename CharType, typename IntegerType>
IntegerType to_unsigned_integer_type_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
IntegerType ToUnsignedIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
bool was_overflow_ll = false;
SetOverflow(was_overflow, false);
unsigned long long val = to_ull_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
unsigned long long val = Toull_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
if( was_overflow_ll || val > static_cast<unsigned long long>(std::numeric_limits<IntegerType>::max()) )
{
@ -350,15 +323,15 @@ IntegerType to_unsigned_integer_type_b(const CharType * str, const CharType ** a
template<class CharType>
unsigned long to_ul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned long Toul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type_b<CharType, unsigned long>(str, after_str, was_overflow, allow_skip_whitechars);
return ToUnsignedIntegerType_b<CharType, unsigned long>(str, after_str, was_overflow, allow_skip_whitechars);
}
template<class CharType>
unsigned int to_ui_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned int Toui_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type_b<CharType, unsigned int>(str, after_str, was_overflow, allow_skip_whitechars);
return ToUnsignedIntegerType_b<CharType, unsigned int>(str, after_str, was_overflow, allow_skip_whitechars);
}
@ -366,12 +339,12 @@ unsigned int to_ui_b(const CharType * str, const CharType ** after_str = 0, bool
template<typename CharType, typename IntegerType>
IntegerType to_integer_type_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
IntegerType ToIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
bool was_overflow_ll = false;
SetOverflow(was_overflow, false);
long long val = to_ll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
long long val = Toll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
if( was_overflow_ll ||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
@ -387,15 +360,15 @@ IntegerType to_integer_type_b(const CharType * str, const CharType ** after_str
template<class CharType>
long to_l_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
long Tol_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type_b<CharType, long>(str, after_str, was_overflow, allow_skip_whitechars);
return ToIntegerType_b<CharType, long>(str, after_str, was_overflow, allow_skip_whitechars);
}
template<class CharType>
int to_i_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
int Toi_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type_b<CharType, int>(str, after_str, was_overflow, allow_skip_whitechars);
return ToIntegerType_b<CharType, int>(str, after_str, was_overflow, allow_skip_whitechars);
}

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2017-2022, Tomasz Sowa
* Copyright (c) 2017-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstddef>
@ -532,18 +535,6 @@ void trim(std::wstring & str, wchar_t c)
}
void trim_last_new_lines(std::string & str, bool check_carriage_return_too)
{
pt_private::trim_last_generic(str, '\n', check_carriage_return_too, '\r');
}
void trim_last_new_lines(std::wstring & str, bool check_carriage_return_too)
{
pt_private::trim_last_generic(str, '\n', check_carriage_return_too, '\r');
}
}

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2017-2022, Tomasz Sowa
* Copyright (c) 2017-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_text
#define headerfile_pikotools_src_convert_text
#ifndef headerfile_picotools_convert_text
#define headerfile_picotools_convert_text
#include <string>
@ -44,49 +47,6 @@ namespace pt
bool is_white(wchar_t c, bool check_additional_chars = true, bool treat_new_line_as_white = true);
bool is_digit(wchar_t c, int base = 10, int * digit = 0);
// IMPROVEME add tests
template<typename HexStringPointerType, typename BytesStringType>
bool hex_string_pointer_to_bytes(const HexStringPointerType * hex_string, BytesStringType & bytes, bool clear_bytes = true)
{
bool converted_correctly = true;
size_t i = 0;
if( clear_bytes )
bytes.clear();
for( ; converted_correctly && hex_string[i] != 0 && hex_string[i+1] != 0 ; i += 2)
{
int c1, c2;
converted_correctly = converted_correctly && is_digit(hex_string[i], 16, &c1);
converted_correctly = converted_correctly && is_digit(hex_string[i+1], 16, &c2);
if( converted_correctly )
{
bytes += (char)(unsigned char)(((c1 << 4) | c2));
}
}
if( hex_string[i] != 0 )
{
// one digit has left
converted_correctly = false;
}
return converted_correctly;
}
// IMPROVEME add tests
template<typename HexStringType, typename BytesStringType>
bool hex_string_to_bytes(const HexStringType & hex_string, BytesStringType & bytes, bool clear_bytes = true)
{
return hex_string_pointer_to_bytes(hex_string.c_str(), bytes, clear_bytes);
}
const char * skip_white(const char * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
const wchar_t * skip_white(const wchar_t * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
@ -201,8 +161,6 @@ void trim_last(std::wstring & str, wchar_t c);
void trim(std::string & str, wchar_t c);
void trim(std::wstring & str, wchar_t c);
void trim_last_new_lines(std::string & str, bool check_carriage_return_too = true);
void trim_last_new_lines(std::wstring & str, bool check_carriage_return_too = true);

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2022, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_convert_text_private
#define headerfile_pikotools_src_convert_text_private
#ifndef headerfile_picotools_convert_text_private
#define headerfile_picotools_convert_text_private
#include <string>
#include "text.h"
@ -367,24 +370,24 @@ size_t i;
template<typename StringType>
void trim_last_generic(StringType & s, wchar_t c, bool has_c2 = false, wchar_t c2 = 0)
void trim_last_generic(StringType & s, wchar_t c)
{
size_t i;
if( s.empty() )
return;
// looking for the 'c' or 'c2' (if defined) characters at the end
for(i=s.size()-1 ; i>0 && (s[i]==c || (has_c2 && s[i]==c2)) ; --i);
// looking for the 'c' characters at the end
for(i=s.size()-1 ; i>0 && s[i]==c ; --i);
if( i==0 && (s[i]==c || (has_c2 && s[i]==c2)) )
if( i==0 && s[i]==c )
{
// the whole string has the 'c' and 'c2' characters
// the whole string has the 'c' characters
s.clear();
return;
}
// deleting 'c' and 'c2' characters at the end
// deleting 'c' characters at the end
if( i != s.size() - 1 )
s.erase(i+1, StringType::npos);
}

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,25 +11,28 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "csvparser.h"

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,29 +11,32 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_csv_csvparser
#define headerfile_pikotools_src_csv_csvparser
#ifndef headerfile_picotools_csv_csvparser
#define headerfile_picotools_csv_csvparser
#include <string>
#include <fstream>

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2022, Tomasz Sowa
* Copyright (c) 2012-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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "date.h"
@ -69,24 +72,30 @@ Date::Date(const tm & t)
Date::Date(const char * str)
{
// parsing can be break in the middle of the string (if errors)
// and some values would not be initialized
Clear();
Parse(str);
}
Date::Date(const wchar_t * str)
{
Clear();
Parse(str);
}
Date::Date(const std::string & str)
{
Clear();
Parse(str);
}
Date::Date(const std::wstring & str)
{
Clear();
Parse(str);
}
@ -241,9 +250,9 @@ bool Date::operator!=(const Date & d) const
}
int Date::CompareDate(const Date & d, bool ignore_year) const
int Date::Compare(const Date & d) const
{
if( !ignore_year && year != d.year )
if( year != d.year )
return year - d.year;
if( month != d.month )
@ -252,13 +261,6 @@ int Date::CompareDate(const Date & d, bool ignore_year) const
if( day != d.day )
return day - d.day;
// dates are equal
return 0;
}
int Date::CompareTime(const Date & d) const
{
if( hour != d.hour )
return hour - d.hour;
@ -268,24 +270,11 @@ int Date::CompareTime(const Date & d) const
if( sec != d.sec )
return sec - d.sec;
// times are equal
// dates are equal
return 0;
}
int Date::Compare(const Date & d, bool ignore_year) const
{
int res = CompareDate(d, ignore_year);
if( res == 0 )
{
res = CompareTime(d);
}
return res;
}
bool Date::operator>(const Date & d) const
{
return Compare(d) > 0;
@ -310,29 +299,17 @@ bool Date::operator<=(const Date & d) const
}
void Date::ClearDate()
void Date::Clear()
{
year = 1970;
month = 1;
day = 1;
}
void Date::ClearTime()
{
hour = 0;
min = 0;
sec = 0;
}
void Date::Clear()
{
ClearDate();
ClearTime();
}
void Date::AssertRange(int & val, int val_min, int val_max)
{
if( val < val_min )

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2023, Tomasz Sowa
* Copyright (c) 2012-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_date_date
#define headerfile_pikotools_src_date_date
#ifndef headerfile_picotools_mainparser_mainparser
#define headerfile_picotools_mainparser_mainparser
#include <ctime>
#include <string>
@ -60,16 +63,11 @@ class Date
{
public:
static const time_t ONE_MIN = 60; /* one minute in seconds */
static const time_t ONE_HOUR = ONE_MIN * 60; /* one hour in seconds */
static const time_t ONE_DAY = ONE_HOUR * 24; /* one day in seconds */
static const time_t ONE_WEEK = ONE_DAY * 7; /* one week in seconds */
/*
the date
*/
int year; // 1970 - 10000
int year; // 1970 - ...
int month; // 1 - 12
int day; // 1 - 31
int hour; // 0 - 23
@ -161,15 +159,12 @@ public:
time_t operator-(const Date & d) const;
/*
'Compare' methods returns zero if this and d are equal
'Compare' returns zero if this and d are equal
return value less than zero if this is lower than d
and a value greater than zero if this is greater than d
*/
int CompareDate(const Date & d, bool ignore_year = false) const;
int CompareTime(const Date & d) const;
int Compare(const Date & d, bool ignore_year = false) const;
int Compare(const Date & d) const;
/*
@ -196,21 +191,7 @@ public:
/*
set the date to: 1970-01-01
the time is not changed
*/
void ClearDate();
/*
set the time to 00:00:00
the date is not changed
*/
void ClearTime();
/*
set the Unix Epoch: 1970-01-01 00:00:00
set the Unix Epoch: 1970.01.01 00:00:00
*/
void Clear();
@ -344,7 +325,7 @@ public:
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseDayMonthYear(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseDayMonthYear(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseDayMonthYear(const StringType & str);
@ -369,7 +350,7 @@ public:
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseYearMonthDay(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseYearMonthDay(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseYearMonthDay(const StringType & str);
@ -394,7 +375,7 @@ public:
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseMonthDay(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseMonthDay(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseMonthDay(const StringType & str);
@ -416,7 +397,7 @@ public:
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseHourMinSec(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseHourMinSec(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseHourMinSec(const StringType & str);
@ -438,83 +419,34 @@ public:
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseHourMin(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseHourMin(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseHourMin(const StringType & str);
template<class CStringType>
bool ParseZoneOffset(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseZoneOffset(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseZoneOffset(const StringType & str);
/*
parsing a year and a month (if exists) and a day (if exists)
the input string can be as follows: YYYY[sep]MM[sep]DD
the separator is optional, it can be: '.', '-', '/' or just white characters
(white characters before and after a separator are skipped)
sample valid dates:
"2022"
" 2022"
"202212"
" 2022 12"
"2022-12"
" 2022 - 12"
"20221222"
" 20221222 "
" 2022 12 22 "
" 2022 12 22 "
"2022-12-22"
" 2022-12-22"
" 2022 - 12 - 22 "
if the month is not provided then it is set to 01,
if the day is not provided then it is set to 01 too
this method doesn't test if the values are correct
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseDate(const CStringType * str, const CStringType ** str_after = nullptr);
template<class StringType>
bool ParseDate(const StringType & str);
/*
parsing an hour and minutes (if exists) and seconds (if exists)
the input string can be as follows: HH[sep]MM[sep]SS
separator is optional, it can be ':' or just white characters
(white characters before and after a separator are skipped)
parsing hour and minutes (if exists) and seconds (if exists)
the input string can be as follows:
"14" -- only an hour given (min and sec will be zero)
"14:10" -- hour with minutes (sec will be zero)
"1410" -- the same as above
"14:10:35" -- hour, minutes and seconds
"141035" -- the same as above
"14 10 35" -- the same as above
white characters are ommited so these are valid strings too:
" 14 : 10 : 35 "
" 14 : 10 : 35some text "
a white character means a space or a tab
a decimal fraction may be added to the lowest order time element present, e.g:
"14.500" = 14:30:00
"14:10.500" = 14:10:30
"14:10:35.500" = 14:10:35 (the seconds' fraction is skipped)
instead of dot you can use a comma too,
the fraction part can be from 1 to 9 digits
this method doesn't test if the values are correct
use IsCorrectDate() to check
*/
template<class CStringType>
bool ParseTime(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseTime(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseTime(const StringType & str);
@ -534,11 +466,9 @@ public:
this method doesn't test if the values are correct
use IsCorrectDate() to check
the time is parsed with ParseTime() method, look there for an additional description about available time strings
*/
template<class CStringType>
bool ParseMonthDayTime(const CStringType * str, const CStringType ** str_after = nullptr);
bool ParseMonthDayTime(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool ParseMonthDayTime(const StringType & str);
@ -547,57 +477,31 @@ public:
/*
parsing year/month/day hour:min:sec
the input strings can be as follows:
"20081012 141035"
"20081012T141035"
"20081012141035"
"2008-10-12 14:10:35"
"2008/10/12 14:10:35"
"2008.10.12 14:10:35"
"2008-10/12 14:10:35"
"2008-10-12 14:10:35.500"
"2008-10-12 14:10.500"
"2008-10-12 14.500"
"2008-10-12 14"
"2008-10 14"
"2008 14"
months and days can be omitted - in such a case 01 is set,
similar min and sec can be omitted (they are assumed to be 00 in such a case)
white characters are ommited
so the input string can be:
" 2008 - 10 / 12 14 : 10 : 35 "
a white character means a space or a tab
as an optional separator for the date can be '-' '/' or '.'
as an optional separator for the time is the ':' character
as a separator for year/month/day can be '-' '/' or '.'
see ParseYearMonthDay() for details
at the and of the string there can be defined a time zone in the form of "+01:00", e.g:
"2008-10-12 14:10:35+01:00"
"2008-10-12 14:10:35-02:00"
or without a separator:
"2008-10-12 14:10:35+0100"
"2008-10-12 14:10:35-0200"
or just a 'Z' character indicating 00:00 zone
"2008-10-12 14:10:35Z"
as a separator for hour:min:sec is the ':' character
see ParseHourMinSec() for details
at the end the method checks if the values are correct
(by using IsCorrectDate())
the format is similar to ISO 8601
https://en.wikipedia.org/wiki/ISO_8601
at the moment there is no support for week dates e.g. "2008-W01-3" and ordinal dates e.g. "2008-010" formats
see ParseDate() and ParseTime for more examples
*/
template<class CStringType>
bool Parse(const CStringType * str, const CStringType ** str_after, bool check_time_zone = true);
template<class CStringType>
bool Parse(const CStringType * str, bool check_time_zone = true);
bool Parse(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool Parse(const StringType & str, bool check_time_zone = true);
bool Parse(const StringType & str);
private:
@ -617,16 +521,7 @@ private:
void SkipWhite(const CStringType * & str);
template<class CStringType>
bool IsDigit(const CStringType * str);
template<class CStringType>
bool ReadInt(const CStringType * & str, int & result, size_t max_digits = 0, size_t * digits_read = nullptr, int * digits_base = nullptr);
template<class CStringType>
bool ReadCommaInt(const CStringType * & str, bool & was_comma, int & result, size_t max_digits = 0, size_t * digits_read = nullptr, int * base = nullptr);
template<class CStringType>
bool ParseTimeValue(const CStringType * & str, int & value, bool & has_fraction, int & fraction, int * fraction_base);
bool ReadInt(const CStringType * & str, int & result, size_t max_digits = 0);
template<class CStringType>
bool SkipSeparator(const CStringType * & str, int separator, int separator2 = -1, int separator3 = -1);
@ -820,6 +715,7 @@ bool Date::ParseMonthDay(const StringType & str)
}
template<class CStringType>
bool Date::ParseHourMinSec(const CStringType * str, const CStringType ** str_after)
{
@ -882,37 +778,39 @@ bool Date::ParseZoneOffset(const CStringType * str, const CStringType ** str_aft
is_sign = true;
str += 1;
result = true; // if there are no digits we return true
if( IsDigit(str) )
if( ReadInt(str, offset_hour, 2) && offset_hour >= -12 && offset_hour <= 14 )
{
result = ReadInt(str, offset_hour, 2) && offset_hour >= -12 && offset_hour <= 14;
SkipWhite(str);
SetAfter(str, str_after);
if( result )
if( *str == ':' )
{
SkipSeparator(str, ':');
if( IsDigit(str) )
{
// offset_min is optional
result = ReadInt(str, offset_min, 2) && offset_min > -60 && offset_min < 60;
}
if( result )
{
time_t offset = (time_t)offset_hour * 60 * 60 + (time_t)offset_min * 60;
if( is_sign )
offset = -offset;
FromTime(ToTime() - offset);
}
str += 1;
SkipWhite(str);
SetAfter(str, str_after);
}
if( ReadInt(str, offset_min, 2) && offset_min > -60 && offset_min < 60 )
{
SetAfter(str, str_after);
}
else
{
offset_min = 0;
}
time_t offset = (time_t)offset_hour * 60 * 60 + (time_t)offset_min * 60;
result = true;
if( is_sign )
offset = -offset;
FromTime(ToTime() - offset);
}
}
SetAfter(str, str_after);
return result;
return result;
}
@ -924,114 +822,47 @@ bool Date::ParseZoneOffset(const StringType & str)
template<class CStringType>
bool Date::ParseDate(const CStringType * str, const CStringType ** str_after)
{
bool status = false;
ClearDate();
if( ReadInt(str, year, 4) )
{
status = true;
SkipSeparator(str, '.', '-', '/');
if( IsDigit(str) )
{
status = ReadInt(str, month, 2);
SkipSeparator(str, '.', '-', '/');
if( status && IsDigit(str) )
{
status = ReadInt(str, day, 2);
}
}
}
SetAfter(str, str_after);
return status;
}
template<class StringType>
bool Date::ParseDate(const StringType & str)
{
return ParseDate(str.c_str());
}
template<class CStringType>
bool Date::ParseTimeValue(const CStringType * & str, int & value, bool & has_fraction, int & fraction, int * fraction_base)
{
bool status = false;
size_t digits_read = 0;
bool was_comma = false;
has_fraction = false;
fraction = 0;
*fraction_base = 1;
if( ReadInt(str, value, 2) )
{
status = ReadCommaInt(str, was_comma, fraction, 0, &digits_read, fraction_base);
if( status )
{
if( was_comma )
has_fraction = true;
SkipSeparator(str, ':');
}
}
return status;
}
template<class CStringType>
bool Date::ParseTime(const CStringType * str, const CStringType ** str_after)
{
bool status = false;
bool has_fraction = false;
int fraction = 0;
int fraction_base = 0;
ClearTime();
if( ParseTimeValue(str, hour, has_fraction, fraction, &fraction_base) )
if( !ReadInt(str, hour) )
{
status = true;
SetAfter(str, str_after);
return false;
}
if( has_fraction )
{
min = (60L * fraction) / (long)(fraction_base);
int min_rem = (60L * fraction) % (long)(fraction_base);
sec = (min_rem * 60L) / (long)fraction_base;
}
else
if( IsDigit(str) )
{
status = ParseTimeValue(str, min, has_fraction, fraction, &fraction_base);
min = 0;
sec = 0;
if( status )
{
if( has_fraction )
{
sec = (60L * fraction) / (long)(fraction_base);
}
else
if( IsDigit(str) )
{
status = ParseTimeValue(str, sec, has_fraction, fraction, &fraction_base);
// ignore the seconds fraction if exists
}
}
}
if( !SkipSeparator(str, ':') )
{
SetAfter(str, str_after);
return true; // only an hour given
}
if( !ReadInt(str, min) )
{
SetAfter(str, str_after);
return false;
}
if( !SkipSeparator(str, ':') )
{
SetAfter(str, str_after);
return true; // only an hour and minutes given
}
if( !ReadInt(str, sec) )
{
SetAfter(str, str_after);
return false;
}
SetAfter(str, str_after);
return status;
return true;
}
@ -1069,40 +900,36 @@ bool Date::ParseMonthDayTime(const StringType & str)
template<class CStringType>
bool Date::Parse(const CStringType * str, const CStringType ** str_after, bool check_time_zone)
bool Date::Parse(const CStringType * str, const CStringType ** str_after)
{
const CStringType * after;
bool result = false;
Clear();
const CStringType * after;
bool result = false;
if( ParseDate(str, &after) )
if( ParseYearMonthDay(str, &after) )
{
result = true;
SkipWhite(after);
if( *after == 'T' )
{
// ISO 8601 format
// https://en.wikipedia.org/wiki/ISO_8601
// at the moment skip the 'T' character only
after += 1;
SkipWhite(after);
}
if( IsDigit(after) )
if( ParseHourMinSec(after, &after) )
{
result = ParseTime(after, &after);
SkipWhite(after);
result = true;
if( result && check_time_zone )
if( *after == 'Z' )
{
SkipWhite(after);
if( *after == 'Z' )
{
after += 1;
}
else
if( *after == '-' || *after == '+' )
{
result = ParseZoneOffset(after, &after);
}
after += 1;
}
else
{
// we dont have to check errors here
ParseZoneOffset(after, &after);
}
}
}
@ -1112,23 +939,14 @@ bool Date::Parse(const CStringType * str, const CStringType ** str_after, bool c
if( result )
result = IsCorrectDate();
return result;
}
template<class CStringType>
bool Date::Parse(const CStringType * str, bool check_time_zone)
{
const CStringType * str_after = nullptr;
return Parse(str, &str_after, check_time_zone);
return result;
}
template<class StringType>
bool Date::Parse(const StringType & str, bool check_time_zone)
bool Date::Parse(const StringType & str)
{
const typename StringType::value_type * after_string = nullptr;
return Parse(str.c_str(), &after_string, check_time_zone);
return Parse(str.c_str());
}
@ -1151,80 +969,34 @@ void Date::SkipWhite(const CStringType * & str)
}
template<class CStringType>
bool Date::IsDigit(const CStringType * str)
{
return (*str >= '0' && *str <= '9');
}
template<class CStringType>
bool Date::ReadInt(const CStringType * & str, int & result, size_t max_digits, size_t * digits_read, int * digits_base)
bool Date::ReadInt(const CStringType * & str, int & result, size_t max_digits)
{
bool something_read = false;
SkipWhite(str);
result = 0;
size_t read_chars = 0;
int base = 1;
bool skip_last_digits = false;
size_t len = 0;
if( max_digits == 0 )
skip_last_digits = true;
if( max_digits == 0 || max_digits > 9 )
max_digits = 9;
while( IsDigit(str) && read_chars < max_digits )
while( *str >= '0' && *str <= '9' && (max_digits == 0 || len < max_digits))
{
result = result * 10 + (*str - '0');
base = base * 10;
str += 1;
read_chars += 1;
}
len += 1;
something_read = true;
if( skip_last_digits )
{
while( IsDigit(str) )
if( result > 10000 )
{
str += 1;
read_chars += 1;
// we assumed the max year to be 10000
return false;
}
}
if( digits_read )
*digits_read = read_chars;
if( digits_base )
*digits_base = base;
return read_chars > 0 && read_chars <= max_digits;
return something_read;
}
template<class CStringType>
bool Date::ReadCommaInt(const CStringType * & str, bool & was_comma, int & result, size_t max_digits, size_t * digits_read, int * base)
{
bool status = true; // the comma is optional so we return true if it not exists
result = 0;
was_comma = false;
if( digits_read )
*digits_read = 0;
if( base )
*base = 1;
if( *str == '.' || *str == ',' )
{
str += 1;
was_comma = true;
if( IsDigit(str) )
status = ReadInt(str, result, max_digits, digits_read, base);
}
return status;
}
template<class CStringType>
bool Date::SkipSeparator(const CStringType * & str, int separator, int separator2, int separator3)
@ -1234,25 +1006,22 @@ bool Date::SkipSeparator(const CStringType * & str, int separator, int separator
if( *str == separator )
{
str += 1;
SkipWhite(str);
return true;
}
if( separator2 != -1 && *str == separator2 )
{
str += 1;
SkipWhite(str);
return true;
}
if( separator3 != -1 && *str == separator3 )
{
str += 1;
SkipWhite(str);
return true;
}
return false;
return false;
}

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -10,26 +10,29 @@
*
* 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.
*
* 1. 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.
*
* 2. 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "bbcodeparser.h"

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -10,30 +10,33 @@
*
* 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.
*
* 1. 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.
*
* 2. 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_html_bbcodeparser
#define headerfile_pikotools_src_html_bbcodeparser
#ifndef headerfile_winix_core_bbcodeparser
#define headerfile_winix_core_bbcodeparser
#include "htmlparser.h"

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2008-2024, Tomasz Sowa
* Copyright (c) 2008-2021, 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.
*
* 1. 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.
*
* 2. 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "htmlparser.h"
@ -57,10 +60,6 @@ void HTMLParser::clear_input_flags()
out_stream = nullptr;
out_space = nullptr;
line_len = 0;
char_was_escaped = false;
escaped_chars_buffer.clear();
escaped_char_index = 0;
filter_mode = false;
}
@ -69,17 +68,15 @@ void HTMLParser::clear_input_flags()
void HTMLParser::Item::Clear()
{
name.clear();
type = none;
is_commentary = false;
is_cdata = false;
porphans = nullptr;
new_line_before = false;
new_line_after = false;
type = none;
is_commentary = false;
porphans = nullptr;
new_line_before = false;
new_line = false;
new_line_in_the_middle = false;
white_char_before = false;
has_body_tag = false;
tree_index = 0;
space = nullptr;
has_body_tag = false;
tree_index = 0;
space = nullptr;
}
@ -104,31 +101,24 @@ void HTMLParser::parse_html(const wchar_t * in, Space & space, bool compact_mode
}
void HTMLParser::set_item_parsed_listener(Listener * listener)
void HTMLParser::set_item_parsed_listener(ItemParsedListener * listener)
{
this->listener = listener;
item_parsed_listener = listener;
}
void HTMLParser::prepare_to_parse_xml(Space & out_space, bool compact_mode, bool clear_space)
{
clear_input_flags();
parsing_html = false;
this->out_space = &out_space;
xml_compact_mode = compact_mode;
if( clear_space )
{
this->out_space->clear();
}
}
HTMLParser::Status HTMLParser::parse_xml_file(const char * file_name, Space & out_space, bool compact_mode, bool clear_space)
{
prepare_to_parse_xml(out_space, compact_mode, clear_space);
clear_input_flags();
parsing_html = false;
reading_from_file = true;
xml_compact_mode = compact_mode;
this->out_space = &out_space;
if( clear_space )
this->out_space->clear();
file.clear();
file.open(file_name, std::ios_base::binary | std::ios_base::in);
@ -171,79 +161,6 @@ HTMLParser::Status HTMLParser::parse_xml_file(const std::wstring & file_name, Sp
}
HTMLParser::Status HTMLParser::parse_xml(const char * str, Space & out_space, bool compact_mode, bool clear_space)
{
prepare_to_parse_xml(out_space, compact_mode, clear_space);
pchar_ascii = str;
Init();
Read();
Uninit();
return status;
}
HTMLParser::Status HTMLParser::parse_xml(const std::string & str, Space & out_space, bool compact_mode, bool clear_space)
{
return parse_xml(str.c_str(), out_space, compact_mode, clear_space);
}
HTMLParser::Status HTMLParser::parse_xml(const wchar_t * str, Space & out_space, bool compact_mode, bool clear_space)
{
prepare_to_parse_xml(out_space, compact_mode, clear_space);
pchar_unicode = str;
Init();
Read();
Uninit();
return status;
}
HTMLParser::Status HTMLParser::parse_xml(const std::wstring & str, Space & out_space, bool compact_mode, bool clear_space)
{
return parse_xml(str.c_str(), out_space, compact_mode, clear_space);
}
HTMLParser::Status HTMLParser::parse_xml(const pt::TextStream & str, Space & out_space, bool compact_mode, bool clear_space)
{
prepare_to_parse_xml(out_space, compact_mode, clear_space);
pt::TextStream::const_iterator start = str.begin();
pt::TextStream::const_iterator end = str.end();
text_stream_iterator = &start;
text_stream_iterator_end = &end;
Init();
Read();
Uninit();
return status;
}
HTMLParser::Status HTMLParser::parse_xml(const pt::WTextStream & str, Space & out_space, bool compact_mode, bool clear_space)
{
prepare_to_parse_xml(out_space, compact_mode, clear_space);
pt::WTextStream::const_iterator start = str.begin();
pt::WTextStream::const_iterator end = str.end();
wtext_stream_iterator = &start;
wtext_stream_iterator_end = &end;
Init();
Read();
Uninit();
return status;
}
void HTMLParser::filter(const wchar_t * in, std::wstring & out, bool clear_out_string)
{
@ -251,7 +168,6 @@ void HTMLParser::filter(const wchar_t * in, std::wstring & out, bool clear_out_s
pchar_unicode = in;
out_string = &out;
filter_mode = true;
if( clear_out_string )
out_string->clear();
@ -288,8 +204,8 @@ void HTMLParser::filter(const WTextStream & in, Stream & out, bool clear_out_str
wtext_stream_iterator = &begin;
wtext_stream_iterator_end = &end;
out_stream = &out;
filter_mode = true;
if( clear_out_stream )
out_stream->clear();
@ -311,7 +227,6 @@ HTMLParser::Status HTMLParser::filter_file(const char * file_name, std::wstring
file.open(file_name, std::ios_base::binary | std::ios_base::in);
out_string = &out;
filter_mode = true;
if( clear_out_stream )
out_string->clear();
@ -375,12 +290,6 @@ int HTMLParser::get_last_parsed_line()
}
int HTMLParser::get_last_parsed_column()
{
return column;
}
void HTMLParser::SetSomeDefaults()
{
white_mode = WHITE_MODE_ORIGIN;
@ -393,7 +302,7 @@ void HTMLParser::SetSomeDefaults()
skip_commentaries = false;
skip_entities = false;
analyze_entities = false;
listener = nullptr;
item_parsed_listener = nullptr;
}
@ -693,7 +602,7 @@ void HTMLParser::SkipAndCheckClosingTag(std::wstring * remember_text)
while( lastc != -1 )
{
if( !char_was_escaped && (lastc == '"' || lastc == '\'') )
if( lastc == '"' || lastc == '\'' )
{
if( is_quoted )
{
@ -714,7 +623,7 @@ void HTMLParser::SkipAndCheckClosingTag(std::wstring * remember_text)
LastItem().type = Item::simple;
}
else
if( !is_quoted && (!char_was_escaped && IsClosingTagMark(lastc)) )
if( !is_quoted && IsClosingTagMark(lastc) )
{
read_char();
break;
@ -734,7 +643,7 @@ bool HTMLParser::IsValidCharForName(int c)
if( (c>='a' && c<='z') ||
(c>='A' && c<='Z') ||
(c>='0' && c<='9') ||
c=='-' || c=='!' || c==':' || c=='-' || c=='_' || c=='[') // : is for a namespace character, - is for a commentary, [ is for CDATA
c=='-' || c=='!' || c==':' || c=='-' || c=='_') // : is for a namespace character, - is for a commentary
return true;
return false;
@ -784,13 +693,6 @@ size_t i;
read_char();
break;
}
if( LastItem().type == Item::special && name == L"![CDATA[" )
{
LastItem().is_cdata = true;
read_char();
break;
}
}
read_char();
@ -837,18 +739,15 @@ void HTMLParser::ReadItemAttrValue(bool has_quote, wchar_t quote_char)
while( lastc != -1 )
{
if( !char_was_escaped )
if( has_quote )
{
if( has_quote )
{
if( lastc == quote_char )
break;
}
else
{
if( IsClosingTagMark(lastc) || lastc == 10 || IsWhite(lastc) )
break;
}
if( lastc == quote_char )
break;
}
else
{
if( IsClosingTagMark(lastc) || lastc == 10 || IsWhite(lastc) )
break;
}
if( lastc==10 || IsWhite(lastc) )
@ -881,18 +780,15 @@ void HTMLParser::ReadXMLItemAttrValue(bool has_quote, wchar_t quote_char)
while( lastc != -1 )
{
if( !char_was_escaped )
if( has_quote )
{
if( has_quote )
{
if( lastc == quote_char )
break;
}
else
{
if( IsClosingTagMark(lastc) || lastc == 10 || IsWhite(lastc) )
break;
}
if( lastc == quote_char )
break;
}
else
{
if( IsClosingTagMark(lastc) || lastc == 10 || IsWhite(lastc) )
break;
}
// IMPROVEME add support for analyze_entities?
@ -1097,49 +993,10 @@ return CheckOrphan(str, end, LastItem().porphans->tab);
}
bool HTMLParser::PutNormalNonWhite(std::wstring & str, bool allow_put_new_line, bool allow_put_space, bool is_cdata)
void HTMLParser::PutNormalNonWhite(std::wstring & str, bool allow_put_new_line, bool allow_put_space)
{
bool was_closing_tag = false;
while( lastc != -1 && lastc != 10 && !IsWhite(lastc) )
while( lastc != -1 && lastc != 10 && !IsWhite(lastc) && !IsOpeningTagMark(lastc) )
{
if( is_cdata )
{
if( lastc == ']' )
{
read_char();
if( lastc == ']' )
{
read_char();
if( IsClosingTagMark(lastc) )
{
read_char();
was_closing_tag = true;
break;
}
else
{
str += ']';
str += ']';
}
}
else
{
str += ']';
}
}
}
else
{
if( !char_was_escaped && IsOpeningTagMark(lastc) )
{
was_closing_tag = true;
break;
}
}
str += lastc;
read_char();
}
@ -1162,8 +1019,6 @@ bool HTMLParser::PutNormalNonWhite(std::wstring & str, bool allow_put_new_line,
AnalyzeEntitiesAndPut(str.c_str(), str.c_str() + str.size(), nullptr);
else
Put(str);
return was_closing_tag;
}
@ -1262,18 +1117,10 @@ bool HTMLParser::PutOpeningTag()
return false;
}
if( current_white_char_mode() == WHITE_MODE_TREE )
if( current_white_char_mode() == WHITE_MODE_TREE && LastItem().new_line_before )
{
if( LastItem().new_line_before )
{
Put(10);
PutTabs(LastItem().tree_index);
}
else
if( LastItem().white_char_before )
{
Put(' ');
}
Put(10);
PutTabs(LastItem().tree_index);
}
PutOpeningTagMark();
@ -1413,7 +1260,7 @@ void HTMLParser::ReadTextUntilClosingCommentary()
tmp_text += lastc;
read_char();
if( !char_was_escaped && IsClosingTagMark(lastc) )
if( IsClosingTagMark(lastc) )
{
tmp_text += lastc;
read_char();
@ -1434,7 +1281,6 @@ void HTMLParser::ReadTextUntilClosingCommentary()
}
bool HTMLParser::IsClosingTagForLastItem(bool put_closing_tag_as_well)
{
tmp_text.clear();
@ -1486,7 +1332,7 @@ void HTMLParser::ReadTextUntilClosingTag(bool put_closing_tag_as_well)
{
while( lastc != -1 )
{
if( !char_was_escaped && IsOpeningTagMark(lastc) )
if( IsOpeningTagMark(lastc) )
{
if( IsClosingTagForLastItem(put_closing_tag_as_well) )
{
@ -1506,11 +1352,8 @@ void HTMLParser::ReadTextUntilClosingTag(bool put_closing_tag_as_well)
// reading text between html tags
void HTMLParser::ReadText(bool is_cdata)
void HTMLParser::ReadText()
{
new_item_has_new_line_before = false;
new_item_has_white_char_before = false;
bool was_white_char = false;
bool was_new_line = false;
@ -1521,7 +1364,7 @@ void HTMLParser::ReadText(bool is_cdata)
if( current_white_char_mode() == WHITE_MODE_TREE )
{
if( LastItem().new_line_after || (wrap_line != 0 && LastItem().has_body_tag && line_len >= wrap_line) )
if( LastItem().new_line || (wrap_line != 0 && LastItem().has_body_tag && line_len >= wrap_line) )
{
allow_put_new_line = true;
}
@ -1539,18 +1382,10 @@ void HTMLParser::ReadText(bool is_cdata)
text_space_wstr = &wstr_space.value.value_wstring;
}
bool was_closing_tag = false;
while( lastc != -1 && !was_closing_tag )
while( lastc != -1 && !IsOpeningTagMark(lastc) )
{
tmp_text.clear();
was_closing_tag = PutNormalNonWhite(tmp_text, allow_put_new_line, allow_put_space, is_cdata);
if( lastc == -1 || was_closing_tag )
{
new_item_has_new_line_before = was_new_line;
new_item_has_white_char_before = was_white_char;
}
PutNormalNonWhite(tmp_text, allow_put_new_line, allow_put_space);
if( !tmp_text.empty() )
{
@ -1568,7 +1403,6 @@ void HTMLParser::ReadText(bool is_cdata)
{
SkipWhiteLines(text_space_wstr);
PutNonBreakingSpace();
was_new_line = false;
}
}
else
@ -1586,7 +1420,7 @@ void HTMLParser::ReadText(bool is_cdata)
LastItem().new_line_in_the_middle = true;
if( !was_non_white_text )
LastItem().new_line_after = true;
LastItem().new_line = true;
}
else
{
@ -1603,10 +1437,11 @@ void HTMLParser::ReadText(bool is_cdata)
if( text_space_wstr && !text_space_wstr->empty() && was_non_white_text )
{
AddTextSpaceToSpaceTree(*text_space);
AddSpaceToSpaceTree(*text_space);
}
text_space_tmp.clear();
new_item_has_new_line_before = was_new_line;
}
@ -1644,7 +1479,7 @@ bool HTMLParser::ReadItemAttr()
read_char(); // skipping '='
SkipWhiteLines();
bool has_quote = !char_was_escaped && (lastc == '"' || lastc == '\'');
bool has_quote = (lastc == '\"' || lastc == '\'');
wchar_t quote_char = lastc;
if( has_quote )
@ -1656,7 +1491,7 @@ bool HTMLParser::ReadItemAttr()
else
ReadXMLItemAttrValue(has_quote, quote_char);
if( has_quote && !char_was_escaped && lastc == quote_char )
if( has_quote && lastc == quote_char )
read_char(); // skipping the last quote mark
return true;
@ -1783,11 +1618,6 @@ void HTMLParser::ReadItemSpecial()
Put(LastItem().name);
}
else
if( LastItem().is_cdata )
{
// do nothing
}
else
{
tmp_text.clear();
SkipWhiteLines();
@ -1858,7 +1688,6 @@ bool HTMLParser::ReadItem()
return false;
LastItem().new_line_before = new_item_has_new_line_before; // new_item_has_new_line_before is set by ReadText() method
LastItem().white_char_before = new_item_has_white_char_before; // new_item_has_white_char_before is set by ReadText() method
if( stack_len > 1 && pstack[stack_len-2].new_line_in_the_middle )
LastItem().tree_index += 1;
@ -2072,7 +1901,7 @@ void HTMLParser::CheckStackPrintRest()
{
while( stack_len-- > 0 )
{
if( stack_len==0 || pstack[stack_len-1].new_line_after )
if( stack_len==0 || pstack[stack_len-1].new_line )
{
if( current_white_char_mode() == WHITE_MODE_TREE )
{
@ -2119,14 +1948,19 @@ void HTMLParser::CheckClosingTags()
return;
}
// CHECK ME
if( RemoveIfNeeded(stack_len - 2) )
{
RemoveLastSpace(i);
}
for(int z=(int)stack_len-2 ; z >= i ; --z)
{
CallListener(z); // space from the item can be set as null here (when a should_remove() callback returned true)
CheckWhiteCharsExceptions(pstack[z]);
if( !skip_tags && IsTagSafe(LastItem().name) && !IsNameEqual(no_filter_tag, LastItem().name) )
{
if( pstack[z].new_line_after )
if( pstack[z].new_line )
{
if( current_white_char_mode() == WHITE_MODE_TREE )
{
@ -2190,34 +2024,26 @@ void HTMLParser::AddItemToSpace()
if( xml_compact_mode )
{
bool has_parent_object_name = false;
Space * space = parent->get_space(pstack[stack_len-1].name);
if( parent->is_object() )
if( space )
{
Space::ObjectType::iterator i = parent->value.value_object.find(pstack[stack_len-1].name);
if( i != parent->value.value_object.end() )
if( space->is_table() )
{
has_parent_object_name = true;
Space & child = space->add_empty_space();
pstack[stack_len-1].space = &child;
}
else
{
Space * tab = new Space();
tab->add(space);
Space & child = tab->add_empty_space();
if( i->second->is_table() )
{
Space & child = i->second->add_empty_space();
pstack[stack_len-1].space = &child;
}
else
{
Space * old_space = i->second;
i->second = new Space();
i->second->set_empty_table();
i->second->value.value_table.push_back(old_space);
Space & child = i->second->add_empty_space();
pstack[stack_len-1].space = &child;
}
parent->value.value_object[pstack[stack_len-1].name] = tab;
pstack[stack_len-1].space = &child;
}
}
if( !has_parent_object_name )
else
{
Space & space = parent->add_empty_space(pstack[stack_len-1].name);
pstack[stack_len-1].space = &space;
@ -2229,10 +2055,12 @@ void HTMLParser::AddItemToSpace()
Space & child = childs_tab.add_empty_space();
pstack[stack_len-1].space = &child;
}
}
}
void HTMLParser::RemoveLastSpace(size_t index)
{
if( out_space )
@ -2246,53 +2074,23 @@ void HTMLParser::RemoveLastSpace(size_t index)
if( xml_compact_mode )
{
if( parent->is_object() )
{
Space::ObjectType::iterator i = parent->value.value_object.find(pstack[index].name);
if( i != parent->value.value_object.end() )
{
if( i->second->is_table() )
{
size_t len = i->second->table_size();
if( len > 0 && i->second->value.value_table[len-1] == pstack[index].space )
{
i->second->remove(len - 1);
pstack[index].space = nullptr;
}
}
else
{
if( i->second == pstack[index].space )
{
parent->remove(i);
pstack[index].space = nullptr;
}
}
}
}
// IMPLEMENT ME
}
else
{
Space * childs_tab = parent->get_space(L"childs");
size_t len = childs_tab->table_size();
if( childs_tab && childs_tab->is_table() )
if( childs_tab && childs_tab->is_table() && len > 0 && childs_tab->value.value_table[len-1] == pstack[stack_len-2].space )
{
size_t len = childs_tab->table_size();
if( len > 0 && childs_tab->value.value_table[len-1] == pstack[index].space )
{
childs_tab->remove(len - 1);
pstack[index].space = nullptr;
}
childs_tab->remove(len - 1);
pstack[stack_len-2].space = nullptr;
}
}
}
}
void HTMLParser::AddTextSpaceToSpaceTree(const Space & space)
void HTMLParser::AddSpaceToSpaceTree(const Space & space)
{
const std::wstring * text = space.get_wstr(L"text");
@ -2300,32 +2098,23 @@ void HTMLParser::AddTextSpaceToSpaceTree(const Space & space)
{
if( xml_compact_mode )
{
bool has_space_text = false;
Space * child_text = LastItem().space->get_space(L"text");
if( LastItem().space->is_object() )
if( child_text )
{
Space::ObjectType::iterator i = LastItem().space->value.value_object.find(L"text");
if( i != LastItem().space->value.value_object.end() )
if( child_text->is_table() )
{
has_space_text = true;
if( i->second->is_table() )
{
i->second->add(*text);
}
else
{
Space * old_space = i->second;
i->second = new Space();
i->second->set_empty_table();
i->second->value.value_table.push_back(old_space);
i->second->add(*text);
}
child_text->add(*text);
}
else
{
Space * tab = new Space();
tab->add(*child_text);
tab->add(*text);
LastItem().space->value.value_object[L"text"] = tab;
}
}
if( !has_space_text )
else
{
LastItem().space->add(L"text", *text);
}
@ -2339,26 +2128,27 @@ void HTMLParser::AddTextSpaceToSpaceTree(const Space & space)
}
void HTMLParser::CallListener(size_t index)
{
if( listener )
{
listener->item_parsed(pstack[index]);
if( listener->should_remove(pstack[index]) )
bool HTMLParser::RemoveIfNeeded(size_t index)
{
if( item_parsed_listener )
{
if( !item_parsed_listener->item_parsed(pstack[index]) )
{
RemoveLastSpace(index);
return true;
}
}
return false;
}
void HTMLParser::ReadLoop()
{
while( status == ok && ReadItem() )
{
bool was_cdata = false;
if( LastItem().type == Item::opening )
{
if( parsing_html )
@ -2375,9 +2165,6 @@ void HTMLParser::ReadLoop()
if( LastItem().is_commentary )
ReadTextUntilClosingCommentary();
if( LastItem().is_cdata )
was_cdata = true;
PopStack();
}
else
@ -2385,7 +2172,8 @@ void HTMLParser::ReadLoop()
{
if( stack_len > 0 )
{
CallListener(stack_len - 1);
if( RemoveIfNeeded(stack_len - 1) )
RemoveLastSpace(stack_len - 1);
}
PopStack();
@ -2402,7 +2190,7 @@ void HTMLParser::ReadLoop()
if( status == ok )
{
ReadText(was_cdata);
ReadText();
}
is_first_item = false;
@ -2410,116 +2198,6 @@ void HTMLParser::ReadLoop()
}
void HTMLParser::read_char_from_entity_buffer()
{
if( escaped_char_index < escaped_chars_buffer.size() )
{
lastc = escaped_chars_buffer[escaped_char_index];
escaped_char_index += 1;
if( escaped_char_index >= escaped_chars_buffer.size() )
{
escaped_chars_buffer.clear();
escaped_char_index = 0;
}
}
else
{
lastc = -1;
}
}
void HTMLParser::read_xml_entity()
{
const size_t max_entity_length = 6; // length of "&apos;" string
escaped_chars_buffer.clear();
escaped_char_index = 0;
escaped_chars_buffer += '&';
do
{
read_char_no_escape();
if( lastc != -1 )
{
escaped_chars_buffer += lastc;
}
}
while( escaped_chars_buffer.size() < max_entity_length && lastc != -1 && lastc != ';' );
}
bool HTMLParser::check_escape_sequentions()
{
if( escaped_chars_buffer == L"&amp;" )
{
lastc = '&';
char_was_escaped = true;
}
else
if( escaped_chars_buffer == L"&lt;" )
{
lastc = '<';
char_was_escaped = true;
}
else
if( escaped_chars_buffer == L"&gt;" )
{
lastc = '>';
char_was_escaped = true;
}
else
if( escaped_chars_buffer == L"&quot;" )
{
lastc = '"';
char_was_escaped = true;
}
else
if( escaped_chars_buffer == L"&apos;" )
{
lastc = '\'';
char_was_escaped = true;
}
if( char_was_escaped )
{
escaped_chars_buffer.clear();
escaped_char_index = 0;
}
return char_was_escaped;
}
int HTMLParser::read_char()
{
char_was_escaped = false;
if( escaped_char_index < escaped_chars_buffer.size() )
{
read_char_from_entity_buffer();
}
else
{
read_char_no_escape();
if( !filter_mode && lastc == '&' )
{
read_xml_entity();
if( !check_escape_sequentions() )
{
read_char_from_entity_buffer();
}
}
}
return lastc;
}
void HTMLParser::Read()
{
@ -2534,7 +2212,7 @@ void HTMLParser::Read()
// it can be some text or white lines before the first html tag (we print it if using filtering)
// but they are not added to the Space tree
ReadText(false);
ReadText();
// reading the whole html source
ReadLoop();

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2008-2024, Tomasz Sowa
* Copyright (c) 2008-2021, 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.
*
* 1. 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.
*
* 2. 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_html_htmlparser
#define headerfile_pikotools_src_html_htmlparser
#ifndef headerfile_picotools_html_htmlfilter
#define headerfile_picotools_html_htmlfilter
#include <string>
#include <map>
@ -129,20 +132,14 @@ public:
bool is_commentary;
bool is_cdata;
// is a new line before this tag (or just a new line and some white characters)
bool new_line_before;
// is there a new line after this tag (or just some white characters and a new line)
bool new_line_after;
// is there a new line after this tag
bool new_line;
// is there a new line in the middle after this tag and before the next tag
// is there a new
bool new_line_in_the_middle;
// is there a white char (but not new line) before this tag
bool white_char_before;
// current orphans table
// (will be propagated)
Orphans * porphans;
@ -160,15 +157,14 @@ public:
};
class Listener
class ItemParsedListener
{
public:
Listener() {}
ItemParsedListener() {}
virtual void item_parsed(const Item & item) { }
virtual bool should_remove(const Item & item) { return false; }
virtual ~Listener() {}
virtual bool item_parsed(const Item & item) { return true; }
virtual ~ItemParsedListener() {}
};
@ -183,7 +179,7 @@ public:
HTMLParser & operator=(const HTMLParser & f);
virtual ~HTMLParser();
void set_item_parsed_listener(Listener * listener);
void set_item_parsed_listener(ItemParsedListener * listener);
void parse_html(const wchar_t * in, Space & space, bool compact_mode = false);
@ -193,14 +189,6 @@ public:
Status parse_xml_file(const wchar_t * file_name, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml_file(const std::wstring & file_name, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml(const char * str, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml(const std::string & str, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml(const wchar_t * str, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml(const std::wstring & str, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml(const pt::TextStream & str, Space & out_space, bool compact_mode = false, bool clear_space = true);
Status parse_xml(const pt::WTextStream & str, Space & out_space, bool compact_mode = false, bool clear_space = true);
// main methods used for filtering
@ -217,12 +205,12 @@ public:
/*
*
* returns a number of a last parsed line/column
* returns a number of a last parsed line
* can be used to obtain the line in which there was a syntax error
*
*/
int get_last_parsed_line();
int get_last_parsed_column();
@ -305,22 +293,10 @@ protected:
// html <nofilter> tag name
std::wstring no_filter_tag;
Listener * listener;
ItemParsedListener * item_parsed_listener;
/*
true if the lastc was escaped (with a backslash)
we have to know if the last sequence was \" or just "
*/
bool char_was_escaped;
std::wstring escaped_chars_buffer;
size_t escaped_char_index;
/*
* filter mode, a method filter(...) was called
* in filter mode we do not unescape xml sequences such as &lt; &gt; ...
*/
bool filter_mode;
void clear_input_flags();
@ -332,8 +308,6 @@ protected:
virtual void Init();
virtual void Uninit();
void prepare_to_parse_xml(Space & out_space, bool compact_mode, bool clear_space);
virtual bool IsOpeningTagMark(wchar_t c);
virtual bool IsClosingTagMark(wchar_t c);
virtual bool IsClosingTagIndicator(wchar_t c);
@ -412,7 +386,7 @@ protected:
void CheckStackPrintRest();
void AddForgottenTags();
void CheckClosingTags();
void ReadText(bool is_cdata);
void ReadText();
bool PrintRest();
bool PrintOpeningItem();
void ReadItemName(std::wstring & name, bool clear_name = true);
@ -435,7 +409,7 @@ protected:
void CheckChar(wchar_t c);
bool PutNormalNonWhite(std::wstring & str, bool allow_put_new_line, bool allow_put_space, bool is_cdata);
void PutNormalNonWhite(std::wstring & str, bool allow_put_new_line, bool allow_put_space);
void PutNormalWhite(bool & was_white_char, bool & was_new_line, std::wstring * result_text = nullptr);
void PutTabs(size_t len);
@ -444,14 +418,9 @@ protected:
void AddItemToSpace();
void RemoveLastSpace(size_t index);
void AddTextSpaceToSpaceTree(const Space & space);
void AddSpaceToSpaceTree(const Space & space);
void CallListener(size_t index);
bool check_escape_sequentions();
void read_xml_entity();
void read_char_from_entity_buffer();
int read_char() override;
bool RemoveIfNeeded(size_t index);
Item empty;
Item * pstack; // stack pointer
@ -466,7 +435,6 @@ protected:
//bool last_new_line;
bool new_item_has_new_line_before;
bool new_item_has_white_char_before;
int white_mode;
bool is_first_item;
size_t wrap_line; // insert a new line character into long lines

View File

@ -1,11 +1,11 @@
/*
* This file is a part of PikoTools
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -58,6 +58,17 @@ FileLog::~FileLog()
}
bool FileLog::synchro_lock()
{
return true;
}
void FileLog::synchro_unlock()
{
}
void FileLog::init(const std::wstring & log_file, bool log_stdout, int log_level, bool save_each_line)
{
this->log_stdout = log_stdout;
@ -94,28 +105,38 @@ void FileLog::save_log(WTextStream * buffer)
if( buffer->empty() )
return;
if( log_stdout )
if( synchro_lock() )
{
wide_stream_to_utf8(*buffer, std::cout);
std::cout.flush();
}
if( !log_file.empty() )
{
if( !log_file_open || !file )
try
{
file.close();
file.clear();
if( log_stdout )
{
wide_stream_to_utf8(*buffer, std::cout);
}
open_file();
if( !log_file.empty() )
{
if( !log_file_open || !file )
{
file.close();
file.clear();
open_file();
}
if( file )
{
wide_stream_to_utf8(*buffer, file);
file.flush();
}
}
}
if( file )
catch(...)
{
wide_stream_to_utf8(*buffer, file);
file.flush();
}
}
synchro_unlock();
}

View File

@ -1,11 +1,11 @@
/*
* This file is a part of PikoTools
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,8 +32,8 @@
*
*/
#ifndef headerfile_pikotools_src_log_filelog
#define headerfile_pikotools_src_log_filelog
#ifndef headerfile_picotools_log_filelog
#define headerfile_picotools_log_filelog
#include <sstream>
#include <fstream>
@ -79,6 +79,9 @@ protected:
// whether to save each line (for debug)
bool save_each_line;
virtual bool synchro_lock();
virtual void synchro_unlock();
virtual void open_file();
};

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctime>
@ -104,7 +107,7 @@ size_t Log::size() const
{
if( buffer )
{
return buffer->size();
buffer->size();
}
return 0;
@ -124,7 +127,7 @@ size_t Log::capacity() const
{
if( buffer )
{
return buffer->capacity();
buffer->capacity();
}
return 0;
@ -681,51 +684,5 @@ void Log::save_log()
}
Log & Log::put_multiline(const char * prefix, const char * msg)
{
put_multiline_generic(prefix, msg);
return *this;
}
Log & Log::put_multiline(const wchar_t * prefix, const wchar_t * msg)
{
put_multiline_generic(prefix, msg);
return *this;
}
Log & Log::put_multiline(const char * prefix, const std::string & msg)
{
put_multiline_generic(prefix, msg.c_str());
return *this;
}
Log & Log::put_multiline(const wchar_t * prefix, const std::wstring & msg)
{
put_multiline_generic(prefix, msg.c_str());
return *this;
}
Log & Log::put_multiline(const std::string & prefix, const std::string & msg)
{
put_multiline_generic(prefix.c_str(), msg.c_str());
return *this;
}
Log & Log::put_multiline(const std::wstring & prefix, const std::wstring & msg)
{
put_multiline_generic(prefix.c_str(), msg.c_str());
return *this;
}
} // namespace

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_log_log
#define headerfile_pikotools_src_log_log
#ifndef headerfile_picotools_log_log
#define headerfile_picotools_log_log
#include <string>
#include <fstream>
@ -153,17 +156,6 @@ public:
virtual Log & put_binary_blob(const char * blob, size_t blob_len);
virtual Log & put_binary_blob(const std::string & blob);
/*
* put multiline message
* first and last new characters are trimmed
* at the beginning of each line a prefix is inserted
*/
virtual Log & put_multiline(const char * prefix, const char * msg);
virtual Log & put_multiline(const wchar_t * prefix, const wchar_t * msg);
virtual Log & put_multiline(const char * prefix, const std::string & msg);
virtual Log & put_multiline(const wchar_t * prefix, const std::wstring & msg);
virtual Log & put_multiline(const std::string & prefix, const std::string & msg);
virtual Log & put_multiline(const std::wstring & prefix, const std::wstring & msg);
protected:
@ -192,10 +184,6 @@ protected:
virtual void save_log();
virtual void save_log_and_clear();
template<typename CharType>
void put_multiline_generic(const CharType * prefix, const CharType * msg);
};
@ -233,61 +221,11 @@ Log & Log::log_string_generic(const StringType & value, size_t max_size)
template<typename CharType>
void Log::put_multiline_generic(const CharType * prefix, const CharType * msg)
{
bool put_prefix = true;
bool was_new_line = false;
bool was_something_printed = false;
while( *msg )
{
if( static_cast<CharType>(*msg) == static_cast<CharType>('\n') )
{
was_new_line = true;
put_prefix = true;
}
else
{
if( was_new_line )
{
if( was_something_printed )
{
operator<<(logend);
}
was_new_line = false;
}
if( put_prefix )
{
operator<<(prefix);
put_prefix = false;
}
operator<<(*msg);
was_something_printed = true;
}
msg += 1;
}
if( was_something_printed )
{
operator<<(logend);
}
}
} // namespace
#endif

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -10,26 +10,29 @@
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -10,30 +10,33 @@
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_mainoptions_mainoptionsparser
#define headerfile_pikotools_src_mainoptions_mainoptionsparser
#ifndef headerfile_picotools_mainoptions_mainoptionsparser
#define headerfile_picotools_mainoptions_mainoptionsparser
#include "space/space.h"
#include <string>

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -10,30 +10,33 @@
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_membuffer_membuffer
#define headerfile_pikotools_src_membuffer_membuffer
#ifndef headerfile_picotools_membuffer_membuffer
#define headerfile_picotools_membuffer_membuffer
#include <iostream>

View File

@ -1,44 +0,0 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2022, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 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 HOLDER 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_pikotools_src_pikotools_version
#define headerfile_pikotools_src_pikotools_version
#define PIKOTOOLS_VERSION_MAJOR 0
#define PIKOTOOLS_VERSION_MINOR 7
#define PIKOTOOLS_VERSION_PATCH 0
#endif

View File

@ -1,277 +0,0 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2023, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 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 HOLDER 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 <cstdlib>
#include <wchar.h>
#include "keyvalueparser.h"
#include "utf8/utf8.h"
#include "convert/strtoint.h"
namespace pt
{
KeyValueParser::KeyValueParser()
{
root_space = nullptr;
separator = '=';
option_delimiter = ',';
input_as_utf8 = true;
}
void KeyValueParser::use_utf8(bool utf)
{
input_as_utf8 = utf;
}
void KeyValueParser::set_separator(wchar_t separator)
{
this->separator = separator;
}
void KeyValueParser::set_option_delimiter(wchar_t option_delimiter)
{
this->option_delimiter = option_delimiter;
}
void KeyValueParser::prepare_to_parsing()
{
clear_input_flags();
}
KeyValueParser::Status KeyValueParser::parse_file(const char * file_name, Space & out_space, bool clear_space)
{
prepare_to_parsing();
reading_from_file = true;
root_space = &out_space;
file.open(file_name, std::ios_base::binary | std::ios_base::in);
if( file )
{
parse(clear_space);
file.close();
}
return status;
}
KeyValueParser::Status KeyValueParser::parse_file(const std::string & file_name, Space & out_space, bool clear_space)
{
return parse_file(file_name.c_str(), out_space, clear_space);
}
KeyValueParser::Status KeyValueParser::parse_file(const wchar_t * file_name, Space & out_space, bool clear_space)
{
std::string file_name_utf8;
wide_to_utf8(file_name, file_name_utf8);
return parse_file(file_name_utf8.c_str(), out_space, clear_space);
}
KeyValueParser::Status KeyValueParser::parse_file(const std::wstring & file_name, Space & out_space, bool clear_space)
{
return parse_file(file_name.c_str(), out_space, clear_space);
}
KeyValueParser::Status KeyValueParser::parse(const char * str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pchar_ascii = str;
root_space = &out_space;
parse(clear_space);
return status;
}
KeyValueParser::Status KeyValueParser::parse(const std::string & str, Space & out_space, bool clear_space)
{
return parse(str.c_str(), out_space, clear_space);
}
KeyValueParser::Status KeyValueParser::parse(const wchar_t * str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pchar_unicode = str;
root_space = &out_space;
parse(clear_space);
return status;
}
KeyValueParser::Status KeyValueParser::parse(const std::wstring & str, Space & out_space, bool clear_space)
{
return parse(str.c_str(), out_space, clear_space);
}
KeyValueParser::Status KeyValueParser::parse(const pt::TextStream & str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pt::TextStream::const_iterator start = str.begin();
pt::TextStream::const_iterator end = str.end();
text_stream_iterator = &start;
text_stream_iterator_end = &end;
root_space = &out_space;
parse(clear_space);
return status;
}
KeyValueParser::Status KeyValueParser::parse(const pt::WTextStream & str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pt::WTextStream::const_iterator start = str.begin();
pt::WTextStream::const_iterator end = str.end();
wtext_stream_iterator = &start;
wtext_stream_iterator_end = &end;
root_space = &out_space;
parse(clear_space);
return status;
}
void KeyValueParser::parse(bool clear_root_space)
{
line = 1;
status = ok;
if( clear_root_space )
{
root_space->set_empty_object();
}
read_char(); // put first character to lastc
while( lastc != -1 )
{
key.clear();
value.clear();
wchar_t delimit = read_token(key, separator, option_delimiter);
if( delimit == separator )
{
read_token(value, option_delimiter);
}
if( !key.empty() || delimit == separator )
{
root_space->add(key, value);
}
}
key.clear();
value.clear();
}
wchar_t pt::KeyValueParser::read_token(std::wstring & token, wchar_t delimit1, wchar_t delimit2)
{
token.clear();
skip_white();
wchar_t delimit = -1;
while( lastc != -1 )
{
if( delimit1 != -1 && lastc == delimit1 )
{
delimit = delimit1;
read_char();
break;
}
if( delimit2 != -1 && lastc == delimit2 )
{
delimit = delimit2;
read_char();
break;
}
token += lastc;
read_char();
}
pt::trim_white(token, false, false);
return delimit;
}
void pt::KeyValueParser::skip_white()
{
while( pt::is_white(lastc, true, true) )
{
read_char();
}
}
int pt::KeyValueParser::read_char()
{
return read_char_no_escape();
}
} // namespace

View File

@ -1,173 +0,0 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2023, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 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 HOLDER 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_pikotools_src_space_keyvalueparser
#define headerfile_pikotools_src_space_keyvalueparser
#include "space.h"
#include "convert/baseparser.h"
namespace pt
{
class KeyValueParser : public BaseParser
{
public:
/*
* ctor -- sets default values
*/
KeyValueParser();
/*
* status of parsing
* ok - input stream has been parsed correctly
* cant_open_file - I cannot open the file (returns only in a case when parsing a file)
*
*/
enum Status {
ok,
cant_open_file,
};
/*
* the last status of parsing, set by parse() methods
*/
Status status;
/*
* if true then the input file or string (char* or std::string) is treated as UTF-8
* default true
*
* the internal storage for strings is std::wstring so if you call set_utf8(false) then
* the characters of input string will be simple static_cast<> from char to wchar_t
*
*/
void use_utf8(bool utf);
/*
* set a separator between a variable and a value
* default: '='
*/
void set_separator(wchar_t separator);
/*
* set an option delimiter
* default: ','
*/
void set_option_delimiter(wchar_t option_delimiter);
/*
*
*/
Status parse_file(const char * file_name, Space & out_space, bool clear_space = true);
Status parse_file(const std::string & file_name, Space & out_space, bool clear_space = true);
Status parse_file(const wchar_t * file_name, Space & out_space, bool clear_space = true);
Status parse_file(const std::wstring & file_name, Space & out_space, bool clear_space = true);
/*
*
*/
Status parse(const char * str, Space & out_space, bool clear_space = true);
Status parse(const std::string & str, Space & out_space, bool clear_space = true);
Status parse(const wchar_t * str, Space & out_space, bool clear_space = true);
Status parse(const std::wstring & str, Space & out_space, bool clear_space = true);
Status parse(const pt::TextStream & str, Space & out_space, bool clear_space = true);
Status parse(const pt::WTextStream & str, Space & out_space, bool clear_space = true);
private:
/*
* current output space
*
*/
Space * root_space;
/*
* separator between a variable and a value, default: '='
*/
wchar_t separator;
/*
* option delimiter, default: ','
*/
wchar_t option_delimiter;
/*
*
*/
std::wstring key;
/*
*
*/
std::wstring value;
void prepare_to_parsing();
void parse(bool clear_root_space);
wchar_t read_token(std::wstring & token, wchar_t delimit1, wchar_t delimit2 = -1);
void skip_white();
int read_char();
};
} // namespace
#endif

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2008-2024, Tomasz Sowa
* Copyright (c) 2008-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <wchar.h>
@ -161,24 +164,12 @@ Space::Space(const char * str)
set(str);
}
Space::Space(const char * str, size_t len)
{
initialize();
set(str, len);
}
Space::Space(const wchar_t * str)
{
initialize();
set(str);
}
Space::Space(const wchar_t * str, size_t len)
{
initialize();
set(str, len);
}
Space::Space(const std::string & str)
{
initialize();
@ -395,19 +386,6 @@ void Space::set(const char * str)
}
}
void Space::set(const char * str, size_t len)
{
if( str == nullptr )
{
initialize_value_null_if_needed();
}
else
{
initialize_value_string_if_needed();
value.value_string.assign(str, len);
}
}
void Space::set(const wchar_t * str)
{
if( str == nullptr )
@ -421,19 +399,6 @@ void Space::set(const wchar_t * str)
}
}
void Space::set(const wchar_t * str, size_t len)
{
if( str == nullptr )
{
initialize_value_null_if_needed();
}
else
{
initialize_value_wstring_if_needed();
value.value_wstring.assign(str, len);
}
}
void Space::set(const std::string & str)
{
initialize_value_string_if_needed();
@ -446,20 +411,6 @@ void Space::set(const std::wstring & str)
value.value_wstring = str;
}
void Space::set(const Stream & stream)
{
if( stream.is_char_stream() )
{
initialize_value_string_if_needed();
stream.to_str(value.value_string);
}
else
{
initialize_value_wstring_if_needed();
stream.to_str(value.value_wstring);
}
}
void Space::set(const Space & space)
{
copy_from(space);
@ -556,21 +507,11 @@ Space & Space::add(const char * val)
return add_generic(val);
}
Space & Space::add_to_table(const char * val, size_t len)
{
return add_generic_len(val, len);
}
Space & Space::add(const wchar_t * val)
{
return add_generic(val);
}
Space & Space::add_to_table(const wchar_t * val, size_t len)
{
return add_generic_len(val, len);
}
Space & Space::add(const std::string & val)
{
return add_generic(val);
@ -581,24 +522,6 @@ Space & Space::add(const std::wstring & val)
return add_generic(val);
}
Space & Space::add(const Stream & stream)
{
if( stream.is_char_stream() )
{
pt::Space & new_item = add_empty_space();
new_item.set_empty_string();
stream.to_str(new_item.value.value_string);
return new_item;
}
else
{
pt::Space & new_item = add_empty_space();
new_item.set_empty_wstring();
stream.to_str(new_item.value.value_wstring);
return new_item;
}
}
Space & Space::add(const Space & space)
{
return add_generic(space);
@ -697,21 +620,11 @@ Space & Space::add(const wchar_t * field, const char * val)
return add_generic(field, val);
}
Space & Space::add(const wchar_t * field, const char * val, size_t len)
{
return add_generic_len(field, val, len);
}
Space & Space::add(const wchar_t * field, const wchar_t * val)
{
return add_generic(field, val);
}
Space & Space::add(const wchar_t * field, const wchar_t * val, size_t len)
{
return add_generic_len(field, val, len);
}
Space & Space::add(const wchar_t * field, const std::string & val)
{
return add_generic(field, val);
@ -722,24 +635,6 @@ Space & Space::add(const wchar_t * field, const std::wstring & val)
return add_generic(field, val);
}
Space & Space::add(const wchar_t * field, const Stream & stream)
{
if( stream.is_char_stream() )
{
pt::Space & new_item = add_empty_space(field);
new_item.set_empty_string();
stream.to_str(new_item.value.value_string);
return new_item;
}
else
{
pt::Space & new_item = add_empty_space(field);
new_item.set_empty_wstring();
stream.to_str(new_item.value.value_wstring);
return new_item;
}
}
Space & Space::add(const wchar_t * field, const Space & space)
{
return add_generic(field, space);
@ -784,107 +679,92 @@ Space & Space::add_empty_space(const wchar_t * field)
Space & Space::add(const std::wstring & field, bool val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, short val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, int val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, long val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, long long val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, unsigned short val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, unsigned int val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, unsigned long val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, unsigned long long val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, float val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, double val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, long double val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const char * val)
{
return add_generic_string(field, val);
}
Space & Space::add(const std::wstring & field, const char * val, size_t len)
{
return add_generic_string_len(field, val, len);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const wchar_t * val)
{
return add_generic_string(field, val);
}
Space & Space::add(const std::wstring & field, const wchar_t * val, size_t len)
{
return add_generic_string_len(field, val, len);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const std::string & val)
{
return add_generic_string(field, val);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const std::wstring & val)
{
return add_generic_string(field, val);
}
Space & Space::add(const std::wstring & field, const Stream & stream)
{
return add(field.c_str(), stream);
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const Space & space)
{
return add_generic_string(field, space);
return add_generic(field, space);
}
Space & Space::add(const std::wstring & field, const Space * space)
{
return add_generic_string(field, space);
return add_generic(field, space);
}
Space & Space::add(const std::wstring & field, Space && space)
@ -894,12 +774,12 @@ Space & Space::add(const std::wstring & field, Space && space)
Space & Space::add(const std::wstring & field, const Date & date)
{
return add_generic_string(field, date);
return add_generic(field, date);
}
Space & Space::add_empty_space(const std::wstring & field)
{
return add_generic_string(field, static_cast<Space*>(nullptr));
return add_generic(field, static_cast<Space*>(nullptr));
}
@ -1072,7 +952,7 @@ long long Space::convert_string_to_long_long() const
{
bool was_overflow = false;
const char * after_str;
long long val = to_ll(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
long long val = Toll(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1082,7 +962,7 @@ long long Space::convert_wstring_to_long_long() const
{
bool was_overflow = false;
const wchar_t * after_str;
long long val = to_ll(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
long long val = Toll(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1092,7 +972,7 @@ unsigned long long Space::convert_string_to_ulong_long() const
{
bool was_overflow = false;
const char * after_str;
unsigned long long val = to_ull(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
unsigned long long val = Toull(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1102,7 +982,7 @@ unsigned long long Space::convert_wstring_to_ulong_long() const
{
bool was_overflow = false;
const wchar_t * after_str;
unsigned long long val = to_ull(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
unsigned long long val = Toull(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1305,19 +1185,6 @@ std::wstring Space::to_wstr() const
}
Date Space::to_date() const
{
if( type == type_wstring )
return Date(value.value_wstring);
if( type == type_string )
return Date(value.value_string);
if( type == type_long )
return Date(static_cast<time_t>(value.value_long));
return Date();
}
void Space::to_list(std::list<std::string> & output_list, bool clear_list) const
@ -1413,27 +1280,6 @@ unsigned long long Space::to_ulong_long(const wchar_t * field, unsigned long lon
return space ? space->to_ulong_long() : default_value;
}
float Space::to_float(const wchar_t * field, float default_value) const
{
const Space * space = get_space(field);
return space ? space->to_float() : default_value;
}
double Space::to_double(const wchar_t * field, double default_value) const
{
const Space * space = get_space(field);
return space ? space->to_double() : default_value;
}
long double Space::to_long_double(const wchar_t * field, long double default_value) const
{
const Space * space = get_space(field);
return space ? space->to_long_double() : default_value;
}
std::string Space::to_str(const wchar_t * field, const char * default_value) const
{
const Space * space = get_space(field);
@ -1484,20 +1330,6 @@ std::wstring Space::to_wstr(const wchar_t * field, const std::wstring & default_
}
Date Space::to_date(const wchar_t * field) const
{
const Space * space = get_space(field);
return space ? space->to_date() : Date();
}
Date Space::to_date(const wchar_t * field, const Date & default_value) const
{
const Space * space = get_space(field);
return space ? space->to_date() : default_value;
}
bool Space::to_list(const wchar_t * field, std::list<std::string> & output_list, bool clear_list) const
{
return to_list_generic(field, output_list, clear_list);
@ -2153,7 +1985,7 @@ Space * Space::get_space_nc(const wchar_t * field)
{
ObjectType::iterator i = value.value_object.begin();
for( ; i != value.value_object.end() ; ++i )
while( i != value.value_object.end() )
{
if( is_equal_nc(field, i->first.c_str()) )
{
@ -2178,7 +2010,7 @@ const Space * Space::get_space_nc(const wchar_t * field) const
{
ObjectType::const_iterator i = value.value_object.cbegin();
for( ; i != value.value_object.cend() ; ++i )
while( i != value.value_object.cend() )
{
if( is_equal_nc(field, i->first.c_str()) )
{
@ -2223,21 +2055,6 @@ void Space::remove(const std::wstring & field)
}
void Space::remove(ObjectType::iterator & iterator)
{
if( type == type_object )
{
if( iterator != value.value_object.end() )
{
delete iterator->second;
iterator->second = nullptr;
value.value_object.erase(iterator);
}
}
}
void Space::remove(size_t table_index)
{
if( type == type_table && table_index < value.value_table.size() )
@ -2760,18 +2577,6 @@ void Space::remove_value_table(bool only_clear)
}
Space * Space::get_table_item(size_t index)
{
if( is_table() && index < table_size() )
{
return value.value_table[index];
}
return nullptr;
}
} // namespace

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2010-2024, Tomasz Sowa
* Copyright (c) 2010-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_space_space
#define headerfile_pikotools_src_space_space
#ifndef headerfile_picotools_space_space
#define headerfile_picotools_space_space
#include <string>
#include <list>
@ -200,9 +203,7 @@ public:
Space(double val);
Space(long double val);
Space(const char * str);
Space(const char * str, size_t len);
Space(const wchar_t * str);
Space(const wchar_t * str, size_t len);
Space(const std::string & str);
Space(const std::wstring & str);
Space(const Space * space);
@ -237,12 +238,9 @@ public:
void set(double val);
void set(long double val);
void set(const char * str);
void set(const char * str, size_t len);
void set(const wchar_t * str);
void set(const wchar_t * str, size_t len);
void set(const std::string & str);
void set(const std::wstring & str);
void set(const Stream & stream);
void set(const Space & space);
void set(const Space * space);
void set(Space && space);
@ -263,12 +261,9 @@ public:
Space & add(double val);
Space & add(long double val);
Space & add(const char * val);
Space & add_to_table(const char * val, size_t len); // the name add would collapse with add(const char * field, unsigned long val) below;
Space & add(const wchar_t * val);
Space & add_to_table(const wchar_t * val, size_t len); // the name add would collapse with add(const wchar_t * field, unsigned long val) below;
Space & add(const std::string & val);
Space & add(const std::wstring & val);
Space & add(const Stream & stream);
Space & add(const Space & space);
Space & add(const Space * space);
Space & add(Space && space);
@ -291,12 +286,9 @@ public:
Space & add(const wchar_t * field, double val);
Space & add(const wchar_t * field, long double val);
Space & add(const wchar_t * field, const char * val);
Space & add(const wchar_t * field, const char * val, size_t len);
Space & add(const wchar_t * field, const wchar_t * val);
Space & add(const wchar_t * field, const wchar_t * val, size_t len);
Space & add(const wchar_t * field, const std::string & val);
Space & add(const wchar_t * field, const std::wstring & val);
Space & add(const wchar_t * field, const Stream & stream);
Space & add(const wchar_t * field, const Space & space);
Space & add(const wchar_t * field, const Space * space);
Space & add(const wchar_t * field, Space && space);
@ -317,11 +309,8 @@ public:
Space & add(const std::wstring & field, long double val);
Space & add(const std::wstring & field, const char * val);
Space & add(const std::wstring & field, const wchar_t * val);
Space & add(const std::wstring & field, const char * val, size_t len);
Space & add(const std::wstring & field, const wchar_t * val, size_t len);
Space & add(const std::wstring & field, const std::string & val);
Space & add(const std::wstring & field, const std::wstring & val);
Space & add(const std::wstring & field, const Stream & stream);
Space & add(const std::wstring & field, const Space & space);
Space & add(const std::wstring & field, const Space * space);
Space & add(const std::wstring & field, Space && space);
@ -382,7 +371,6 @@ public:
long double to_long_double() const;
std::string to_str() const;
std::wstring to_wstr() const;
Date to_date() const;
void to_list(std::list<std::string> & output_list, bool clear_list = true) const;
void to_list(std::list<std::wstring> & output_list, bool clear_list = true) const;
@ -402,18 +390,11 @@ public:
unsigned long to_ulong(const wchar_t * field, unsigned long default_value = 0) const;
unsigned long long to_ullong(const wchar_t * field, unsigned long long default_value = 0) const;
unsigned long long to_ulong_long(const wchar_t * field, unsigned long long default_value = 0) const;
float to_float(const wchar_t * field, float default_value = 0.0f) const;
double to_double(const wchar_t * field, double default_value = 0.0) const;
long double to_long_double(const wchar_t * field, long double default_value = 0.0L) const;
std::string to_str(const wchar_t * field, const char * default_value = nullptr) const;
std::string to_str(const wchar_t * field, const std::string & default_value) const;
std::wstring to_wstr(const wchar_t * field, const wchar_t * default_value = nullptr) const;
std::wstring to_wstr(const wchar_t * field, const std::wstring & default_value) const;
Date to_date(const wchar_t * field) const;
Date to_date(const wchar_t * field, const Date & default_value) const;
bool to_list(const wchar_t * field, std::list<std::string> & output_list, bool clear_list = true) const;
bool to_list(const wchar_t * field, std::list<std::wstring> & output_list, bool clear_list = true) const;
bool to_list(const std::wstring & field, std::list<std::string> & output_list, bool clear_list = true) const;
@ -538,7 +519,6 @@ public:
// remove a field from an object
void remove(const wchar_t * field);
void remove(const std::wstring & field);
void remove(ObjectType::iterator & iterator);
// remove a table item
void remove(size_t table_index);
@ -655,7 +635,7 @@ public:
bool has_value(const wchar_t * field, const std::wstring & val) const;
Space * get_table_item(size_t index);
protected:
@ -672,16 +652,6 @@ protected:
return *value.value_table.back();
}
template<typename ArgType>
Space & add_generic_len(const ArgType & val, size_t len)
{
initialize_value_table_if_needed();
Space * new_space = new Space(val, len);
value.value_table.push_back(new_space);
return *value.value_table.back();
}
template<typename ArgType>
Space & add_generic(const wchar_t * field, const ArgType & val)
@ -701,35 +671,13 @@ protected:
return *(insert_res.first->second);
}
template<typename ArgType>
Space & add_generic_len(const wchar_t * field, const ArgType & val, size_t len)
{
initialize_value_object_if_needed();
auto insert_res = value.value_object.insert(std::make_pair(field, nullptr));
if( insert_res.second )
{
insert_res.first->second = new Space(val, len);
}
else
{
insert_res.first->second->set(val, len);
}
return *(insert_res.first->second);
}
template<typename ArgType>
Space & add_generic_string(const std::wstring & field, const ArgType & val)
Space & add_generic(const std::wstring & field, const ArgType & val)
{
return add_generic(field.c_str(), val);
}
template<typename ArgType>
Space & add_generic_string_len(const std::wstring & field, const ArgType & val, size_t len)
{
return add_generic_len(field.c_str(), val, len);
}
template<typename ArgType>
ArgType to_generic_numeric_signed_value() const

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2023, Tomasz Sowa
* Copyright (c) 2012-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdlib>
@ -52,10 +55,6 @@ SpaceParser::SpaceParser()
space_end = '}';
option_delimiter = ',';
input_as_utf8 = true;
object_items_limit = 0;
table_items_limit = 0;
all_items_limit = 0;
nested_levels_limit = 0;
}
@ -72,73 +71,10 @@ int SpaceParser::get_last_parsed_line()
}
int SpaceParser::get_last_parsed_column()
{
return column;
}
void SpaceParser::set_object_items_limit(size_t val)
{
this->object_items_limit = val;
}
void SpaceParser::set_table_items_limit(size_t val)
{
this->table_items_limit = val;
}
void SpaceParser::set_all_items_limit(size_t val)
{
this->all_items_limit = val;
}
void SpaceParser::set_nested_level_limit(size_t val)
{
this->nested_levels_limit = val;
}
size_t SpaceParser::get_object_items_limit()
{
return object_items_limit;
}
size_t SpaceParser::get_table_items_limit()
{
return table_items_limit;
}
size_t SpaceParser::get_all_items_limit()
{
return all_items_limit;
}
size_t SpaceParser::get_nested_level_limit()
{
return nested_levels_limit;
}
void SpaceParser::prepare_to_parsing()
{
clear_input_flags();
current_items_counter = 0;
current_nested_level = 0;
}
SpaceParser::Status SpaceParser::parse_json_file(const char * file_name, Space & out_space, bool clear_space)
{
prepare_to_parsing();
clear_input_flags();
reading_from_file = true;
parsing_space = false;
@ -190,7 +126,7 @@ SpaceParser::Status SpaceParser::parse_json_file(const std::wstring & file_name,
SpaceParser::Status SpaceParser::parse_space_file(const char * file_name, Space & out_space, bool clear_space)
{
prepare_to_parsing();
clear_input_flags();
reading_from_file = true;
parsing_space = true;
@ -240,7 +176,7 @@ SpaceParser::Status SpaceParser::parse_space_file(const std::wstring & file_name
SpaceParser::Status SpaceParser::parse_json(const char * str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
clear_input_flags();
pchar_ascii = str;
parsing_space = false;
@ -260,7 +196,7 @@ SpaceParser::Status SpaceParser::parse_json(const std::string & str, Space & out
SpaceParser::Status SpaceParser::parse_json(const wchar_t * str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
clear_input_flags();
pchar_unicode = str;
parsing_space = false;
@ -279,48 +215,11 @@ SpaceParser::Status SpaceParser::parse_json(const std::wstring & str, Space & ou
SpaceParser::Status SpaceParser::parse_json(const pt::TextStream & str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pt::TextStream::const_iterator start = str.begin();
pt::TextStream::const_iterator end = str.end();
text_stream_iterator = &start;
text_stream_iterator_end = &end;
parsing_space = false;
root_space = &out_space;
parse_root_space(clear_space);
return status;
}
SpaceParser::Status SpaceParser::parse_json(const pt::WTextStream & str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pt::WTextStream::const_iterator start = str.begin();
pt::WTextStream::const_iterator end = str.end();
wtext_stream_iterator = &start;
wtext_stream_iterator_end = &end;
parsing_space = false;
root_space = &out_space;
parse_root_space(clear_space);
return status;
}
SpaceParser::Status SpaceParser::parse_space(const char * str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
clear_input_flags();
pchar_ascii = str;
parsing_space = true;
@ -340,7 +239,7 @@ SpaceParser::Status SpaceParser::parse_space(const std::string & str, Space & ou
SpaceParser::Status SpaceParser::parse_space(const wchar_t * str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
clear_input_flags();
pchar_unicode = str;
parsing_space = true;
@ -358,41 +257,6 @@ SpaceParser::Status SpaceParser::parse_space(const std::wstring & str, Space & o
}
SpaceParser::Status SpaceParser::parse_space(const pt::TextStream & str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pt::TextStream::const_iterator start = str.begin();
pt::TextStream::const_iterator end = str.end();
text_stream_iterator = &start;
text_stream_iterator_end = &end;
parsing_space = true;
root_space = &out_space;
parse_root_space(clear_space);
return status;
}
SpaceParser::Status SpaceParser::parse_space(const pt::WTextStream & str, Space & out_space, bool clear_space)
{
prepare_to_parsing();
pt::WTextStream::const_iterator start = str.begin();
pt::WTextStream::const_iterator end = str.end();
wtext_stream_iterator = &start;
wtext_stream_iterator_end = &end;
parsing_space = true;
root_space = &out_space;
parse_root_space(clear_space);
return status;
}
@ -423,13 +287,10 @@ void SpaceParser::parse_root_space(bool clear_root_space)
parse(root_space, false, false);
}
if( status == ok )
{
skip_white();
skip_white();
if( lastc != -1 )
status = syntax_error;
}
if( lastc != -1 )
status = syntax_error;
token.clear();
}
@ -501,45 +362,32 @@ void SpaceParser::parse(Space * space, bool is_object_value, bool is_table_value
void SpaceParser::parse_space(Space * space)
{
if( nested_levels_limit == 0 || current_nested_level++ < nested_levels_limit )
/*
* in Space format in global namespace the space start character is not required
*/
bool need_space_start_character = !parsing_space || space != root_space;
if( need_space_start_character )
{
/*
* in Space format in global namespace the space start character is not required
*/
bool need_space_start_character = !parsing_space || space != root_space;
if( need_space_start_character )
{
read_char(); // inserting a next character after the space_start char to lastc
}
if( !space->is_object() )
space->set_empty_object();
parse_key_value_pairs(space);
if( status == ok )
{
if( need_space_start_character )
{
if( lastc == space_end )
{
read_char();
}
else
{
status = syntax_error;
}
}
}
}
else
{
status = limit_nested_level_exceeded;
read_char(); // inserting a next character after the space_start char to lastc
}
if( current_nested_level > 0 )
current_nested_level -= 1;
if( !space->is_object() )
space->set_empty_object();
parse_key_value_pairs(space);
if( need_space_start_character )
{
if( lastc == space_end )
{
read_char();
}
else
{
status = syntax_error;
}
}
}
@ -572,7 +420,7 @@ void SpaceParser::parse_integer_value(Space * space)
base = 8;
}
long long val = to_ll(token.c_str(), base, &after_str, &was_overflow, false);
long long val = Toll(token.c_str(), base, &after_str, &was_overflow, false);
if( was_overflow )
{
@ -615,31 +463,18 @@ void SpaceParser::parse_floating_point_value(Space * space)
void SpaceParser::parse_table(Space * space)
{
if( nested_levels_limit == 0 || current_nested_level++ < nested_levels_limit )
{
read_char(); // inserting a next character after the table_start char to lastc
space->set_empty_table();
parse_values_list(space);
read_char(); // inserting a next character after the table_start char to lastc
space->set_empty_table();
parse_values_list(space);
if( status == ok )
{
if( lastc == table_end )
{
read_char();
}
else
{
status = syntax_error;
}
}
if( lastc == table_end )
{
read_char();
}
else
{
status = limit_nested_level_exceeded;
status = syntax_error;
}
if( current_nested_level > 0 )
current_nested_level -= 1;
}
@ -689,23 +524,8 @@ void SpaceParser::parse_key_value_pairs(Space * space)
{
read_char(); // inserting a next character after the separator to lastc
if( object_items_limit == 0 || !space->is_object() || (space->object_size() < object_items_limit) )
{
Space & new_space = space->add_empty_space(token.c_str());
if( all_items_limit == 0 || current_items_counter++ < all_items_limit )
{
parse(&new_space, true, false);
}
else
{
status = limit_all_items_exceeded;
}
}
else
{
status = limit_object_items_exceeded;
}
Space & new_space = space->add(token.c_str(), new Space());
parse(&new_space, true, false);
}
else
{
@ -715,11 +535,7 @@ void SpaceParser::parse_key_value_pairs(Space * space)
}
is_first = false;
if( status == ok )
{
skip_white();
}
skip_white();
}
}
@ -760,31 +576,12 @@ void SpaceParser::parse_values_list(Space * space)
if( status == ok )
{
if( table_items_limit == 0 || !space->is_table() || (space->table_size() < table_items_limit) )
{
Space & new_space = space->add_empty_space();
if( all_items_limit == 0 || current_items_counter++ < all_items_limit )
{
parse(&new_space, false, true);
}
else
{
status = limit_all_items_exceeded;
}
}
else
{
status = limit_table_items_exceeded;
}
Space * new_space = &space->add(new Space());
parse(new_space, false, true);
}
is_first = false;
if( status == ok )
{
skip_white();
}
skip_white();
}
}

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2022, Tomasz Sowa
* Copyright (c) 2012-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_space_spaceparser
#define headerfile_pikotools_src_space_spaceparser
#ifndef headerfile_picotools_space_jsonspaceparser
#define headerfile_picotools_space_jsonspaceparser
#include <fstream>
#include "space.h"
@ -60,23 +63,8 @@ public:
/*
status of parsing
ok - input stream has been parsed correctly
cant_open_file - I cannot open the file (returns only in a case when parsing a file)
syntax_error - a syntax error in the input stream
limit_object_items_exceeded - limit of object items has been exceeded
limit_table_items_exceeded - limit of table items has been exceeded
limit_all_items_exceeded - limit of items (key/value pairs of objects or table items) throughout the whole tree has been exceeded
limit_nested_level_exceeded - limit of nested objects/tables has been exceeded
*/
enum Status {
ok,
cant_open_file,
syntax_error,
limit_object_items_exceeded,
limit_table_items_exceeded,
limit_all_items_exceeded,
limit_nested_level_exceeded
};
enum Status { ok, cant_open_file, syntax_error };
/*
@ -121,8 +109,6 @@ public:
Status parse_json(const wchar_t * str, Space & out_space, bool clear_space = true);
Status parse_json(const std::wstring & str, Space & out_space, bool clear_space = true);
Status parse_json(const pt::TextStream & str, Space & out_space, bool clear_space = true);
Status parse_json(const pt::WTextStream & str, Space & out_space, bool clear_space = true);
Status parse_space(const char * str, Space & out_space, bool clear_space = true);
@ -130,8 +116,6 @@ public:
Status parse_space(const wchar_t * str, Space & out_space, bool clear_space = true);
Status parse_space(const std::wstring & str, Space & out_space, bool clear_space = true);
Status parse_space(const pt::TextStream & str, Space & out_space, bool clear_space = true);
Status parse_space(const pt::WTextStream & str, Space & out_space, bool clear_space = true);
/*
@ -160,45 +144,6 @@ public:
*
*/
int get_last_parsed_line();
int get_last_parsed_column();
/*
* get/set limit of object items in one object
* default: 0 (disabled)
*/
void set_object_items_limit(size_t val);
size_t get_object_items_limit();
/*
* get/set limit of items in one table
* default: 0 (disabled)
*
*/
void set_table_items_limit(size_t val);
size_t get_table_items_limit();
/*
* get/set limit of all items (objects items and table items) througout the whole tree
* default: 0 (disabled)
*
*/
void set_all_items_limit(size_t val);
size_t get_all_items_limit();
/*
* get/set nested level limit
* limit of nested objects and tables
* default: 0 (disabled)
*
*/
void set_nested_level_limit(size_t val);
size_t get_nested_level_limit();
private:
@ -266,23 +211,6 @@ private:
bool parsing_space;
/*
* object_items_limit - limit of key/value pairs of one object
* table_items_limit - limit of items of one table
* all_items_limit - limit of all items of all objects and all tables
* nested_levels_limit - limit of nested objects/tables
*/
size_t object_items_limit;
size_t table_items_limit;
size_t all_items_limit;
size_t nested_levels_limit;
/*
* current_items_counter - how many items (key/value pairs of objects or table items) throughout the whole tree
* current_nested_level - current nested level of objects and tables
*/
size_t current_items_counter;
size_t current_nested_level;
void parse_root_space(bool clear_root_space);
void parse(Space * space, bool is_object_value, bool is_table_value);
@ -324,8 +252,6 @@ private:
void read_unicode_floating_format();
void read_unicode_code_point();
void prepare_to_parsing();
};

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,29 +11,32 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_textstream_stream
#define headerfile_pikotools_src_textstream_stream
#ifndef headerfile_picotools_textstream_stream
#define headerfile_picotools_textstream_stream
#include <string>

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2023, Tomasz Sowa
* Copyright (c) 2012-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_textstream_textstream
#define headerfile_pikotools_src_textstream_textstream
#ifndef headerfile_picotools_textstream_textstream
#define headerfile_picotools_textstream_textstream
#include <string>
#include "stream.h"
@ -68,67 +71,8 @@ public:
typedef CharT char_type;
typedef MemBuffer<char_type, stack_size, heap_block_size> buffer_type;
class iterator
{
public:
typename buffer_type::iterator membuffer_iterator;
iterator();
iterator(const iterator & i);
iterator & operator=(const iterator & i);
iterator(const typename buffer_type::iterator & i);
iterator & operator=(const typename buffer_type::iterator & i);
bool operator==(const iterator & i) const;
bool operator!=(const iterator & i) const;
iterator & operator++(); // prefix ++
iterator operator++(int); // postfix ++
iterator & operator--(); // prefix --
iterator operator--(int); // postfix --
CharT & operator*();
wchar_t get_unicode_and_advance(const iterator & end);
};
class const_iterator
{
public:
typename buffer_type::const_iterator membuffer_const_iterator;
const_iterator();
const_iterator(const const_iterator & i);
const_iterator(const iterator & i);
const_iterator & operator=(const const_iterator & i);
const_iterator & operator=(const iterator & i);
const_iterator(const typename buffer_type::const_iterator & i);
const_iterator(const typename buffer_type::iterator & i);
const_iterator & operator=(const typename buffer_type::const_iterator & i);
const_iterator & operator=(const typename buffer_type::iterator & i);
bool operator==(const const_iterator & i) const;
bool operator!=(const const_iterator & i) const;
const_iterator & operator++(); // prefix ++
const_iterator operator++(int); // postfix ++
const_iterator & operator--(); // prefix --
const_iterator operator--(int); // postfix --
CharT operator*();
wchar_t get_unicode_and_advance(const const_iterator & end);
};
typedef typename buffer_type::iterator iterator;
typedef typename buffer_type::const_iterator const_iterator;
bool is_char_stream() const;
@ -155,13 +99,6 @@ public:
std::string to_str() const;
std::wstring to_wstr() const;
/*
* returns true if the buffer was sufficient large and there were no convertion errors
*/
bool to_str(char * str, size_t max_buf_len) const;
bool to_str(wchar_t * str, size_t max_buf_len) const;
char get_char(size_t index) const;
wchar_t get_wchar(size_t index) const;
@ -175,7 +112,7 @@ public:
TextStreamBase & operator<<(char);
TextStreamBase & operator<<(unsigned char);
TextStreamBase & operator<<(wchar_t); // no surrogate pairs are used
TextStreamBase & operator<<(wchar_t);
TextStreamBase & operator<<(bool);
TextStreamBase & operator<<(short);
TextStreamBase & operator<<(int);
@ -198,12 +135,6 @@ public:
TextStreamBase & operator<<(const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
bool operator==(const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & stream) const;
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
bool operator!=(const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & stream) const;
// min width for integer output
// if the output value has less digits then first zeroes are added
@ -225,9 +156,6 @@ public:
protected:
template<typename char_type_to, size_t stack_size_to, size_t heap_block_size_to>
void put_stream(const TextStreamBase<char_type_to, stack_size_to, heap_block_size_to> & stream);
void put_stream(const Stream & stream);
@ -245,272 +173,6 @@ TextStreamBase<char_type, stack_size, heap_block_size>::TextStreamBase()
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::iterator()
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::iterator(const iterator & i) : membuffer_iterator(i)
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator=(const iterator & i)
{
membuffer_iterator = i;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::iterator(const typename buffer_type::iterator & i) : membuffer_iterator(i)
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator=(const typename buffer_type::iterator & i)
{
membuffer_iterator = i;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator==(const iterator & i) const
{
return membuffer_iterator == i.membuffer_iterator;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator!=(const iterator & i) const
{
return membuffer_iterator != i.membuffer_iterator;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator++()
{
++membuffer_iterator;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::iterator
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator++(int)
{
const_iterator old(*this);
membuffer_iterator++;
return old;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator--()
{
--membuffer_iterator;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::iterator
TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator--(int)
{
const_iterator old(*this);
membuffer_iterator--;
return old;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
char_type & TextStreamBase<char_type, stack_size, heap_block_size>::iterator::operator*()
{
return *membuffer_iterator;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
wchar_t TextStreamBase<char_type, stack_size, heap_block_size>::iterator::get_unicode_and_advance(const iterator & end)
{
if( *this != end )
{
if constexpr (sizeof(char_type) == sizeof(char) )
{
int res;
bool correct;
utf8_to_int(*this, end, res, correct);
if( correct )
return static_cast<wchar_t>(res);
else
return static_cast<wchar_t>(0xFFFD); // U+FFFD "replacement character"
}
else
{
wchar_t c = operator*();
++membuffer_iterator;
return c;
}
}
return 0;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::const_iterator()
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::const_iterator(const const_iterator & i) : membuffer_const_iterator(i.membuffer_const_iterator)
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::const_iterator(const iterator & i) : membuffer_const_iterator(i.membuffer_iterator)
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator=(const const_iterator & i)
{
membuffer_const_iterator = i.membuffer_const_iterator;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator=(const iterator & i)
{
membuffer_const_iterator = i.membuffer_iterator;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::const_iterator(const typename buffer_type::const_iterator & i) : membuffer_const_iterator(i)
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::const_iterator(const typename buffer_type::iterator & i) : membuffer_const_iterator(i)
{
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator=(const typename buffer_type::const_iterator & i)
{
membuffer_const_iterator = i;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator=(const typename buffer_type::iterator & i)
{
membuffer_const_iterator = i;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator==(const const_iterator & i) const
{
return membuffer_const_iterator == i.membuffer_const_iterator;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator!=(const const_iterator & i) const
{
return membuffer_const_iterator != i.membuffer_const_iterator;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator++()
{
++membuffer_const_iterator;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator++(int)
{
const_iterator old(*this);
membuffer_const_iterator++;
return old;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator &
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator--()
{
--membuffer_const_iterator;
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator
TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator--(int)
{
const_iterator old(*this);
membuffer_const_iterator--;
return old;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
char_type TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::operator*()
{
return *membuffer_const_iterator;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
wchar_t TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator::get_unicode_and_advance(const const_iterator & end)
{
if( *this != end )
{
if constexpr (sizeof(char_type) == sizeof(char) )
{
int res;
bool correct;
pt::utf8_to_int(*this, end, res, correct);
if( correct )
return static_cast<wchar_t>(res);
else
return static_cast<wchar_t>(0xFFFD); // U+FFFD "replacement character"
}
else
{
wchar_t c = operator*();
++membuffer_const_iterator;
return c;
}
}
return 0;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::is_char_stream() const
{
@ -610,12 +272,12 @@ void TextStreamBase<char_type, stack_size, heap_block_size>::to_str(std::string
if( str.capacity() < str.size() + size() )
str.reserve(str.size() + size());
if constexpr (sizeof(char_type) == sizeof(char) )
{
const_iterator i = begin();
const_iterator i_end = end();
for( ; i != i_end ; ++i)
for( ; i != end() ; ++i)
str += *i;
}
else
@ -644,7 +306,10 @@ void TextStreamBase<char_type, stack_size, heap_block_size>::to_str(std::wstring
}
else
{
utf8_to_wide(*this, str, false);
// IMPROVE ME don't use a temporary object
std::string utf8;
to_str(utf8);
utf8_to_wide(utf8, str, false);
}
}
@ -670,89 +335,6 @@ std::wstring TextStreamBase<char_type, stack_size, heap_block_size>::to_wstr() c
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::to_str(char * str, size_t max_buf_len) const
{
bool converted_correctly = false;
if( max_buf_len > 0 )
{
if constexpr (sizeof(char_type) == sizeof(char) )
{
converted_correctly = true;
const_iterator i = begin();
const_iterator i_end = end();
size_t len = 0;
max_buf_len -= 1; // for terminating null character
for( ; i != i_end ; ++i, ++len)
{
if( len < max_buf_len )
{
str[len] = *i;
}
else
{
converted_correctly = false;
break;
}
}
str[len] = 0;
}
else
{
converted_correctly = wide_stream_to_utf8(*this, str, max_buf_len);
}
}
return converted_correctly;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::to_str(wchar_t * str, size_t max_buf_len) const
{
bool converted_correctly = false;
if( max_buf_len > 0 )
{
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
converted_correctly = true;
const_iterator i = begin();
const_iterator i_end = end();
size_t len = 0;
max_buf_len -= 1; // for terminating null character
for( ; i != i_end ; ++i, ++len)
{
if( len < max_buf_len )
{
str[len] = *i;
}
else
{
converted_correctly = false;
break;
}
}
str[len] = 0;
}
else
{
converted_correctly = utf8_to_wide(*this, str, max_buf_len);
}
}
return converted_correctly;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
char TextStreamBase<char_type, stack_size, heap_block_size>::get_char(size_t index) const
{
@ -810,14 +392,7 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const std::string & str)
{
if constexpr ( sizeof(char_type) == sizeof(char) )
{
buffer.append(str.c_str(), str.size());
}
else
{
utf8_to_wide(str, *this, false);
}
operator<<(str.c_str());
return *this;
}
@ -835,7 +410,7 @@ TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const wchar_t
}
else
{
wide_to_utf8(str, *this, false);
wide_to_utf8(str, *this);
}
return *this;
@ -847,14 +422,7 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const std::wstring & str)
{
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
buffer.append(str.c_str(), str.size());
}
else
{
wide_to_utf8(str, *this, false);
}
operator<<(str.c_str());
return *this;
}
@ -865,14 +433,10 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(char v)
{
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
buffer.append(static_cast<char_type>(static_cast<unsigned char>(v)));
}
else
{
buffer.append(v);
}
// IMPROVEME
// if char_type == 1 then if v <= 127 then put that char but if (unsigned)v > 127 put replacement character
// if char_type > 1 then simply put that character
buffer.append(static_cast<char_type>(v));
return *this;
}
@ -882,6 +446,9 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(unsigned char v)
{
// IMPROVEME
// if char_type == 1 then if v <= 127 then put that char but if v > 127 put replacement character
// if char_type > 1 then simply put that character
buffer.append(static_cast<char_type>(v));
return *this;
@ -892,14 +459,8 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(wchar_t v)
{
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
buffer.append(v);
}
else
{
pt::int_to_utf8(static_cast<int>(v), *this);
}
// IMPROVEME add utf8/wide conversion, if v is from surrogate pair we can skip it
buffer.append(static_cast<char_type>(v));
return *this;
}
@ -1097,44 +658,6 @@ TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const Stream
template<typename char_type, size_t stack_size, size_t heap_block_size>
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
void TextStreamBase<char_type, stack_size, heap_block_size>::put_stream(
const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & stream
)
{
if( (sizeof(char_type) == sizeof(char) && stream.is_char_stream()) ||
(sizeof(char_type) == sizeof(wchar_t) && stream.is_wchar_stream()) )
{
// from utf8 to utf8 or from wide to wide
typename TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size>::const_iterator i = stream.begin();
for( ; i != stream.end() ; ++i)
{
operator<<(*i);
}
}
else
if( sizeof(char_type) == sizeof(wchar_t) && stream.is_char_stream() )
{
// from utf8 to wide
utf8_to_wide(stream, *this, false);
}
else
if( sizeof(char_type) == sizeof(char) && stream.is_wchar_stream() )
{
// from wide to utf8
wide_stream_to_utf8(stream, *this, false);
}
else
{
operator<<("such conversion is not implemented");
}
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
void TextStreamBase<char_type, stack_size, heap_block_size>::put_stream(const Stream & stream)
{
@ -1183,7 +706,7 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const Space & space)
{
space.serialize_to_json_stream(*this, true);
space.serialize_to_space_stream(*this, true);
return *this;
}
@ -1206,52 +729,17 @@ TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(
const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg)
{
/*
* in the future we can have a faster implementation
* which uses iterators instead of get_char() and get_wchar() methods
*
*/
put_stream(arg);
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::operator==(const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & stream) const
{
bool are_the_same = false;
/*
* at the moment we do not make any conversions for == and != operators
* this may change in the future
*/
if( sizeof(char_type) == sizeof(arg_char_type) && size() == stream.size() )
{
are_the_same = true;
const_iterator i1 = begin();
const_iterator i2 = stream.begin();
const_iterator i1_end = end();
while( i1 != i1_end )
{
if( *i1 != *i2 )
{
are_the_same = false;
break;
}
++i1;
++i2;
}
}
return are_the_same;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
bool TextStreamBase<char_type, stack_size, heap_block_size>::operator!=(const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & stream) const
{
return !operator==(stream);
}
@ -1283,35 +771,6 @@ TextStreamBase<char_type, stack_size, heap_block_size>::fill_up_if_needed(wchar_
}
/*!
this function converts an UTF-8 stream into wide stream or wide string
(is declared in utf8/utf8.h)
input:
iterator_in - an TextStream iterator for reading from
iterator_end - an end iterator (can be returned by end() method from TextStream)
output:
out_stream - an output wide stream or wide string
this function returns false if there were some errors when converting
*/
template<size_t stack_size, size_t heap_block_size, typename StreamOrStringType>
bool utf8_to_wide(
const TextStreamBase<char, stack_size, heap_block_size> & utf8,
StreamOrStringType & out_stream,
bool clear_stream,
int mode
)
{
typename TextStreamBase<char, stack_size, heap_block_size>::const_iterator i_begin = utf8.begin();
return utf8_to_wide(i_begin, utf8.end(), out_stream, clear_stream, mode);
}
} // namespace

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2022, Tomasz Sowa
* Copyright (c) 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_textstream_types
#define headerfile_pikotools_src_textstream_types
#ifndef headerfile_picotools_space_types
#define headerfile_picotools_space_types

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2010-2023, Tomasz Sowa
* Copyright (c) 2010-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fstream>
@ -128,64 +131,6 @@ bool surrogate_pair_to_int(int c1, int c2, int & z)
/*
* converts an int to a wide string
*
* this method will not terminate the output string with a null character
* return how many characters have been written (0, 1 or 2)
*/
size_t int_to_wide(int c, wchar_t * res, size_t max_buf_len)
{
if( sizeof(wchar_t)==2 && c>0xffff )
{
if( max_buf_len > 1 )
{
// UTF16 surrogate pairs
c -= 0x10000;
res[0] = static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
res[1] = static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
return 2;
}
}
else
{
if( max_buf_len > 0 )
{
res[0] = static_cast<wchar_t>(c);
return 1;
}
}
return 0;
}
/*
converts an int to a wide string
returns true if a character was inserted to the string
*/
bool int_to_wide(int c, std::wstring & res)
{
wchar_t buf[2];
size_t used = int_to_wide(c, buf, sizeof(buf) / sizeof(wchar_t));
if( used == 1 )
{
res += buf[0];
}
else
if( used == 2 )
{
res += buf[0];
res += buf[1];
}
return used > 0;
}
/*!
this function converts one UTF-8 character into one wide-character
@ -370,7 +315,7 @@ unsigned char uz;
uz = utf8.get_char(stream_index + i);
if( !private_namespace::utf8_to_int_add_next_octet(uz, res) )
return i + 1;
return i;
}
if( utf8_check_range(res, len) )
@ -388,6 +333,26 @@ unsigned char uz;
/*
*/
static void int_to_wide(int c, std::wstring & res)
{
if( sizeof(wchar_t)==2 && c>0xffff )
{
// UTF16 surrogate pairs
c -= 0x10000;
res += static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
res += static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
}
else
{
res += static_cast<wchar_t>(c);
}
}
/*!

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2010-2023, Tomasz Sowa
* Copyright (c) 2010-2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_utf8_utf8
#define headerfile_pikotools_src_utf8_utf8
#ifndef headerfile_picotools_utf8_utf8
#define headerfile_picotools_utf8_utf8
#include <string>
#include "textstream/stream.h"
@ -128,35 +131,14 @@ size_t utf8_to_int(const std::string & utf8, int & res, bool & correct
size_t utf8_to_int(std::istream & utf8, int & res, bool & correct);
size_t utf8_to_int(const Stream & utf8, size_t stream_index, int & res, bool & correct);
template<typename StreamIteratorType>
size_t utf8_to_int(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, int & res, bool & correct);
/*!
converting one character from int to wide stream
returns true if a character was inserted to the stream
*/
template<typename StreamType>
bool int_to_wide(int c, StreamType & res);
void int_to_wide(int c, StreamType & res);
/*!
converting one character from int to wide string
this method will not terminate the output string with a null character
return how many characters have been written (0, 1 or 2)
*/
size_t int_to_wide(int c, wchar_t * res, size_t max_buf_len);
/*!
converting one character from int to wide string
returns true if a character was inserted to the string
*/
bool int_to_wide(int c, std::wstring & res);
/*!
converting UTF-8 string to a wide string
@ -176,27 +158,10 @@ template<typename StreamType>
bool utf8_to_wide(const std::string & utf8, StreamType & res, bool clear = true, int mode = 1); // need to be tested
template<typename StreamType>
bool utf8_to_wide(std::istream & utf8, StreamType & res, bool clear = true, int mode = 1);
template<typename StreamOrStringType>
bool utf8_to_wide(const Stream & stream, StreamOrStringType & res, bool clear = true, int mode = 1);
template<typename StreamIteratorType, typename StreamOrStringType>
bool utf8_to_wide(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, StreamOrStringType & out_stream, bool clear_stream = true, int mode = 1);
template<typename CharT, size_t stack_size, size_t heap_block_size>
class TextStreamBase;
// defined at the end in textstream.h
template<size_t stack_size, size_t heap_block_size, typename StreamOrStringType>
bool utf8_to_wide(const TextStreamBase<char, stack_size, heap_block_size> & utf8, StreamOrStringType & out_stream, bool clear_stream = true, int mode = 1);
template<typename StreamIteratorType>
bool utf8_to_wide(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, wchar_t * out_buffer, size_t max_buffer_len, int mode = 1, bool * was_buffer_sufficient_large = nullptr);
bool utf8_to_wide(std::istream & utf8, StreamType & res, bool clear = true, int mode = 1); // need to be tested
template<typename StreamType>
bool utf8_to_wide(const StreamType & stream, wchar_t * out_buffer, size_t max_buffer_len, bool * was_buffer_sufficient_large = nullptr, int mode = 1);
bool utf8_to_wide(const Stream & stream, StreamType & res, bool clear = true, int mode = 1);
/*
@ -237,6 +202,8 @@ bool wide_to_utf8(const wchar_t * wide_string, StreamType & utf8, int mode = 1);
template<typename StreamType>
bool wide_to_utf8(const std::wstring & wide_string, StreamType & utf8, int mode = 1);
bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool wide_to_utf8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool wide_to_utf8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
@ -246,16 +213,13 @@ bool wide_to_utf8(const wchar_t * wide_string, char * utf8, s
bool wide_to_utf8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
template<typename StreamType>
bool wide_stream_to_utf8(StreamType & buffer, std::string & utf8, bool clear = true, int mode = 1);
void wide_stream_to_utf8(StreamType & buffer, std::string & utf8, bool clear = true, int mode = 1); // not tested
template<typename StreamType>
bool wide_stream_to_utf8(const Stream & stream, StreamType & utf8, bool clear = true, int mode = 1);
template<typename StreamTypeIn, typename StreamTypeOut>
bool wide_stream_to_utf8(StreamTypeIn & buffer, StreamTypeOut & utf8, bool clear = true, int mode = 1);
template<typename StreamType>
bool wide_stream_to_utf8(StreamType & buffer, char * utf8, std::size_t max_buffer_size, bool * was_buffer_sufficient_large = nullptr, int mode = 1);
void wide_stream_to_utf8(StreamTypeIn & buffer, StreamTypeOut & utf8, bool clear = true, int mode = 1); // not tested, IMPROVE ME mode parameter is not used
} // namespace

View File

@ -1,35 +1,38 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2023, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "utf8_private.h"
@ -102,9 +105,9 @@ size_t wide_to_int(const wchar_t * wide_string, size_t string_len, int & z, bool
z = static_cast<int>(*wide_string);
correct = true;
if( sizeof(wchar_t) == 2 && is_first_surrogate_char(z) )
if( sizeof(wchar_t) == 2 && is_surrogate_char(z) )
{
if( string_len > 1 )
if( is_first_surrogate_char(z) && string_len>1 )
{
int z2 = *(wide_string+1);
@ -116,7 +119,7 @@ size_t wide_to_int(const wchar_t * wide_string, size_t string_len, int & z, bool
else
{
correct = false;
return 1;
return 2;
}
}
else

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2023, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_utf8_utf8_private
#define headerfile_pikotools_src_utf8_utf8_private
#ifndef headerfile_picotools_utf8_utf8_private
#define headerfile_picotools_utf8_utf8_private
#include <string>
@ -56,7 +59,7 @@ namespace private_namespace
bool utf8_to_int_first_octet(unsigned char uz, size_t & len, int & res);
bool utf8_to_int_add_next_octet(unsigned char uz, int & res);
size_t wide_to_int(const wchar_t * wide_string, size_t string_len, int & z, bool & correct); // may these methods make public?
size_t wide_to_int(const wchar_t * wide_string, size_t string_len, int & z, bool & correct);
size_t wide_to_int(const wchar_t * wide_string, int & z, bool & correct);
size_t wide_one_to_utf8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len,
@ -163,14 +166,15 @@ return !was_error;
// FIX ME it is not using surrogate pairs from input stream
// and is not using mode parameter
template<typename StreamType, typename function_type>
bool wide_to_utf8_generic(StreamType & buffer, int mode, function_type write_function)
void wide_to_utf8_generic(StreamType & buffer, int mode, function_type write_function)
{
char utf8_buffer[256];
std::size_t buffer_len = sizeof(utf8_buffer) / sizeof(char);
std::size_t utf8_sequence_max_length = 10;
std::size_t index = 0;
bool was_error = false;
typename StreamType::const_iterator i = buffer.begin();
@ -178,78 +182,18 @@ bool wide_to_utf8_generic(StreamType & buffer, int mode, function_type write_fun
{
if( index + utf8_sequence_max_length > buffer_len )
{
bool write_status = write_function(utf8_buffer, index);
write_function(utf8_buffer, index);
index = 0;
if( !write_status )
{
was_error = true;
break;
}
}
int c = 0xFFFD; // U+FFFD "replacement character";
bool seems_to_be_correct = false;
wchar_t w1 = *i;
if( sizeof(wchar_t) == 2 && is_first_surrogate_char(w1) )
{
++i;
if( i != buffer.end() )
{
wchar_t w2 = *i;
if( surrogate_pair_to_int(w1, w2, c) )
{
seems_to_be_correct = true;
++i;
}
else
{
was_error = true;
}
}
else
{
was_error = true;
}
}
else
{
c = w1;
seems_to_be_correct = true; // we do not test utf8_check_range(...) here because it is tested in int_to_utf8(...) below
++i;
}
if( seems_to_be_correct || mode == 1 )
{
size_t seq_len = int_to_utf8(c, utf8_buffer + index, buffer_len - index);
// here seq_len can be zero only when c is an incorrect unicode char (the buffer is large enough)
if( seq_len == 0 )
{
was_error = true;
if( mode == 1 )
{
seq_len = int_to_utf8(0xFFFD, utf8_buffer + index, buffer_len - index); // U+FFFD "replacement character";
}
}
index += seq_len;
}
index += int_to_utf8(*i, utf8_buffer + index, buffer_len - index);
++i;
}
if( index > 0 )
{
if( !write_function(utf8_buffer, index) )
{
was_error = true;
}
write_function(utf8_buffer, index);
}
return !was_error;
}

104
src/utf8/utf8_stream.h Normal file
View File

@ -0,0 +1,104 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021, 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_utf8_utf8_stream
#define headerfile_picotools_utf8_utf8_stream
#include "textstream/textstream.h"
namespace pt
{
/*!
this function converts one UTF-8 character into one wide-character
input:
iterator_in - an TextStream iterator for reading from
iterator_end - an end iterator (can be returned by end() method from TextStream)
output:
res - an output character
correct - true if it is a correct character
the function returns how many characters have been used from the input stream
*/
template<typename StreamIteratorType>
size_t utf8_to_int(
StreamIteratorType & iterator_in,
StreamIteratorType & iterator_end,
int & res,
bool & correct)
{
size_t i, len;
unsigned char uz;
res = 0;
correct = false;
if( iterator_in == iterator_end )
return 0;
uz = *iterator_in;
++iterator_in;
if( !private_namespace::utf8_to_int_first_octet(uz, len, res) )
return 1;
for(i=1 ; i<len ; ++i)
{
if( iterator_in == iterator_end )
return i;
uz = *iterator_in;
++iterator_in;
if( !private_namespace::utf8_to_int_add_next_octet(uz, res) )
return i;
}
if( utf8_check_range(res, len) )
correct = true;
return len;
}
}
#endif

View File

@ -1,39 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2023, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_utf8_utf8_templates
#define headerfile_pikotools_src_utf8_utf8_templates
#ifndef headerfile_picotools_utf8_utf8_templates
#define headerfile_picotools_utf8_utf8_templates
// this file is included at the end of utf8.h
@ -46,75 +49,22 @@ namespace pt
template<typename StreamType>
bool int_to_wide(int c, StreamType & res)
void int_to_wide(int c, StreamType & res)
{
wchar_t buf[2];
size_t used = int_to_wide(c, buf, sizeof(buf) / sizeof(wchar_t));
if( used == 1 )
if( sizeof(wchar_t)==2 && c>0xffff )
{
res << buf[0];
// UTF16 surrogate pairs
c -= 0x10000;
res << static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
res << static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
}
else
if( used == 2 )
{
res << buf[0];
res << buf[1];
res << static_cast<wchar_t>(c);
}
return used > 0;
}
/*!
this function converts one UTF-8 character into int
input:
iterator_in - an stream iterator for reading from (the stream can by any stream, we use *, ++ and == operators only)
iterator_end - an end iterator
output:
res - an output character
correct - true if it is a correct character
the function returns how many characters have been used from the input stream
*/
template<typename StreamIteratorType>
size_t utf8_to_int(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, int & res, bool & correct)
{
size_t i, len;
unsigned char uz;
res = 0;
correct = false;
if( iterator_in == iterator_end )
return 0;
uz = *iterator_in;
++iterator_in;
if( !private_namespace::utf8_to_int_first_octet(uz, len, res) )
return 1;
for(i=1 ; i<len ; ++i)
{
if( iterator_in == iterator_end )
return i;
uz = *iterator_in;
++iterator_in;
if( !private_namespace::utf8_to_int_add_next_octet(uz, res) )
return i + 1;
}
if( utf8_check_range(res, len) )
correct = true;
return len;
}
/*!
@ -159,6 +109,7 @@ bool utf8_to_wide(const std::string & utf8, StreamType & res, bool clear, int mo
// need to be tested
template<typename StreamType>
bool utf8_to_wide(std::istream & utf8, StreamType & res, bool clear, int mode)
{
@ -188,7 +139,7 @@ return !was_error;
/*
this function converts a UTF-8 stream into a wide stream or a wide string
this function converts a UTF-8 stream into wide stream
input:
stream - a UTF-8 stream for converting
@ -197,12 +148,12 @@ input:
1: put U+FFFD "replacement character" istead of the invalid character (default)
output:
res - a wide stream or a wide string for the output sequence
res - a wide stream for the output sequence
this function returns false if there were some errors when converting
*/
template<typename StreamOrStringType>
bool utf8_to_wide(const Stream & stream, StreamOrStringType & res, bool clear, int mode)
template<typename StreamType>
bool utf8_to_wide(const Stream & stream, StreamType & res, bool clear, int mode)
{
size_t len;
bool correct;
@ -224,7 +175,7 @@ bool utf8_to_wide(const Stream & stream, StreamOrStringType & res, bool clear, i
if( !correct )
{
if( mode == 1 )
int_to_wide(0xFFFD, res); // U+FFFD "replacement character"
res << 0xFFFD; // U+FFFD "replacement character"
was_error = true;
}
@ -246,148 +197,6 @@ bool utf8_to_wide(const Stream & stream, StreamOrStringType & res, bool clear, i
/*!
this function converts UTF-8 stream into a wide stream or a wide string
input:
iterator_in - a stream iterator for reading from (the stream can by any stream, we use *, ++ and == operators only)
iterator_end - an end iterator
output:
out_stream - an output wide stream or wide string (the stream can by of any kind, we use only << operator for a stream and += for a string)
this function returns false if there were some errors when converting
*/
template<typename StreamIteratorType, typename StreamOrStringType>
bool utf8_to_wide(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, StreamOrStringType & out_stream, bool clear_stream, int mode)
{
if( clear_stream )
out_stream.clear();
int res;
bool correct;
bool was_error = false;
while( iterator_in != iterator_end )
{
utf8_to_int(iterator_in, iterator_end, res, correct);
if( correct )
{
int_to_wide(res, out_stream);
}
else
{
if( mode == 1 )
int_to_wide(0xFFFD, out_stream); // U+FFFD "replacement character"
was_error = true;
}
}
return !was_error;
}
/*!
this function converts UTF-8 stream into a wide string
input:
iterator_in - a stream iterator for reading from (the stream can by any stream, we use *, ++ and == operators only)
iterator_end - an end iterator
output:
out_buffer - an output wide string
max_buffer_len - how many characters can be write (we write the terminating null character too)
was_buffer_sufficient_large - a pointer to a bool value - if provided it is set to true if the buffer was sufficient large
this function returns false if there were some errors when converting or if the output buffer was too short
*/
template<typename StreamIteratorType>
bool utf8_to_wide(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, wchar_t * out_buffer, size_t max_buffer_len, int mode, bool * was_buffer_sufficient_large)
{
int res;
bool correct;
bool was_error = true;
bool was_buffer_ok = false;
if( max_buffer_len > 0 )
{
max_buffer_len -= 1; // for terminating null character
was_error = false;
was_buffer_ok = true;
while( iterator_in != iterator_end )
{
utf8_to_int(iterator_in, iterator_end, res, correct);
if( !correct )
{
was_error = true;
if( mode == 1 )
{
res = 0xFFFD; // U+FFFD "replacement character"
correct = true;
}
}
if( correct )
{
size_t len = int_to_wide(res, out_buffer, max_buffer_len);
// if len is zero then the output buffer is too short - the res input value was correct (it was returned from utf_to_int(...) beforehand)
if( len == 0 )
{
was_error = true;
was_buffer_ok = false;
break;
}
else
{
out_buffer += len;
max_buffer_len -= len;
}
}
}
*out_buffer = 0;
}
if( was_buffer_sufficient_large )
*was_buffer_sufficient_large = was_buffer_ok;
return !was_error;
}
/*!
this function converts UTF-8 stream into a wide string
input:
stream - a stream for reading from
output:
out_buffer - an output wide string
max_buffer_len - how many characters can be write (we write the terminating null character too)
was_buffer_sufficient_large - a pointer to a bool value - if provided it is set to true if the buffer was sufficient large
this function returns false if there were some errors when converting or if the output buffer was too short
*/
template<typename StreamType>
bool utf8_to_wide(const StreamType & stream, wchar_t * out_buffer, size_t max_buffer_len, bool * was_buffer_sufficient_large, int mode)
{
typename StreamType::const_iterator stream_begin = stream.begin();
typename StreamType::const_iterator stream_end = stream.end();
return utf8_to_wide(stream_begin, stream_end, out_buffer, max_buffer_len, mode, was_buffer_sufficient_large);
}
/*!
this function converts one wide character into UTF-8 stream
@ -503,14 +312,13 @@ bool wide_to_utf8(const std::wstring & wide_string, StreamType & utf8, int mode)
template<typename StreamType>
bool wide_stream_to_utf8(StreamType & buffer, std::string & utf8, bool clear, int mode)
void wide_stream_to_utf8(StreamType & buffer, std::string & utf8, bool clear, int mode)
{
if( clear )
utf8.clear();
return private_namespace::wide_to_utf8_generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len) -> bool {
private_namespace::wide_to_utf8_generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
utf8.append(utf8_buffer, buffer_len);
return true;
});
}
@ -568,77 +376,20 @@ bool wide_stream_to_utf8(const Stream & stream, StreamType & utf8, bool clear, i
}
// not tested
template<typename StreamTypeIn, typename StreamTypeOut>
bool wide_stream_to_utf8(StreamTypeIn & buffer, StreamTypeOut & utf8, bool clear, int mode)
void wide_stream_to_utf8(StreamTypeIn & buffer, StreamTypeOut & utf8, bool clear, int mode)
{
if( clear )
utf8.clear();
return private_namespace::wide_to_utf8_generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len) -> bool {
private_namespace::wide_to_utf8_generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
utf8.write(utf8_buffer, buffer_len);
return true;
});
}
/*!
this function converts a wide stream into a utf8 string
input:
buffer - a wide stream for reading from
output:
utf8 - an output utf8 string
max_buffer_len - how many characters can be write (we write the terminating null character too)
was_buffer_sufficient_large - a pointer to a bool value - if provided it is set to true if the buffer was sufficient large
this function returns false if there were some errors when converting or if the output buffer was too short
*/
template<typename StreamType>
bool wide_stream_to_utf8(StreamType & buffer, char * utf8, std::size_t max_buffer_size, bool * was_buffer_sufficient_large, int mode)
{
bool buffer_ok = false;
bool is_ok = false;
if( max_buffer_size > 0 )
{
buffer_ok = true;
max_buffer_size -= 1; // for terminating null character
is_ok = private_namespace::wide_to_utf8_generic(buffer, mode, [&utf8, &max_buffer_size, &buffer_ok](const char * utf8_buffer, std::size_t buffer_len) -> bool {
std::size_t i=0;
for( ; i < buffer_len ; ++i)
{
if( i < max_buffer_size )
{
*utf8 = utf8_buffer[i];
utf8 += 1;
}
else
{
buffer_ok = false;
break;
}
}
max_buffer_size -= i;
*utf8 = 0;
return buffer_ok;
});
}
if( was_buffer_sufficient_large )
*was_buffer_sufficient_large = buffer_ok;
return is_ok;
}
} // namespace pt
#endif

View File

@ -1,15 +1,16 @@
# DO NOT DELETE
./convert.o: convert.h ../src/convert/convert.h ../src/convert/inttostr.h
./convert.o: ../src/convert/patternreplacer.h ../src/textstream/textstream.h
./convert.o: ../src/textstream/stream.h ../src/space/space.h
./convert.o: ../src/textstream/types.h ../src/convert/inttostr.h
./convert.o: ../src/utf8/utf8.h ../src/textstream/stream.h
./convert.o: ../src/utf8/utf8_templates.h ../src/utf8/utf8_private.h
./convert.o: ../src/date/date.h ../src/membuffer/membuffer.h
./convert.o: ../src/textstream/types.h ../src/convert/strtoint.h
./convert.o: ../src/convert/text.h ../src/convert/misc.h
./convert.o: ../src/convert/double.h test.h
./convert.o: convert.h test.h ../src/convert/convert.h
./convert.o: ../src/convert/inttostr.h ../src/convert/patternreplacer.h
./convert.o: ../src/textstream/textstream.h ../src/textstream/stream.h
./convert.o: ../src/space/space.h ../src/textstream/types.h
./convert.o: ../src/convert/inttostr.h ../src/utf8/utf8.h
./convert.o: ../src/textstream/stream.h ../src/utf8/utf8_templates.h
./convert.o: ../src/utf8/utf8_private.h ../src/date/date.h
./convert.o: ../src/membuffer/membuffer.h ../src/textstream/types.h
./convert.o: ../src/convert/strtoint.h ../src/convert/text.h
./convert.o: ../src/convert/misc.h ../src/utf8/utf8_stream.h
./convert.o: ../src/convert/double.h
./csvparser.o: csvparser.h ../src/csv/csvparser.h ../src/space/space.h
./csvparser.o: ../src/textstream/types.h ../src/convert/inttostr.h
./csvparser.o: ../src/utf8/utf8.h ../src/textstream/stream.h
@ -17,16 +18,7 @@
./csvparser.o: ../src/convert/baseparser.h ../src/textstream/textstream.h
./csvparser.o: ../src/textstream/stream.h ../src/date/date.h
./csvparser.o: ../src/membuffer/membuffer.h ../src/textstream/types.h test.h
./main.o: convert.h ../src/convert/convert.h ../src/convert/inttostr.h
./main.o: ../src/convert/patternreplacer.h ../src/textstream/textstream.h
./main.o: ../src/textstream/stream.h ../src/space/space.h
./main.o: ../src/textstream/types.h ../src/convert/inttostr.h
./main.o: ../src/utf8/utf8.h ../src/textstream/stream.h
./main.o: ../src/utf8/utf8_templates.h ../src/utf8/utf8_private.h
./main.o: ../src/date/date.h ../src/membuffer/membuffer.h
./main.o: ../src/textstream/types.h ../src/convert/strtoint.h
./main.o: ../src/convert/text.h ../src/convert/misc.h ../src/convert/double.h
./main.o: test.h mainoptionsparser.h csvparser.h
./main.o: convert.h mainoptionsparser.h csvparser.h
./test.o: test.h
./mainoptionsparser.o: mainoptionsparser.h test.h
./mainoptionsparser.o: ../src/mainoptions/mainoptionsparser.h
@ -41,4 +33,5 @@
./mainoptionsparser.o: ../src/textstream/stream.h ../src/date/date.h
./mainoptionsparser.o: ../src/membuffer/membuffer.h ../src/textstream/types.h
./mainoptionsparser.o: ../src/convert/strtoint.h ../src/convert/text.h
./mainoptionsparser.o: ../src/convert/misc.h ../src/convert/double.h
./mainoptionsparser.o: ../src/convert/misc.h ../src/utf8/utf8_stream.h
./mainoptionsparser.o: ../src/convert/double.h

View File

@ -1,39 +1,44 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2022, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include "convert.h"
#include "test.h"
#include "convert/convert.h"
namespace pt
@ -1917,181 +1922,6 @@ void test_text46()
}
void test_text47()
{
reset_test_counter("to_ull");
IntegerHelper<unsigned long long> helper_tab[] = {
{L"", 0ULL, 10, L"", false, false},
{L"0", 0ULL, 10, L"", false, false},
{L"00", 0ULL, 10, L"", false, false},
{L"1", 1ULL, 10, L"", false, false},
{L"50", 50ULL, 10, L"", false, false},
{L"050", 50ULL, 10, L"", false, false},
{L"100", 100ULL, 10, L"", false, false},
{L"00100", 100ULL, 10, L"", false, false},
{L"128", 128ULL, 10, L"", false, false},
{L"1000", 1000ULL, 10, L"", false, false},
{L"65535", 65535ULL, 10, L"", false, false},
{L"65536", 65536ULL, 10, L"", false, false},
{L"65537", 65537ULL, 10, L"", false, false},
{L"4294967295", 4294967295ULL, 10, L"", false, false},
{L"4294967296", 4294967296ULL, 10, L"", false, false},
{L"4294967297", 4294967297ULL, 10, L"", false, false},
{L"18446744073709551614", 18446744073709551614ULL, 10, L"", false, false},
{L"18446744073709551615", 18446744073709551615ULL, 10, L"", false, false},
{L" 1024", 0ULL, 10, L" 1024", false, false},
{L" 1024", 1024ULL, 10, L"", false, true},
{L" \t 1024", 0ULL, 10, L" \t 1024", false, false},
{L" \t 1024", 1024ULL, 10, L"", false, true},
{L" 1024foo", 0ULL, 10, L" 1024foo", false, false},
{L" 1024foo", 1024ULL, 10, L"foo", false, true},
{L" \t 1024foo", 0ULL, 10, L" \t 1024foo", false, false},
{L" \t 1024foo", 1024ULL, 10, L"foo", false, true},
{L"", 0ULL, 2, L"", false, false},
{L"0", 0ULL, 2, L"", false, false},
{L"1", 1ULL, 2, L"", false, false},
{L"2", 0ULL, 2, L"2", false, false},
{L"101001011", 0b101001011ULL, 2, L"", false, false},
{L"001011001101", 0b001011001101ULL, 2, L"", false, false},
{L"", 0ULL, 8, L"", false, false},
{L"0", 0ULL, 8, L"", false, false},
{L"1", 1ULL, 8, L"", false, false},
{L"7", 7ULL, 8, L"", false, false},
{L"8", 0ULL, 8, L"8", false, false},
{L"54321760123", 054321760123ULL, 8, L"", false, false},
{L"54012418", 05401241ULL, 8, L"8", false, false},
{L"01256252foo", 01256252ULL, 8, L"foo", false, false},
{L"", 0ULL, 16, L"", false, false},
{L"0", 0ULL, 16, L"", false, false},
{L"9", 9ULL, 16, L"", false, false},
{L"a", 10ULL, 16, L"", false, false},
{L"f", 15ULL, 16, L"", false, false},
{L"g", 0ULL, 16, L"g", false, false},
{L"A", 10ULL, 16, L"", false, false},
{L"F", 15ULL, 16, L"", false, false},
{L"G", 0ULL, 16, L"G", false, false},
{L"FFFF", 0xFFFFULL, 16, L"", false, false},
{L"54FB91", 0x54FB91ULL, 16, L"", false, false},
{L"abcdef00123", 0xabcdef00123ULL, 16, L"", false, false},
{L"0a65bC3aDB1", 0x0a65bC3aDB1ULL, 16, L"", false, false},
{L"0a65bC3aDB1g", 0x0a65bC3aDB1ULL, 16, L"g", false, false},
{L"0a65bC3aDB1G", 0x0a65bC3aDB1ULL, 16, L"G", false, false},
{L"0a65bC3aDB1z", 0x0a65bC3aDB1ULL, 16, L"z", false, false},
{L"0a65bC3aDB1Z", 0x0a65bC3aDB1ULL, 16, L"Z", false, false},
};
size_t len = sizeof(helper_tab) / sizeof(IntegerHelper<unsigned long long>);
make_str_to_int_tests<std::wstring, unsigned long long>(&to_ull, &to_ull, helper_tab, len);
}
void test_text48()
{
reset_test_counter("to_ll");
IntegerHelper<long long> helper_tab[] = {
{L"", 0LL, 10, L"", false, false},
{L"0", 0LL, 10, L"", false, false},
{L"-0", 0LL, 10, L"", false, false},
{L"00", 0LL, 10, L"", false, false},
{L"-00", 0LL, 10, L"", false, false},
{L"100", 100LL, 10, L"", false, false},
{L"-100", -100LL, 10, L"", false, false},
{L"00100", 100LL, 10, L"", false, false},
{L"-00100", -100LL, 10, L"", false, false},
{L"128", 128LL, 10, L"", false, false},
{L"-128", -128LL, 10, L"", false, false},
{L"1000", 1000LL, 10, L"", false, false},
{L"-1000", -1000LL, 10, L"", false, false},
{L"65535", 65535LL, 10, L"", false, false},
{L"65536", 65536LL, 10, L"", false, false},
{L"65537", 65537LL, 10, L"", false, false},
{L"-65535", -65535LL, 10, L"", false, false},
{L"-65536", -65536LL, 10, L"", false, false},
{L"-65537", -65537LL, 10, L"", false, false},
{L"4294967295", 4294967295LL, 10, L"", false, false},
{L"4294967296", 4294967296LL, 10, L"", false, false},
{L"4294967297", 4294967297LL, 10, L"", false, false},
{L"-4294967295", -4294967295LL, 10, L"", false, false},
{L"-4294967296", -4294967296LL, 10, L"", false, false},
{L"-4294967297", -4294967297LL, 10, L"", false, false},
{L"9223372036854775806", 9223372036854775806LL, 10, L"", false, false},
{L"9223372036854775807", 9223372036854775807LL, 10, L"", false, false},
{L"9223372036854775808", 0LL, 10, L"", true, false},
{L"-9223372036854775807", -9223372036854775807LL, 10, L"", false, false},
{L"-9223372036854775808", std::numeric_limits<long long>::min(), 10, L"", false, false}, // gcc and clang not allow -9223372036854775808LL as a literal
{L"-9223372036854775809", 0LL, 10, L"", true, false},
{L"123456789876543210123", 0LL, 10, L"", true, false},
{L"-123456789876543210123", 0LL, 10, L"", true, false},
{L" -1024", 0LL, 10, L" -1024", false, false},
{L" -1024", -1024LL, 10, L"", false, true},
{L" \t -1024", 0LL, 10, L" \t -1024", false, false},
{L" \t -1024", -1024LL, 10, L"", false, true},
{L" -1024foo", 0LL, 10, L" -1024foo", false, false},
{L" -1024foo", -1024LL, 10, L"foo", false, true},
{L" \t -1024foo", 0LL, 10, L" \t -1024foo", false, false},
{L" \t -1024foo", -1024LL, 10, L"foo", false, true},
{L"", 0LL, 2, L"", false, false},
{L"0", 0LL, 2, L"", false, false},
{L"-0", 0LL, 2, L"", false, false},
{L"1", 1LL, 2, L"", false, false},
{L"2", 0LL, 2, L"2", false, false},
{L"-1", -1LL, 2, L"", false, false},
{L"-2", 0LL, 2, L"2", false, false},
{L"-101001011", -0b101001011LL, 2, L"", false, false},
{L"-001011001101", -0b001011001101LL, 2, L"", false, false},
{L"", 0LL, 8, L"", false, false},
{L"0", 0LL, 8, L"", false, false},
{L"-0", 0LL, 8, L"", false, false},
{L"1", 1LL, 8, L"", false, false},
{L"7", 7LL, 8, L"", false, false},
{L"8", 0LL, 8, L"8", false, false},
{L"-1", -1LL, 8, L"", false, false},
{L"-7", -7LL, 8, L"", false, false},
{L"-8", 0LL, 8, L"8", false, false},
{L"54321760123", 054321760123LL, 8, L"", false, false},
{L"54012418", 05401241LL, 8, L"8", false, false},
{L"01256252foo", 01256252LL, 8, L"foo", false, false},
{L"-54321760123", -054321760123LL, 8, L"", false, false},
{L"-54012418", -05401241LL, 8, L"8", false, false},
{L"-01256252foo", -01256252LL, 8, L"foo", false, false},
{L"", 0LL, 16, L"", false, false},
{L"0", 0LL, 16, L"", false, false},
{L"-0", 0LL, 16, L"", false, false},
{L"ab65c", 0xab65cLL, 16, L"", false, false},
{L"-654FEc23", -0x654FEc23LL, 16, L"", false, false},
{L"076af12b", 0x76af12bLL, 16, L"", false, false},
{L"-076de12be", -0x076de12beLL, 16, L"", false, false},
{L"7FFFFFFFFFFFFFFF", 0x7FFFFFFFFFFFFFFFLL, 16, L"", false, false},
{L"8000000000000000", 0LL, 16, L"", true, false},
{L"8000000000000001", 0LL, 16, L"", true, false},
{L"abcdef0123456789", 0LL, 16, L"", true, false},
{L"-7FFFFFFFFFFFFFFF", -0x7FFFFFFFFFFFFFFFLL, 16, L"", false, false},
{L"-8000000000000000", std::numeric_limits<long long>::min(), 16, L"", false, false},
{L"-8000000000000001", 0LL, 16, L"", true, false},
{L"-abcdef0123456789", 0LL, 16, L"", true, false},
};
size_t len = sizeof(helper_tab) / sizeof(IntegerHelper<long long>);
make_str_to_int_tests<std::wstring, long long>(&to_ll, &to_ll, helper_tab, len);
}
// IMPROVEME put integer tests for functions: to_ul(), to_ui(), to_l(), to_i(), to_ull_b(), to_ll_b(), to_ul_b(), to_ui_b(), to_l_b(), to_i_b()
void make_tests()
@ -2142,8 +1972,6 @@ void make_tests()
test_text44();
test_text45();
test_text46();
test_text47();
test_text48();
}

View File

@ -1,43 +1,42 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021-2022, Tomasz Sowa
* Copyright (c) 2021, 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:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_tests_convert
#define headerfile_pikotools_tests_convert
#include "convert/convert.h"
#include "test.h"
#ifndef headerfile_picotools_tests_convert
#define headerfile_picotools_tests_convert
namespace pt
{
@ -45,43 +44,6 @@ namespace pt
namespace pt_convert_tests
{
template<typename IntegerType>
struct IntegerHelper {
const wchar_t * val;
IntegerType val_int;
int base;
const wchar_t * after_str;
bool was_overflow;
bool allow_skip_whitechars;
};
template<typename StringType, typename IntegerType>
void make_str_to_int_tests(
IntegerType (*convert_function1)(const typename StringType::value_type *, int, const typename StringType::value_type **, bool *, bool allow_skip_whitechars),
IntegerType (*convert_function2)(const StringType &, int, const typename StringType::value_type **, bool *, bool allow_skip_whitechars),
IntegerHelper<IntegerType> * helper_tab,
size_t len)
{
for(size_t i=0 ; i<len ; ++i)
{
IntegerHelper<IntegerType> & helper = helper_tab[i];
std::wstring str1 = helper.val;
const wchar_t * after;
bool was_overflow;
test(convert_function1(str1.c_str(), helper.base, &after, &was_overflow, helper.allow_skip_whitechars), helper.val_int);
test(helper.after_str, after);
test(helper.was_overflow, was_overflow);
test(convert_function2(str1, helper.base, &after, &was_overflow, helper.allow_skip_whitechars), helper.val_int);
test(helper.after_str, after);
test(helper.was_overflow, was_overflow);
}
}
void make_tests();

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,25 +11,28 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "csvparser.h"

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,29 +11,32 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_tests_csvparser
#define headerfile_pikotools_tests_csvparser
#ifndef headerfile_picotools_tests_csvparser
#define headerfile_picotools_tests_csvparser
namespace pt

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,25 +11,28 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "convert.h"

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,25 +11,28 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,29 +11,32 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_tests_mainoptionsparser
#define headerfile_pikotools_tests_mainoptionsparser
#ifndef headerfile_picotools_tests_mainoptionsparser
#define headerfile_picotools_tests_mainoptionsparser
namespace pt

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,25 +11,28 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.h"

View File

@ -1,6 +1,6 @@
/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
@ -11,29 +11,32 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
* * 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 HOLDER OR CONTRIBUTORS BE
* 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.
*
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_pikotools_src_tests_test
#define headerfile_pikotools_src_tests_test
#ifndef headerfile_picotools_tests_test
#define headerfile_picotools_tests_test
#include <iostream>
#include <cstring>