SDL Window Engine  20200905
swe_systems.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_SYSTEM_
24 #define _SWE_SYSTEM_
25 
26 #include <ostream>
27 #include <string>
28 #include <vector>
29 #include <utility>
30 
31 #if defined(__MINGW32CE__)
32 #include <locale.h>
33 #else
34 #include <clocale>
35 #endif
36 
37 #include "swe_binarybuf.h"
38 #include "swe_cstring.h"
39 
41 namespace SWE
42 {
43  class LogWrapper
44  {
45  protected:
46  static std::ostream* os;
47  static std::string id;
48 
49  public:
50  LogWrapper();
51  ~LogWrapper();
52 
53  static void init(const std::string &, const char* = nullptr);
54 
55 /*
56  template<typename Value>
57  LogWrapper & cout(Value&& val)
58  {
59  if(os) *os << std::forward<Value>(val);
60  return *this;
61  }
62 
63  template<typename Value, typename... Values>
64  LogWrapper & cout(Value&& val, Values&&... vals)
65  {
66  if(os) *os << std::forward<Value>(val);
67  return cout(std::forward<Values>(vals)...);
68  }
69 
70  template<typename... Values>
71  LogWrapper & cout(Values&&... vals)
72  {
73  return cout(std::forward<Values>(vals)...);
74  }
75 */
76 
77  LogWrapper & endl(void)
78  {
79  if(os) *os << std::endl;
80  return *this;
81  }
82 
83  template<typename Value>
84  LogWrapper & operator<< (const Value & val)
85  {
86  if(os) *os << val;
87  return *this;
88  }
89  };
90 
91  namespace Engine
92  {
93  int debugMode(void);
94  }
95 
96  std::string shortPrettyName(const std::string &);
97 
98 
99  namespace Systems
100  {
101  int setEnvironment(const char* name, const char* value);
102  const char* environment(const char*);
103 
104 #ifdef SWE_DLOPEN
105  void* openLib(const std::string &);
106  void closeLib(void*);
107  void* procAddressLib(void*, const std::string &);
108  const char* suffixLib(void);
109 #endif
110 
111  bool makeDirectory(const std::string &);
112  std::string concatePath(const std::string &, const std::string &);
113  std::string concatePath(const std::string &, const std::string &, const std::string &);
114  std::string concatePath(const StringList &);
115  StringList findFiles(const std::string &, const std::string & = "", bool sensitive = false);
116  StringList readDir(const std::string &, bool fullpath = true);
117 
118  std::string dirname(const std::string &);
119  std::string basename(const std::string &);
120 
121  std::string nulFile(void);
122  std::string homeDirectory(const std::string &);
123  StringList shareDirectories(const std::string &);
124 
125  void assetsInit(void);
126  const StringList &
127  assetsList(void);
128 
129  bool isFile(const std::string &, bool writable = false);
130  bool isDirectory(const std::string &, bool writable = false);
131  int remove(const std::string &);
132 
133  void setLocale(int, const char*);
134  std::string messageLocale(int length /* 1, 2, 3 */);
135 
136  bool saveFile(const BinaryBuf &, const std::string &, int offset = 0 /* -1 end */);
137  BinaryBuf readFile(const std::string &, size_t offset = 0, size_t size = 0);
138 
139  bool saveString2File(const std::string &, const std::string &);
140  bool readFile2String(const std::string &, std::string &);
141 
142  int GetCommandOptions(int argc, char* const argv[], const char* optstring);
143  char* GetOptionsArgument(void);
144 
145  bool isEmbeded(void);
146  size_t memoryUsage(void);
147  }
148 
149 } // SWE
150 
151 #define COUT(x) SWE::LogWrapper() << x << "\n";
152 
153 #if defined(SWE_DEBUG_MESSAGES) || defined(SWE_DEBUG)
154  #if defined(__WIN32__) || (__MINGW32__)
155  #define PRETTY(x, y) COUT(SWE::String::time() << ": " << x << "\t" << SWE::shortPrettyName(__PRETTY_FUNCTION__) << ": " << y)
156  #define VERBOSE(x) { PRETTY("[VERBOSE]", x); }
157  #define ERROR(x) { PRETTY("[ERROR]", x); }
158  #define FIXME(x) { PRETTY("[FIXME]", x); }
159  #define DEBUGN(x,n){ if(n <= SWE::Engine::debugMode()) PRETTY("[DEBUG]", x); }
160  #else
161  // colored console
162  #define PRETTY3(x, y, c) COUT(SWE::String::time() << ": " << x << "\t\x1B[35m" << SWE::shortPrettyName(__PRETTY_FUNCTION__) << ": " << c << y << "\033[0m")
163  #define PRETTY(x, y) PRETTY3(x, y, "\x1B[36m")
164  #define VERBOSE(x) { PRETTY("\x1B[31m[VERBOSE]", x); }
165  #define ERROR(x) { PRETTY3("\x1B[93m[ERROR]", x, "\x1B[93m"); }
166  #define FIXME(x) { PRETTY("\x1B[32m[FIXME]", x); }
167  #define DEBUG(x) { if(SWE::Engine::debugMode()) PRETTY("\x1B[34m[DEBUG]", x); }
168  #define DEBUGN(x,n){ if(n <= SWE::Engine::debugMode()) PRETTY("\x1B[34m[DEBUG]", x); }
169  #endif
170 #else
171  #define PRETTY(x, y) COUT(SWE::String::time() << ": " << x << "\t" << SWE::shortPrettyName(__PRETTY_FUNCTION__) << ": " << y)
172  #define VERBOSE(x) { PRETTY("[VERBOSE]", x); }
173  #define ERROR(x) { PRETTY("[ERROR]", x); }
174  #define FIXME(x) { PRETTY("[FIXME]", x); }
175  #define DEBUG(x) {}
176  #define DEBUGN(x) {}
177 #endif
178 
179 
180 #endif
Definition: swe_systems.h:43
пространство SWE.
Definition: swe_binarybuf.cpp:30