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