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 <openssl/evp.h>
26 #include "configuration.h"
29 extern ScrobbyConfig config
;
31 pthread_mutex_t log_file
= PTHREAD_MUTEX_INITIALIZER
;
33 size_t write_data(char *buffer
, size_t size
, size_t nmemb
, std::string data
)
35 size_t result
= size
* nmemb
;
36 data
.append(buffer
, result
);
45 std::ofstream
f(config
.file_pid
.c_str(), std::ios_base::app
);
59 std::ofstream
f(config
.file_cache
.c_str(), std::ios::trunc
);
63 void GetCachedSongs(std::vector
<std::string
> &v
)
65 std::ifstream
f(config
.file_cache
.c_str());
78 void Cache(const std::string
&s
)
80 std::ofstream
f(config
.file_cache
.c_str(), std::ios::app
);
88 void Log(LogLevel ll
, const char *format
, ...)
90 if (config
.log_level
< ll
)
92 pthread_mutex_lock(&log_file
);
93 FILE *f
= fopen(config
.file_log
.c_str(), "a");
96 perror("Cannot open log file!\n");
99 fprintf(f
, "[%s] ", DateTime().c_str());
101 va_start(list
, format
);
102 vfprintf(f
, format
, list
);
106 pthread_mutex_unlock(&log_file
);
109 void ignore_newlines(std::string
&s
)
111 for (size_t i
= s
.find("\n"); i
!= std::string::npos
; i
= s
.find("\n"))
115 std::string
md5sum(const std::string
&s
)
118 unsigned char md_value
[EVP_MAX_MD_SIZE
];
122 EVP_DigestInit(&mdctx
, EVP_md5());
123 EVP_DigestUpdate(&mdctx
, s
.c_str(), s
.length());
124 EVP_DigestFinal_ex(&mdctx
, md_value
, &md_len
);
125 EVP_MD_CTX_cleanup(&mdctx
);
127 for (unsigned i
= 0; i
< md_len
; i
++)
128 sprintf(&result
[i
*2], "%02x", md_value
[i
]);
133 std::string
DateTime()
140 result
[strftime(result
, 31, "%Y/%m/%d %X", t
)] = 0;
144 int StrToInt(const std::string
&s
)
146 return atoi(s
.c_str());