initial commit
[scrobby.git] / src / configuration.cpp
blob7000694b69eb4602f7756208607a288553b95aec
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 <iostream>
22 #include <fstream>
24 #include "configuration.h"
25 #include "misc.h"
27 using std::string;
29 namespace
31 std::string GetLineValue(const string &line, char a = '"', char b = '"')
33 int i = 0;
34 int begin = -1, end = -1;
35 for (string::const_iterator it = line.begin(); it != line.end(); i++, it++)
37 if (*it == a || *it == b)
39 if (begin < 0)
40 begin = i+1;
41 else
42 end = i;
45 if (begin >= 0 && end >= 0)
46 return line.substr(begin, end-begin);
47 else
48 return "";
52 bool CheckFiles(ScrobbyConfig &conf)
54 std::ofstream f;
56 f.open(conf.file_log.c_str(), std::ios_base::app);
57 if (!f.is_open())
59 std::cerr << "Cannot create/open log file: " << conf.file_log << std::endl;
60 return false;
62 f.close();
64 f.open(conf.file_cache.c_str(), std::ios_base::app);
65 if (!f.is_open())
67 std::cerr << "Cannot create/open cache file: " << conf.file_cache << std::endl;
68 return false;
70 f.close();
72 std::ifstream g(conf.file_pid.c_str());
73 if (g.is_open())
75 string pid;
76 getline(g, pid);
77 std::cerr << "scrobby is already running with PID " << pid << "!\n";
78 return false;
80 f.open(conf.file_pid.c_str(), std::ios_base::app);
81 if (!f.is_open())
83 std::cerr << "Cannot create/open pid file: " << conf.file_pid << std::endl;
84 return false;
86 f.close();
88 return true;
91 void DefaultConfiguration(ScrobbyConfig &conf)
93 conf.mpd_host = "localhost";
94 conf.mpd_port = 6600;
95 conf.mpd_timeout = 15;
97 conf.file_log = "/var/log/scrobby.log";
98 conf.file_pid = "/var/run/scrobby.pid";
99 conf.file_cache = "/var/cache/scrobby/scrobby.cache";
102 bool ReadConfiguration(ScrobbyConfig &conf, const string &file)
104 string line, v;
105 std::ifstream f(file.c_str());
107 if (!f.is_open())
108 return false;
110 while (!f.eof())
112 getline(f, line);
113 if (!line.empty() && line[0] != '#')
115 v = GetLineValue(line);
117 if (line.find("mpd_host") != string::npos)
119 if (!line.empty())
120 conf.mpd_host = v;
122 else if (line.find("mpd_password") != string::npos)
124 if (!v.empty())
125 conf.mpd_password = v;
127 else if (line.find("mpd_port") != string::npos)
129 if (!v.empty())
130 conf.mpd_port = StrToInt(v);
132 else if (line.find("mpd_timeout") != string::npos)
134 if (!v.empty())
135 conf.mpd_timeout = StrToInt(v);
137 else if (line.find("log_file") != string::npos)
139 if (!v.empty())
140 conf.file_log = v;
142 else if (line.find("pid_file") != string::npos)
144 if (!v.empty())
145 conf.file_pid = v;
147 else if (line.find("cache_file") != string::npos)
149 if (!v.empty())
150 conf.file_cache = v;
152 else if (line.find("lastfm_user") != string::npos)
154 if (!v.empty())
155 conf.lastfm_user = v;
157 else if (line.find("lastfm_password") != string::npos)
159 if (!v.empty())
160 conf.lastfm_password = v;
162 else if (line.find("lastfm_md5_password") != string::npos)
164 if (!v.empty())
165 conf.lastfm_md5_password = v;
169 f.close();
170 return true;