diff --git a/convert/convert.h b/convert/convert.h index 99f68b7..2feb28c 100644 --- a/convert/convert.h +++ b/convert/convert.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012, Tomasz Sowa + * Copyright (c) 2012-2018, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -40,6 +40,7 @@ #include "inttostr.h" +#include "patternreplacer.h" #include "strtoint.h" #include "text.h" diff --git a/convert/patternreplacer.h b/convert/patternreplacer.h new file mode 100644 index 0000000..0d89a8f --- /dev/null +++ b/convert/patternreplacer.h @@ -0,0 +1,169 @@ +/* + * This file is a part of PikoTools + * and is distributed under the (new) BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2018, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name Tomasz Sowa nor the names of contributors to this + * project may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef headerfile_picotools_convert_patternreplacer +#define headerfile_picotools_convert_patternreplacer + +#include +#include "textstream/textstream.h" +#include "strtoint.h" + + + +namespace PT +{ + +template +class PatternReplacerBase +{ +public: + + /* + * output_string can be the same string as input pattern + */ + template + void ReplaceToString(const StrType & pattern, StrType & output_string, Types ... types) + { + ReplaceGeneric(pattern, output_string, types...); + } + + + template + StrType Replace(const StrType & pattern, Types ... types) + { + StrType output_string; + ReplaceGeneric(pattern, output_string, types...); + + return output_string; + } + + +private: + + std::vector params; + TextStreamBase buffer; + StrType temp_str; + + + + template + void ReplaceGeneric(const StrType & pattern, StrType & output_string, Types ... types) + { + params.clear(); + AddParams(types...); + ReplacePattern(pattern); + buffer.to_string(output_string); + + params.clear(); + buffer.clear(); + temp_str.clear(); + } + + + void AddParams() + { + } + + + template + void AddParams(const Type & type, Types ... types) + { + buffer.clear(); + buffer << type; + buffer.to_string(temp_str); + params.push_back(temp_str); + temp_str.clear(); + + AddParams(types...); + } + + + void ReplacePattern(const StrType & pattern) + { + buffer.clear(); + + for(size_t i=0 ; i index_start_str && (size_t)index < params.size() ) + { + i = i + 1 + (after_str - index_start_str); + buffer << params[index]; + pattern_changed = true; + } + } + } + } + + if( !pattern_changed ) + { + buffer << pattern[i]; + i += 1; + } + } + } + + +}; + + +typedef PatternReplacerBase PatternReplacer; +typedef PatternReplacerBase WPatternReplacer; + + +} + + +#endif diff --git a/date/Makefile.dep b/date/Makefile.dep index dd46ea6..4b933e3 100644 --- a/date/Makefile.dep +++ b/date/Makefile.dep @@ -1,4 +1,3 @@ # DO NOT DELETE -date.o: date.h ../convert/convert.h ../convert/inttostr.h -date.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h +date.o: date.h ../convert/inttostr.h diff --git a/date/date.h b/date/date.h index 42f20d9..b891413 100644 --- a/date/date.h +++ b/date/date.h @@ -40,7 +40,8 @@ #include #include -#include "convert/convert.h" +#include "convert/inttostr.h" + namespace PT diff --git a/log/Makefile.dep b/log/Makefile.dep index 733e3c8..1a14a56 100644 --- a/log/Makefile.dep +++ b/log/Makefile.dep @@ -1,12 +1,9 @@ # DO NOT DELETE filelog.o: filelog.h ../textstream/textstream.h ../space/space.h -filelog.o: ../textstream/types.h ../date/date.h ../convert/convert.h -filelog.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h -filelog.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h -filelog.o: ../utf8/utf8.h +filelog.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h +filelog.o: ../membuffer/membuffer.h ../textstream/types.h ../utf8/utf8.h log.o: log.h ../textstream/textstream.h ../space/space.h -log.o: ../textstream/types.h ../date/date.h ../convert/convert.h -log.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h -log.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h -log.o: filelog.h ../utf8/utf8.h +log.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h +log.o: ../membuffer/membuffer.h ../textstream/types.h filelog.h +log.o: ../utf8/utf8.h diff --git a/mainspaceparser/Makefile.dep b/mainspaceparser/Makefile.dep index 9893b1b..ed0b0f8 100644 --- a/mainspaceparser/Makefile.dep +++ b/mainspaceparser/Makefile.dep @@ -2,6 +2,5 @@ mainspaceparser.o: mainspaceparser.h ../space/space.h ../textstream/types.h mainspaceparser.o: ../utf8/utf8.h ../textstream/textstream.h ../date/date.h -mainspaceparser.o: ../convert/convert.h ../convert/inttostr.h -mainspaceparser.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h -mainspaceparser.o: ../membuffer/membuffer.h ../textstream/types.h +mainspaceparser.o: ../convert/inttostr.h ../membuffer/membuffer.h +mainspaceparser.o: ../textstream/types.h diff --git a/space/Makefile.dep b/space/Makefile.dep index adb4879..23c03ed 100644 --- a/space/Makefile.dep +++ b/space/Makefile.dep @@ -2,18 +2,16 @@ jsontospaceparser.o: jsontospaceparser.h space.h ../textstream/types.h jsontospaceparser.o: ../utf8/utf8.h ../textstream/textstream.h -jsontospaceparser.o: ../space/space.h ../date/date.h ../convert/convert.h -jsontospaceparser.o: ../convert/inttostr.h ../convert/strtoint.h -jsontospaceparser.o: ../convert/text.h ../convert/misc.h +jsontospaceparser.o: ../space/space.h ../date/date.h ../convert/inttostr.h jsontospaceparser.o: ../membuffer/membuffer.h ../textstream/types.h space.o: space.h ../textstream/types.h ../utf8/utf8.h space.o: ../textstream/textstream.h ../space/space.h ../date/date.h -space.o: ../convert/convert.h ../convert/inttostr.h ../convert/strtoint.h -space.o: ../convert/text.h ../convert/misc.h ../membuffer/membuffer.h -space.o: ../textstream/types.h +space.o: ../convert/inttostr.h ../membuffer/membuffer.h ../textstream/types.h +space.o: ../convert/convert.h ../convert/inttostr.h +space.o: ../convert/patternreplacer.h ../convert/strtoint.h ../convert/text.h +space.o: ../convert/misc.h spaceparser.o: spaceparser.h space.h ../textstream/types.h ../utf8/utf8.h spaceparser.o: ../textstream/textstream.h ../space/space.h ../date/date.h -spaceparser.o: ../convert/convert.h ../convert/inttostr.h -spaceparser.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h -spaceparser.o: ../membuffer/membuffer.h ../textstream/types.h +spaceparser.o: ../convert/inttostr.h ../membuffer/membuffer.h +spaceparser.o: ../textstream/types.h spacetojson.o: spacetojson.h space.h ../textstream/types.h diff --git a/textstream/textstream.h b/textstream/textstream.h index eb5e28c..b488951 100644 --- a/textstream/textstream.h +++ b/textstream/textstream.h @@ -41,7 +41,7 @@ #include #include "space/space.h" #include "date/date.h" -#include "convert/convert.h" +#include "convert/inttostr.h" #include "membuffer/membuffer.h" #include "types.h" diff --git a/utf8/Makefile.dep b/utf8/Makefile.dep index ff68e4a..9d53a86 100644 --- a/utf8/Makefile.dep +++ b/utf8/Makefile.dep @@ -1,6 +1,5 @@ # DO NOT DELETE utf8.o: utf8.h ../textstream/textstream.h ../space/space.h -utf8.o: ../textstream/types.h ../date/date.h ../convert/convert.h -utf8.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h -utf8.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h +utf8.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h +utf8.o: ../membuffer/membuffer.h ../textstream/types.h