Common.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "TextsCore.h"
  8. #include "Interface.h"
  9. #include <StrUtils.hpp>
  10. #include <math.h>
  11. #include <shfolder.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. //---------------------------------------------------------------------------
  16. // TCriticalSection
  17. //---------------------------------------------------------------------------
  18. __fastcall TCriticalSection::TCriticalSection()
  19. {
  20. FAcquired = 0;
  21. InitializeCriticalSection(&FSection);
  22. }
  23. //---------------------------------------------------------------------------
  24. __fastcall TCriticalSection::~TCriticalSection()
  25. {
  26. assert(FAcquired == 0);
  27. DeleteCriticalSection(&FSection);
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall TCriticalSection::Enter()
  31. {
  32. EnterCriticalSection(&FSection);
  33. FAcquired++;
  34. }
  35. //---------------------------------------------------------------------------
  36. void __fastcall TCriticalSection::Leave()
  37. {
  38. FAcquired--;
  39. LeaveCriticalSection(&FSection);
  40. }
  41. //---------------------------------------------------------------------------
  42. // TGuard
  43. //---------------------------------------------------------------------------
  44. __fastcall TGuard::TGuard(TCriticalSection * ACriticalSection) :
  45. FCriticalSection(ACriticalSection)
  46. {
  47. assert(ACriticalSection != NULL);
  48. FCriticalSection->Enter();
  49. }
  50. //---------------------------------------------------------------------------
  51. __fastcall TGuard::~TGuard()
  52. {
  53. FCriticalSection->Leave();
  54. }
  55. //---------------------------------------------------------------------------
  56. // TUnguard
  57. //---------------------------------------------------------------------------
  58. __fastcall TUnguard::TUnguard(TCriticalSection * ACriticalSection) :
  59. FCriticalSection(ACriticalSection)
  60. {
  61. assert(ACriticalSection != NULL);
  62. FCriticalSection->Leave();
  63. }
  64. //---------------------------------------------------------------------------
  65. __fastcall TUnguard::~TUnguard()
  66. {
  67. FCriticalSection->Enter();
  68. }
  69. //---------------------------------------------------------------------------
  70. //---------------------------------------------------------------------------
  71. const char EngShortMonthNames[12][4] =
  72. {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  73. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  74. //---------------------------------------------------------------------------
  75. AnsiString ReplaceChar(AnsiString Str, Char A, Char B)
  76. {
  77. for (Integer Index = 0; Index < Str.Length(); Index++)
  78. if (Str[Index+1] == A) Str[Index+1] = B;
  79. return Str;
  80. }
  81. //---------------------------------------------------------------------------
  82. AnsiString DeleteChar(AnsiString Str, Char C)
  83. {
  84. int P;
  85. while ((P = Str.Pos(C)) > 0)
  86. {
  87. Str.Delete(P, 1);
  88. }
  89. return Str;
  90. }
  91. //---------------------------------------------------------------------------
  92. void PackStr(AnsiString &Str)
  93. {
  94. // Following will free unnecessary bytes
  95. Str = Str.c_str();
  96. }
  97. //---------------------------------------------------------------------------
  98. AnsiString MakeValidFileName(AnsiString FileName)
  99. {
  100. AnsiString IllegalChars = ";,=+<>|\"[] \\/?*";
  101. for (int Index = 0; Index < IllegalChars.Length(); Index++)
  102. {
  103. FileName = ReplaceChar(FileName, IllegalChars[Index+1], '-');
  104. }
  105. return FileName;
  106. }
  107. //---------------------------------------------------------------------------
  108. AnsiString RootKeyToStr(HKEY RootKey)
  109. {
  110. if (RootKey == HKEY_USERS) return "HKEY_USERS";
  111. else
  112. if (RootKey == HKEY_LOCAL_MACHINE) return "HKEY_LOCAL_MACHINE";
  113. else
  114. if (RootKey == HKEY_CURRENT_USER) return "HKEY_CURRENT_USER";
  115. else
  116. if (RootKey == HKEY_CLASSES_ROOT) return "HKEY_CLASSES_ROOT";
  117. else
  118. if (RootKey == HKEY_CURRENT_CONFIG) return "HKEY_CURRENT_CONFIG";
  119. else
  120. if (RootKey == HKEY_DYN_DATA) return "HKEY_DYN_DATA";
  121. else
  122. { Abort(); return ""; };
  123. }
  124. //---------------------------------------------------------------------------
  125. AnsiString BooleanToEngStr(bool B)
  126. {
  127. return B ? "Yes" : "No";
  128. }
  129. //---------------------------------------------------------------------------
  130. AnsiString BooleanToStr(bool B)
  131. {
  132. return B ? LoadStr(YES_STR) : LoadStr(NO_STR);
  133. }
  134. //---------------------------------------------------------------------------
  135. AnsiString DefaultStr(const AnsiString & Str, const AnsiString & Default)
  136. {
  137. if (!Str.IsEmpty())
  138. {
  139. return Str;
  140. }
  141. else
  142. {
  143. return Default;
  144. }
  145. }
  146. //---------------------------------------------------------------------------
  147. AnsiString CutToChar(AnsiString &Str, Char Ch, bool Trim)
  148. {
  149. Integer P = Str.Pos(Ch);
  150. AnsiString Result;
  151. if (P)
  152. {
  153. Result = Str.SubString(1, P-1);
  154. Str.Delete(1, P);
  155. }
  156. else
  157. {
  158. Result = Str;
  159. Str = "";
  160. }
  161. if (Trim)
  162. {
  163. Result = Result.TrimRight();
  164. Str = Str.TrimLeft();
  165. }
  166. return Result;
  167. }
  168. //---------------------------------------------------------------------------
  169. AnsiString CopyToChars(const AnsiString & Str, int & From, AnsiString Chs, bool Trim,
  170. char * Delimiter)
  171. {
  172. int P;
  173. for (P = From; P <= Str.Length(); P++)
  174. {
  175. if (IsDelimiter(Chs, Str, P))
  176. {
  177. break;
  178. }
  179. }
  180. AnsiString Result;
  181. if (P <= Str.Length())
  182. {
  183. if (Delimiter != NULL)
  184. {
  185. *Delimiter = Str[P];
  186. }
  187. Result = Str.SubString(From, P-From);
  188. From = P+1;
  189. }
  190. else
  191. {
  192. if (Delimiter != NULL)
  193. {
  194. *Delimiter = '\0';
  195. }
  196. Result = Str.SubString(From, Str.Length() - From + 1);
  197. From = P;
  198. }
  199. if (Trim)
  200. {
  201. Result = Result.TrimRight();
  202. while ((P <= Str.Length()) && (Str[P] == ' '))
  203. {
  204. P++;
  205. }
  206. }
  207. return Result;
  208. }
  209. //---------------------------------------------------------------------------
  210. AnsiString DelimitStr(AnsiString Str, AnsiString Chars)
  211. {
  212. for (int i = 1; i <= Str.Length(); i++)
  213. {
  214. if (Str.IsDelimiter(Chars, i))
  215. {
  216. Str.Insert("\\", i);
  217. i++;
  218. }
  219. }
  220. return Str;
  221. }
  222. //---------------------------------------------------------------------------
  223. AnsiString ShellDelimitStr(AnsiString Str, char Quote)
  224. {
  225. AnsiString Chars = "$\\";
  226. if (Quote == '"')
  227. {
  228. Chars += "`\"";
  229. }
  230. return DelimitStr(Str, Chars);
  231. }
  232. //---------------------------------------------------------------------------
  233. AnsiString ExceptionLogString(Exception *E)
  234. {
  235. assert(E);
  236. if (E->InheritsFrom(__classid(Exception)))
  237. {
  238. AnsiString Msg;
  239. Msg = FORMAT("(%s) %s", (E->ClassName(), E->Message));
  240. if (E->InheritsFrom(__classid(ExtException)))
  241. {
  242. TStrings * MoreMessages = ((ExtException*)E)->MoreMessages;
  243. if (MoreMessages)
  244. {
  245. Msg += "\n" +
  246. StringReplace(MoreMessages->Text, "\r", "", TReplaceFlags() << rfReplaceAll);
  247. }
  248. }
  249. return Msg;
  250. }
  251. else
  252. {
  253. char Buffer[1024];
  254. ExceptionErrorMessage(ExceptObject(), ExceptAddr(), Buffer, sizeof(Buffer));
  255. return AnsiString(Buffer);
  256. }
  257. }
  258. //---------------------------------------------------------------------------
  259. bool IsDots(const AnsiString Str)
  260. {
  261. char * str = Str.c_str();
  262. return (str[strspn(str, ".")] == '\0');
  263. }
  264. //---------------------------------------------------------------------------
  265. bool IsNumber(const AnsiString Str)
  266. {
  267. int Value;
  268. return TryStrToInt(Str, Value);
  269. }
  270. //---------------------------------------------------------------------------
  271. AnsiString __fastcall SystemTemporaryDirectory()
  272. {
  273. AnsiString TempDir;
  274. TempDir.SetLength(MAX_PATH);
  275. TempDir.SetLength(GetTempPath(MAX_PATH, TempDir.c_str()));
  276. return TempDir;
  277. }
  278. //---------------------------------------------------------------------------
  279. AnsiString __fastcall GetShellFolderPath(int CSIdl)
  280. {
  281. AnsiString Result;
  282. HMODULE Shell32Lib = LoadLibrary("SHELL32.DLL");
  283. if (Shell32Lib != NULL)
  284. {
  285. PFNSHGETFOLDERPATH SHGetFolderPath = (PFNSHGETFOLDERPATH)
  286. GetProcAddress(Shell32Lib, "SHGetFolderPathA");
  287. if (SHGetFolderPath != NULL)
  288. {
  289. char Path[2 * MAX_PATH + 10] = "\0";
  290. if (SUCCEEDED(SHGetFolderPath(NULL, CSIdl, NULL, SHGFP_TYPE_CURRENT, Path)))
  291. {
  292. Result = Path;
  293. }
  294. }
  295. }
  296. return Result;
  297. }
  298. //---------------------------------------------------------------------------
  299. AnsiString __fastcall StripPathQuotes(const AnsiString Path)
  300. {
  301. if ((Path.Length() >= 2) &&
  302. (Path[1] == '\"') && (Path[Path.Length()] == '\"'))
  303. {
  304. return Path.SubString(2, Path.Length() - 2);
  305. }
  306. else
  307. {
  308. return Path;
  309. }
  310. }
  311. //---------------------------------------------------------------------------
  312. AnsiString __fastcall AddPathQuotes(AnsiString Path)
  313. {
  314. Path = StripPathQuotes(Path);
  315. if (Path.Pos(" "))
  316. {
  317. Path = "\"" + Path + "\"";
  318. }
  319. return Path;
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall SplitCommand(AnsiString Command, AnsiString &Program,
  323. AnsiString & Params, AnsiString & Dir)
  324. {
  325. Command = Command.Trim();
  326. Params = "";
  327. Dir = "";
  328. if (!Command.IsEmpty() && (Command[1] == '\"'))
  329. {
  330. Command.Delete(1, 1);
  331. int P = Command.Pos('"');
  332. if (P)
  333. {
  334. Program = Command.SubString(1, P-1).Trim();
  335. Params = Command.SubString(P + 1, Command.Length() - P).Trim();
  336. }
  337. else
  338. {
  339. throw Exception(FMTLOAD(INVALID_SHELL_COMMAND, ("\"" + Command)));
  340. }
  341. }
  342. else
  343. {
  344. int P = Command.Pos(" ");
  345. if (P)
  346. {
  347. Program = Command.SubString(1, P).Trim();
  348. Params = Command.SubString(P + 1, Command.Length() - P).Trim();
  349. }
  350. else
  351. {
  352. Program = Command;
  353. }
  354. }
  355. int B = Program.LastDelimiter("\\/");
  356. if (B)
  357. {
  358. Dir = Program.SubString(1, B).Trim();
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. AnsiString __fastcall ExtractProgram(AnsiString Command)
  363. {
  364. AnsiString Program;
  365. AnsiString Params;
  366. AnsiString Dir;
  367. SplitCommand(Command, Program, Params, Dir);
  368. return Program;
  369. }
  370. //---------------------------------------------------------------------------
  371. AnsiString __fastcall FormatCommand(AnsiString Program, AnsiString Params)
  372. {
  373. Program = Program.Trim();
  374. Params = Params.Trim();
  375. if (!Params.IsEmpty()) Params = " " + Params;
  376. if (Program.Pos(" ")) Program = "\"" + Program + "\"";
  377. return Program + Params;
  378. }
  379. //---------------------------------------------------------------------------
  380. const char ShellCommandFileNamePattern[] = "!.!";
  381. //---------------------------------------------------------------------------
  382. void __fastcall ReformatFileNameCommand(AnsiString & Command)
  383. {
  384. AnsiString Program, Params, Dir;
  385. SplitCommand(Command, Program, Params, Dir);
  386. if (Params.Pos(ShellCommandFileNamePattern) == 0)
  387. {
  388. Params = Params + (Params.IsEmpty() ? "" : " ") + ShellCommandFileNamePattern;
  389. }
  390. Command = FormatCommand(Program, Params);
  391. }
  392. //---------------------------------------------------------------------------
  393. AnsiString __fastcall ExpandFileNameCommand(const AnsiString Command,
  394. const AnsiString FileName)
  395. {
  396. return AnsiReplaceStr(Command, ShellCommandFileNamePattern,
  397. AddPathQuotes(FileName));
  398. }
  399. //---------------------------------------------------------------------------
  400. AnsiString __fastcall ExpandEnvironmentVariables(const AnsiString & Str)
  401. {
  402. AnsiString Buf;
  403. unsigned int Size = 1024;
  404. Buf.SetLength(Size);
  405. Buf.Unique();
  406. unsigned int Len = ExpandEnvironmentStrings(Str.c_str(), Buf.c_str(), Size);
  407. if (Len > Size)
  408. {
  409. Buf.SetLength(Len);
  410. Buf.Unique();
  411. ExpandEnvironmentStrings(Str.c_str(), Buf.c_str(), Len);
  412. }
  413. PackStr(Buf);
  414. return Buf;
  415. }
  416. //---------------------------------------------------------------------------
  417. bool __fastcall CompareFileName(const AnsiString & Path1, const AnsiString & Path2)
  418. {
  419. AnsiString ShortPath1 = ExtractShortPathName(Path1);
  420. AnsiString ShortPath2 = ExtractShortPathName(Path2);
  421. bool Result;
  422. // ExtractShortPathName returns empty string if file does not exist
  423. if (ShortPath1.IsEmpty() || ShortPath2.IsEmpty())
  424. {
  425. Result = AnsiSameText(Path1, Path2);
  426. }
  427. else
  428. {
  429. Result = AnsiSameText(ExtractShortPathName(Path1), ExtractShortPathName(Path2));
  430. }
  431. return Result;
  432. }
  433. //---------------------------------------------------------------------------
  434. bool __fastcall ComparePaths(const AnsiString & Path1, const AnsiString & Path2)
  435. {
  436. // TODO: ExpandUNCFileName
  437. return AnsiSameText(IncludeTrailingBackslash(Path1), IncludeTrailingBackslash(Path2));
  438. }
  439. //---------------------------------------------------------------------------
  440. bool __fastcall IsReservedName(AnsiString FileName)
  441. {
  442. int P = FileName.Pos(".");
  443. if (P > 0)
  444. {
  445. FileName.SetLength(P - 1);
  446. }
  447. static AnsiString Reserved[] = {
  448. "CON", "PRN", "AUX", "NUL",
  449. "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
  450. "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
  451. for (int Index = 0; Index < LENOF(Reserved); Index++)
  452. {
  453. if (SameText(FileName, Reserved[Index]))
  454. {
  455. return true;
  456. }
  457. }
  458. return false;
  459. }
  460. //---------------------------------------------------------------------------
  461. AnsiString __fastcall DisplayableStr(const AnsiString Str)
  462. {
  463. bool Displayable = true;
  464. int Index = 1;
  465. while ((Index <= Str.Length()) && Displayable)
  466. {
  467. if ((Str[Index] < '\32') &&
  468. (Str[Index] != '\n') && (Str[Index] != '\r') && (Str[Index] != '\t') && (Str[Index] != '\b'))
  469. {
  470. Displayable = false;
  471. }
  472. Index++;
  473. }
  474. AnsiString Result;
  475. if (Displayable)
  476. {
  477. Result = "\"";
  478. for (int Index = 1; Index <= Str.Length(); Index++)
  479. {
  480. switch (Str[Index])
  481. {
  482. case '\n':
  483. Result += "\\n";
  484. break;
  485. case '\r':
  486. Result += "\\r";
  487. break;
  488. case '\t':
  489. Result += "\\t";
  490. break;
  491. case '\b':
  492. Result += "\\b";
  493. break;
  494. case '\\':
  495. Result += "\\\\";
  496. break;
  497. case '"':
  498. Result += "\\\"";
  499. break;
  500. default:
  501. Result += Str[Index];
  502. break;
  503. }
  504. }
  505. Result += "\"";
  506. }
  507. else
  508. {
  509. Result = "0x" + StrToHex(Str);
  510. }
  511. return Result;
  512. }
  513. //---------------------------------------------------------------------------
  514. AnsiString __fastcall CharToHex(char Ch, bool UpperCase)
  515. {
  516. static char UpperDigits[] = "0123456789ABCDEF";
  517. static char LowerDigits[] = "0123456789abcdef";
  518. const char * Digits = (UpperCase ? UpperDigits : LowerDigits);
  519. AnsiString Result;
  520. Result.SetLength(2);
  521. Result[1] = Digits[((unsigned char)Ch & 0xF0) >> 4];
  522. Result[2] = Digits[ (unsigned char)Ch & 0x0F];
  523. return Result;
  524. }
  525. //---------------------------------------------------------------------------
  526. AnsiString __fastcall StrToHex(const AnsiString Str, bool UpperCase, char Separator)
  527. {
  528. AnsiString Result;
  529. for (int i = 1; i <= Str.Length(); i++)
  530. {
  531. Result += CharToHex(Str[i], UpperCase);
  532. if ((Separator != '\0') && (i < Str.Length()))
  533. {
  534. Result += Separator;
  535. }
  536. }
  537. return Result;
  538. }
  539. //---------------------------------------------------------------------------
  540. AnsiString __fastcall HexToStr(const AnsiString Hex)
  541. {
  542. static AnsiString Digits = "0123456789ABCDEF";
  543. AnsiString Result;
  544. int L, P1, P2;
  545. L = Hex.Length();
  546. if (L % 2 == 0)
  547. {
  548. for (int i = 1; i <= Hex.Length(); i += 2)
  549. {
  550. P1 = Digits.Pos((char)toupper(Hex[i]));
  551. P2 = Digits.Pos((char)toupper(Hex[i + 1]));
  552. if (P1 <= 0 || P2 <= 0)
  553. {
  554. Result = "";
  555. break;
  556. }
  557. else
  558. {
  559. Result += static_cast<char>((P1 - 1) * 16 + P2 - 1);
  560. }
  561. }
  562. }
  563. return Result;
  564. }
  565. //---------------------------------------------------------------------------
  566. unsigned int __fastcall HexToInt(const AnsiString Hex, int MinChars)
  567. {
  568. static AnsiString Digits = "0123456789ABCDEF";
  569. int Result = 0;
  570. int I = 1;
  571. while (I <= Hex.Length())
  572. {
  573. int A = Digits.Pos((char)toupper(Hex[I]));
  574. if (A <= 0)
  575. {
  576. if ((MinChars < 0) || (I <= MinChars))
  577. {
  578. Result = 0;
  579. }
  580. break;
  581. }
  582. Result = (Result * 16) + (A - 1);
  583. I++;
  584. }
  585. return Result;
  586. }
  587. //---------------------------------------------------------------------------
  588. char __fastcall HexToChar(const AnsiString Hex, int MinChars)
  589. {
  590. return (char)HexToInt(Hex, MinChars);
  591. }
  592. //---------------------------------------------------------------------------
  593. bool __fastcall FileSearchRec(const AnsiString FileName, TSearchRec & Rec)
  594. {
  595. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  596. bool Result = (FindFirst(FileName, FindAttrs, Rec) == 0);
  597. if (Result)
  598. {
  599. FindClose(Rec);
  600. }
  601. return Result;
  602. }
  603. //---------------------------------------------------------------------------
  604. void __fastcall ProcessLocalDirectory(AnsiString DirName,
  605. TProcessLocalFileEvent CallBackFunc, void * Param,
  606. int FindAttrs)
  607. {
  608. assert(CallBackFunc);
  609. if (FindAttrs < 0)
  610. {
  611. FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  612. }
  613. TSearchRec SearchRec;
  614. DirName = IncludeTrailingBackslash(DirName);
  615. if (FindFirst(DirName + "*.*", FindAttrs, SearchRec) == 0)
  616. {
  617. try
  618. {
  619. do
  620. {
  621. if ((SearchRec.Name != ".") && (SearchRec.Name != ".."))
  622. {
  623. CallBackFunc(DirName + SearchRec.Name, SearchRec, Param);
  624. }
  625. } while (FindNext(SearchRec) == 0);
  626. }
  627. __finally
  628. {
  629. FindClose(SearchRec);
  630. }
  631. }
  632. }
  633. //---------------------------------------------------------------------------
  634. struct TDateTimeParams
  635. {
  636. TDateTime UnixEpoch;
  637. double BaseDifference;
  638. long BaseDifferenceSec;
  639. double CurrentDaylightDifference;
  640. long CurrentDaylightDifferenceSec;
  641. double CurrentDifference;
  642. long CurrentDifferenceSec;
  643. double StandardDifference;
  644. long StandardDifferenceSec;
  645. double DaylightDifference;
  646. long DaylightDifferenceSec;
  647. SYSTEMTIME StandardDate;
  648. SYSTEMTIME DaylightDate;
  649. };
  650. static bool DateTimeParamsInitialized = false;
  651. static TDateTimeParams DateTimeParams;
  652. static TCriticalSection DateTimeParamsSection;
  653. //---------------------------------------------------------------------------
  654. static TDateTimeParams * __fastcall GetDateTimeParams()
  655. {
  656. if (!DateTimeParamsInitialized)
  657. {
  658. TGuard Guard(&DateTimeParamsSection);
  659. if (!DateTimeParamsInitialized)
  660. {
  661. TIME_ZONE_INFORMATION TZI;
  662. unsigned long GTZI;
  663. GTZI = GetTimeZoneInformation(&TZI);
  664. switch (GTZI)
  665. {
  666. case TIME_ZONE_ID_UNKNOWN:
  667. DateTimeParams.CurrentDifferenceSec = 0;
  668. DateTimeParams.CurrentDaylightDifferenceSec = 0;
  669. break;
  670. case TIME_ZONE_ID_STANDARD:
  671. DateTimeParams.CurrentDaylightDifferenceSec = TZI.StandardBias;
  672. break;
  673. case TIME_ZONE_ID_DAYLIGHT:
  674. DateTimeParams.CurrentDaylightDifferenceSec = TZI.DaylightBias;
  675. break;
  676. case TIME_ZONE_ID_INVALID:
  677. default:
  678. throw Exception(TIMEZONE_ERROR);
  679. }
  680. // Is it same as SysUtils::UnixDateDelta = 25569 ??
  681. DateTimeParams.UnixEpoch = EncodeDate(1970, 1, 1);
  682. DateTimeParams.BaseDifferenceSec = TZI.Bias;
  683. DateTimeParams.BaseDifference = double(TZI.Bias) / 1440;
  684. DateTimeParams.BaseDifferenceSec *= 60;
  685. DateTimeParams.CurrentDifferenceSec = TZI.Bias +
  686. DateTimeParams.CurrentDaylightDifferenceSec;
  687. DateTimeParams.CurrentDifference =
  688. double(DateTimeParams.CurrentDifferenceSec) / 1440;
  689. DateTimeParams.CurrentDifferenceSec *= 60;
  690. DateTimeParams.CurrentDaylightDifference =
  691. double(DateTimeParams.CurrentDaylightDifferenceSec) / 1440;
  692. DateTimeParams.CurrentDaylightDifferenceSec *= 60;
  693. DateTimeParams.DaylightDifferenceSec = TZI.DaylightBias * 60;
  694. DateTimeParams.DaylightDifference = double(TZI.DaylightBias) / 1440;
  695. DateTimeParams.StandardDifferenceSec = TZI.StandardBias * 60;
  696. DateTimeParams.StandardDifference = double(TZI.StandardBias) / 1440;
  697. DateTimeParams.StandardDate = TZI.StandardDate;
  698. DateTimeParams.DaylightDate = TZI.DaylightDate;
  699. DateTimeParamsInitialized = true;
  700. }
  701. }
  702. return &DateTimeParams;
  703. }
  704. //---------------------------------------------------------------------------
  705. static void __fastcall EncodeDSTMargin(const SYSTEMTIME & Date, unsigned short Year,
  706. TDateTime & Result)
  707. {
  708. if (Date.wYear == 0)
  709. {
  710. TDateTime Temp = EncodeDate(Year, Date.wMonth, 1);
  711. Result = Temp + ((Date.wDayOfWeek - DayOfWeek(Temp) + 8) % 7) +
  712. (7 * (Date.wDay - 1));
  713. if (Date.wDay == 5)
  714. {
  715. unsigned short Month = static_cast<unsigned short>(Date.wMonth + 1);
  716. if (Month > 12)
  717. {
  718. Month = static_cast<unsigned short>(Month - 12);
  719. Year++;
  720. }
  721. if (Result > EncodeDate(Year, Month, 1))
  722. {
  723. Result -= 7;
  724. }
  725. }
  726. Result += EncodeTime(Date.wHour, Date.wMinute, Date.wSecond,
  727. Date.wMilliseconds);
  728. }
  729. else
  730. {
  731. Result = EncodeDate(Year, Date.wMonth, Date.wDay) +
  732. EncodeTime(Date.wHour, Date.wMinute, Date.wSecond, Date.wMilliseconds);
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. static bool __fastcall IsDateInDST(const TDateTime & DateTime)
  737. {
  738. struct TDSTCache
  739. {
  740. bool Filled;
  741. unsigned short Year;
  742. TDateTime StandardDate;
  743. TDateTime DaylightDate;
  744. };
  745. static TDSTCache DSTCache[10];
  746. static int DSTCacheCount = 0;
  747. static TCriticalSection Section;
  748. TDateTimeParams * Params = GetDateTimeParams();
  749. bool Result;
  750. if (Params->StandardDate.wMonth == 0)
  751. {
  752. Result = false;
  753. }
  754. else
  755. {
  756. unsigned short Year, Month, Day;
  757. DecodeDate(DateTime, Year, Month, Day);
  758. TDSTCache * CurrentCache = &DSTCache[0];
  759. int CacheIndex = 0;
  760. while ((CacheIndex < DSTCacheCount) && (CacheIndex < LENOF(DSTCache)) &&
  761. CurrentCache->Filled && (CurrentCache->Year != Year))
  762. {
  763. CacheIndex++;
  764. CurrentCache++;
  765. }
  766. if ((CacheIndex < DSTCacheCount) && (CacheIndex < LENOF(DSTCache)) &&
  767. CurrentCache->Filled)
  768. {
  769. assert(CurrentCache->Year == Year);
  770. Result = (DateTime >= CurrentCache->DaylightDate) &&
  771. (DateTime < CurrentCache->StandardDate);
  772. }
  773. else
  774. {
  775. TDSTCache NewCache;
  776. EncodeDSTMargin(Params->StandardDate, Year, NewCache.StandardDate);
  777. EncodeDSTMargin(Params->DaylightDate, Year, NewCache.DaylightDate);
  778. if (DSTCacheCount < LENOF(DSTCache))
  779. {
  780. TGuard Guard(&Section);
  781. if (DSTCacheCount < LENOF(DSTCache))
  782. {
  783. NewCache.Year = Year;
  784. DSTCache[DSTCacheCount] = NewCache;
  785. DSTCache[DSTCacheCount].Filled = true;
  786. DSTCacheCount++;
  787. }
  788. }
  789. Result = (DateTime >= NewCache.DaylightDate) &&
  790. (DateTime < NewCache.StandardDate);
  791. }
  792. }
  793. return Result;
  794. }
  795. //---------------------------------------------------------------------------
  796. TDateTime __fastcall UnixToDateTime(__int64 TimeStamp, TDSTMode DSTMode)
  797. {
  798. TDateTimeParams * Params = GetDateTimeParams();
  799. TDateTime Result;
  800. Result = Params->UnixEpoch + (double(TimeStamp) / 86400);
  801. if ((DSTMode == dstmWin) || (DSTMode == dstmUnix))
  802. {
  803. Result -= Params->CurrentDifference;
  804. }
  805. else if (DSTMode == dstmKeep)
  806. {
  807. Result -= Params->BaseDifference;
  808. }
  809. if ((DSTMode == dstmUnix) || (DSTMode == dstmKeep))
  810. {
  811. Result -= (IsDateInDST(Result) ?
  812. Params->DaylightDifference : Params->StandardDifference);
  813. }
  814. return Result;
  815. }
  816. //---------------------------------------------------------------------------
  817. inline __int64 __fastcall Round(double Number)
  818. {
  819. double Floor = floor(Number);
  820. double Ceil = ceil(Number);
  821. return ((Number - Floor) > (Ceil - Number)) ? Ceil : Floor;
  822. }
  823. //---------------------------------------------------------------------------
  824. #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
  825. ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
  826. #define TIME_WIN_TO_POSIX(ft, t) ((t) = (__int64) \
  827. ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
  828. //---------------------------------------------------------------------------
  829. static __int64 __fastcall DateTimeToUnix(const TDateTime DateTime)
  830. {
  831. TDateTimeParams * Params = GetDateTimeParams();
  832. return Round(double(DateTime - Params->UnixEpoch) * 86400) +
  833. Params->CurrentDifferenceSec;
  834. }
  835. //---------------------------------------------------------------------------
  836. FILETIME __fastcall DateTimeToFileTime(const TDateTime DateTime,
  837. TDSTMode /*DSTMode*/)
  838. {
  839. FILETIME Result;
  840. __int64 UnixTimeStamp = DateTimeToUnix(DateTime);
  841. TIME_POSIX_TO_WIN(UnixTimeStamp, Result);
  842. return Result;
  843. }
  844. //---------------------------------------------------------------------------
  845. __int64 __fastcall ConvertTimestampToUnix(const FILETIME & FileTime,
  846. TDSTMode DSTMode)
  847. {
  848. __int64 Result;
  849. TIME_WIN_TO_POSIX(FileTime, Result);
  850. if ((DSTMode == dstmUnix) || (DSTMode == dstmKeep))
  851. {
  852. FILETIME LocalFileTime;
  853. SYSTEMTIME SystemTime;
  854. TDateTime DateTime;
  855. FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
  856. FileTimeToSystemTime(&LocalFileTime, &SystemTime);
  857. DateTime = SystemTimeToDateTime(SystemTime);
  858. TDateTimeParams * Params = GetDateTimeParams();
  859. Result += (IsDateInDST(DateTime) ?
  860. Params->DaylightDifferenceSec : Params->StandardDifferenceSec);
  861. if (DSTMode == dstmKeep)
  862. {
  863. Result -= Params->CurrentDaylightDifferenceSec;
  864. }
  865. }
  866. return Result;
  867. }
  868. //---------------------------------------------------------------------------
  869. TDateTime __fastcall ConvertTimestampToUTC(TDateTime DateTime)
  870. {
  871. TDateTimeParams * Params = GetDateTimeParams();
  872. DateTime += Params->CurrentDifference;
  873. DateTime +=
  874. (IsDateInDST(DateTime) ?
  875. Params->DaylightDifference : Params->StandardDifference);
  876. return DateTime;
  877. }
  878. //---------------------------------------------------------------------------
  879. __int64 __fastcall ConvertTimestampToUnixSafe(const FILETIME & FileTime,
  880. TDSTMode DSTMode)
  881. {
  882. __int64 Result;
  883. if ((FileTime.dwLowDateTime == 0) &&
  884. (FileTime.dwHighDateTime == 0))
  885. {
  886. Result = DateTimeToUnix(Now());
  887. }
  888. else
  889. {
  890. Result = ConvertTimestampToUnix(FileTime, DSTMode);
  891. }
  892. return Result;
  893. }
  894. //---------------------------------------------------------------------------
  895. TDateTime __fastcall AdjustDateTimeFromUnix(TDateTime DateTime, TDSTMode DSTMode)
  896. {
  897. TDateTimeParams * Params = GetDateTimeParams();
  898. if ((DSTMode == dstmWin) || (DSTMode == dstmUnix))
  899. {
  900. DateTime = DateTime - Params->CurrentDaylightDifference;
  901. }
  902. if (!IsDateInDST(DateTime))
  903. {
  904. if (DSTMode == dstmWin)
  905. {
  906. DateTime = DateTime - Params->DaylightDifference;
  907. }
  908. }
  909. else
  910. {
  911. DateTime = DateTime - Params->StandardDifference;
  912. }
  913. return DateTime;
  914. }
  915. //---------------------------------------------------------------------------
  916. __inline static bool __fastcall UnifySignificance(unsigned short & V1,
  917. unsigned short & V2)
  918. {
  919. bool Result = (V1 == 0) || (V2 == 0);
  920. if (Result)
  921. {
  922. V1 = 0;
  923. V2 = 0;
  924. }
  925. return Result;
  926. }
  927. //---------------------------------------------------------------------------
  928. void __fastcall UnifyDateTimePrecision(TDateTime & DateTime1, TDateTime & DateTime2)
  929. {
  930. unsigned short Y1, M1, D1, H1, N1, S1, MS1;
  931. unsigned short Y2, M2, D2, H2, N2, S2, MS2;
  932. bool Changed;
  933. if (DateTime1 != DateTime2)
  934. {
  935. DateTime1.DecodeDate(&Y1, &M1, &D1);
  936. DateTime1.DecodeTime(&H1, &N1, &S1, &MS1);
  937. DateTime2.DecodeDate(&Y2, &M2, &D2);
  938. DateTime2.DecodeTime(&H2, &N2, &S2, &MS2);
  939. Changed = UnifySignificance(MS1, MS2);
  940. if (Changed && UnifySignificance(S1, S2) && UnifySignificance(N1, N2) &&
  941. UnifySignificance(H1, H2) && UnifySignificance(D1, D2) &&
  942. UnifySignificance(M1, M2))
  943. {
  944. UnifySignificance(Y1, Y2);
  945. }
  946. if (Changed)
  947. {
  948. DateTime1 = EncodeDate(Y1, M1, D1) + EncodeTime(H1, N1, S1, MS1);
  949. DateTime2 = EncodeDate(Y2, M2, D2) + EncodeTime(H2, N2, S2, MS2);
  950. }
  951. }
  952. }
  953. //---------------------------------------------------------------------------
  954. AnsiString __fastcall FixedLenDateTimeFormat(const AnsiString & Format)
  955. {
  956. AnsiString Result = Format;
  957. bool AsIs = false;
  958. int Index = 1;
  959. while (Index <= Result.Length())
  960. {
  961. char F = Result[Index];
  962. if ((F == '\'') || (F == '\"'))
  963. {
  964. AsIs = !AsIs;
  965. Index++;
  966. }
  967. else if (!AsIs && ((F == 'a') || (F == 'A')))
  968. {
  969. if (Result.SubString(Index, 5).LowerCase() == "am/pm")
  970. {
  971. Index += 5;
  972. }
  973. else if (Result.SubString(Index, 3).LowerCase() == "a/p")
  974. {
  975. Index += 3;
  976. }
  977. else if (Result.SubString(Index, 4).LowerCase() == "ampm")
  978. {
  979. Index += 4;
  980. }
  981. else
  982. {
  983. Index++;
  984. }
  985. }
  986. else
  987. {
  988. if (!AsIs && (strchr("dDeEmMhHnNsS", F) != NULL) &&
  989. ((Index == Result.Length()) || (Result[Index + 1] != F)))
  990. {
  991. Result.Insert(F, Index);
  992. }
  993. while ((Index <= Result.Length()) && (F == Result[Index]))
  994. {
  995. Index++;
  996. }
  997. }
  998. }
  999. return Result;
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. int __fastcall CompareFileTime(TDateTime T1, TDateTime T2)
  1003. {
  1004. // "FAT" time precision
  1005. // (when one time is seconds-precision and other is millisecond-precision,
  1006. // we may have times like 12:00:00.000 and 12:00:01.999, which should
  1007. // be treated the same)
  1008. static TDateTime TwoSeconds(0, 0, 2, 0);
  1009. int Result;
  1010. if (T1 == T2)
  1011. {
  1012. // just optimalisation
  1013. Result = 0;
  1014. }
  1015. else if ((T1 < T2) && (T2 - T1 >= TwoSeconds))
  1016. {
  1017. Result = -1;
  1018. }
  1019. else if ((T1 > T2) && (T1 - T2 >= TwoSeconds))
  1020. {
  1021. Result = 1;
  1022. }
  1023. else
  1024. {
  1025. Result = 0;
  1026. }
  1027. return Result;
  1028. }
  1029. //---------------------------------------------------------------------------
  1030. bool __fastcall RecursiveDeleteFile(const AnsiString FileName, bool ToRecycleBin)
  1031. {
  1032. SHFILEOPSTRUCT Data;
  1033. memset(&Data, 0, sizeof(Data));
  1034. Data.hwnd = NULL;
  1035. Data.wFunc = FO_DELETE;
  1036. AnsiString FileList(FileName);
  1037. FileList.SetLength(FileList.Length() + 2);
  1038. FileList[FileList.Length() - 1] = '\0';
  1039. FileList[FileList.Length()] = '\0';
  1040. Data.pFrom = FileList.c_str();
  1041. Data.pTo = "";
  1042. Data.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION | FOF_NOCONFIRMMKDIR |
  1043. FOF_NOERRORUI | FOF_SILENT;
  1044. if (ToRecycleBin)
  1045. {
  1046. Data.fFlags |= FOF_ALLOWUNDO;
  1047. }
  1048. int ErrorCode = SHFileOperation(&Data);
  1049. bool Result = (ErrorCode == 0);
  1050. if (!Result)
  1051. {
  1052. // according to MSDN, SHFileOperation may return following non-Win32
  1053. // error codes
  1054. if (((ErrorCode >= 0x71) && (ErrorCode <= 0x88)) ||
  1055. (ErrorCode == 0xB7) || (ErrorCode == 0x402) || (ErrorCode == 0x10000) ||
  1056. (ErrorCode == 0x10074))
  1057. {
  1058. ErrorCode = 0;
  1059. }
  1060. SetLastError(ErrorCode);
  1061. }
  1062. return Result;
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. int __fastcall CancelAnswer(int Answers)
  1066. {
  1067. int Result;
  1068. if ((Answers & qaCancel) != 0)
  1069. {
  1070. Result = qaCancel;
  1071. }
  1072. else if ((Answers & qaNo) != 0)
  1073. {
  1074. Result = qaNo;
  1075. }
  1076. else if ((Answers & qaAbort) != 0)
  1077. {
  1078. Result = qaAbort;
  1079. }
  1080. else if ((Answers & qaOK) != 0)
  1081. {
  1082. Result = qaOK;
  1083. }
  1084. else
  1085. {
  1086. assert(false);
  1087. Result = qaCancel;
  1088. }
  1089. return Result;
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. int __fastcall AbortAnswer(int Answers)
  1093. {
  1094. int Result;
  1095. if (FLAGSET(Answers, qaAbort))
  1096. {
  1097. Result = qaAbort;
  1098. }
  1099. else
  1100. {
  1101. Result = CancelAnswer(Answers);
  1102. }
  1103. return Result;
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. int __fastcall ContinueAnswer(int Answers)
  1107. {
  1108. int Result;
  1109. if (FLAGSET(Answers, qaSkip))
  1110. {
  1111. Result = qaSkip;
  1112. }
  1113. else if (FLAGSET(Answers, qaIgnore))
  1114. {
  1115. Result = qaIgnore;
  1116. }
  1117. else if (FLAGSET(Answers, qaYes))
  1118. {
  1119. Result = qaYes;
  1120. }
  1121. else if (FLAGSET(Answers, qaOK))
  1122. {
  1123. Result = qaOK;
  1124. }
  1125. else if (FLAGSET(Answers, qaRetry))
  1126. {
  1127. Result = qaRetry;
  1128. }
  1129. else
  1130. {
  1131. Result = CancelAnswer(Answers);
  1132. }
  1133. return Result;
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. TPasLibModule * __fastcall FindModule(void * Instance)
  1137. {
  1138. TPasLibModule * CurModule;
  1139. CurModule = reinterpret_cast<TPasLibModule*>(LibModuleList);
  1140. while (CurModule)
  1141. {
  1142. if (CurModule->Instance == Instance)
  1143. {
  1144. break;
  1145. }
  1146. else
  1147. {
  1148. CurModule = CurModule->Next;
  1149. }
  1150. }
  1151. return CurModule;
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. AnsiString __fastcall LoadStr(int Ident, unsigned int MaxLength)
  1155. {
  1156. TPasLibModule * MainModule = FindModule(HInstance);
  1157. assert(MainModule != NULL);
  1158. AnsiString Result;
  1159. Result.SetLength(MaxLength);
  1160. int Length = LoadString(MainModule->ResInstance, Ident, Result.c_str(), MaxLength);
  1161. Result.SetLength(Length);
  1162. return Result;
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. AnsiString __fastcall LoadStrPart(int Ident, int Part)
  1166. {
  1167. AnsiString Result;
  1168. AnsiString Str = LoadStr(Ident);
  1169. while (Part > 0)
  1170. {
  1171. Result = CutToChar(Str, '|', false);
  1172. Part--;
  1173. }
  1174. return Result;
  1175. }
  1176. //---------------------------------------------------------------------------
  1177. AnsiString __fastcall DecodeUrlChars(AnsiString S)
  1178. {
  1179. int i = 1;
  1180. while (i <= S.Length())
  1181. {
  1182. switch (S[i])
  1183. {
  1184. case '+':
  1185. S[i] = ' ';
  1186. break;
  1187. case '%':
  1188. if (i <= S.Length() - 2)
  1189. {
  1190. AnsiString C = HexToStr(S.SubString(i + 1, 2));
  1191. if (C.Length() == 1)
  1192. {
  1193. S[i] = C[1];
  1194. S.Delete(i + 1, 2);
  1195. }
  1196. }
  1197. break;
  1198. }
  1199. i++;
  1200. }
  1201. return S;
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. AnsiString __fastcall DoEncodeUrl(AnsiString S, AnsiString Chars)
  1205. {
  1206. int i = 1;
  1207. while (i <= S.Length())
  1208. {
  1209. if (Chars.Pos(S[i]) > 0)
  1210. {
  1211. AnsiString H = CharToHex(S[i]);
  1212. S.Insert(H, i + 1);
  1213. S[i] = '%';
  1214. i += H.Length();
  1215. }
  1216. i++;
  1217. }
  1218. return S;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. AnsiString __fastcall EncodeUrlChars(AnsiString S, AnsiString Ignore)
  1222. {
  1223. AnsiString Chars;
  1224. if (Ignore.Pos(' ') == 0)
  1225. {
  1226. Chars += ' ';
  1227. }
  1228. if (Ignore.Pos('/') == 0)
  1229. {
  1230. Chars += '/';
  1231. }
  1232. return DoEncodeUrl(S, Chars);
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. AnsiString __fastcall NonUrlChars()
  1236. {
  1237. AnsiString S;
  1238. for (unsigned int I = 0; I <= 255; I++)
  1239. {
  1240. char C = static_cast<char>(I);
  1241. if (((C >= 'a') && (C <= 'z')) ||
  1242. ((C >= 'A') && (C <= 'Z')) ||
  1243. ((C >= '0') && (C <= '9')) ||
  1244. (C == '_') || (C == '-') || (C == '.'))
  1245. {
  1246. // noop
  1247. }
  1248. else
  1249. {
  1250. S += C;
  1251. }
  1252. }
  1253. return S;
  1254. }
  1255. //---------------------------------------------------------------------------
  1256. AnsiString __fastcall EncodeUrlString(AnsiString S)
  1257. {
  1258. return DoEncodeUrl(S, NonUrlChars());
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. void __fastcall OemToAnsi(AnsiString & Str)
  1262. {
  1263. if (!Str.IsEmpty())
  1264. {
  1265. Str.Unique();
  1266. OemToChar(Str.c_str(), Str.c_str());
  1267. }
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. void __fastcall AnsiToOem(AnsiString & Str)
  1271. {
  1272. if (!Str.IsEmpty())
  1273. {
  1274. Str.Unique();
  1275. CharToOem(Str.c_str(), Str.c_str());
  1276. }
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. AnsiString __fastcall EscapeHotkey(const AnsiString & Caption)
  1280. {
  1281. return StringReplace(Caption, "&", "&&", TReplaceFlags() << rfReplaceAll);
  1282. }
  1283. //---------------------------------------------------------------------------
  1284. bool __fastcall CutToken(AnsiString & Str, AnsiString & Token)
  1285. {
  1286. bool Result;
  1287. Token = "";
  1288. // inspired by Putty's sftp_getcmd() from PSFTP.C
  1289. int Index = 1;
  1290. while ((Index <= Str.Length()) &&
  1291. ((Str[Index] == ' ') || (Str[Index] == '\t')))
  1292. {
  1293. Index++;
  1294. }
  1295. if (Index <= Str.Length())
  1296. {
  1297. bool Quoting = false;
  1298. while (Index <= Str.Length())
  1299. {
  1300. if (!Quoting && ((Str[Index] == ' ') || (Str[Index] == '\t')))
  1301. {
  1302. break;
  1303. }
  1304. else if ((Str[Index] == '"') && (Index + 1 <= Str.Length()) &&
  1305. (Str[Index + 1] == '"'))
  1306. {
  1307. Index += 2;
  1308. Token += '"';
  1309. }
  1310. else if (Str[Index] == '"')
  1311. {
  1312. Index++;
  1313. Quoting = !Quoting;
  1314. }
  1315. else
  1316. {
  1317. Token += Str[Index];
  1318. Index++;
  1319. }
  1320. }
  1321. if (Index <= Str.Length())
  1322. {
  1323. Index++;
  1324. }
  1325. Str = Str.SubString(Index, Str.Length());
  1326. Result = true;
  1327. }
  1328. else
  1329. {
  1330. Result = false;
  1331. Str = "";
  1332. }
  1333. return Result;
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall AddToList(AnsiString & List, const AnsiString & Value, char Delimiter)
  1337. {
  1338. if (!List.IsEmpty() && (List[List.Length()] != Delimiter))
  1339. {
  1340. List += Delimiter;
  1341. }
  1342. List += Value;
  1343. }