X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fsettings.cc;fp=src%2Fmoof%2Fsettings.cc;h=4e026929dd779c8dfbffaa0dfa98b26f15f6a928;hp=0000000000000000000000000000000000000000;hb=831f04d4bc19a390415ac0bbac4331c7a65509bc;hpb=299af4f2047e767e5d79501c26444473bda64c64 diff --git a/src/moof/settings.cc b/src/moof/settings.cc new file mode 100644 index 0000000..4e02692 --- /dev/null +++ b/src/moof/settings.cc @@ -0,0 +1,117 @@ + +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include // getenv + +#include "log.hh" +#include "settings.hh" + + +namespace moof { + + +settings::settings(int argc, char* argv[], const std::string& path) +{ + script_.import_base_library(); + log::import(script_); + + parse_args(argc, argv); + load_files(path); +} + +settings::~settings() +{ + save(); +} + + +void settings::parse_args(int argc, char* argv[]) +{ + for (int i = 1; i < argc; ++i) + { + if (script_.do_string(argv[i]) != script::success) + { + std::string str; + script_[-1].get(str); + log_warning << "invalid option: " << argv[i] + << ": " << str << std::endl; + script_.clear_stack(); + } + } +} + + +void settings::load_files(const std::string& path) +{ + std::vector paths; + boost::split(paths, path, boost::is_any_of(":")); + + load_files(paths); +} + +void settings::load_files(const std::vector& path) +{ + std::vector copy(path); + std::vector::iterator it; + +#if defined(_WIN32) + char* homeDrive = getenv("HOMEDRIVE"); + char* homePath = getenv("HOMEPATH"); + std::string home(homeDrive ? homeDrive : ""); + if (homePath) home += homePath; +#else + char *homePath = getenv("HOME"); + std::string home(homePath ? homePath : ""); +#endif + + for (it = copy.begin(); it != copy.end(); ++it) + { + if (!home.empty()) + { + boost::replace_all(*it, "$HOME", home); + + //logDebug("Copying global settings..."); + //mUserFile = *it; + //mGlobals.push_copy(); + //script_.set("globals", script::registry_index); + } + + if (script_.do_file(*it) != script::success) + { + std::string str; + script_[-1].get(str); + log_warning(str); + script_.clear_stack(); + } + } +} + + +void settings::clear() +{ + script_.reset(); +} + + +void settings::save_as(const std::string& path) +{ + mUserFile = path; + save(); +} + +void settings::save() const +{ + // TODO saving settings not yet implemented +} + + +} // namepsace moof +