Common.cpp 39 KB

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