splitStringIntoVector now takes various length separators

This commit is contained in:
matthias@arch 2022-12-15 19:01:35 +01:00
parent 9b16c645ac
commit 6dbd4c7ced
5 changed files with 43 additions and 22 deletions

View File

@ -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

View File

@ -17,12 +17,12 @@ DEPENDS = ${OBJECTS:.o=.d}
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,6 +63,13 @@ $(OBJECT_DIR)/%.stamp: %.hpp $(OBJECT_DIR)
@touch $@
@chmod 777 $@
#
# TESTING
#
test: CXXFLAGS += -DGZ_UTIL_TESTING
test: default
#
# EXTRAS
#
@ -70,5 +78,6 @@ clean:
-rm -rf $(OBJECT_DIR)
-rm $(LIB)
docs:
doxygen .doxygen_config

View File

@ -102,7 +102,9 @@ namespace gz {
Log::Log(std::shared_ptr<LogResources>&& 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";
/* file << message; */
if (showLog) { std::cout << message; }
std::cout << message;
}
}
else {
std::cout << COLORS[RED] << "LOG ERROR: " << COLORS[RESET] << "Could not open file '" << logFile() << "'." << '\n';
}
file.close();
}
} // namespace gz

View File

@ -7,9 +7,7 @@
#include <iostream>
#include <string>
#ifndef LOG_NO_SUBLOGS
#define LOG_SUBLOGS
#endif
#ifdef LOG_SUBLOGS
#include <memory>
@ -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<LogResources>&& 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<std::string>& 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;

View File

@ -39,7 +39,7 @@ std::vector<T> 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