~manky/bmdc++/trunk

« back to all changes in this revision

Viewing changes to Plugins/LuaPlugin/LuaPlugin/Util.h

  • Committer: M@niu
  • Date: 2017-12-08 16:42:20 UTC
  • Revision ID: m@niu-20171208164220-co5o2eqljpxnv09q
remove plugins folder . some small fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2012 Jacek Sieka, arnetheduck on gmail point com
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 
 */
18
 
 
19
 
#ifndef LUA_PLUGIN_UTIL_H
20
 
#define LUA_PLUGIN_UTIL_H
21
 
 
22
 
#ifdef _WIN32
23
 
# define PATH_SEPARATOR '\\'
24
 
# define PATH_SEPARATOR_STR "\\"
25
 
#else
26
 
# define PATH_SEPARATOR '/'
27
 
# define PATH_SEPARATOR_STR "/"
28
 
# include <sys/stat.h> 
29
 
#endif
30
 
 
31
 
#ifdef _WIN32
32
 
# ifdef _MSC_VER
33
 
#  define snprintf _snprintf
34
 
#  define snwprintf _snwprintf
35
 
# endif
36
 
#elif __GNUC__
37
 
# define stricmp strcasecmp
38
 
# define strnicmp strncasecmp
39
 
#else
40
 
# error No supported compiler found
41
 
#endif
42
 
 
43
 
using std::string;
44
 
 
45
 
class Util
46
 
{
47
 
public:
48
 
        static void initialize(DCUtilsPtr coreUtils, DCConfigPtr coreConfig, DCLogPtr coreLogger) {
49
 
                utils = coreUtils;
50
 
                config = coreConfig;
51
 
                logger = coreLogger;
52
 
        }
53
 
 
54
 
        static string toString(uint16_t val) {
55
 
                char buf[8];
56
 
                snprintf(buf, sizeof(buf), "%u", (unsigned int)val);
57
 
                return buf;
58
 
        }
59
 
 
60
 
        static string toString(int32_t val) {
61
 
                char buf[16];
62
 
                snprintf(buf, sizeof(buf), "%d", val);
63
 
                return buf;
64
 
        }
65
 
 
66
 
        static bool fileExists(const string& aFile) {
67
 
#ifdef _WIN32
68
 
                DWORD attr = GetFileAttributesA(aFile.c_str());
69
 
                return (attr != 0xFFFFFFFF);
70
 
#else
71
 
                struct stat fi;
72
 
                return (stat(aFile.c_str(), &fi) == 0);
73
 
#endif
74
 
        }
75
 
 
76
 
        static string formatBytes(double val);
77
 
 
78
 
        // Make BCDC's Lua user commands work... hopefully
79
 
        static string ucLuaConvert(const string& str, bool isAdc);
80
 
 
81
 
        // API Stuff
82
 
        static string getPath(PathType type) {
83
 
                return config->get_path(type);
84
 
        }
85
 
 
86
 
        static void logMessage(const string& message) {
87
 
                logger->log(message.c_str());
88
 
        }
89
 
 
90
 
        static void setConfig(const char* name, const char* value);
91
 
        static void setConfig(const char* name, const string& value) { setConfig(name, value.c_str()); }
92
 
        static void setConfig(const char* name, bool state) { setConfig(name, string(state ? "1" : "0")); }
93
 
 
94
 
        static string getConfig(const char *name);
95
 
        static bool getBoolConfig(const char* name);
96
 
 
97
 
        static ConfigValuePtr getCoreConfig(const char* name);
98
 
        static void freeCoreConfig(ConfigValuePtr val) { config->release(val); }
99
 
 
100
 
        static string fromUtf8(const string& str);
101
 
        static string toUtf8(const string& str);
102
 
 
103
 
private:
104
 
        static DCUtilsPtr utils;
105
 
        static DCConfigPtr config;
106
 
        static DCLogPtr logger;
107
 
};
108
 
 
109
 
#define SETTING(k) Util::getConfig(#k)
110
 
#define BOOLSETTING(k) (Util::getBoolConfig(#k))
111
 
 
112
 
#endif // LUA_PLUGIN_UTIL_H