From d1f5d9aa88a67ba6813b97f0cd1945c28cc9a739 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 9 Nov 2023 10:57:11 +0100 Subject: [PATCH] (winixcli): add a --sort-tables option while here: - add a fil_max_size filter for putting a space if the inner stream is greater than x characters --- .gitignore | 1 + winixcli/src/Makefile | 16 ++++++--- winixcli/src/Makefile.dep | 14 ++++---- winixcli/src/main.cpp | 41 +++++++++++++++++++--- winixcli/src/pgmodeler.cpp | 70 +++++++++++++++++++++++++++++++++----- winixcli/src/pgmodeler.h | 8 +++-- 6 files changed, 124 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index a3e7806..24f945c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so winixd/winix winixcli/src/winix +winixcli/m .clangd .qtc_clangd/ winix.cflags diff --git a/winixcli/src/Makefile b/winixcli/src/Makefile index 68ddbce..6d4ac61 100644 --- a/winixcli/src/Makefile +++ b/winixcli/src/Makefile @@ -12,9 +12,17 @@ ifndef CXX CXX = g++ endif +ifndef ADDITIONAL_CXXFLAGS +ADDITIONAL_CXXFLAGS=-O2 +endif + +ifndef ADDITIONAL_LDFLAGS +ADDITIONAL_LDFLAGS=-s +endif + ifndef CXXFLAGS # /usr/include/postgresql is in Linux distros (Alpine) -CXXFLAGS = -Wall -pedantic -O2 -std=c++20 \ +CXXFLAGS = -Wall -pedantic -std=c++20 $(ADDITIONAL_CXXFLAGS) \ -I/usr/local/include \ -I/usr/include/postgresql \ -I$(GLOBAL_WORKING_DIR)/winix/winixcli \ @@ -25,15 +33,13 @@ CXXFLAGS = -Wall -pedantic -O2 -std=c++20 \ $(WINIX_NEEDED_MACROS) endif - -ifndef LDFLAGS -LDFLAGS = -L/usr/local/lib -s -endif +LDFLAGS = -L/usr/local/lib $(ADDITIONAL_LDFLAGS) export CXX export CXXFLAGS export LDFLAGS +export GLOBAL_WORKING_DIR export WINIX_NEEDED_MACROS diff --git a/winixcli/src/Makefile.dep b/winixcli/src/Makefile.dep index c0b704d..d9a66da 100644 --- a/winixcli/src/Makefile.dep +++ b/winixcli/src/Makefile.dep @@ -1,13 +1,15 @@ # DO NOT DELETE -./main.o: ../../../pikotools/src/mainoptions/mainoptionsparser.h +./main.o: ../../../pikotools/src/convert/strtoint.h +./main.o: ../../../pikotools/src/convert/text.h misc.h ./main.o: ../../../pikotools/src/space/space.h ./main.o: ../../../pikotools/src/textstream/types.h ./main.o: ../../../pikotools/src/convert/inttostr.h ./main.o: ../../../pikotools/src/utf8/utf8.h ./main.o: ../../../pikotools/src/textstream/stream.h ./main.o: ../../../pikotools/src/utf8/utf8_templates.h -./main.o: ../../../pikotools/src/utf8/utf8_private.h pgmodeler.h misc.h +./main.o: ../../../pikotools/src/utf8/utf8_private.h +./main.o: ../../../pikotools/src/mainoptions/mainoptionsparser.h pgmodeler.h ./main.o: ../../../ezc/src/ezc.h ../../../ezc/src/version.h ./main.o: ../../../ezc/src/generator.h ../../../ezc/src/blocks.h ./main.o: ../../../ezc/src/item.h ../../../ezc/src/cache.h @@ -28,10 +30,10 @@ ./main.o: ../../../morm/src/modelconnector.h ../../../morm/src/clearer.h ./main.o: ../../../morm/src/ft.h ../../../morm/src/dbconnector.h ./main.o: ../../../morm/src/queryresult.h ../../../morm/src/flatconnector.h -./main.o: ../../../morm/src/dbexpression.h ../../../morm/src/baseexpression.h -./main.o: ../../../morm/src/morm_types.h ../../../morm/src/modelenv.h -./main.o: ../../../morm/src/modeldata.h ../../../morm/src/cursorhelper.h -./main.o: ../../../morm/src/finderhelper.h +./main.o: ../../../morm/src/export.h ../../../morm/src/dbexpression.h +./main.o: ../../../morm/src/baseexpression.h ../../../morm/src/morm_types.h +./main.o: ../../../morm/src/modelenv.h ../../../morm/src/modeldata.h +./main.o: ../../../morm/src/cursorhelper.h ../../../morm/src/finderhelper.h ./main.o: ../../../morm/src/fieldvaluehelper.h ../../../morm/src/select.h ./main.o: ../../../pikotools/src/convert/text.h ./main.o: ../../../morm/src/flatexpression.h ../../../ezc/src/patternparser.h diff --git a/winixcli/src/main.cpp b/winixcli/src/main.cpp index 5ea3384..78c288d 100644 --- a/winixcli/src/main.cpp +++ b/winixcli/src/main.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2022, Tomasz Sowa + * Copyright (c) 2022-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,6 +33,7 @@ */ #include +#include "convert/strtoint.h" #include "mainoptions/mainoptionsparser.h" #include "pgmodeler.h" #include "misc.h" @@ -60,10 +61,10 @@ std::wstring get_param(const pt::Space::TableType * input_mode) } -bool use_pgmodeler(const std::wstring & input_file, const pt::Space & input_options, pt::Space & schema) +bool use_pgmodeler(const std::wstring & input_file, const pt::Space & input_options, pt::Space & schema, bool sort_tables) { PGModeler pgmodeler; - return pgmodeler.parse(input_file, schema); + return pgmodeler.parse(input_file, schema, sort_tables); } @@ -125,6 +126,35 @@ void fil_tex(Ezc::FunInfo & env) } } + +void fil_max_size(Ezc::FunInfo & env) +{ + auto i = env.in.begin(); + size_t max_len = pt::to_ul(env.par); + size_t len = 0; + + for( ; i != env.in.end() ; ++i) + { + if( len >= max_len ) + { + env.out << ' '; + len = 0; + } + + if( pt::is_white(*i) ) + { + len = 0; + } + else + { + len += 1; + } + + env.out << *i; + } +} + + void cmp(Ezc::FunInfo & env) { if( env.params.size() >= 2 ) @@ -176,6 +206,7 @@ bool generate(const pt::Space & input_options, pt::Space & schema) functions.Insert("fil_tex", fil_tex); functions.Insert("cmp", cmp); + functions.Insert("fil_max_size", fil_max_size); parser.Directory(dir); parser.SetBlocks(blocks); @@ -237,9 +268,11 @@ int main(int argc, const char ** argv) print("input-mode", input_mode); print("input-file", input_file); + bool sort_tables = input_options.has_key(L"sort-tables"); + if( input_mode == L"pgmodeler" ) { - if( !use_pgmodeler(input_file, input_options, schema) ) + if( !use_pgmodeler(input_file, input_options, schema, sort_tables) ) return 3; } else diff --git a/winixcli/src/pgmodeler.cpp b/winixcli/src/pgmodeler.cpp index 6208196..45c4b7d 100644 --- a/winixcli/src/pgmodeler.cpp +++ b/winixcli/src/pgmodeler.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2022, Tomasz Sowa + * Copyright (c) 2022-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -53,7 +53,7 @@ PGModeler::~PGModeler() -bool PGModeler::parse(const std::wstring & input_file, pt::Space & schema) +bool PGModeler::parse(const std::wstring & input_file, pt::Space & schema, bool sort_tables) { schema.clear(); pt::HTMLParser parser; @@ -74,23 +74,77 @@ bool PGModeler::parse(const std::wstring & input_file, pt::Space & schema) } else { - parse(schema); + if( sort_tables ) + make_sort_tables(schema); } return true; } -bool PGModeler::parse(pt::Space & schema) + +std::pair PGModeler::get_schema_table_names(const pt::Space & table) { - std::string tmp; + const pt::Space * schema = table.get_space(L"schema"); + const std::wstring * schema_name = nullptr; + const std::wstring * table_name = nullptr; -// schema.serialize_to_json_to(tmp, true); -// std::cout << tmp << std::endl; + if( schema ) + { + const pt::Space * attr = schema->get_space(L"attr"); - return true; + if( attr ) + { + schema_name = attr->get_wstr(L"name"); + } + } + + const pt::Space * attr = table.get_space(L"attr"); + + if( attr ) + { + table_name = attr->get_wstr(L"name"); + } + + return std::make_pair(schema_name, table_name); } +void PGModeler::make_sort_tables(pt::Space & schema) +{ + pt::Space * dbmodel = schema.get_space(L"dbmodel"); + + if( dbmodel ) + { + pt::Space::TableType * table = dbmodel->get_table(L"table"); + + if( table ) + { + std::sort(table->begin(), table->end(), [](const pt::Space * t1, const pt::Space * t2) -> bool { + auto t1_names = get_schema_table_names(*t1); + auto t2_names = get_schema_table_names(*t2); + + if( t1_names.first && t2_names.second && t2_names.first && t2_names.second ) + { + if( *t1_names.first != *t2_names.first ) + { + return *t1_names.first < *t2_names.first; + } + else + { + return *t1_names.second < *t2_names.second; + } + } + else + { + return false; + } + }); + } + } +} + + + } diff --git a/winixcli/src/pgmodeler.h b/winixcli/src/pgmodeler.h index 33edfd8..1827df5 100644 --- a/winixcli/src/pgmodeler.h +++ b/winixcli/src/pgmodeler.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2022, Tomasz Sowa + * Copyright (c) 2022-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -49,12 +49,14 @@ public: PGModeler(); ~PGModeler(); - bool parse(const std::wstring & input_file, pt::Space & schema); + bool parse(const std::wstring & input_file, pt::Space & schema, bool sort_tables); + private: - bool parse(pt::Space & schema); + void make_sort_tables(pt::Space & schema); + static std::pair get_schema_table_names(const pt::Space & table); };