]> Dogcows Code - chaz/yoink/blob - src/moof/settings.cc
the massive refactoring effort
[chaz/yoink] / src / moof / settings.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <cstdlib> // getenv
13
14 #include "log.hh"
15 #include "settings.hh"
16
17
18 namespace moof {
19
20
21 settings::settings(int argc, char* argv[], const std::string& path)
22 {
23 script_.import_base_library();
24 log::import(script_);
25
26 parse_args(argc, argv);
27 load_files(path);
28 }
29
30 settings::~settings()
31 {
32 save();
33 }
34
35
36 void settings::parse_args(int argc, char* argv[])
37 {
38 for (int i = 1; i < argc; ++i)
39 {
40 if (script_.do_string(argv[i]) != script::success)
41 {
42 std::string str;
43 script_[-1].get(str);
44 log_warning << "invalid option: " << argv[i]
45 << ": " << str << std::endl;
46 script_.clear_stack();
47 }
48 }
49 }
50
51
52 void settings::load_files(const std::string& path)
53 {
54 std::vector<std::string> paths;
55 boost::split(paths, path, boost::is_any_of(":"));
56
57 load_files(paths);
58 }
59
60 void settings::load_files(const std::vector<std::string>& path)
61 {
62 std::vector<std::string> copy(path);
63 std::vector<std::string>::iterator it;
64
65 #if defined(_WIN32)
66 char* homeDrive = getenv("HOMEDRIVE");
67 char* homePath = getenv("HOMEPATH");
68 std::string home(homeDrive ? homeDrive : "");
69 if (homePath) home += homePath;
70 #else
71 char *homePath = getenv("HOME");
72 std::string home(homePath ? homePath : "");
73 #endif
74
75 for (it = copy.begin(); it != copy.end(); ++it)
76 {
77 if (!home.empty())
78 {
79 boost::replace_all(*it, "$HOME", home);
80
81 //logDebug("Copying global settings...");
82 //mUserFile = *it;
83 //mGlobals.push_copy();
84 //script_.set("globals", script::registry_index);
85 }
86
87 if (script_.do_file(*it) != script::success)
88 {
89 std::string str;
90 script_[-1].get(str);
91 log_warning(str);
92 script_.clear_stack();
93 }
94 }
95 }
96
97
98 void settings::clear()
99 {
100 script_.reset();
101 }
102
103
104 void settings::save_as(const std::string& path)
105 {
106 mUserFile = path;
107 save();
108 }
109
110 void settings::save() const
111 {
112 // TODO saving settings not yet implemented
113 }
114
115
116 } // namepsace moof
117
This page took 0.035313 seconds and 4 git commands to generate.