This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
Isotope-Toolbox/SimpleIniParser/source/SimpleIniParser/IniOption.cpp
2020-06-15 22:32:49 -05:00

58 lines
2.1 KiB
C++

/*
* SimpleIniParser
* Copyright (c) 2019 Nichole Mattera
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "IniOption.hpp"
#include "IniStringHelper.hpp"
using namespace std;
namespace simpleIniParser {
IniOption::IniOption(IniOptionType t, string k, string v) {
type = t;
key = k;
value = v;
}
string IniOption::build() {
switch (type) {
case IniOptionType::SemicolonComment:
return "; " + value + "\n";
case IniOptionType::HashtagComment:
return "# " + value + "\n";
default:
return key + "=" + value + "\n";
}
}
IniOption * IniOption::parse(string line) {
if (line.at(0) == ';') {
return new IniOption(IniOptionType::SemicolonComment, "", IniStringHelper::trim_copy(line.substr(1, line.size() - 1)));
} else if (line.at(0) == '#') {
return new IniOption(IniOptionType::HashtagComment, "", IniStringHelper::trim_copy(line.substr(1, line.size() - 1)));
} else {
size_t pos = line.find('=');
if (pos != string::npos && pos > 0) {
return new IniOption(IniOptionType::Option, IniStringHelper::rtrim_copy(line.substr(0, pos)), IniStringHelper::ltrim_copy(line.substr(pos + 1)));
} else {
return nullptr;
}
}
}
}