2023-09-12 17:46:22 +02:00
|
|
|
#pragma once
|
2023-10-23 00:11:03 +02:00
|
|
|
#include <functional>
|
2023-09-12 17:46:22 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include "../macros.hpp"
|
|
|
|
|
|
|
|
class CVarList {
|
|
|
|
public:
|
2023-09-19 10:44:54 +02:00
|
|
|
/** Split string into arg list
|
|
|
|
@param lastArgNo stop splitting after argv reaches maximum size, last arg will contain rest of unsplit args
|
|
|
|
@param delim if delimiter is 's', use std::isspace
|
|
|
|
@param removeEmpty remove empty args from argv
|
|
|
|
*/
|
|
|
|
CVarList(const std::string& in, const size_t maxSize = 0, const char delim = ',', const bool removeEmpty = false);
|
2023-09-12 17:46:22 +02:00
|
|
|
|
|
|
|
~CVarList() = default;
|
|
|
|
|
|
|
|
size_t size() const {
|
|
|
|
return m_vArgs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const;
|
|
|
|
|
2023-10-23 00:11:03 +02:00
|
|
|
void map(std::function<void(std::string&)> func) {
|
|
|
|
for (auto& s : m_vArgs)
|
|
|
|
func(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(const std::string arg) {
|
|
|
|
m_vArgs.emplace_back(arg);
|
|
|
|
}
|
|
|
|
|
2023-09-19 10:44:54 +02:00
|
|
|
std::string operator[](const size_t& idx) const {
|
2023-09-12 17:46:22 +02:00
|
|
|
if (idx >= m_vArgs.size())
|
|
|
|
return "";
|
|
|
|
return m_vArgs[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
// for range-based loops
|
|
|
|
std::vector<std::string>::iterator begin() {
|
|
|
|
return m_vArgs.begin();
|
|
|
|
}
|
|
|
|
std::vector<std::string>::const_iterator begin() const {
|
|
|
|
return m_vArgs.begin();
|
|
|
|
}
|
|
|
|
std::vector<std::string>::iterator end() {
|
|
|
|
return m_vArgs.end();
|
|
|
|
}
|
|
|
|
std::vector<std::string>::const_iterator end() const {
|
|
|
|
return m_vArgs.end();
|
|
|
|
}
|
|
|
|
|
2023-12-24 15:08:48 +01:00
|
|
|
bool contains(const std::string& el) {
|
|
|
|
for (auto& a : m_vArgs) {
|
|
|
|
if (a == el)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:46:22 +02:00
|
|
|
private:
|
|
|
|
std::vector<std::string> m_vArgs;
|
|
|
|
};
|