1 |
cycrow |
1 |
/*********************************************************
|
|
|
2 |
*
|
|
|
3 |
* STRUTILS char handling functions
|
|
|
4 |
* modified: 2.9.2004 09:34
|
|
|
5 |
*
|
|
|
6 |
* define STRUTILS_WIN_WCHAR to use str2wchar and wchar2str
|
|
|
7 |
*
|
|
|
8 |
********************************************************/
|
|
|
9 |
|
|
|
10 |
#ifndef STRUTILS_INCLUDED
|
|
|
11 |
#define STRUTILS_INCLUDED
|
|
|
12 |
|
|
|
13 |
#include <locale.h>
|
|
|
14 |
#include <string.h>
|
|
|
15 |
#ifdef STRUTILS_WIN_WCHAR
|
|
|
16 |
#include <windows.h>
|
|
|
17 |
#endif
|
|
|
18 |
|
|
|
19 |
#define STRUTILS_MACRO
|
|
|
20 |
|
|
|
21 |
//---------------------------------------------------------------------------------
|
|
|
22 |
size_t memrep(void *buffer, int c, int n, size_t count, size_t limit=0);
|
|
|
23 |
size_t strrep(char *string, char c, char n, size_t limit=0);
|
|
|
24 |
bool isinteger(const char *string);
|
|
|
25 |
bool isfloat(const char *string);
|
|
|
26 |
bool isblank(const char *string);
|
|
|
27 |
char ** strexplode(char separator, char *string, size_t *size, size_t limit=0);
|
|
|
28 |
char ** lineexplode(char *string, size_t size, size_t *count);
|
|
|
29 |
char * ExtractFileName(const char *pszPath, bool bNoExtension=false);
|
114 |
cycrow |
30 |
#ifdef STRUTILS_WIN_WCHAR
|
|
|
31 |
wchar_t * ExtractFileNameW(const wchar_t *pszPath, bool bNoExtension=false);
|
|
|
32 |
#endif
|
1 |
cycrow |
33 |
char * ExtractFilePath(const char *pszPath);
|
|
|
34 |
char * strcat2(int num, ...);
|
|
|
35 |
bool isblank(const char *str);
|
|
|
36 |
char * ChangeFileExtension(const char *pszFileName, const char *pszExt);
|
|
|
37 |
const char * GetFileExtension(const char *pszFileName);
|
|
|
38 |
int hextoi(const char *str);
|
|
|
39 |
bool ishexa(const char *str);
|
|
|
40 |
|
|
|
41 |
#ifdef STRUTILS_WIN_WCHAR
|
|
|
42 |
wchar_t * str2wchar(const char *sz);
|
|
|
43 |
char * wchar2str(const wchar_t *ws);
|
|
|
44 |
#endif
|
|
|
45 |
//---------------------------------------------------------------------------------
|
|
|
46 |
#ifdef STRUTILS_MACRO
|
|
|
47 |
|
|
|
48 |
#define strcreate(buffer, src) { delete[] buffer; \
|
|
|
49 |
buffer=new char[(src==NULL ? 0 : strlen(src)) + 1]; \
|
|
|
50 |
buffer[0]=0; \
|
|
|
51 |
if(src) strcpy(buffer, src); }
|
|
|
52 |
|
|
|
53 |
#else
|
|
|
54 |
|
|
|
55 |
inline void strcreate(char *&buffer, const char *src)
|
|
|
56 |
{
|
|
|
57 |
buffer=new char[(src==NULL ? 0 : strlen(src)) + 1];
|
|
|
58 |
buffer[0]=0;
|
|
|
59 |
if(src) strcpy(buffer, src);
|
|
|
60 |
}
|
|
|
61 |
#endif
|
|
|
62 |
//---------------------------------------------------------------------------------
|
|
|
63 |
#ifdef STRUTILS_MACRO
|
|
|
64 |
|
|
|
65 |
#define wstrcreate(buffer, src) delete[] buffer; \
|
|
|
66 |
buffer=new wchar_t[(src==NULL ? 0 : wcslen(src)) + 1]; \
|
|
|
67 |
buffer[0]=0; \
|
|
|
68 |
if(src) wcscpy(buffer, src);
|
|
|
69 |
|
|
|
70 |
#else
|
|
|
71 |
|
|
|
72 |
inline void wstrcreate(wchar_t *&buffer, const wchar_t *src)
|
|
|
73 |
{
|
|
|
74 |
buffer=new wchar_t[(src==NULL ? 0 : wcslen(src)) + 1];
|
|
|
75 |
buffer[0]=0;
|
|
|
76 |
if(src) wcscpy(buffer, src);
|
|
|
77 |
}
|
|
|
78 |
#endif
|
|
|
79 |
//---------------------------------------------------------------------------------
|
|
|
80 |
inline
|
|
|
81 |
char * trim(char *text)
|
|
|
82 |
{
|
|
|
83 |
while(*text==' ' || *text==9)
|
|
|
84 |
text++;
|
|
|
85 |
|
|
|
86 |
int e=(int)strlen(text);
|
|
|
87 |
for( ; e > 0 && text[e-1]==' ' || text[e-1]==9; e--)
|
|
|
88 |
;
|
|
|
89 |
|
|
|
90 |
text[e]=0;
|
|
|
91 |
return text;
|
|
|
92 |
}
|
|
|
93 |
//---------------------------------------------------------------------------------
|
|
|
94 |
inline size_t memrep(void *buffer, int c, int n, size_t count, size_t limit)
|
|
|
95 |
{
|
|
|
96 |
char *pos=(char*)buffer, *end=(char*)buffer + count;
|
|
|
97 |
size_t rep=0;
|
|
|
98 |
do{
|
|
|
99 |
pos=(char*)memchr(pos, c, end - pos);
|
|
|
100 |
if(pos) {
|
114 |
cycrow |
101 |
*pos = (char)n;
|
1 |
cycrow |
102 |
pos++;
|
|
|
103 |
rep++;
|
|
|
104 |
if(limit && rep >= limit) break;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
while(pos && (pos < end));
|
|
|
108 |
return rep;
|
|
|
109 |
}
|
|
|
110 |
//---------------------------------------------------------------------------------
|
|
|
111 |
inline size_t strrep(char *string, char c, char n, size_t limit)
|
|
|
112 |
{
|
|
|
113 |
return memrep(string, c, n, strlen(string), limit);
|
|
|
114 |
}
|
|
|
115 |
//---------------------------------------------------------------------------------
|
|
|
116 |
inline bool isinteger(const char *string)
|
|
|
117 |
{
|
|
|
118 |
size_t i=0;
|
|
|
119 |
size_t s=strlen(string);
|
|
|
120 |
|
|
|
121 |
bool bRes = s > 0;
|
|
|
122 |
if(bRes){
|
|
|
123 |
if(string[0]=='+' || string[0]=='-') i++;
|
|
|
124 |
for( ; i < s; ++i){
|
|
|
125 |
if(string[i] < '0' || string[i] > '9'){
|
|
|
126 |
bRes=false;
|
|
|
127 |
break;
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
return bRes;
|
|
|
132 |
}
|
|
|
133 |
//---------------------------------------------------------------------------------
|
|
|
134 |
inline bool isfloat(const char *string)
|
|
|
135 |
{
|
|
|
136 |
lconv *lc=localeconv();
|
|
|
137 |
size_t size=strlen(string), i=0;
|
|
|
138 |
int dec=0;
|
|
|
139 |
bool bRes = (size > 0);
|
|
|
140 |
if(bRes){
|
|
|
141 |
if(string[i]=='+' || string[i]=='-') i++;
|
|
|
142 |
// no decimal point on begining or end
|
|
|
143 |
if((bRes=(string[i]!=*(lc->decimal_point) && string[size-1]!=*(lc->decimal_point)))==true){
|
|
|
144 |
for( ; i < size; i++){
|
|
|
145 |
// only one decimal point allowed
|
|
|
146 |
if(string[i]==*(lc->decimal_point) && ++dec > 1) return false;
|
|
|
147 |
// allowed chars are 0-9 and decimal point
|
|
|
148 |
if((bRes=((string[i] >= '0' && string[i] <= '9') || string[i]==*(lc->decimal_point)))==false) break;
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
if(bRes==false){
|
|
|
153 |
if((string[i]|0x20)=='e' || (string[i]|0x20)=='d'){
|
|
|
154 |
i++;
|
|
|
155 |
if(string[i]=='+' || string[i]=='-') i++;
|
|
|
156 |
for( ; i < size; i++){
|
|
|
157 |
if((bRes=(string[i] >= '0' && string[i] <= '9'))==false) break;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
return bRes;
|
|
|
162 |
}
|
|
|
163 |
//---------------------------------------------------------------------------------
|
|
|
164 |
inline bool isblank(const char *str)
|
|
|
165 |
{
|
|
|
166 |
size_t i;
|
|
|
167 |
for(i=0; str[i]!=0; i++){
|
|
|
168 |
if(str[i]!=' ') return false;
|
|
|
169 |
}
|
|
|
170 |
return true;
|
|
|
171 |
}
|
|
|
172 |
//---------------------------------------------------------------------------------
|
|
|
173 |
#ifdef STRUTILS_WIN_WCHAR
|
|
|
174 |
inline wchar_t * str2wchar(const char *sz)
|
|
|
175 |
{
|
|
|
176 |
size_t len=strlen(sz);
|
|
|
177 |
wchar_t *ws=new wchar_t[len + 1];
|
114 |
cycrow |
178 |
MultiByteToWideChar(CP_ACP, 0, sz, (int)len, ws, (int)len);
|
1 |
cycrow |
179 |
ws[len]=0;
|
|
|
180 |
return ws;
|
|
|
181 |
}
|
|
|
182 |
//---------------------------------------------------------------------------------
|
|
|
183 |
inline char * wchar2str(const wchar_t *ws)
|
|
|
184 |
{
|
|
|
185 |
size_t len=wcslen(ws);
|
|
|
186 |
char *sz=new char[len + 1];
|
114 |
cycrow |
187 |
WideCharToMultiByte(CP_ACP, 0, ws, (int)len, sz, (int)len, 0, 0);
|
1 |
cycrow |
188 |
sz[len]=0;
|
|
|
189 |
return sz;
|
|
|
190 |
}
|
|
|
191 |
//---------------------------------------------------------------------------------
|
|
|
192 |
#endif // defined(STRUTILS_WIN_WCHAR)
|
|
|
193 |
|
|
|
194 |
#endif // !defined(STRUTILS_INCLUDED)
|