void KeyValue::process_line(std::string line, const std::string& filename, ZipReader *zip) throw (KeyValueException) { hash_value += get_hash_of_string(line.c_str()); line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); if (line.length()) { size_t pos = line.find('='); if (pos != std::string::npos) { std::string key = line.substr(0, pos); std::string value = line.substr(pos + 1); if (key == "include") { size_t pos = filename.rfind('.'); if (pos != std::string::npos) { std::string suffix = filename.substr(pos); pos = filename.rfind('/'); std::string prefix; if (pos != std::string::npos) { prefix = filename.substr(0, pos + 1); } std::string new_filename = prefix + value + suffix; read(new_filename, zip); } } else { entries[key] = value; } } else { throw KeyValueException("Malformed expression in key value file: " + filename); } } }
void Properties::reload_configuration() throw (KeyValueException) { if (!filename.length()) { throw KeyValueException("No filename specified"); } clear(); read(filename); fetch(); }
void KeyValue::read(const std::string& filename, ZipReader *zip) throw (KeyValueException) { depth_counter++; if (depth_counter > 128) { throw KeyValueException("Too many nested includes: " + filename); } if (!zip) { /* regular file */ std::ifstream f(filename.c_str()); if (f.is_open()) { std::string line; while (getline(f, line)) { process_line(line, filename, zip); } } else { throw KeyValueException("Can't open key value file: " + filename); } } else { /* zip file */ size_t sz; try { const char *data = zip->extract(filename, &sz); const char *dptr = data; std::string line; while (sz) { if (*dptr != '\r') { if (*dptr == '\n') { process_line(line, filename, zip); line.clear(); } else { line += *dptr; } } dptr++; sz--; } zip->destroy(data); } catch (const ZipReaderException& e) { throw KeyValueException(e.what()); } } depth_counter--; }
void KeyValue::save(const std::string& filename) throw (KeyValueException) { modify_zip_file_test(); std::ofstream f(filename.c_str()); if (f.is_open()) { for (Entries::iterator it = entries.begin(); it != entries.end(); it++) { f << it->first << "=" << it->second << std::endl; } } else { throw KeyValueException("Write failed: " + std::string(strerror(errno))); } modified = false; }
void KeyValue::modify_zip_file_test() throw (KeyValueException) { if (is_zip_file()) { throw KeyValueException("A packaged file can't be modified."); } }
void Properties::save_configuration() throw (KeyValueException) { if (!filename.length()) { throw KeyValueException("No filename specified"); } save(filename); }