Rev 1627 | 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 | |||
| 1642 | runge | 28 | #include "common/common.h" |
| 1619 | runge | 29 | |
| 30 | namespace atom { |
||
| 31 | namespace storage { |
||
| 32 | |||
| 33 | Store::Store(std::string filename) |
||
| 34 | { |
||
| 35 | this->filename_ = filename; |
||
| 36 | this->modified_ = false; |
||
| 1627 | runge | 37 | this->flush_policy = FLUSH_INSTANT; |
| 1619 | runge | 38 | |
| 39 | std::ifstream file(filename.data()); |
||
| 40 | |||
| 41 | if (!file.is_open()) |
||
| 42 | { |
||
| 43 | file.close(); |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | std::string line; |
||
| 48 | size_t pos; |
||
| 49 | |||
| 50 | while (!file.eof()) |
||
| 51 | { |
||
| 52 | std::getline(file, line); |
||
| 53 | |||
| 54 | if (file.eof()) |
||
| 55 | { |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | |||
| 59 | pos = line.find_first_of("="); |
||
| 60 | |||
| 61 | if (pos == std::string::npos) |
||
| 62 | { |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | this->parameters_[line.substr(0, pos)] = line.substr(pos + 1); |
||
| 67 | } |
||
| 68 | |||
| 69 | file.close(); |
||
| 70 | } |
||
| 71 | |||
| 72 | Store::~Store() |
||
| 73 | { |
||
| 74 | |||
| 75 | } |
||
| 76 | |||
| 1627 | runge | 77 | void Store::SetFlushPolicy(Store::FlushPolicy flush_policy) |
| 78 | { |
||
| 79 | this->flush_policy = flush_policy; |
||
| 80 | } |
||
| 81 | |||
| 1619 | runge | 82 | void Store::Flush() |
| 83 | { |
||
| 84 | if (this->modified_) |
||
| 85 | { |
||
| 86 | std::ofstream file(this->filename_.data()); |
||
| 87 | |||
| 88 | if (!file.is_open()) |
||
| 89 | { |
||
| 90 | throw std::runtime_error("Could not open " + this->filename_); |
||
| 91 | } |
||
| 92 | |||
| 93 | for (ParameterList::iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++) |
||
| 94 | { |
||
| 95 | file << it->first << "=" << it->second << "\n"; |
||
| 96 | } |
||
| 97 | |||
| 98 | file.close(); |
||
| 99 | this->modified_ = false; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | Store::ParameterList& Store::GetParameters() |
||
| 104 | { |
||
| 105 | return this->parameters_; |
||
| 106 | } |
||
| 107 | |||
| 108 | std::string Store::GetParameter(std::string name) |
||
| 109 | { |
||
| 110 | ParameterList::iterator it = this->parameters_.find(name); |
||
| 111 | |||
| 112 | if (it == this->parameters_.end()) |
||
| 113 | { |
||
| 114 | throw std::runtime_error("No such parameter"); |
||
| 115 | } |
||
| 116 | |||
| 117 | return it->second; |
||
| 118 | } |
||
| 119 | |||
| 120 | void Store::SetParameter(std::string name, std::string value) |
||
| 121 | { |
||
| 122 | if (this->parameters_[name] != value) |
||
| 123 | { |
||
| 1627 | runge | 124 | if (value == "") |
| 125 | { |
||
| 126 | this->parameters_.erase(name); |
||
| 127 | } |
||
| 128 | else |
||
| 129 | { |
||
| 130 | this->parameters_[name] = value; |
||
| 131 | } |
||
| 132 | |||
| 1619 | runge | 133 | this->modified_ = true; |
| 134 | |||
| 1627 | runge | 135 | if (this->flush_policy == FLUSH_INSTANT) |
| 136 | { |
||
| 137 | this->Flush(); |
||
| 138 | } |
||
| 1619 | runge | 139 | } |
| 140 | } |
||
| 141 | |||
| 142 | }; // namespace storage |
||
| 143 | }; // namespace atom |