| 1 | /* |
| 2 | * File: ConfigLine.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | #include "ConfigLine.h" |
| 25 | #include "HerculesStudio.h" |
| 26 | |
| 27 | #include <iostream> |
| 28 | #include <algorithm> |
| 29 | |
| 30 | ConfigLine::ConfigLine(const char *line) : |
| 31 | mLine(line), mRemark(false), mDeleted(false), mUpdated(false), mNew(false), mInError(false), mTokens() |
| 32 | { |
| 33 | if (mLine.find_first_of("\n") == std::string::npos) |
| 34 | mLine = mLine + "\n"; |
| 35 | parseLine(); |
| 36 | } |
| 37 | |
| 38 | ConfigLine::~ConfigLine() |
| 39 | { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | const std::string& ConfigLine::getLine() const |
| 44 | { |
| 45 | return mLine; |
| 46 | } |
| 47 | |
| 48 | std::string ConfigLine::getToken(int i) const |
| 49 | { |
| 50 | if (i >= mSize) return ""; |
| 51 | std::string result = mLine.substr(mTokens[i].first, mTokens[i].second ) ; |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | std::string ConfigLine::getLowercaseToken(int i) const |
| 56 | { |
| 57 | std::string result = getToken(i); |
| 58 | std::transform(result.begin(), result.end(), result.begin(), ::tolower); |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | std::string ConfigLine::getUppercaseToken(int i) const |
| 63 | { |
| 64 | std::string result = getToken(i); |
| 65 | std::transform(result.begin(), result.end(), result.begin(), ::toupper); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | std::string ConfigLine::getMultiToken(int from, int to) const |
| 70 | { |
| 71 | if (from >= mSize) return ""; |
| 72 | if (to >= mSize || to == 0) to = mSize-1; |
| 73 | |
| 74 | return mLine.substr(mTokens[from].first, mTokens[to].first+mTokens[to].second-mTokens[from].first ) ; |
| 75 | } |
| 76 | |
| 77 | int ConfigLine::getAbsoluteTokenColumn(int i) const |
| 78 | { |
| 79 | if (i >= mSize) return -1; |
| 80 | int result = mTokens[i].first; |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | int ConfigLine::getAbsoluteTokenEnd(int i) const |
| 85 | { |
| 86 | if (i >= mSize) return -1; |
| 87 | int result = mTokens[i].second; |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | void ConfigLine::replaceLine(const std::string& newLine) |
| 92 | { |
| 93 | hOutDebug(5,"configLine: new Line '" << newLine << "'" << std::endl); |
| 94 | hOutDebug(5,"configLine: old Line '" << mLine << "'" << std::endl); |
| 95 | mLine = newLine; |
| 96 | mUpdated = true; |
| 97 | parseLine(); |
| 98 | } |
| 99 | |
| 100 | void ConfigLine::replaceParameters(const std::string& newParm) |
| 101 | { |
| 102 | std::string newString = " " + getToken(0) + " " + newParm + " #edited By Hercules Studio\n"; |
| 103 | hOutDebug(5,"configLine: line is now '" << newString << "'" << std::endl); |
| 104 | hOutDebug(5,"configLine: old string '" << mLine << "'" << std::endl); |
| 105 | mLine = newString; |
| 106 | mUpdated = true; |
| 107 | mNew = false; |
| 108 | parseLine(); |
| 109 | } |
| 110 | |
| 111 | void ConfigLine::parseLine() |
| 112 | { |
| 113 | tokenize(" \t\n\r"); |
| 114 | mRemark = false; |
| 115 | if (mTokens.size() > 0 && |
| 116 | mLine[mTokens[0].first] == '#') |
| 117 | { |
| 118 | mRemark = true; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | mKeyStart=mLine.find_first_not_of(" \t\n\r"); |
| 123 | if (mKeyStart == std::string::npos) // blank line |
| 124 | { |
| 125 | mRemark = true; |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void ConfigLine::tokenize(const std::string& delimiters) |
| 131 | { |
| 132 | std::string::size_type lastPos = mLine.find_first_not_of(delimiters, 0); |
| 133 | std::string::size_type pos = mLine.find_first_of(delimiters, lastPos); |
| 134 | mTokens.clear(); |
| 135 | mSize = 0; |
| 136 | |
| 137 | while (std::string::npos != pos || std::string::npos != lastPos) |
| 138 | { |
| 139 | if (pos == std::string::npos) pos = mLine.length() - lastPos; |
| 140 | TokenPair newToken(lastPos, pos - lastPos); |
| 141 | mTokens.push_back(newToken); |
| 142 | mSize++; |
| 143 | lastPos = mLine.find_first_not_of(delimiters, pos); |
| 144 | pos = mLine.find_first_of(delimiters, lastPos); |
| 145 | if (lastPos != std::string::npos && mLine[lastPos] == '#') break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void ConfigLine::setDeleted(bool parm) |
| 150 | { |
| 151 | mDeleted = parm; |
| 152 | } |