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 
41 // Small helpers
42 template <class T> inline T Abs(T val) { return val > 0 ? val : -val; }
43 template <class T, class U, class V> inline bool Inside(T ival, U lbound, V rbound) { return ival >= lbound && ival <= rbound; }
44 template <class T> inline T Clamp(T bval, T lbound, T rbound) { return bval < lbound ? lbound : bval > rbound ? rbound : bval; }
45 template <class T> inline int Sign(T val) { return val < 0 ? -1 : val > 0 ? 1 : 0; }
46 
47 inline int DWordAligned(int val)
48 {
49  if (val%4) { val>>=2; val<<=2; val+=4; }
50  return val;
51 }
52 
53 int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2);
54 int Angle(int iX1, int iY1, int iX2, int iY2);
55 int Pow(int base, int exponent);
56 int32_t StrToI32(const char *s, int base, const char **scan_end);
57 
58 template <class T>
59 typename std::enable_if<std::is_pod<T>::value>::type
60 inline ZeroMem(T *lpMem, size_t dwSize)
61 {
62  std::memset(lpMem,'\0',dwSize);
63 }
64 
65 inline void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
66 {
67  std::memmove(lpMem2,lpMem1,dwSize);
68 }
69 
70 inline char CharCapital(char cChar) { return std::toupper(cChar); }
71 bool IsIdentifier(char cChar);
72 inline bool IsWhiteSpace(char cChar) { return !!std::isspace((unsigned char)cChar); }
73 
74 inline size_t SLen(const char *sptr) { return sptr?std::strlen(sptr):0; }
75 inline size_t SLenUntil(const char *szStr, char cUntil)
76 {
77  if (!szStr) return 0;
78  const char *end = std::strchr(szStr, cUntil);
79  return end ? end-szStr : std::strlen(szStr);
80 }
81 
82 // get a character at the current string pos and advance pos by that character
83 uint32_t GetNextUTF8Character(const char **pszString); // GetNextCharacter helper
84 inline uint32_t GetNextCharacter(const char **pszString)
85 {
86  unsigned char c=**pszString;
87  if (c<128) { ++*pszString; return c; }
88  else return GetNextUTF8Character(pszString);
89 }
90 // Get string length in characters (not bytes)
91 int GetCharacterCount(const char * s);
92 
93 inline bool SEqual(const char *szStr1, const char *szStr2) { return szStr1&&szStr2?!std::strcmp(szStr1,szStr2):false; }
94 bool SEqual2(const char *szStr1, const char *szStr2);
95 bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild);
96 bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen=-1);
97 bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen = -1);
98 
99 void SCopy(const char *szSource, char *sTarget);
100 void SCopy(const char *szSource, char *sTarget, size_t iMaxL);
101 void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL=-1, int iIndex=0);
102 void SCopyUntil(const char *szSource, char *sTarget, const char * sUntil, size_t iMaxL);
103 void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL=0);
104 bool SCopySegment(const char *fstr, int segn, char *tstr, char sepa=';', int iMaxL=-1, bool fSkipWhitespace=false);
105 bool SCopySegmentEx(const char *fstr, int segn, char *tstr, char sepa1, char sepa2, int iMaxL=-1, bool fSkipWhitespace=false);
106 bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize);
107 
108 void SAppend(const char *szSource, char *szTarget, int iMaxL=-1);
109 void SAppendChar(char cChar, char *szStr);
110 
111 void SInsert(char *szString, const char *szInsert, int iPosition=0, int iMaxLen=-1);
112 void SDelete(char *szString, int iLen, int iPosition=0);
113 
114 int SCharPos(char cTarget, const char *szInStr, int iIndex=0);
115 int SCharLastPos(char cTarget, const char *szInStr);
116 unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil=nullptr);
117 unsigned int SCharCountEx(const char *szString, const char *szCharList);
118 
119 void SReplaceChar(char *str, char fc, char tc);
120 
121 const char *SSearch(const char *szString, const char *szIndex);
122 const char *SSearchNoCase(const char *szString, const char *szIndex);
123 
124 const char *SAdvanceSpace(const char *szSPos);
125 const char *SAdvancePast(const char *szSPos, char cPast);
126 
127 bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize=-1);
128 bool SIsModule(const char *szList, const char *szString, int *ipIndex=nullptr, bool fCaseSensitive=false);
129 bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive=false);
130 bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive=false);
131 bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive=false);
132 bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive=false);
133 int SModuleCount(const char *szList);
134 
135 void SNewSegment(char *szStr, const char *szSepa=";");
136 void SCapitalize(char *szString);
137 void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine);
138 int SClearFrontBack(char *szString, char cClear=' ');
139 
140 int SGetLine(const char *szText, const char *cpPosition);
141 int SLineGetCharacters(const char *szText, const char *cpPosition);
142 
143 // case sensitive wildcard match with some extra functionality
144 // can match strings like "*Cl?nk*vour" to "Clonk Endeavour"
145 bool SWildcardMatchEx(const char *szString, const char *szWildcard);
146 
147 #define LineFeed "\x00D\x00A"
148 
149 // sprintf wrapper
150 
151 #ifdef _WIN32
152 #define vsnprintf _vsprintf_p
153 #endif
154 
155 // old, insecure sprintf
156 inline int osprintf(char *str, const char *fmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
157 inline int osprintf(char *str, const char *fmt, ...)
158 {
159  va_list args; va_start(args, fmt);
160  return vsprintf(str, fmt, args);
161 }
162 
163 // secure sprintf
164 #define sprintf ssprintf
165 template <size_t N> inline int ssprintf(char(&str)[N], const char *fmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
166 template <size_t N>
167 inline int ssprintf(char(&str)[N], const char *fmt, ...)
168 {
169  va_list args; va_start(args, fmt);
170  int m = vsnprintf(str, N, fmt, args);
171  // Quick exit if vsnprintf failed
172  if (m < 0) return m;
173  if (static_cast<size_t>(m) >= N) { m = N-1; str[m] = 0; }
174  return m;
175 }
176 
177 // Checks a string for conformance with UTF-8
178 bool IsValidUtf8(const char *string, int length = -1);
179 
180 
181 std::string vstrprintf(const char *format, va_list args);
182 std::string strprintf(const char *format, ...) GNUC_FORMAT_ATTRIBUTE;
183 
184 #endif // INC_STANDARD
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen=-1)
Definition: Standard.cpp:198
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:157
bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive=false)
Definition: Standard.cpp:577
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:176
int DWordAligned(int val)
Definition: Standard.h:47
int Pow(int base, int exponent)
Definition: Standard.cpp:47
#define inline
Definition: C4Real.h:37
bool SIsModule(const char *szList, const char *szString, int *ipIndex=nullptr, bool fCaseSensitive=false)
Definition: Standard.cpp:519
void SNewSegment(char *szStr, const char *szSepa=";")
Definition: Standard.cpp:446
uint32_t GetNextCharacter(const char **pszString)
Definition: Standard.h:84
#define GNUC_FORMAT_ATTRIBUTE
size_t SLenUntil(const char *szStr, char cUntil)
Definition: Standard.h:75
int32_t StrToI32(const char *s, int base, const char **scan_end)
Definition: Standard.cpp:98
bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize=-1)
Definition: Standard.cpp:511
bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive=false)
Definition: Standard.cpp:548
bool SCopySegmentEx(const char *fstr, int segn, char *tstr, char sepa1, char sepa2, int iMaxL=-1, bool fSkipWhitespace=false)
Definition: Standard.cpp:270
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:341
uint32_t GetNextUTF8Character(const char **pszString)
Definition: Standard.cpp:727
bool IsWhiteSpace(char cChar)
Definition: Standard.h:72
int Sign(T val)
Definition: Standard.h:45
std::string vstrprintf(const char *format, va_list args)
Definition: Standard.cpp:794
void SCopy(const char *szSource, char *sTarget)
Definition: Standard.cpp:139
size_t SLen(const char *sptr)
Definition: Standard.h:74
void SAppend(const char *szSource, char *szTarget, int iMaxL=-1)
Definition: Standard.cpp:235
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
int Angle(int iX1, int iY1, int iX2, int iY2)
Definition: Standard.cpp:36
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
int SLineGetCharacters(const char *szText, const char *cpPosition)
Definition: Standard.cpp:463
int SGetLine(const char *szText, const char *cpPosition)
Definition: Standard.cpp:451
bool IsValidUtf8(const char *string, int length=-1)
Definition: Standard.cpp:674
unsigned int SCharCountEx(const char *szString, const char *szCharList)
Definition: Standard.cpp:315
void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine)
Definition: Standard.cpp:371
#define GNUC_FORMAT_ATTRIBUTE_O
const char * SAdvanceSpace(const char *szSPos)
Definition: Standard.cpp:392
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:243
bool SWildcardMatchEx(const char *szString, const char *szWildcard)
Definition: Standard.cpp:607
bool SCopySegment(const char *fstr, int segn, char *tstr, char sepa=';', int iMaxL=-1, bool fSkipWhitespace=false)
Definition: Standard.cpp:251
const char * SAdvancePast(const char *szSPos, char cPast)
Definition: Standard.cpp:410
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:25
bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
Definition: Standard.cpp:501
void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
Definition: Standard.h:65
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:326
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:763
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:225
bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive=false)
Definition: Standard.cpp:560
char CharCapital(char cChar)
Definition: Standard.h:70
int SCharPos(char cTarget, const char *szInStr, int iIndex=0)
Definition: Standard.cpp:211
int ssprintf(char(&str)[N], const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:167
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil=nullptr)
Definition: Standard.cpp:298
void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL=0)
Definition: Standard.cpp:421
void SCapitalize(char *szString)
Definition: Standard.cpp:332
std::string strprintf(const char *format,...) GNUC_FORMAT_ATTRIBUTE
Definition: Standard.cpp:810
T Abs(T val)
Definition: Standard.h:42
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:60
void SDelete(char *szString, int iLen, int iPosition=0)
Definition: Standard.cpp:493
bool IsIdentifier(char cChar)
Definition: Standard.cpp:68
void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL=-1, int iIndex=0)
Definition: Standard.cpp:146
bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive=false)
Definition: Standard.cpp:535
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen=-1)
Definition: Standard.cpp:185
int SModuleCount(const char *szList)
Definition: Standard.cpp:589
#define s
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild)
Definition: Standard.cpp:162
void SInsert(char *szString, const char *szInsert, int iPosition=0, int iMaxLen=-1)
Definition: Standard.cpp:481
const char * SSearchNoCase(const char *szString, const char *szIndex)
Definition: Standard.cpp:356
int SClearFrontBack(char *szString, char cClear=' ')
Definition: Standard.cpp:433
int iSize
Definition: TstC4NetIO.cpp:32