GlistEngine
Loading...
Searching...
No Matches
gUtils.h
Go to the documentation of this file.
1/*
2 * gUtils.h
3 *
4 * Created on: 5 Ara 2020
5 * Author: Noyan
6 */
7
8#pragma once
9
10#ifndef UTILS_GUTILS_H_
11#define UTILS_GUTILS_H_
12
13#include <string>
14#include <sstream>
15#include <ostream>
16#include <iostream>
17#include <iomanip>
18#include "utf8.h"
19#include <stdlib.h>
20#include <wchar.h>
21#include <vector>
22#include <memory>
23#include "gConstants.h"
24#include "gKeyCode.h"
25#include "gGUIEvents.h"
26#include "tinyfiledialogs.h"
27#include "gColor.h"
28#include <cstdint>
29#include <deque>
30
31#include "gTargetDefines.h"
32
33#ifndef PI
34 #define PI 3.14159265358979323846
35#endif
36
37class gImage;
45
46float gRadToDeg(float radians);
47float gDegToRad(float degrees);
48
50float gRandom(float max);
51float gRandomf();
52
54unsigned int gGetUnixTime();
55
56/*
57 * Returns YYYY
58 */
60
61/*
62 * Returns M, starting from 1
63 */
65
66/*
67 * Returns D, starting from 1
68 */
69int gGetDay();
70
75
76
77#if defined(WIN32) || defined(LINUX) || defined(APPLE) //Available Ram Size
78uint64_t gGetAvailableRamSize();
79#endif
80
81#if defined(WIN32) || defined(LINUX) || defined(APPLE) //Current Ram Size Used by Glist Engine
82uint64_t gGetRamSizeUsedbyGE();
83#endif
84
85#if defined(WIN32) || defined(LINUX) || defined(APPLE) //Total Ram Size
86uint64_t gGetTotalRamSize();
87#endif
88
89/*
90 * Rotates The Pixel Data upside down. Hence rotates flips the image upside down
91 */
92void gFlipImageDataVertically(unsigned char* pixelData, int width, int height, int numChannels);
93
94std::string gGetTimestampString();
95std::string gGetTimestampString(const std::string& format);
96void gStringReplace(std::string& input, const std::string& searchStr, const std::string& replaceStr);
97std::locale gGetLocale(const std::string & locale);
98std::string gToLower(const std::string& src, const std::string & locale = "");
99std::string gToUpper(const std::string& src, const std::string & locale = "");
100std::vector<std::string> gSplitString(const std::string& textToSplit, const std::string& delimiter);
101std::deque<std::string> gSplitStringToDeque(const std::string& textToSplit, const std::string& delimiter);
102std::string gReplaceAll(const std::string& source, const std::string& from, const std::string& to);
103bool gIsValidFilename(std::string fileName);
104
105std::string gGetFirstLineOfTextFile(std::string fullFilepath);
106std::string gGetFirstLineOfText(const std::string& text);
107
108bool gIsInt(const std::string& str);
109bool gIsFloat(const std::string& str);
110bool gIsNumber(const std::string& str);
111bool gIsString(const std::string& str);
112
113template <class T>
114std::string gToStr(const T& numValue) {
115 std::ostringstream out;
116 out << numValue;
117 return out.str();
118}
119
120template <class T>
121std::string gToHex(const T& numValue, int width = sizeof(T)*2) {
122 std::ostringstream stream;
123 stream << "0x"
124 << std::setfill ('0') << std::setw(width)
125 << std::hex << numValue;
126 return stream.str();
127}
128
129template <class T>
130std::string gToStr(const T& value, int width, char fill) {
131 std::ostringstream out;
132 out << std::fixed << std::setfill(fill) << std::setw(width) << value;
133 return out.str();
134}
135
136int gToInt(const std::string& intString);
137float gToFloat(const std::string& floatString);
138double gToDouble(const std::string& doubleString);
139std::string gWStrToStr(const std::wstring& s);
140
141template <typename T>
142int gSign(T val) {
143 return (T(0) < val) - (val < T(0));
144}
145
146std::string gCodepointToStr(unsigned int codepoint);
147
148std::string gEncodeBase64(unsigned char* data, int len);
149std::string gDecodeBase64(const std::string& encoded_string);
150
151bool gIsBase64(char c);
152
154void gOpenUrlInDefaultBrowser(std::string url);
155
157public:
158 gUTF8Iterator(const std::string & str);
159 utf8::iterator<std::string::const_iterator> begin() const;
160 utf8::iterator<std::string::const_iterator> end() const;
161 utf8::iterator<std::string::const_reverse_iterator> rbegin() const;
162 utf8::iterator<std::string::const_reverse_iterator> rend() const;
163
164private:
165 std::string src_valid;
166};
167
168class gLog {
169public:
170 enum {
175 };
176
178 gLog(const std::string& tag);
180
181 gLog& operator<<(std::ostream& (*func)(std::ostream&)){
182 func(logmessage);
183 return *this;
184 }
185
186 template <typename T>
187 gLog& operator<<(const T& value) {
188 logmessage << value;
189 return *this;
190 }
191
193 static bool isLoggingEnabled();
194
195 static std::string getLogLevelName(int logLevel);
196
197protected:
198 static bool isloggingenabled;
200 std::stringstream logmessage;
201 std::string logtag;
202 static std::string loglevelname[];
203
204private:
205 std::string logmsg;
206};
207
208class gLogi : public gLog {
209public:
210 gLogi(const std::string& tag = "");
211};
212
213class gLogd : public gLog {
214public:
215 gLogd(const std::string& tag = "");
216};
217
218class gLogw : public gLog {
219public:
220 gLogw(const std::string& tag = "");
221};
222
223class gLoge : public gLog {
224public:
225 gLoge(const std::string& tag = "");
226};
227
231
232template<typename T>
233T gClamp(T t, T min, T max) {
234 return t > max ? max : t < min ? min : t;
235}
236
274 const std::string& dialogTitle,
275 const std::string& defaultPathAndFile,
276 int filterNum,
277 const std::string* filterPatterns,
278 const std::string& filterDescription,
279 bool isMultipleSelectionAllowed = false);
280
306 const std::string& dialogTitle,
307 const std::string& defaultPathAndFile,
308 std::initializer_list<const char*> filterPatterns,
309 const std::string& filterDescription,
310 bool isMultipleSelectionAllowed = false);
311
313 const std::string& dialogTitle,
314 const std::string& defaultPathAndFile,
315 int filterNum,
316 const char** filterPatterns,
317 const std::string& filterDescription,
318 bool isMultipleSelectionAllowed = false);
319
321 const std::string& dialogTitle,
322 const std::string& defaultPathAndFile,
323 int filterNum,
324 const std::string* filterPatterns,
325 const std::string& filterDescription);
326
327
329 const std::string& dialogTitle,
330 const std::string& defaultPathAndFile,
331 std::initializer_list<const char*> filterPatterns,
332 const std::string& filterDescription);
333
335 const std::string& dialogTitle,
336 const std::string& defaultPathAndFile,
337 int filterNum,
338 const char** filterPatterns,
339 const std::string& filterDescription);
340
342 const std::string& dialogTitle,
343 const std::string& defaultPath);
344
345
356 const std::string& title,
357 const std::string& message,
358 const std::string& dialogue,
359 const std::string& iconType,
360 int defaultButton);
361
370std::string gShowInputBox(
371 const std::string& title,
372 const std::string& message,
373 const std::string& defaultInput);
374
384 const std::string& title,
385 const std::string& defaultHex,
386 unsigned char const defaultRGB[3]);
387
388bool gCheckCollision(int xLeft1, int yUp1, int xRight1, int yBottom1,
389 int xLeft2, int yUp2, int xRight2, int yBottom2);
390
391bool gCheckPixelPerfectCollision(gImage* image1, int x1, int y1, gImage* image2, int x2, int y2);
392
393std::string gFixPath(const std::string& path);
394
395class gUtils {
396public:
398 virtual ~gUtils();
399};
400
401#endif /* UTILS_GUTILS_H_ */
Definition gColor.h:17
Definition gImage.h:34
gLog(const std::string &tag)
@ LOGLEVEL_DEBUG
Definition gUtils.h:172
@ LOGLEVEL_WARNING
Definition gUtils.h:173
@ LOGLEVEL_ERROR
Definition gUtils.h:174
@ LOGLEVEL_INFO
Definition gUtils.h:171
static bool isloggingenabled
Definition gUtils.h:198
static void setLoggingEnabled(bool isLoggingEnabled)
std::string logtag
Definition gUtils.h:201
gLog & operator<<(const T &value)
Definition gUtils.h:187
static std::string getLogLevelName(int logLevel)
gLog & operator<<(std::ostream &(*func)(std::ostream &))
Definition gUtils.h:181
static std::string loglevelname[]
Definition gUtils.h:202
static bool isLoggingEnabled()
int loglevel
Definition gUtils.h:199
std::stringstream logmessage
Definition gUtils.h:200
gLogd(const std::string &tag="")
gLoge(const std::string &tag="")
gLogi(const std::string &tag="")
gLogw(const std::string &tag="")
unsigned int format
Definition gTexture.h:168
int width
Definition gTexture.h:171
int height
Definition gTexture.h:171
unsigned char * data
Definition gTexture.h:172
utf8::iterator< std::string::const_iterator > begin() const
gUTF8Iterator(const std::string &str)
utf8::iterator< std::string::const_reverse_iterator > rbegin() const
utf8::iterator< std::string::const_iterator > end() const
utf8::iterator< std::string::const_reverse_iterator > rend() const
virtual ~gUtils()
std::string gToStr(const T &numValue)
Definition gUtils.h:114
bool gCheckCollision(int xLeft1, int yUp1, int xRight1, int yBottom1, int xLeft2, int yUp2, int xRight2, int yBottom2)
bool gIsOnline()
int gGetMinutes()
int gGetYear()
int gDefaultMonitorHeight()
std::string gToLower(const std::string &src, const std::string &locale="")
std::string gShowOpenFileDialog(const std::string &dialogTitle, const std::string &defaultPathAndFile, int filterNum, const std::string *filterPatterns, const std::string &filterDescription, bool isMultipleSelectionAllowed=false)
int gShowMessageBox(const std::string &title, const std::string &message, const std::string &dialogue, const std::string &iconType, int defaultButton)
int gDefaultUnitWidth()
bool gIsValidFilename(std::string fileName)
bool gIsString(const std::string &str)
std::string gToHex(const T &numValue, int width=sizeof(T) *2)
Definition gUtils.h:121
void gFlipImageDataVertically(unsigned char *pixelData, int width, int height, int numChannels)
std::string gShowSelectFolderDialog(const std::string &dialogTitle, const std::string &defaultPath)
int gGetHours()
float gRandomf()
uint64_t gGetSystemTimeMillis()
bool gIsBase64(char c)
int gToInt(const std::string &intString)
int gGetMonth()
std::string gCodepointToStr(unsigned int codepoint)
bool gIsLoggingEnabled()
std::string gGetFirstLineOfTextFile(std::string fullFilepath)
double gToDouble(const std::string &doubleString)
std::vector< std::string > gSplitString(const std::string &textToSplit, const std::string &delimiter)
std::string gReplaceAll(const std::string &source, const std::string &from, const std::string &to)
std::string gGetFirstLineOfText(const std::string &text)
gColor gShowColorChooser(const std::string &title, const std::string &defaultHex, unsigned char const defaultRGB[3])
int gGetSeconds()
std::string gToUpper(const std::string &src, const std::string &locale="")
std::string gShowInputBox(const std::string &title, const std::string &message, const std::string &defaultInput)
std::string gWStrToStr(const std::wstring &s)
void gOpenUrlInDefaultBrowser(std::string url)
void gEnableLogging()
std::string gDecodeBase64(const std::string &encoded_string)
bool gCheckPixelPerfectCollision(gImage *image1, int x1, int y1, gImage *image2, int x2, int y2)
float gRadToDeg(float radians)
std::string gFixPath(const std::string &path)
int gGetWeekday()
int gDefaultHeight()
int gDefaultUnitHeight()
int gSign(T val)
Definition gUtils.h:142
std::locale gGetLocale(const std::string &locale)
unsigned int gGetUnixTime()
bool gIsNumber(const std::string &str)
T gClamp(T t, T min, T max)
Definition gUtils.h:233
float gRandom(float max)
int gDefaultMonitorWidth()
float gToFloat(const std::string &floatString)
void gDisableLogging()
std::string gEncodeBase64(unsigned char *data, int len)
std::string gShowSaveFileDialog(const std::string &dialogTitle, const std::string &defaultPathAndFile, int filterNum, const std::string *filterPatterns, const std::string &filterDescription)
int gDefaultScreenScaling()
void gSeedRandom()
int gGetDay()
bool gIsFloat(const std::string &str)
std::deque< std::string > gSplitStringToDeque(const std::string &textToSplit, const std::string &delimiter)
float gDegToRad(float degrees)
std::string gGetTimestampString()
int gDefaultWidth()
void gStringReplace(std::string &input, const std::string &searchStr, const std::string &replaceStr)
bool gIsInt(const std::string &str)