OpenClonk
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 int32_t Angle(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iPrec = 1);
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 // sprintf wrapper
148 
149 #ifdef _WIN32
150 #define vsnprintf _vsprintf_p
151 #endif
152 
153 // old, insecure sprintf
154 inline int osprintf(char *str, const char *fmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
155 inline int osprintf(char *str, const char *fmt, ...)
156 {
157  va_list args; va_start(args, fmt);
158  return vsprintf(str, fmt, args);
159 }
160 
161 // secure sprintf
162 #define sprintf ssprintf
163 template <size_t N> inline int ssprintf(char(&str)[N], const char *fmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
164 template <size_t N>
165 inline int ssprintf(char(&str)[N], const char *fmt, ...)
166 {
167  va_list args; va_start(args, fmt);
168  int m = vsnprintf(str, N, fmt, args);
169  // Quick exit if vsnprintf failed
170  if (m < 0) return m;
171  if (static_cast<size_t>(m) >= N) { m = N-1; str[m] = 0; }
172  return m;
173 }
174 
175 // Checks a string for conformance with UTF-8
176 bool IsValidUtf8(const char *string, int length = -1);
177 
178 
179 std::string vstrprintf(const char *format, va_list args);
180 std::string strprintf(const char *format, ...) GNUC_FORMAT_ATTRIBUTE;
181 
182 #endif // INC_STANDARD
#define s
#define inline
Definition: C4Real.h:37
#define GNUC_FORMAT_ATTRIBUTE
#define GNUC_FORMAT_ATTRIBUTE_O
std::string strprintf(const char *format,...) GNUC_FORMAT_ATTRIBUTE
Definition: Standard.cpp:838
bool SWildcardMatchEx(const char *szString, const char *szWildcard)
Definition: Standard.cpp:635
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:354
bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive=false)
Definition: Standard.cpp:605
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:369
bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
Definition: Standard.cpp:529
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:204
void SDelete(char *szString, int iLen, int iPosition=0)
Definition: Standard.cpp:521
bool SCopySegmentEx(const char *fstr, int segn, char *tstr, char sepa1, char sepa2, int iMaxL=-1, bool fSkipWhitespace=false)
Definition: Standard.cpp:298
int DWordAligned(int val)
Definition: Standard.h:47
bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive=false)
Definition: Standard.cpp:576
bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild)
Definition: Standard.cpp:190
bool IsIdentifier(char cChar)
Definition: Standard.cpp:90
int Sign(T val)
Definition: Standard.h:45
void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL=0)
Definition: Standard.cpp:449
void SCopy(const char *szSource, char *sTarget)
Definition: Standard.cpp:163
int32_t StrToI32(const char *s, int base, const char **scan_end)
Definition: Standard.cpp:120
int SGetLine(const char *szText, const char *cpPosition)
Definition: Standard.cpp:479
uint32_t GetNextCharacter(const char **pszString)
Definition: Standard.h:84
void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL=-1, int iIndex=0)
Definition: Standard.cpp:172
bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive=false)
Definition: Standard.cpp:588
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:60
void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine)
Definition: Standard.cpp:399
int Pow(int base, int exponent)
Definition: Standard.cpp:69
const char * SAdvanceSpace(const char *szSPos)
Definition: Standard.cpp:420
int ssprintf(char(&str)[N], const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:165
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:25
int SLineGetCharacters(const char *szText, const char *cpPosition)
Definition: Standard.cpp:491
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen=-1)
Definition: Standard.cpp:226
size_t SLenUntil(const char *szStr, char cUntil)
Definition: Standard.h:75
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:155
bool IsValidUtf8(const char *string, int length=-1)
Definition: Standard.cpp:702
void SNewSegment(char *szStr, const char *szSepa=";")
Definition: Standard.cpp:474
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen=-1)
Definition: Standard.cpp:213
int SModuleCount(const char *szList)
Definition: Standard.cpp:617
void SInsert(char *szString, const char *szInsert, int iPosition=0, int iMaxLen=-1)
Definition: Standard.cpp:509
int GetCharacterCount(const char *s)
Definition: Standard.cpp:791
std::string vstrprintf(const char *format, va_list args)
Definition: Standard.cpp:822
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:271
int SCharPos(char cTarget, const char *szInStr, int iIndex=0)
Definition: Standard.cpp:239
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil=nullptr)
Definition: Standard.cpp:326
uint32_t GetNextUTF8Character(const char **pszString)
Definition: Standard.cpp:755
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:253
bool IsWhiteSpace(char cChar)
Definition: Standard.h:72
T Abs(T val)
Definition: Standard.h:42
std::enable_if< std::is_nothrow_default_constructible< T >::value >::type InplaceReconstruct(T *obj)
Definition: Standard.h:35
bool SCopySegment(const char *fstr, int segn, char *tstr, char sepa=';', int iMaxL=-1, bool fSkipWhitespace=false)
Definition: Standard.cpp:279
int32_t Angle(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iPrec=1)
Definition: Standard.cpp:37
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
bool SIsModule(const char *szList, const char *szString, int *ipIndex=nullptr, bool fCaseSensitive=false)
Definition: Standard.cpp:547
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
const char * SSearchNoCase(const char *szString, const char *szIndex)
Definition: Standard.cpp:384
int SClearFrontBack(char *szString, char cClear=' ')
Definition: Standard.cpp:461
unsigned int SCharCountEx(const char *szString, const char *szCharList)
Definition: Standard.cpp:343
bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive=false)
Definition: Standard.cpp:563
void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
Definition: Standard.h:65
void SCapitalize(char *szString)
Definition: Standard.cpp:360
void SAppend(const char *szSource, char *szTarget, int iMaxL=-1)
Definition: Standard.cpp:263
const char * SAdvancePast(const char *szSPos, char cPast)
Definition: Standard.cpp:438
bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize=-1)
Definition: Standard.cpp:539
char CharCapital(char cChar)
Definition: Standard.h:70
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
size_t SLen(const char *sptr)
Definition: Standard.h:74
int iSize
Definition: TstC4NetIO.cpp:32