1 /***************************************************************************
2 * *
3 * Copyright (C) 2009-2010 Alexandr Tkachev <tka4ev@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 ***************************************************************************/
11
12 // Created on: 17.08.2009
13
14 //---------------------------------------------------------------------------
15 #include "stdafx.h"
16 #include "utility.h"
17 #ifndef _WIN32
18 #include <syslog.h>
19 #include <sys/stat.h>
20 #endif
21 #if (defined(__GNUC__) && !defined(__clang__)) && (__GNUC__ == 4 && __GNUC_MINOR__ < 7)
22 #include <boost/lexical_cast.hpp>
23 #define USE_BOOST_LEXICAL_CAST 1
24 #elif defined(__clang__) && (__clang_major__ == 3 && __clang_minor__ < 2)
25 #include <boost/lexical_cast.hpp>
26 #define USE_BOOST_LEXICAL_CAST 1
27 #endif
28 //---------------------------------------------------------------------------
29
30 string LOCAL_PATH="", PATH = "", sTitle = "", LOG_FILE= "";
31
logging(bool d,bool s,bool b,const string & msg)32 void logging(bool d, bool s, bool b, const string& msg) {
33 #ifndef _WIN32
34 if (d) {
35 if (s) {
36 if (b) syslog(LOG_USER | LOG_INFO, "%s", msg.c_str());
37 else syslog(LOG_USER | LOG_ERR, "%s", msg.c_str());
38 } else {
39 Log(msg);
40 }
41 } else {
42 printf("%s\n",msg.c_str());
43 }
44 #else
45 Log(msg);
46 #endif
47 }
48
DirExist(char * sPath)49 bool DirExist(char * sPath) {
50 #ifdef _WIN32
51 DWORD code = GetFileAttributes(sPath);
52 if(code != INVALID_FILE_ATTRIBUTES && code == FILE_ATTRIBUTE_DIRECTORY) {
53 #else
54 struct stat st;
55 if(!stat(sPath, &st) && S_ISDIR(st.st_mode)) {
56 #endif
57 return true;
58 }
59
60 return false;
61 }
62
63 void Log(const string & sData) {
64 FILE * fw = fopen(LOG_FILE.c_str(), "a");
65 if(!fw) return;
66
67 time_t acc_time;
68 time(&acc_time);
69
70 struct tm * acc_tm;
71 acc_tm = localtime(&acc_time);
72 char sBuf[64];
73 strftime(sBuf, 64, "%d.%m.%Y %H:%M:%S", acc_tm);
74 string sTmp = string(sBuf) + " - " + sData + "\n";
75
76 fprintf(fw, "%s", sTmp.c_str());
77 fclose(fw);
78 }
79
80 #include "extra/magnet.h"
81 bool splitMagnet(const string &magnet, string &name, int64_t &size, string &tth) {
82 name = "Unknown";
83 size = 0;
84 tth = "Unknown";
85
86 StringMap params;
87 if (magnet::parseUri(magnet,params)) {
88 tth=params["xt"];
89 #if defined(USE_BOOST_LEXICAL_CAST)
90 size = boost::lexical_cast<long long>(params["xl"]);
91 #else
92 size = stoll(params["xl"]);
93 #endif
94 name = params["dn"];
95 return true;
96 }
97 return false;
98 }
99