1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
24 #include "configuration.h"
31 std::string
GetLineValue(const string
&line
, char a
= '"', char b
= '"')
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
)
45 if (begin
>= 0 && end
>= 0)
46 return line
.substr(begin
, end
-begin
);
51 LogLevel
IntoLogLevel(const string
&value
)
53 LogLevel result
= llInfo
;
58 else if (value
== "info")
62 else if (value
== "verbose")
71 bool CheckFiles(ScrobbyConfig
&conf
)
76 g
.open(conf
.file_pid
.c_str());
82 pid_t pid
= StrToInt(strpid
);
85 std::cerr
<< "pid file: " << conf
.file_pid
<< " is invalid, trying to remove...\n";
86 if (unlink(conf
.file_pid
.c_str()) == 0)
88 std::cout
<< "pid file succesfully removed.\n";
92 std::cerr
<< "couldn't remove pid file.\n";
98 std::cerr
<< "scrobby is already running with PID " << pid
<< "!\n";
103 f
.open(conf
.file_pid
.c_str(), std::ios_base::app
);
106 std::cerr
<< "Cannot create/open pid file: " << conf
.file_pid
<< std::endl
;
111 f
.open(conf
.file_log
.c_str(), std::ios_base::app
);
114 std::cerr
<< "Cannot create/open log file: " << conf
.file_log
<< std::endl
;
119 f
.open(conf
.file_cache
.c_str(), std::ios_base::app
);
122 std::cerr
<< "Cannot create/open cache file: " << conf
.file_cache
<< std::endl
;
130 void DefaultConfiguration(ScrobbyConfig
&conf
)
132 conf
.mpd_host
= "localhost";
133 conf
.mpd_port
= 6600;
134 conf
.mpd_timeout
= 15;
136 conf
.file_log
= "/var/log/scrobby.log";
137 conf
.file_pid
= "/var/run/scrobby.pid";
138 conf
.file_cache
= "/var/cache/scrobby/scrobby.cache";
140 conf
.log_level
= llInfo
;
143 bool ReadConfiguration(ScrobbyConfig
&conf
, const string
&file
)
146 std::ifstream
f(file
.c_str());
154 if (!line
.empty() && line
[0] != '#')
156 v
= GetLineValue(line
);
158 if (line
.find("mpd_host") != string::npos
)
163 else if (line
.find("mpd_password") != string::npos
)
166 conf
.mpd_password
= v
;
168 else if (line
.find("mpd_port") != string::npos
)
171 conf
.mpd_port
= StrToInt(v
);
173 else if (line
.find("mpd_timeout") != string::npos
)
176 conf
.mpd_timeout
= StrToInt(v
);
178 else if (line
.find("log_file") != string::npos
)
183 else if (line
.find("pid_file") != string::npos
)
188 else if (line
.find("cache_file") != string::npos
)
193 else if (line
.find("lastfm_user") != string::npos
)
196 conf
.lastfm_user
= v
;
198 else if (line
.find("lastfm_password") != string::npos
)
201 conf
.lastfm_password
= v
;
203 else if (line
.find("lastfm_md5_password") != string::npos
)
206 conf
.lastfm_md5_password
= v
;
208 else if (line
.find("log_level") != string::npos
)
211 conf
.log_level
= IntoLogLevel(v
);