1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 MartiƱo Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_UTIL_UTIL_H_
#define _SHARED_UTIL_UTIL_H_
#include <string>
#include <vector>
#include <fstream>
#include <map>
#include "thread.h"
#include <curl/curl.h>
#include <cstdio>
#include <assert.h>
#include <stdexcept>
#include "leak_dumper.h"
using std::string;
using namespace Shared::Platform;
//#define UNDEF_DEBUG
namespace Shared { namespace Util {
enum GlobalStaticFlagTypes {
gsft_none = 0x00,
gsft_lan_mode = 0x01,
//gsft_xx = 0x02
//gsft__xx = 0x04,
//gsft__xx = 0x08,
//gsft__xx = 0x10,
};
class GlobalStaticFlags {
public:
static bool getIsNonGraphicalModeEnabled() {
return isNonGraphicalMode;
}
static void setIsNonGraphicalModeEnabled(bool value) {
isNonGraphicalMode = value;
}
static void setFlags(uint64 flagsValue) { flags = flagsValue; }
static uint64 getFlags() { return flags; }
static void setFlag(GlobalStaticFlagTypes flag) { flags |= flag; }
static bool isFlagSet(GlobalStaticFlagTypes flag) { return (flags & (unsigned int)flag) == (unsigned int)flag; }
protected:
static bool isNonGraphicalMode;
static uint64 flags;
};
class SystemFlags
{
public:
struct httpMemoryStruct {
char *memory;
size_t size;
};
enum DebugType {
debugSystem,
debugNetwork,
debugPerformance,
debugWorldSynch,
debugUnitCommands,
debugPathFinder,
debugLUA,
debugSound,
debugError
};
class SystemFlagsType
{
protected:
DebugType debugType;
public:
SystemFlagsType() {
this->debugType = debugSystem;
this->enabled = false;
this->fileStream = NULL;
this->debugLogFileName = "";
this->fileStreamOwner = false;
this->mutex = NULL;
}
SystemFlagsType(DebugType debugType) {
this->debugType = debugType;
this->enabled = false;
this->fileStream = NULL;
this->debugLogFileName = "";
this->fileStreamOwner = false;
this->mutex = NULL;
}
~SystemFlagsType() {
Close();
}
SystemFlagsType(DebugType debugType,bool enabled,
std::ofstream *fileStream,std::string debugLogFileName) {
this->debugType = debugType;
this->enabled = enabled;
this->fileStream = fileStream;
this->debugLogFileName = debugLogFileName;
this->fileStreamOwner = false;
this->mutex = NULL;
}
void Close() {
if(this->fileStreamOwner == true) {
if( this->fileStream != NULL &&
this->fileStream->is_open() == true) {
this->fileStream->close();
}
delete this->fileStream;
delete this->mutex;
}
this->fileStream = NULL;
this->fileStreamOwner = false;
this->mutex = NULL;
}
bool enabled;
std::ofstream *fileStream;
std::string debugLogFileName;
bool fileStreamOwner;
Mutex *mutex;
};
protected:
static int lockFile;
static string lockfilename;
static int lockFileCountIndex;
static std::map<DebugType,SystemFlagsType> *debugLogFileList;
static bool haveSpecialOutputCommandLineOption;
static bool curl_global_init_called;
static SystemFlagsType * setupRequiredMembers();
public:
static CURL *curl_handle;
static int DEFAULT_HTTP_TIMEOUT;
static bool VERBOSE_MODE_ENABLED;
static bool ENABLE_THREADED_LOGGING;
static bool SHUTDOWN_PROGRAM_MODE;
SystemFlags();
~SystemFlags();
static void init(bool haveSpecialOutputCommandLineOption);
//static SystemFlagsType & getSystemSettingType(DebugType type);
inline static SystemFlagsType & getSystemSettingType(DebugType type) {
if(SystemFlags::debugLogFileList == NULL) {
SystemFlagsType *result = setupRequiredMembers();
if(result != NULL) {
return *result;
}
else if(SystemFlags::debugLogFileList == NULL) {
throw std::runtime_error("unknown return value for SystemFlagsType!");
}
}
return (*debugLogFileList)[type];
}
static size_t httpWriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);
static std::string getHTTP(std::string URL,CURL *handle=NULL, int timeOut=-1, CURLcode *savedResult=NULL);
static std::string escapeURL(std::string URL, CURL *handle=NULL);
static CURL *initHTTP();
static void cleanupHTTP(CURL **handle,bool globalCleanup=false);
static void globalCleanupHTTP();
static bool getThreadedLoggerRunning();
static std::size_t getLogEntryBufferCount();
// Let the macro call into this when require.. NEVER call it automatically.
static void handleDebug(DebugType type, const char *fmt, ...);
static void logDebugEntry(DebugType type, string debugEntry, time_t debugTime);
// If logging is enabled then define the logging method
#ifndef UNDEF_DEBUG
#ifndef WIN32
#define OutputDebug(type, fmt, ...) SystemFlags::handleDebug (type, fmt, ##__VA_ARGS__)
// Uncomment the line below to get the compiler to warn us of badly formatted printf like statements which could trash memory
//#define OutputDebug(type, fmt, ...) type; printf(fmt, ##__VA_ARGS__)
#else
#define OutputDebug(type, fmt, ...) handleDebug (type, fmt, ##__VA_ARGS__)
#endif
// stub out logging
#else
// stub out debugging completely
#ifndef WIN32
#define OutputDebug(type, fmt, ...) type
#else
static void nothing() {}
#define OutputDebug(type, fmt, ...) nothing()
#endif
#endif
static void Close();
};
const string sharedLibVersionString= "v0.4.1";
//string fcs
string lastDir(const string &s);
string lastFile(const string &s);
string cutLastFile(const string &s);
string cutLastExt(const string &s);
string ext(const string &s);
string replaceBy(const string &s, char c1, char c2);
vector<string> split(string s,string d);
string toLower(const string &s);
bool compareNonCaseSensitive(const string a, const string b);
void copyStringToBuffer(char *buffer, int bufferSize, const string& s);
//numeric fcs
int clamp(int value, int min, int max);
float clamp(float value, float min, float max);
int64 clamp(int64 value, int64 min, int64 max);
float saturate(float value);
int round(float f);
//misc
int compareMajorMinorVersion(string versionA,string versionB);
int getMajor(string version);
int getMinor(string version);
bool checkVersionComptability(string clientVersionString, string serverVersionString);
template<typename T>
void enforceMinimumValue(T minValue, T &value) {
if(value < minValue) {
value = minValue;
}
}
template<typename T>
void deleteValues(T beginIt, T endIt){
for(T it= beginIt; it!=endIt; ++it){
delete *it;
}
}
template<typename T>
void deleteMapValues(T beginIt, T endIt){
for(T it= beginIt; it!=endIt; ++it){
delete it->second;
it->second = NULL;
}
}
template <typename T, typename U>
class create_map
{
private:
std::map<T, U> m_map;
public:
create_map(const T& key, const U& val)
{
m_map[key] = val;
}
create_map<T, U>& operator()(const T& key, const U& val)
{
m_map[key] = val;
return *this;
}
operator std::map<T, U>()
{
return m_map;
}
};
}}//end namespace
#endif
|