1 #include "rar.hpp"
2
NullToEmpty(const char * Str)3 const char *NullToEmpty(const char *Str)
4 {
5 return(Str==NULL ? "":Str);
6 }
7
8
NullToEmpty(const wchar * Str)9 const wchar *NullToEmpty(const wchar *Str)
10 {
11 return(Str==NULL ? L"":Str);
12 }
13
14
IntNameToExt(const char * Name)15 char *IntNameToExt(const char *Name)
16 {
17 static char OutName[NM];
18 IntToExt(Name,OutName);
19 return(OutName);
20 }
21
22
ExtToInt(const char * Src,char * Dest)23 void ExtToInt(const char *Src,char *Dest)
24 {
25 #if defined(_WIN_32)
26 CharToOem(Src,Dest);
27 #else
28 if (Dest!=Src)
29 strcpy(Dest,Src);
30 #endif
31 }
32
33
IntToExt(const char * Src,char * Dest)34 void IntToExt(const char *Src,char *Dest)
35 {
36 #if defined(_WIN_32)
37 OemToChar(Src,Dest);
38 #else
39 if (Dest!=Src)
40 strcpy(Dest,Src);
41 #endif
42 }
43
44
strlower(char * Str)45 char* strlower(char *Str)
46 {
47 #ifdef _WIN_32
48 CharLower((LPTSTR)Str);
49 #else
50 for (char *ChPtr=Str;*ChPtr;ChPtr++)
51 *ChPtr=(char)loctolower(*ChPtr);
52 #endif
53 return(Str);
54 }
55
56
strupper(char * Str)57 char* strupper(char *Str)
58 {
59 #ifdef _WIN_32
60 CharUpper((LPTSTR)Str);
61 #else
62 for (char *ChPtr=Str;*ChPtr;ChPtr++)
63 *ChPtr=(char)loctoupper(*ChPtr);
64 #endif
65 return(Str);
66 }
67
68
stricomp(const char * Str1,const char * Str2)69 int stricomp(const char *Str1,const char *Str2)
70 {
71 char S1[NM*2],S2[NM*2];
72 strncpyz(S1,Str1,ASIZE(S1));
73 strncpyz(S2,Str2,ASIZE(S2));
74 return(strcmp(strupper(S1),strupper(S2)));
75 }
76
77
strnicomp(const char * Str1,const char * Str2,size_t N)78 int strnicomp(const char *Str1,const char *Str2,size_t N)
79 {
80 char S1[NM*2],S2[NM*2];
81 strncpyz(S1,Str1,ASIZE(S1));
82 strncpyz(S2,Str2,ASIZE(S2));
83 return(strncmp(strupper(S1),strupper(S2),N));
84 }
85
86
RemoveEOL(char * Str)87 char* RemoveEOL(char *Str)
88 {
89 for (int I=(int)strlen(Str)-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n' || Str[I]==' ' || Str[I]=='\t');I--)
90 Str[I]=0;
91 return(Str);
92 }
93
94
RemoveLF(char * Str)95 char* RemoveLF(char *Str)
96 {
97 for (int I=(int)strlen(Str)-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n');I--)
98 Str[I]=0;
99 return(Str);
100 }
101
102
loctolower(unsigned char ch)103 unsigned char loctolower(unsigned char ch)
104 {
105 #ifdef _WIN_32
106 // Convert to LPARAM first to avoid a warning in 64 bit mode.
107 return((int)(LPARAM)CharLower((LPTSTR)ch));
108 #else
109 return(tolower(ch));
110 #endif
111 }
112
113
loctoupper(unsigned char ch)114 unsigned char loctoupper(unsigned char ch)
115 {
116 #ifdef _WIN_32
117 // Convert to LPARAM first to avoid a warning in 64 bit mode.
118 return((int)(LPARAM)CharUpper((LPTSTR)ch));
119 #else
120 return(toupper(ch));
121 #endif
122 }
123
124
125 // toupper with English only results if English input is provided.
126 // It avoids Turkish (small i) -> (big I with dot) conversion problem.
127 // We do not define 'ch' as 'int' to avoid necessity to cast all
128 // signed chars passed to this function to unsigned char.
etoupper(unsigned char ch)129 unsigned char etoupper(unsigned char ch)
130 {
131 if (ch=='i')
132 return('I');
133 return(toupper(ch));
134 }
135
136
137 // Unicode version of etoupper.
etoupperw(wchar ch)138 wchar etoupperw(wchar ch)
139 {
140 if (ch=='i')
141 return('I');
142 return(toupperw(ch));
143 }
144
145
146 // We do not want to cast every signed char to unsigned when passing to
147 // isdigit, so we implement the replacement. Shall work for Unicode too.
IsDigit(int ch)148 bool IsDigit(int ch)
149 {
150 return(ch>='0' && ch<='9');
151 }
152
153
154 // We do not want to cast every signed char to unsigned when passing to
155 // isspace, so we implement the replacement. Shall work for Unicode too.
IsSpace(int ch)156 bool IsSpace(int ch)
157 {
158 return(ch==' ' || ch=='\t');
159 }
160
161
162
163
164
LowAscii(const char * Str)165 bool LowAscii(const char *Str)
166 {
167 for (int I=0;Str[I]!=0;I++)
168 if ((byte)Str[I]<32 || (byte)Str[I]>127)
169 return(false);
170 return(true);
171 }
172
173
LowAscii(const wchar * Str)174 bool LowAscii(const wchar *Str)
175 {
176 for (int I=0;Str[I]!=0;I++)
177 {
178 // We convert wchar_t to uint just in case if some compiler
179 // uses signed wchar_t.
180 if ((uint)Str[I]<32 || (uint)Str[I]>127)
181 return(false);
182 }
183 return(true);
184 }
185
186
187
188
stricompc(const char * Str1,const char * Str2)189 int stricompc(const char *Str1,const char *Str2)
190 {
191 #if defined(_UNIX)
192 return(strcmp(Str1,Str2));
193 #else
194 return(stricomp(Str1,Str2));
195 #endif
196 }
197
198
199 #ifndef SFX_MODULE
stricompcw(const wchar * Str1,const wchar * Str2)200 int stricompcw(const wchar *Str1,const wchar *Str2)
201 {
202 #if defined(_UNIX)
203 return(strcmpw(Str1,Str2));
204 #else
205 return(stricmpw(Str1,Str2));
206 #endif
207 }
208 #endif
209
210
211 // safe strncpy: copies maxlen-1 max and always returns zero terminated dest
strncpyz(char * dest,const char * src,size_t maxlen)212 char* strncpyz(char *dest, const char *src, size_t maxlen)
213 {
214 if (maxlen>0)
215 {
216 strncpy(dest,src,maxlen-1);
217 dest[maxlen-1]=0;
218 }
219 return(dest);
220 }
221
222 // safe strncpyw: copies maxlen-1 max and always returns zero terminated dest
strncpyzw(wchar * dest,const wchar * src,size_t maxlen)223 wchar* strncpyzw(wchar *dest, const wchar *src, size_t maxlen)
224 {
225 if (maxlen>0)
226 {
227 strncpyw(dest,src,maxlen-1);
228 dest[maxlen-1]=0;
229 }
230 return(dest);
231 }
232
233
itoa(int64 n,char * Str)234 void itoa(int64 n,char *Str)
235 {
236 char NumStr[50];
237 size_t Pos=0;
238
239 do
240 {
241 NumStr[Pos++]=char(n%10)+'0';
242 n=n/10;
243 } while (n!=0);
244
245 for (size_t I=0;I<Pos;I++)
246 Str[I]=NumStr[Pos-I-1];
247 Str[Pos]=0;
248 }
249
250
atoil(char * Str)251 int64 atoil(char *Str)
252 {
253 int64 n=0;
254 while (*Str>='0' && *Str<='9')
255 {
256 n=n*10+*Str-'0';
257 Str++;
258 }
259 return(n);
260 }
261