strfn.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "rar.hpp"
  2. const char *NullToEmpty(const char *Str)
  3. {
  4. return(Str==NULL ? "":Str);
  5. }
  6. const wchar *NullToEmpty(const wchar *Str)
  7. {
  8. return(Str==NULL ? L"":Str);
  9. }
  10. char *IntNameToExt(const char *Name)
  11. {
  12. static char OutName[NM];
  13. IntToExt(Name,OutName);
  14. return(OutName);
  15. }
  16. void ExtToInt(const char *Src,char *Dest)
  17. {
  18. #ifdef _WIN_ALL
  19. CharToOemA(Src,Dest);
  20. #else
  21. if (Dest!=Src)
  22. strcpy(Dest,Src);
  23. #endif
  24. }
  25. void IntToExt(const char *Src,char *Dest)
  26. {
  27. #ifdef _WIN_ALL
  28. OemToCharA(Src,Dest);
  29. #else
  30. if (Dest!=Src)
  31. strcpy(Dest,Src);
  32. #endif
  33. }
  34. char* strlower(char *Str)
  35. {
  36. #ifdef _WIN_ALL
  37. CharLowerA((LPSTR)Str);
  38. #else
  39. for (char *ChPtr=Str;*ChPtr;ChPtr++)
  40. *ChPtr=(char)loctolower(*ChPtr);
  41. #endif
  42. return(Str);
  43. }
  44. char* strupper(char *Str)
  45. {
  46. #ifdef _WIN_ALL
  47. CharUpperA((LPSTR)Str);
  48. #else
  49. for (char *ChPtr=Str;*ChPtr;ChPtr++)
  50. *ChPtr=(char)loctoupper(*ChPtr);
  51. #endif
  52. return(Str);
  53. }
  54. int stricomp(const char *Str1,const char *Str2)
  55. {
  56. char S1[NM*2],S2[NM*2];
  57. strncpyz(S1,Str1,ASIZE(S1));
  58. strncpyz(S2,Str2,ASIZE(S2));
  59. return(strcmp(strupper(S1),strupper(S2)));
  60. }
  61. int strnicomp(const char *Str1,const char *Str2,size_t N)
  62. {
  63. char S1[NM*2],S2[NM*2];
  64. strncpyz(S1,Str1,ASIZE(S1));
  65. strncpyz(S2,Str2,ASIZE(S2));
  66. return(strncmp(strupper(S1),strupper(S2),N));
  67. }
  68. char* RemoveEOL(char *Str)
  69. {
  70. for (int I=(int)strlen(Str)-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n' || Str[I]==' ' || Str[I]=='\t');I--)
  71. Str[I]=0;
  72. return(Str);
  73. }
  74. char* RemoveLF(char *Str)
  75. {
  76. for (int I=(int)strlen(Str)-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n');I--)
  77. Str[I]=0;
  78. return(Str);
  79. }
  80. wchar* RemoveLF(wchar *Str)
  81. {
  82. for (int I=(int)wcslen(Str)-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n');I--)
  83. Str[I]=0;
  84. return(Str);
  85. }
  86. unsigned char loctolower(unsigned char ch)
  87. {
  88. #ifdef _WIN_ALL
  89. // Convert to LPARAM first to avoid a warning in 64 bit mode.
  90. return((int)(LPARAM)CharLowerA((LPSTR)ch));
  91. #else
  92. return(tolower(ch));
  93. #endif
  94. }
  95. unsigned char loctoupper(unsigned char ch)
  96. {
  97. #ifdef _WIN_ALL
  98. // Convert to LPARAM first to avoid a warning in 64 bit mode.
  99. return((int)(LPARAM)CharUpperA((LPSTR)ch));
  100. #else
  101. return(toupper(ch));
  102. #endif
  103. }
  104. // toupper with English only results if English input is provided.
  105. // It avoids Turkish (small i) -> (big I with dot) conversion problem.
  106. // We do not define 'ch' as 'int' to avoid necessity to cast all
  107. // signed chars passed to this function to unsigned char.
  108. unsigned char etoupper(unsigned char ch)
  109. {
  110. if (ch=='i')
  111. return('I');
  112. return(toupper(ch));
  113. }
  114. // Unicode version of etoupper.
  115. wchar etoupperw(wchar ch)
  116. {
  117. if (ch=='i')
  118. return('I');
  119. return(toupperw(ch));
  120. }
  121. // We do not want to cast every signed char to unsigned when passing to
  122. // isdigit, so we implement the replacement. Shall work for Unicode too.
  123. // If chars are signed, conversion from char to int could generate negative
  124. // values, resulting in undefined behavior in standard isdigit.
  125. bool IsDigit(int ch)
  126. {
  127. return(ch>='0' && ch<='9');
  128. }
  129. // We do not want to cast every signed char to unsigned when passing to
  130. // isspace, so we implement the replacement. Shall work for Unicode too.
  131. // If chars are signed, conversion from char to int could generate negative
  132. // values, resulting in undefined behavior in standard isspace.
  133. bool IsSpace(int ch)
  134. {
  135. return(ch==' ' || ch=='\t');
  136. }
  137. // We do not want to cast every signed char to unsigned when passing to
  138. // isalpha, so we implement the replacement. Shall work for Unicode too.
  139. // If chars are signed, conversion from char to int could generate negative
  140. // values, resulting in undefined behavior in standard function.
  141. bool IsAlpha(int ch)
  142. {
  143. return(ch>='A' && ch<='Z' || ch>='a' && ch<='z');
  144. }
  145. #ifndef SFX_MODULE
  146. uint GetDigits(uint Number)
  147. {
  148. uint Digits=1;
  149. while (Number>=10)
  150. {
  151. Number/=10;
  152. Digits++;
  153. }
  154. return(Digits);
  155. }
  156. #endif
  157. bool LowAscii(const char *Str)
  158. {
  159. for (int I=0;Str[I]!=0;I++)
  160. if ((byte)Str[I]<32 || (byte)Str[I]>127)
  161. return(false);
  162. return(true);
  163. }
  164. bool LowAscii(const wchar *Str)
  165. {
  166. for (int I=0;Str[I]!=0;I++)
  167. {
  168. // We convert wchar_t to uint just in case if some compiler
  169. // uses the signed wchar_t.
  170. if ((uint)Str[I]<32 || (uint)Str[I]>127)
  171. return(false);
  172. }
  173. return(true);
  174. }
  175. int stricompc(const char *Str1,const char *Str2)
  176. {
  177. #if defined(_UNIX)
  178. return(strcmp(Str1,Str2));
  179. #else
  180. return(stricomp(Str1,Str2));
  181. #endif
  182. }
  183. #ifndef SFX_MODULE
  184. int wcsicompc(const wchar *Str1,const wchar *Str2)
  185. {
  186. #if defined(_UNIX)
  187. return(wcscmp(Str1,Str2));
  188. #else
  189. return(wcsicomp(Str1,Str2));
  190. #endif
  191. }
  192. #endif
  193. // safe strncpy: copies maxlen-1 max and always returns zero terminated dest
  194. char* strncpyz(char *dest, const char *src, size_t maxlen)
  195. {
  196. if (maxlen>0)
  197. {
  198. strncpy(dest,src,maxlen-1);
  199. dest[maxlen-1]=0;
  200. }
  201. return(dest);
  202. }
  203. // Safe wcsncpy: copies maxlen-1 max and always returns zero terminated dest.
  204. wchar* wcsncpyz(wchar *dest, const wchar *src, size_t maxlen)
  205. {
  206. if (maxlen>0)
  207. {
  208. wcsncpy(dest,src,maxlen-1);
  209. dest[maxlen-1]=0;
  210. }
  211. return(dest);
  212. }
  213. // Safe strncat: resulting dest length cannot exceed maxlen and dest
  214. // is always zero terminated. Note that 'maxlen' parameter defines the entire
  215. // dest buffer size and is not compatible with standard strncat.
  216. char* strncatz(char* dest, const char* src, size_t maxlen)
  217. {
  218. size_t Length = strlen(dest);
  219. if (Length + 1 < maxlen)
  220. strncat(dest, src, maxlen - Length - 1);
  221. return dest;
  222. }
  223. // Safe wcsncat: resulting dest length cannot exceed maxlen and dest
  224. // is always zero terminated. Note that 'maxlen' parameter defines the entire
  225. // dest buffer size and is not compatible with standard wcsncat.
  226. wchar* wcsncatz(wchar* dest, const wchar* src, size_t maxlen)
  227. {
  228. size_t Length = wcslen(dest);
  229. if (Length + 1 < maxlen)
  230. wcsncat(dest, src, maxlen - Length - 1);
  231. return dest;
  232. }
  233. void itoa(int64 n,char *Str)
  234. {
  235. char NumStr[50];
  236. size_t Pos=0;
  237. do
  238. {
  239. NumStr[Pos++]=char(n%10)+'0';
  240. n=n/10;
  241. } while (n!=0);
  242. for (size_t I=0;I<Pos;I++)
  243. Str[I]=NumStr[Pos-I-1];
  244. Str[Pos]=0;
  245. }
  246. int64 atoil(const char *Str)
  247. {
  248. int64 n=0;
  249. while (*Str>='0' && *Str<='9')
  250. {
  251. n=n*10+*Str-'0';
  252. Str++;
  253. }
  254. return(n);
  255. }
  256. void itoa(int64 n,wchar *Str)
  257. {
  258. wchar NumStr[50];
  259. size_t Pos=0;
  260. do
  261. {
  262. NumStr[Pos++]=wchar(n%10)+'0';
  263. n=n/10;
  264. } while (n!=0);
  265. for (size_t I=0;I<Pos;I++)
  266. Str[I]=NumStr[Pos-I-1];
  267. Str[Pos]=0;
  268. }
  269. int64 atoil(const wchar *Str)
  270. {
  271. int64 n=0;
  272. while (*Str>='0' && *Str<='9')
  273. {
  274. n=n*10+*Str-'0';
  275. Str++;
  276. }
  277. return(n);
  278. }
  279. const wchar* GetWide(const char *Src)
  280. {
  281. const size_t MaxLength=NM;
  282. static wchar StrTable[4][MaxLength];
  283. static uint StrNum=0;
  284. if (++StrNum >= ASIZE(StrTable))
  285. StrNum=0;
  286. wchar *Str=StrTable[StrNum];
  287. CharToWide(Src,Str,MaxLength);
  288. Str[MaxLength-1]=0;
  289. return(Str);
  290. }
  291. #ifdef _WIN_ALL
  292. // Parse string containing parameters separated with spaces.
  293. // Support quote marks. Param can be NULL to return the pointer to next
  294. // parameter, which can be used to estimate the buffer size for Param.
  295. const wchar* GetCmdParam(const wchar *CmdLine,wchar *Param,size_t MaxSize)
  296. {
  297. while (IsSpace(*CmdLine))
  298. CmdLine++;
  299. if (*CmdLine==0)
  300. return NULL;
  301. size_t ParamSize=0;
  302. bool Quote=false;
  303. while (*CmdLine!=0 && (Quote || !IsSpace(*CmdLine)))
  304. {
  305. if (*CmdLine=='\"')
  306. {
  307. if (CmdLine[1]=='\"')
  308. {
  309. // Insert the quote character instead of two adjoining quote characters.
  310. if (Param!=NULL && ParamSize<MaxSize-1)
  311. Param[ParamSize++]='\"';
  312. CmdLine++;
  313. }
  314. else
  315. Quote=!Quote;
  316. }
  317. else
  318. if (Param!=NULL && ParamSize<MaxSize-1)
  319. Param[ParamSize++]=*CmdLine;
  320. CmdLine++;
  321. }
  322. if (Param!=NULL)
  323. Param[ParamSize]=0;
  324. return CmdLine;
  325. }
  326. #endif