Rev 1619 | Rev 1627 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1619 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Store.h" |
||
| 22 | |||
| 23 | #include <fstream> |
||
| 24 | #include <stdexcept> |
||
| 25 | |||
| 26 | #include <boost/algorithm/string.hpp> |
||
| 27 | |||
| 28 | #include "type/common.h" |
||
| 29 | |||
| 30 | namespace atom { |
||
| 31 | namespace storage { |
||
| 32 | |||
| 33 | Store::Store(std::string filename) |
||
| 34 | { |
||
| 35 | this->filename_ = filename; |
||
| 36 | this->modified_ = false; |
||
| 37 | |||
| 38 | std::ifstream file(filename.data()); |
||
| 39 | |||
| 40 | if (!file.is_open()) |
||
| 41 | { |
||
| 42 | file.close(); |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | std::string line; |
||
| 47 | size_t pos; |
||
| 48 | |||
| 49 | while (!file.eof()) |
||
| 50 | { |
||
| 51 | std::getline(file, line); |
||
| 52 | |||
| 53 | if (file.eof()) |
||
| 54 | { |
||
| 55 | break; |
||
| 56 | } |
||
| 57 | |||
| 58 | pos = line.find_first_of("="); |
||
| 59 | |||
| 60 | if (pos == std::string::npos) |
||
| 61 | { |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | this->parameters_[line.substr(0, pos)] = line.substr(pos + 1); |
||
| 66 | } |
||
| 67 | |||
| 68 | file.close(); |
||
| 69 | } |
||
| 70 | |||
| 71 | Store::~Store() |
||
| 72 | { |
||
| 73 | |||
| 74 | } |
||
| 75 | |||
| 76 | void Store::Flush() |
||
| 77 | { |
||
| 78 | if (this->modified_) |
||
| 79 | { |
||
| 80 | std::ofstream file(this->filename_.data()); |
||
| 81 | |||
| 82 | if (!file.is_open()) |
||
| 83 | { |
||
| 84 | throw std::runtime_error("Could not open " + this->filename_); |
||
| 85 | } |
||
| 86 | |||
| 87 | for (ParameterList::iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++) |
||
| 88 | { |
||
| 89 | file << it->first << "=" << it->second << "\n"; |
||
| 90 | } |
||
| 91 | |||
| 92 | file.close(); |
||
| 93 | this->modified_ = false; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | Store::ParameterList& Store::GetParameters() |
||
| 98 | { |
||
| 99 | return this->parameters_; |
||
| 100 | } |
||
| 101 | |||
| 102 | std::string Store::GetParameter(std::string name) |
||
| 103 | { |
||
| 104 | ParameterList::iterator it = this->parameters_.find(name); |
||
| 105 | |||
| 106 | if (it == this->parameters_.end()) |
||
| 107 | { |
||
| 108 | throw std::runtime_error("No such parameter"); |
||
| 109 | } |
||
| 110 | |||
| 111 | return it->second; |
||
| 112 | } |
||
| 113 | |||
| 114 | void Store::SetParameter(std::string name, std::string value) |
||
| 115 | { |
||
| 116 | if (this->parameters_[name] != value) |
||
| 117 | { |
||
| 118 | this->parameters_[name] = value; |
||
| 119 | this->modified_ = true; |
||
| 120 | |||
| 121 | this->Flush(); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | }; // namespace storage |
||
| 126 | }; // namespace atom |