OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
Standard.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* All kinds of valuable helpers */
19 
20 #ifndef INC_STANDARD
21 #define INC_STANDARD
22 
23 #include <type_traits>
24 
25 // The Clear/Default functions that exist on most OpenClonk classes are A
26 // BAD IDEA because the caller has no guarantee that every member has been
27 // properly cleared/initialized. The proper way to handle this, which is
28 // using ctors/dtors, is hard to integrate into the current design. To help
29 // with this, InplaceReconstruct will destroy and default-construct an object
30 // in the same memory. The nothrow test attempts to ensure that the object
31 // will not end up in a destroyed state when the ctor fails (by checking that
32 // the ctor cannot fail beforehand).
33 template<class T>
34 typename std::enable_if<std::is_nothrow_default_constructible<T>::value>::type
35 inline InplaceReconstruct(T *obj)
36 {
37  obj->~T();
38  new (obj) T();
39 }
40 
42 
43 // Small helpers
44 template <class T> inline T Abs(T val) { return val > 0 ? val : -val; }
45 template <class T, class U, class V> inline bool Inside(T ival, U lbound, V rbound) { return ival >= lbound && ival <= rbound; }
46 template <class T> inline T Clamp(T bval, T lbound, T rbound) { return bval < lbound ? lbound : bval > rbound ? rbound : bval; }
47 template <class T> inline int Sign(T val) { return val < 0 ? -1 : val > 0 ? 1 : 0; }
48 
49 inline int DWordAligned(int val)
50 {
51  if (val%4) { val>>=2; val<<=2; val+=4; }
52  return val;
53 }
54 
55 int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2);
56 int Angle(int iX1, int iY1, int iX2, int iY2);
57 int Pow(int base, int exponent);
58 int32_t StrToI32(const char *s, int base, const char **scan_end);
59 
60 #include <cstring>
61 template <class T>
62 typename std::enable_if<std::is_pod<T>::value>::type
63 inline ZeroMem(T *lpMem, size_t dwSize)
64 {
65  std::memset(lpMem,'\0',dwSize);
66 }
67 
68 inline void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
69 {
70  std::memmove(lpMem2,lpMem1,dwSize);
71 }
72 
73 #include <cctype>
74 inline char CharCapital(char cChar) { return std::toupper(cChar); }
75 bool IsIdentifier(char cChar);
76 inline bool IsWhiteSpace(char cChar) { return !!std::isspace((unsigned char)cChar); }
77 
78 inline size_t SLen(const char *sptr) { return sptr?std::strlen(sptr):0; }
79 inline size_t SLenUntil(const char *szStr, char cUntil)
80 {
81  if (!szStr) return 0;
82  const char *end = std::strchr(szStr, cUntil);
83  return end ? end-szStr : std::strlen(szStr);
84 }
85 
86 // get a character at the current string pos and advance pos by that character
87 uint32_t GetNextUTF8Character(const char **pszString); // GetNextCharacter helper
88 inline uint32_t GetNextCharacter(const char **pszString)
89 {
90  unsigned char c=**pszString;
91  if (c<128) { ++*pszString; return c; }
92  else return GetNextUTF8Character(pszString);
93 }
94 // Get string length in characters (not bytes)
95 int GetCharacterCount(const char * s);
96 
97 inline bool SEqual(const char *szStr1, const char *szStr2) { return szStr1&&szStr2?!std::strcmp(szStr1,szStr2):false; }
98 bool SEqual2(const char *szStr1, const char *szStr2);
99 bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild);
100 bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen=-1);
101 bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen = -1);
102 
103 void SCopy(const char *szSource, char *sTarget);
104 void SCopy(const char *szSource, char *sTarget, size_t iMaxL);
105 void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL=-1, int iIndex=0);
106 void SCopyUntil(const char *szSource, char *sTarget, const char * sUntil, size_t iMaxL);
107 void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL=0);
108 bool SCopySegment(const char *fstr, int segn, char *tstr, char sepa=';', int iMaxL=-1, bool fSkipWhitespace=false);
109 bool SCopySegmentEx(const char *fstr, int segn, char *tstr, char sepa1, char sepa2, int iMaxL=-1, bool fSkipWhitespace=false);
110 bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize);
111 
112 void SAppend(const char *szSource, char *szTarget, int iMaxL=-1);
113 void SAppendChar(char cChar, char *szStr);
114 
115 void SInsert(char *szString, const char *szInsert, int iPosition=0, int iMaxLen=-1);
116 void SDelete(char *szString, int iLen, int iPosition=0);
117 
118 int SCharPos(char cTarget, const char *szInStr, int iIndex=0);
119 int SCharLastPos(char cTarget, const char *szInStr);
120 unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil=nullptr);
121 unsigned int SCharCountEx(const char *szString, const char *szCharList);
122 
123 void SReplaceChar(char *str, char fc, char tc);
124 
125 const char *SSearch(const char *szString, const char *szIndex);
126 const char *SSearchNoCase(const char *szString, const char *szIndex);
127 
128 const char *SAdvanceSpace(const char *szSPos);
129 const char *SAdvancePast(const char *szSPos, char cPast);
130 
131 bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize=-1);
132 bool SIsModule(const char *szList, const char *szString, int *ipIndex=nullptr, bool fCaseSensitive=false);
133 bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive=false);
134 bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive=false);
135 bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive=false);
136 bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive=false);
137 int SModuleCount(const char *szList);
138 
139 void SNewSegment(char *szStr, const char *szSepa=";");
140 void SCapitalize(char *szString);
141 void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine);
142 int SClearFrontBack(char *szString, char cClear=' ');
143 
144 int SGetLine(const char *szText, const char *cpPosition);
145 int SLineGetCharacters(const char *szText, const char *cpPosition);
146 
147 // case sensitive wildcard match with some extra functionality
148 // can match strings like "*Cl?nk*vour" to "Clonk Endeavour"
149 bool SWildcardMatchEx(const char *szString, const char *szWildcard);
150 
151 #define LineFeed "\x00D\x00A"
152 
153 // sprintf wrapper
154 
155 #include <cstdio>
156 #include <cstdarg>
157 
158 #ifdef _WIN32
159 #define vsnprintf _vsprintf_p
160 #endif
161 
162 // old, insecure sprintf
163 inline int osprintf(char *str, const char *fmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
164 inline int osprintf(char *str, const char *fmt, ...)
165 {
166  va_list args; va_start(args, fmt);
167  return vsprintf(str, fmt, args);
168 }
169 
170 // secure sprintf
171 #define sprintf ssprintf
172 template <size_t N> inline int ssprintf(char(&str)[N], const char *fmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
173 template <size_t N>
174 inline int ssprintf(char(&str)[N], const char *fmt, ...)
175 {
176  va_list args; va_start(args, fmt);
177  int m = vsnprintf(str, N, fmt, args);
178  // Quick exit if vsnprintf failed
179  if (m < 0) return m;
180  if (static_cast<size_t>(m) >= N) { m = N-1; str[m] = 0; }
181  return m;
182 }
183 
184 // Checks a string for conformance with UTF-8
185 bool IsValidUtf8(const char *string, int length = -1);
186 
187 
188 std::string vstrprintf(const char *format, va_list args);
189 std::string strprintf(const char *format, ...) GNUC_FORMAT_ATTRIBUTE;
190 
191 #endif // INC_STANDARD
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen=-1)
Definition: Standard.cpp:190
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:164
bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive=false)
Definition: Standard.cpp:569
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:168
int DWordAligned(int val)
Definition: Standard.h:49
int Pow(int base, int exponent)
Definition: Standard.cpp:46
#define inline
Definition: C4Real.h:37
bool SIsModule(const char *szList, const char *szString, int *ipIndex=nullptr, bool fCaseSensitive=false)
Definition: Standard.cpp:511
void SNewSegment(char *szStr, const char *szSepa=";")
Definition: Standard.cpp:438
uint32_t GetNextCharacter(const char **pszString)
Definition: Standard.h:88
#define GNUC_FORMAT_ATTRIBUTE
size_t SLenUntil(const char *szStr, char cUntil)
Definition: Standard.h:79
int32_t StrToI32(const char *s, int base, const char **scan_end)
Definition: Standard.cpp:97
bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize=-1)
Definition: Standard.cpp:503
bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive=false)
Definition: Standard.cpp:540
bool SCopySegmentEx(const char *fstr, int segn, char *tstr, char sepa1, char sepa2, int iMaxL=-1, bool fSkipWhitespace=false)
Definition: Standard.cpp:262
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:333
uint32_t GetNextUTF8Character(const char **pszString)
Definition: Standard.cpp:719
bool IsWhiteSpace(char cChar)
Definition: Standard.h:76
int Sign(T val)
Definition: Standard.h:47
std::string vstrprintf(const char *format, va_list args)
Definition: Standard.cpp:786
void SCopy(const char *szSource, char *sTarget)
Definition: Standard.cpp:131
size_t SLen(const char *sptr)
Definition: Standard.h:78
void SAppend(const char *szSource, char *szTarget, int iMaxL=-1)
Definition: Standard.cpp:227
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
int Angle(int iX1, int iY1, int iX2, int iY2)
Definition: Standard.cpp:35
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:46
int SLineGetCharacters(const char *szText, const char *cpPosition)
Definition: Standard.cpp:455
int SGetLine(const char *szText, const char *cpPosition)
Definition: Standard.cpp:443
bool IsValidUtf8(const char *string, int length=-1)
Definition: Standard.cpp:666
unsigned int SCharCountEx(const char *szString, const char *szCharList)
Definition: Standard.cpp:307
void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine)
Definition: Standard.cpp:363
#define GNUC_FORMAT_ATTRIBUTE_O
const char * SAdvanceSpace(const char *szSPos)
Definition: Standard.cpp:384
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:235
bool SWildcardMatchEx(const char *szString, const char *szWildcard)
Definition: Standard.cpp:599
bool SCopySegment(const char *fstr, int segn, char *tstr, char sepa=';', int iMaxL=-1, bool fSkipWhitespace=false)
Definition: Standard.cpp:243
const char * SAdvancePast(const char *szSPos, char cPast)
Definition: Standard.cpp:402
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:24
bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
Definition: Standard.cpp:493
void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
Definition: Standard.h:68
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:318
std::enable_if< std::is_nothrow_default_constructible< T >::value >::type InplaceReconstruct(T *obj)
Definition: Standard.h:35
int GetCharacterCount(const char *s)
Definition: Standard.cpp:755
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:217
bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive=false)
Definition: Standard.cpp:552
char CharCapital(char cChar)
Definition: Standard.h:74
int SCharPos(char cTarget, const char *szInStr, int iIndex=0)
Definition: Standard.cpp:203
int ssprintf(char(&str)[N], const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:174
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil=nullptr)
Definition: Standard.cpp:290
void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL=0)
Definition: Standard.cpp:413
void SCapitalize(char *szString)
Definition: Standard.cpp:324
std::string strprintf(const char *format,...) GNUC_FORMAT_ATTRIBUTE
Definition: Standard.cpp:802
T Abs(T val)
Definition: Standard.h:44
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:63
void SDelete(char *szString, int iLen, int iPosition=0)
Definition: Standard.cpp:485
bool IsIdentifier(char cChar)
Definition: Standard.cpp:67
void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL=-1, int iIndex=0)
Definition: Standard.cpp:138
bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive=false)
Definition: Standard.cpp:527
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen=-1)
Definition: Standard.cpp:177
int SModuleCount(const char *szList)
Definition: Standard.cpp:581
#define s
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45
bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild)
Definition: Standard.cpp:154
void SInsert(char *szString, const char *szInsert, int iPosition=0, int iMaxLen=-1)
Definition: Standard.cpp:473
const char * SSearchNoCase(const char *szString, const char *szIndex)
Definition: Standard.cpp:348
int SClearFrontBack(char *szString, char cClear=' ')
Definition: Standard.cpp:425
int iSize
Definition: TstC4NetIO.cpp:35