parse argv and improve dealing with config files
[scrobby.git] / src / configuration.cpp
blob5d90616546306131a146a2ed7e247f86ff53b5eb
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <cstdlib>
22 #include <cstring>
23 #include <iostream>
24 #include <fstream>
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
30 #include "configuration.h"
31 #include "misc.h"
33 using std::string;
35 namespace
37 std::string GetLineValue(const string &line, char a = '"', char b = '"')
39 int i = 0;
40 int begin = -1, end = -1;
41 for (string::const_iterator it = line.begin(); it != line.end(); i++, it++)
43 if (*it == a || *it == b)
45 if (begin < 0)
46 begin = i+1;
47 else
48 end = i;
51 if (begin >= 0 && end >= 0)
52 return line.substr(begin, end-begin);
53 else
54 return "";
57 LogLevel IntoLogLevel(const string &value)
59 LogLevel result = llInfo;
60 if (value == "none")
62 result = llNone;
64 else if (value == "info")
66 result = llInfo;
68 else if (value == "verbose")
70 result = llVerbose;
72 return result;
77 void ParseArgv(ScrobbyConfig &conf, int argc, char **argv)
79 for (int i = 1; i < argc; i++)
81 if (strcmp(argv[i], "--help") == 0)
83 std::cout
84 << "usage:\n"
85 << "scrobby [options] <conf file>\n"
86 << "scrobby [options] (search for ~/.scrobbyconf, then /etc/scrobby.conf)\n\n"
87 << "options:\n"
88 << " --help show this help message\n"
89 << " --no-daemon do not detach from console\n"
90 << " --quiet do not log anything\n"
91 << " --verbose verbose logging\n"
92 << " --version print version information\n"
94 exit(0);
96 else if (strcmp(argv[i], "--no-daemon") == 0)
98 conf.daemonize = false;
100 else if (strcmp(argv[i], "--quiet") == 0)
102 conf.log_level = llNone;
104 else if (strcmp(argv[i], "--verbose") == 0)
106 conf.log_level = llVerbose;
108 else if (strcmp(argv[i], "--version") == 0)
110 std::cout << "scrobby - an audioscrobbler mpd client, version "VERSION"\n";
111 exit(0);
113 else
115 conf.file_config = argv[i];
120 bool CheckFiles(ScrobbyConfig &conf)
122 std::ofstream f;
123 std::ifstream g;
125 g.open(conf.file_pid.c_str());
126 if (g.is_open())
128 string strpid;
129 getline(g, strpid);
130 g.close();
131 pid_t pid = StrToInt(strpid);
132 if (pid < 1)
134 std::cerr << "pid file: " << conf.file_pid << " is invalid, trying to remove...\n";
135 if (unlink(conf.file_pid.c_str()) == 0)
137 std::cout << "pid file succesfully removed.\n";
139 else
141 std::cerr << "couldn't remove pid file.\n";
142 return false;
145 else
147 std::cerr << "scrobby is already running with PID " << pid << "!\n";
148 return false;
152 f.open(conf.file_pid.c_str(), std::ios_base::app);
153 if (!f.is_open())
155 std::cerr << "Cannot create/open pid file: " << conf.file_pid << std::endl;
156 return false;
158 f.close();
160 f.open(conf.file_log.c_str(), std::ios_base::app);
161 if (!f.is_open())
163 std::cerr << "Cannot create/open log file: " << conf.file_log << std::endl;
164 return false;
166 f.close();
168 f.open(conf.file_cache.c_str(), std::ios_base::app);
169 if (!f.is_open())
171 std::cerr << "Cannot create/open cache file: " << conf.file_cache << std::endl;
172 return false;
174 f.close();
176 return true;
179 void DefaultConfiguration(ScrobbyConfig &conf)
181 conf.mpd_host = "localhost";
182 conf.mpd_port = 6600;
183 conf.mpd_timeout = 15;
185 conf.file_log = "/var/log/scrobby.log";
186 conf.file_pid = "/var/run/scrobby.pid";
187 conf.file_cache = "/var/cache/scrobby/scrobby.cache";
189 conf.log_level = llUndefined;
190 conf.daemonize = true;
193 bool ReadConfiguration(ScrobbyConfig &conf, const string &file)
195 string line, v;
196 std::ifstream f(file.c_str());
198 if (!f.is_open())
199 return false;
201 while (!f.eof())
203 getline(f, line);
204 if (!line.empty() && line[0] != '#')
206 v = GetLineValue(line);
208 if (line.find("mpd_host") != string::npos)
210 if (!line.empty())
211 conf.mpd_host = v;
213 else if (line.find("mpd_password") != string::npos)
215 if (!v.empty())
216 conf.mpd_password = v;
218 else if (line.find("mpd_port") != string::npos)
220 if (!v.empty())
221 conf.mpd_port = StrToInt(v);
223 else if (line.find("mpd_timeout") != string::npos)
225 if (!v.empty())
226 conf.mpd_timeout = StrToInt(v);
228 else if (line.find("log_file") != string::npos)
230 if (!v.empty())
231 conf.file_log = v;
233 else if (line.find("pid_file") != string::npos)
235 if (!v.empty())
236 conf.file_pid = v;
238 else if (line.find("cache_file") != string::npos)
240 if (!v.empty())
241 conf.file_cache = v;
243 else if (line.find("lastfm_user") != string::npos)
245 if (!v.empty())
246 conf.lastfm_user = v;
248 else if (line.find("lastfm_password") != string::npos)
250 if (!v.empty())
251 conf.lastfm_password = v;
253 else if (line.find("lastfm_md5_password") != string::npos)
255 if (!v.empty())
256 conf.lastfm_md5_password = v;
258 else if (line.find("log_level") != string::npos)
260 if (!v.empty() && conf.log_level == llUndefined)
261 conf.log_level = IntoLogLevel(v);
265 f.close();
266 return true;