// ---------------------------------------------------------------------- // File: ParsingUtils.hh // Author: Georgios Bitzes - CERN // ---------------------------------------------------------------------- /************************************************************************ * quarkdb - a redis-like highly available key-value store * * Copyright (C) 2016 CERN/Switzerland * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* ************************************************************************/ #ifndef QUARKDB_PARSING_UTILS_HH #define QUARKDB_PARSING_UTILS_HH #include "Utils.hh" #include namespace quarkdb { namespace ParseUtils { inline std::vector split(std::string data, const std::string &token) { std::vector output; size_t pos = std::string::npos; do { pos = data.find(token); output.push_back(data.substr(0, pos)); if(std::string::npos != pos) { data = data.substr(pos + token.size()); } } while (std::string::npos != pos); return output; } inline bool parseInt64(std::string_view str, int64_t &ret) { char *endptr = NULL; ret = strtoll(str.data(), &endptr, 10); if(endptr != str.data() + str.size() || ret == LLONG_MIN || ret == LONG_LONG_MAX) { return false; } return true; } inline bool parseIntegerList(const std::string &buffer, const std::string &separator, std::vector &results) { results.clear(); std::vector items = ParseUtils::split(buffer, separator); for(size_t i = 0; i < items.size(); i++) { int64_t value; if(!ParseUtils::parseInt64(items[i], value)) { return false; } results.emplace_back(value); } return true; } } } #endif