util: simplify string splitting

This commit is contained in:
Robert Swiecki 2018-02-20 14:16:28 +01:00
parent 70b9565250
commit c4a7af980f
3 changed files with 33 additions and 30 deletions

View File

@ -294,12 +294,7 @@ uint64_t parseRLimit(int res, const char* optarg, unsigned long mul) {
return val; return val;
} }
static std::string argByColon(const char* str, size_t pos) { static std::string argFromVec(const std::vector<std::string>& vec, size_t pos) {
if (!str) {
return "";
}
std::vector<std::string> vec;
util::strSplit(str, &vec, ':');
if (pos >= vec.size()) { if (pos >= vec.size()) {
return ""; return "";
} }
@ -635,9 +630,10 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
nsjconf->envs.push_back(optarg); nsjconf->envs.push_back(optarg);
break; break;
case 'u': { case 'u': {
std::string i_id = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string o_id = argByColon(optarg, 1); std::string i_id = argFromVec(subopts, 0);
std::string cnt = argByColon(optarg, 2); std::string o_id = argFromVec(subopts, 1);
std::string cnt = argFromVec(subopts, 2);
size_t count = strtoul(cnt.c_str(), nullptr, 0); size_t count = strtoul(cnt.c_str(), nullptr, 0);
if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ false, if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ false,
/* is_newidmap= */ false)) { /* is_newidmap= */ false)) {
@ -645,9 +641,10 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
} }
} break; } break;
case 'g': { case 'g': {
std::string i_id = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string o_id = argByColon(optarg, 1); std::string i_id = argFromVec(subopts, 0);
std::string cnt = argByColon(optarg, 2); std::string o_id = argFromVec(subopts, 1);
std::string cnt = argFromVec(subopts, 2);
size_t count = strtoul(cnt.c_str(), nullptr, 0); size_t count = strtoul(cnt.c_str(), nullptr, 0);
if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ true, if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ true,
/* is_newidmap= */ false)) { /* is_newidmap= */ false)) {
@ -655,9 +652,10 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
} }
} break; } break;
case 'U': { case 'U': {
std::string i_id = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string o_id = argByColon(optarg, 1); std::string i_id = argFromVec(subopts, 0);
std::string cnt = argByColon(optarg, 2); std::string o_id = argFromVec(subopts, 1);
std::string cnt = argFromVec(subopts, 2);
size_t count = strtoul(cnt.c_str(), nullptr, 0); size_t count = strtoul(cnt.c_str(), nullptr, 0);
if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ false, if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ false,
/* is_newidmap= */ true)) { /* is_newidmap= */ true)) {
@ -665,9 +663,10 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
} }
} break; } break;
case 'G': { case 'G': {
std::string i_id = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string o_id = argByColon(optarg, 1); std::string i_id = argFromVec(subopts, 0);
std::string cnt = argByColon(optarg, 2); std::string o_id = argFromVec(subopts, 1);
std::string cnt = argFromVec(subopts, 2);
size_t count = strtoul(cnt.c_str(), nullptr, 0); size_t count = strtoul(cnt.c_str(), nullptr, 0);
if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ true, if (!user::parseId(nsjconf.get(), i_id, o_id, count, /* is_gid= */ true,
/* is_newidmap= */ true)) { /* is_newidmap= */ true)) {
@ -675,8 +674,9 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
} }
} break; } break;
case 'R': { case 'R': {
std::string src = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string dst = argByColon(optarg, 1); std::string src = argFromVec(subopts, 0);
std::string dst = argFromVec(subopts, 1);
if (dst.empty()) { if (dst.empty()) {
dst = src; dst = src;
} }
@ -689,8 +689,9 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
} }
}; break; }; break;
case 'B': { case 'B': {
std::string src = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string dst = argByColon(optarg, 1); std::string src = argFromVec(subopts, 0);
std::string dst = argFromVec(subopts, 1);
if (dst.empty()) { if (dst.empty()) {
dst = src; dst = src;
} }
@ -712,13 +713,14 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
} }
}; break; }; break;
case 'm': { case 'm': {
std::string src = argByColon(optarg, 0); std::vector<std::string> subopts = util::strSplit(optarg, ':');
std::string dst = argByColon(optarg, 1); std::string src = argFromVec(subopts, 0);
std::string dst = argFromVec(subopts, 1);
if (dst.empty()) { if (dst.empty()) {
dst = src; dst = src;
} }
std::string fs_type = argByColon(optarg, 2); std::string fs_type = argFromVec(subopts, 2);
std::string options = argByColon(optarg, 3); std::string options = argFromVec(subopts, 3);
if (!mnt::addMountPtTail(nsjconf.get(), src, dst, /* fs_type= */ fs_type, if (!mnt::addMountPtTail(nsjconf.get(), src, dst, /* fs_type= */ fs_type,
/* options= */ options, /* flags= */ 0, /* options= */ options, /* flags= */ 0,
/* is_dir= */ mnt::NS_DIR_MAYBE, /* is_mandatory= */ true, /* is_dir= */ mnt::NS_DIR_MAYBE, /* is_mandatory= */ true,

View File

@ -283,12 +283,13 @@ const std::string timeToStr(time_t t) {
return timestr; return timestr;
} }
void strSplit(const std::string str, std::vector<std::string>* vec, char delim) { std::vector<std::string> strSplit(const std::string str, char delim) {
std::string word; std::vector<std::string> vec;
std::istringstream stream(str); std::istringstream stream(str);
for (std::string word; std::getline(stream, word, delim);) { for (std::string word; std::getline(stream, word, delim);) {
vec->push_back(word); vec.push_back(word);
} }
return vec;
} }
} // namespace util } // namespace util

2
util.h
View File

@ -43,7 +43,7 @@ bool isANumber(const char* s);
uint64_t rnd64(void); uint64_t rnd64(void);
const std::string sigName(int signo); const std::string sigName(int signo);
const std::string timeToStr(time_t t); const std::string timeToStr(time_t t);
void strSplit(const std::string str, std::vector<std::string>* vec, char delim); std::vector<std::string> strSplit(const std::string str, char delim);
} // namespace util } // namespace util