From 6dbd4c7cedbd058e9072f71b03bebb5120b16cce Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Thu, 15 Dec 2022 19:01:35 +0100 Subject: [PATCH] splitStringIntoVector now takes various length separators --- src/.extranous_documentation.hpp | 8 ++++++-- src/Makefile | 17 +++++++++++++---- src/log.cpp | 21 +++++++++++++-------- src/log.hpp | 17 ++++++++++------- src/string/utility.cpp | 2 +- 5 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/.extranous_documentation.hpp b/src/.extranous_documentation.hpp index 127c9ad..2eabb3a 100644 --- a/src/.extranous_documentation.hpp +++ b/src/.extranous_documentation.hpp @@ -21,11 +21,15 @@ namespace gz { * * @section main_installation Installation * @subsection main_i_linux Linux - * - Make a clone of this repo: `git clone --recursive https://github.com/MatthiasQuintern/gz-cpp-util` - * - Build and install: `cd src && make && make install` + * - Make a clone of this repo: `git clone https://github.com/MatthiasQuintern/gz-cpp-util` + * - Build and install: `cd src && make && make DESTDIR=/usr/local install` * @subsection main_i_arch Arch Linux (ABS) * - Download PKGBUILD: `wget https://raw.github.com/MatthiasQuintern/gz-cpp-util/main/PKGBUILD` * - Build and install with the Arch Build System: `makepkg -si` + * @subsection main_i_windows Windows + * I do not delevop on windows, but you'll figure it out. + * The only platform specific code is the color support of the logger, which wont work properly on windows (I will address that in the future). + * It should compile with mvsc. * * @subsection main_i_usage Usage * - Add `-lgzutil` to your linker flags diff --git a/src/Makefile b/src/Makefile index a0bc5ae..71e3ad8 100755 --- a/src/Makefile +++ b/src/Makefile @@ -15,14 +15,14 @@ OBJECTS = $($(notdir SRC):%.cpp=$(OBJECT_DIR)/%.o) OBJECT_DIRS = $(OBJECT_DIR) $(foreach dir,$(SRCDIRS), $(OBJECT_DIR)/$(dir)) DEPENDS = ${OBJECTS:.o=.d} -CXXFLAGS += $(IFLAGS) +CXXFLAGS += $(IFLAGS) -.PHONY: install debug run clean docs +.PHONY: install debug run clean docs test # # BUILDING # default: $(LIB) - echo $(OBJECTS) + @echo $(OBJECTS) # with debug flags debug: CXXFLAGS += -g # -DDEBUG @@ -47,6 +47,7 @@ install: $(LIB) $(HEADER_INST) @{ [ -z "$(DESTDIR)" ] && echo "Please set the DESTDIR variable (probably to /usr or /usr/local)" && exit 1; } || true install -D -m 755 $< $(DESTDIR)/lib/$(subst ../,,$<) install -D -m 755 ../gen_enum_str.py $(DESTDIR)/bin/gen-enum-str + -rm $(HEADER_INST) uninstall: @{ [ -z "$(DESTDIR)" ] && echo "Please set the DESTDIR variable (probably to /usr or /usr/local)" && exit 1; } || true @@ -62,13 +63,21 @@ $(OBJECT_DIR)/%.stamp: %.hpp $(OBJECT_DIR) @touch $@ @chmod 777 $@ +# +# TESTING +# +test: CXXFLAGS += -DGZ_UTIL_TESTING +test: default + + # # EXTRAS # # remove all object and dependecy files -clean: +clean: -rm -rf $(OBJECT_DIR) -rm $(LIB) + docs: doxygen .doxygen_config diff --git a/src/log.cpp b/src/log.cpp index c0e4387..e5ddeae 100755 --- a/src/log.cpp +++ b/src/log.cpp @@ -102,7 +102,9 @@ namespace gz { Log::Log(std::shared_ptr&& resources_, bool showLog, const std::string& prefix, Color prefixColor) : resources(std::move(resources_)), showLog(showLog), prefixColor(prefixColor), prefix(prefix) { - + if (!this->prefix.empty()) { + this->prefix += ": "; + } } #endif @@ -160,18 +162,21 @@ namespace gz { void Log::writeLog() { std::ofstream file(logFile(), std::ios_base::app); if (file.is_open()) { - for (std::string message : logLines()) { - file << message; + for (size_t i = 0; i < iter(); i++) { + file << logLines()[i]; + } + iter() = 0; + if (showLog) { + getTime(); + std::string message = time(); + message += "Written log to file: " + logFile() + "\n"; + std::cout << message; } - getTime(); - std::string message = time(); - message += "Written log to file: " + logFile() + "\n"; - /* file << message; */ - if (showLog) { std::cout << message; } } else { std::cout << COLORS[RED] << "LOG ERROR: " << COLORS[RESET] << "Could not open file '" << logFile() << "'." << '\n'; } file.close(); } + } // namespace gz diff --git a/src/log.hpp b/src/log.hpp index 2fe20aa..0f66f51 100755 --- a/src/log.hpp +++ b/src/log.hpp @@ -7,9 +7,7 @@ #include #include -#ifndef LOG_NO_SUBLOGS #define LOG_SUBLOGS -#endif #ifdef LOG_SUBLOGS #include @@ -182,7 +180,7 @@ class Log { */ Log(LogCreateInfo&& createInfo); #ifdef LOG_SUBLOGS - Log createSublog(bool showLog, const std::string& prefix, Color prefixColor); + Log createSublog(bool showLog, const std::string& prefix, Color prefixColor=gz::Color::RESET); private: Log(std::shared_ptr&& resources, bool showLog, const std::string& prefix, Color prefixColor); public: @@ -355,7 +353,7 @@ class Log { bool showTime_; Color timeColor_; /// Stores the current time in yyyy-mm-dd hh:mm:ss format - char[LOG_TIMESTAMP_CHAR_COUNT] time_; + char time_[LOG_TIMESTAMP_CHAR_COUNT]; // getters std::vector& logLines() { return logLines_; }; @@ -369,8 +367,13 @@ class Log { bool& showTime() { return showTime_; }; Color& timeColor() { return timeColor_; }; - std::string& time() { return time_; }; + char* time() { return time_; }; #endif + /** + * @brief Write the log to the logfile + * @details + * Opens the file in append mode and writes the strings in logLines from 0 to iter to it. Sets iter to 0. + */ void writeLog(); bool showLog; @@ -401,7 +404,7 @@ class Log { logLines()[iter()] = time(); } else { - logLines().clear(); + logLines()[iter()].clear(); } argsBegin().emplace_back(logLines()[iter()].size()); logLines()[iter()] += prefix; @@ -439,7 +442,7 @@ class Log { logLines()[iter()] = std::string(time()); } else { - logLines().clear(); + logLines()[iter()].clear(); } argsBegin().emplace_back(logLines()[iter()].size()); logLines()[iter()] += prefix; diff --git a/src/string/utility.cpp b/src/string/utility.cpp index e387fe2..187213a 100644 --- a/src/string/utility.cpp +++ b/src/string/utility.cpp @@ -39,7 +39,7 @@ std::vector splitStringInVector(const std::string_view& s, const std::string& if (!(skipEmptyStrings and posStart == posEnd)) { v.emplace_back(T(s.begin() + posStart, s.begin() + posEnd)); } - posStart = posEnd + 1; + posStart = posEnd + separator.size(); posEnd = s.find(separator, posStart); } // last element