CnBaseUtils.pas 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. unit CnBaseUtils;
  2. interface
  3. {$I CnWizards.inc}
  4. {$IFDEF DELPHI7_UP}
  5. {$WARN SYMBOL_PLATFORM OFF}
  6. {$WARN UNIT_PLATFORM OFF}
  7. {$ENDIF}
  8. uses
  9. SysUtils, Windows, Classes, CnBuffStr;
  10. type
  11. TStringProcessProc = function(const s: string): string;
  12. TStringObjectList = class(TStringList)
  13. private
  14. function GetStringObjects(Index: Integer): string;
  15. procedure SetStringObjects(Index: Integer; const Value: string);
  16. procedure ClearStringObjects;
  17. public
  18. constructor Create;
  19. destructor Destroy; override;
  20. function AddStringObject(const S: string; str: string): Integer;
  21. procedure Clear; override;
  22. procedure Delete(Index: Integer); override;
  23. property StringObjects[Index: Integer]: string read GetStringObjects write SetStringObjects;
  24. end;
  25. TSectionList = class(TStringList)
  26. private
  27. function GetPostions(Index: Integer): TList;
  28. procedure SetPostions(Index: Integer; const Value: TList);
  29. public
  30. constructor Create;
  31. destructor Destroy; override;
  32. function Add(const S: string): Integer; override;
  33. procedure Clear; override;
  34. procedure Delete(Index: Integer); override;
  35. property Postions[Index: Integer]: TList read GetPostions write SetPostions;
  36. end;
  37. TFindFileCallBack = procedure(obj: TObject;
  38. const FileName: string; const Info: TSearchRec; var Abort: Boolean);
  39. TFindDirCallBack = procedure(obj: TObject; const SubDir: string);
  40. TProcMsgCallBack = procedure(obj: TObject);
  41. procedure BulidSectionList(Stream: TStringReader; var sl: TSectionList);
  42. procedure FreeSectionList(var sl: TSectionList);
  43. procedure StringsLoadFromFileWithSection(ss: TStrings;
  44. const FileName: string; const Section: string = '';
  45. const SectionList: TSectionList = nil;
  46. IncludeBlankLine: Boolean = False);
  47. procedure StringsLoadFromTextWithSection(ss: TStrings;
  48. Stream: TStringReader; const Section: string = '';
  49. const SectionList: TSectionList = nil;
  50. IncludeBlankLine: Boolean = False);
  51. procedure StringsSaveToFileWithSection(ss: TStrings;
  52. FileName: string; const Section: string;
  53. IncludeBlankLine: Boolean = False;
  54. ProcessProc: TStringProcessProc = nil);
  55. procedure StringsAppendToFileWithSection(ss: TStrings;
  56. FileName: string; const Section: string;
  57. IncludeBlankLine: Boolean = False;
  58. ProcessProc: TStringProcessProc = nil);
  59. procedure StringsSaveToTextWithSection(ss: TStrings;
  60. var Stream: Text; const Section: string;
  61. IncludeBlankLine: Boolean = False;
  62. ProcessProc: TStringProcessProc = nil);
  63. function MatchFileName(const Mask, FileName: string): Boolean;
  64. procedure FileMasksToStrings(const FileMasks: string; MaskList: TStrings;
  65. CaseSensitive: Boolean);
  66. function FileMatchesMasks(const FileName, FileMasks: string;
  67. CaseSensitive: Boolean = False): Boolean; overload;
  68. function FileMatchesMasks(const FileName: string; MaskList: TStrings;
  69. CaseSensitive: Boolean = False): Boolean; overload;
  70. function RefString(const S: string): Pointer;
  71. procedure ReleaseString(P: Pointer);
  72. function PointerToString(P: Pointer): string;
  73. procedure SetCommaText(const s: string; ss: TStrings);
  74. function ExtractFileNames(ssFiles, ssNames, ssDuplicated: TStringList): Boolean;
  75. function DirListExtractFileNames(ssFiles, ssNames, ssDuplicated: TStrings): Boolean;
  76. function FindFile(obj: TObject;
  77. const Path: string; const FileName: string = '*.*';
  78. FileProc: TFindFileCallBack = nil; DirProc: TFindDirCallBack = nil;
  79. bSub: Boolean = True; DoMsgProc: TProcMsgCallBack = nil): Boolean;
  80. function IsDelimiter(const S: string; Delimiter: Char; Index: Integer): Boolean;
  81. function LastCharPos(const s: string; chr: Char): Integer;
  82. function RelativePath_API(const aFrom, aTo: string;
  83. FromIsDir, ToIsDir: Boolean): string;
  84. function FileNameMatch(Pattern, FileName: PChar): Integer;
  85. function MatchExt(const ext, FileName: string): Boolean;
  86. procedure FileExtsToStrings(const FileExts: string; ExtList: TStrings;
  87. CaseSensitive: Boolean);
  88. procedure FileMasksToStringsStrict(const FileMasks: string; MaskList: TStrings;
  89. CaseSensitive: Boolean);
  90. function FileMatchesExts(const FileName, FileExts: string;
  91. CaseSensitive: Boolean = False): Boolean; overload;
  92. function FileMatchesExts(const FileName: string; ExtList: TStrings;
  93. CaseSensitive: Boolean = False): Boolean; overload;
  94. implementation
  95. const
  96. cSectionStart = '[';
  97. cSectionEnd = ']';
  98. // 目录尾加'\'修正
  99. function MakePath(const Dir: string): string;
  100. begin
  101. Result := Trim(Dir);
  102. if Result = '' then Exit;
  103. if not IsPathDelimiter(Result, Length(Result)) then
  104. Result := Result + {$IFDEF MSWINDOWS} '\'; {$ELSE} '/'; {$ENDIF};
  105. end;
  106. function StringIsSection(const s: string): Boolean; overload;
  107. begin
  108. Result := (s <> '') and (s[1] = cSectionStart) and (s[Length(s)] = cSectionEnd) and (s <> '[]');
  109. end;
  110. function StringIsSection(const s: string; var Section: string): Boolean; overload;
  111. begin
  112. Result := StringIsSection(s);
  113. if Result then
  114. begin
  115. Section := Copy(s, 2, Length(s) - 2);
  116. end;
  117. end;
  118. function SectionToString(const s: string): string;
  119. begin
  120. Result := cSectionStart + s + cSectionEnd;
  121. end;
  122. { TSectionList }
  123. function TSectionList.Add(const S: string): Integer;
  124. var
  125. lst: TList;
  126. begin
  127. Result := inherited Add(S); // 不能调用 AddObject(s, nil),否则会递归出错;
  128. if Objects[Result] = nil then
  129. begin
  130. lst := TList.Create;
  131. Objects[Result] := lst;
  132. end;
  133. end;
  134. procedure TSectionList.Clear;
  135. var
  136. i: Integer;
  137. begin
  138. for i := 0 to Count - 1 do
  139. begin
  140. Objects[i].Free;
  141. end;
  142. inherited;
  143. end;
  144. constructor TSectionList.Create;
  145. begin
  146. inherited;
  147. Sorted := True;
  148. end;
  149. procedure TSectionList.Delete(Index: Integer);
  150. begin
  151. Objects[Index].Free;
  152. inherited;
  153. end;
  154. destructor TSectionList.Destroy;
  155. begin
  156. Clear;
  157. inherited;
  158. end;
  159. function TSectionList.GetPostions(Index: Integer): TList;
  160. begin
  161. Result := TList(Objects[Index]);
  162. end;
  163. procedure TSectionList.SetPostions(Index: Integer; const Value: TList);
  164. begin
  165. Objects[Index].Free;
  166. PutObject(Index, Value);
  167. end;
  168. function FindSection(sl: TSectionList; const Section: string; const iSection: Integer = -1): Integer;
  169. var
  170. i, idx: Integer;
  171. begin
  172. Result := -1;
  173. idx := sl.IndexOf(Section);
  174. if idx >= 0 then
  175. begin
  176. with sl.Postions[idx] do
  177. begin
  178. if iSection >= 0 then
  179. begin
  180. i := IndexOf(Pointer(iSection));
  181. if (i >= 0) and (i + 1 < Count) then
  182. begin
  183. Result := Integer(Items[i + 1]);
  184. end;
  185. end
  186. else
  187. begin
  188. Result := Integer(Items[0]);
  189. end;
  190. end;
  191. end;
  192. end;
  193. procedure BulidSectionList(Stream: TStringReader; var sl: TSectionList);
  194. var
  195. s, Section: string;
  196. i: Integer;
  197. begin
  198. sl := TSectionList.Create;
  199. Stream.Seek(0, soFromBeginning);
  200. while not Stream.EoS do
  201. begin
  202. s := Stream.ReadLn;
  203. if StringIsSection(s, Section) then
  204. begin
  205. i := sl.Add(Section);
  206. sl.Postions[i].Add(Pointer(Stream.Position));
  207. end;
  208. end;
  209. end;
  210. procedure FreeSectionList(var sl: TSectionList);
  211. begin
  212. sl.Free;
  213. end;
  214. procedure StringsLoadFromFileWithSection(ss: TStrings;
  215. const FileName: string; const Section: string = '';
  216. const SectionList: TSectionList = nil;
  217. IncludeBlankLine: Boolean = False);
  218. var
  219. i: Integer;
  220. Stream: TStringReader;
  221. begin
  222. if not Assigned(ss) then
  223. begin
  224. Exit;
  225. end;
  226. if Section = '' then
  227. begin
  228. ss.LoadFromFile(FileName);
  229. if not IncludeBlankLine then
  230. begin
  231. for i := ss.Count - 1 downto 0 do
  232. begin
  233. if ss[i] = '' then
  234. begin
  235. ss.Delete(i);
  236. end;
  237. end;
  238. end;
  239. Exit;
  240. end;
  241. Stream := TStringReader.Create;
  242. try
  243. Stream.LoadFromFile(FileName);
  244. StringsLoadFromTextWithSection(ss, Stream, Section, SectionList, IncludeBlankLine);
  245. finally
  246. Stream.Free;
  247. end;
  248. end;
  249. procedure StringsLoadFromTextWithSection(ss: TStrings;
  250. Stream: TStringReader; const Section: string = '';
  251. const SectionList: TSectionList = nil;
  252. IncludeBlankLine: Boolean = False);
  253. var
  254. IsSection, IsCurrentSection: Boolean;
  255. s, sSection: string;
  256. iSection: Integer;
  257. begin
  258. if not Assigned(ss) then
  259. begin
  260. Exit;
  261. end;
  262. Stream.Seek(0, soFromBeginning);
  263. IsCurrentSection := False;
  264. ss.BeginUpdate;
  265. try
  266. if Section = '' then
  267. begin
  268. while not Stream.EoS do
  269. begin
  270. s := Stream.ReadLn;
  271. if (not IncludeBlankLine) and (s = '') then
  272. begin
  273. Continue;
  274. end;
  275. ss.Add(s);
  276. end;
  277. end
  278. else if Assigned(SectionList) then
  279. begin
  280. iSection := FindSection(SectionList, Section);
  281. while iSection >= 0 do
  282. begin
  283. Stream.Seek(iSection, soFromBeginning);
  284. while not Stream.EoS do
  285. begin
  286. s := Stream.ReadLn;
  287. if (not IncludeBlankLine) and (s = '') then
  288. begin
  289. Continue;
  290. end;
  291. if StringIsSection(s) then
  292. begin
  293. Break;
  294. end;
  295. ss.Add(s);
  296. end;
  297. iSection := FindSection(SectionList, Section, iSection);
  298. end;
  299. end
  300. else
  301. begin
  302. sSection := SectionToString(Section);
  303. while not Stream.EoS do
  304. begin
  305. s := Stream.ReadLn;
  306. if (s = '') and (not IncludeBlankLine) then
  307. begin
  308. Continue;
  309. end;
  310. IsSection := StringIsSection(s);
  311. if IsSection then
  312. begin
  313. IsCurrentSection := s = sSection;
  314. end;
  315. if (not IsCurrentSection) or IsSection then
  316. begin
  317. Continue;
  318. end;
  319. ss.Add(s);
  320. end;
  321. end;
  322. finally
  323. ss.EndUpdate;
  324. end;
  325. end;
  326. procedure StringsSaveToFileWithSection(ss: TStrings;
  327. FileName: string; const Section: string;
  328. IncludeBlankLine: Boolean = False;
  329. ProcessProc: TStringProcessProc = nil);
  330. var
  331. Stream: TextFile;
  332. begin
  333. if not Assigned(ss) then
  334. begin
  335. Exit;
  336. end;
  337. AssignFile(Stream, FileName);
  338. try
  339. Reset(Stream);
  340. Rewrite(Stream);
  341. StringsSaveToTextWithSection(ss, Stream, Section, IncludeBlankLine, ProcessProc);
  342. finally
  343. CloseFile(Stream);
  344. end;
  345. end;
  346. procedure StringsAppendToFileWithSection(ss: TStrings;
  347. FileName: string; const Section: string;
  348. IncludeBlankLine: Boolean = False;
  349. ProcessProc: TStringProcessProc = nil);
  350. var
  351. Stream: TextFile;
  352. begin
  353. if not Assigned(ss) then
  354. begin
  355. Exit;
  356. end;
  357. AssignFile(Stream, FileName);
  358. try
  359. Append(Stream);
  360. StringsSaveToTextWithSection(ss, Stream, Section, IncludeBlankLine, ProcessProc);
  361. finally
  362. CloseFile(Stream);
  363. end;
  364. end;
  365. procedure StringsSaveToTextWithSection(ss: TStrings;
  366. var Stream: Text; const Section: string;
  367. IncludeBlankLine: Boolean = False;
  368. ProcessProc: TStringProcessProc = nil);
  369. var
  370. i: Integer;
  371. begin
  372. if not Assigned(ss) then
  373. begin
  374. Exit;
  375. end;
  376. if FilePos(Stream) > 0 then
  377. begin
  378. Writeln(Stream);
  379. end;
  380. Writeln(Stream, SectionToString(Section));
  381. for i := 0 to ss.Count - 1 do
  382. begin
  383. if (not IncludeBlankLine) and (ss[i] = '') then
  384. begin
  385. Continue;
  386. end;
  387. if Assigned(ProcessProc) then
  388. begin
  389. Writeln(Stream, ProcessProc(ss[i]));
  390. end
  391. else
  392. begin
  393. Writeln(Stream, ss[i]);
  394. end;
  395. end;
  396. end;
  397. function PointerXX(var X: PChar): PChar;
  398. {$IFDEF PUREPASCAL}
  399. begin
  400. Result := X;
  401. Inc(X);
  402. end;
  403. {$ELSE}
  404. asm
  405. {
  406. EAX = X
  407. }
  408. MOV EDX, [EAX]
  409. INC dword ptr [EAX]
  410. MOV EAX, EDX
  411. end;
  412. {$ENDIF}
  413. // Evaluate operation
  414. function Evaluate(var X: Char; const Value: Char): Char;
  415. {$IFDEF PUREPASCAL}
  416. begin
  417. X := Value;
  418. Result := X;
  419. end;
  420. {$ELSE}
  421. asm
  422. {
  423. EAX = X
  424. EDX = Value (DL)
  425. }
  426. MOV [EAX], DL
  427. MOV AL, [EAX]
  428. end;
  429. {$ENDIF}
  430. function FileNameMatch(Pattern, FileName: PChar): Integer;
  431. var
  432. p, n: PChar;
  433. c: Char;
  434. begin
  435. p := Pattern;
  436. n := FileName;
  437. while Evaluate(c, PointerXX(p)^) <> #0 do
  438. begin
  439. case c of
  440. '?':
  441. begin
  442. if n^ = '.' then
  443. begin
  444. while (p^ <> '.') and (p^ <> #0) do
  445. begin
  446. if (p^ <> '?') and (p^ <> '*') then
  447. begin
  448. Result := -1;
  449. Exit;
  450. end;
  451. Inc(p);
  452. end;
  453. end
  454. else
  455. begin
  456. if n^ <> #0 then
  457. begin
  458. Inc(n);
  459. end;
  460. end;
  461. end;
  462. '>':
  463. begin
  464. if n^ = '.' then
  465. begin
  466. if ((n + 1)^ = #0) and (FileNameMatch(p, n + 1) = 0) then
  467. begin
  468. Result := 0;
  469. Exit;
  470. end;
  471. if FileNameMatch(p, n) = 0 then
  472. begin
  473. Result := 0;
  474. Exit;
  475. end;
  476. Result := -1;
  477. Exit;
  478. end;
  479. if n^ = #0 then
  480. begin
  481. Result := FileNameMatch(p, n);
  482. Exit;
  483. end;
  484. Inc(n);
  485. end;
  486. '*':
  487. begin
  488. while n^ <> #0 do
  489. begin
  490. if FileNameMatch(p, n) = 0 then
  491. begin
  492. Result := 0;
  493. Exit;
  494. end;
  495. Inc(n);
  496. end;
  497. end;
  498. '<':
  499. begin
  500. while n^ <> #0 do
  501. begin
  502. if FileNameMatch(p, n) = 0 then
  503. begin
  504. Result := 0;
  505. Exit;
  506. end;
  507. if (n^ = '.') and (StrScan(n + 1, '.') = nil) then
  508. begin
  509. Inc(n);
  510. Break;
  511. end;
  512. Inc(n);
  513. end;
  514. end;
  515. '"':
  516. begin
  517. if (n^ = #0) and (FileNameMatch(p, n) = 0) then
  518. begin
  519. Result := 0;
  520. Exit;
  521. end;
  522. if n^ <> '.' then
  523. begin
  524. Result := -1;
  525. Exit;
  526. end;
  527. Inc(n);
  528. end;
  529. else
  530. if (c = '.') and (n^ = #0) then
  531. begin
  532. while p^ <> #0 do
  533. begin
  534. if (p^ = '*') and ((p + 1)^ = #0) then
  535. begin
  536. Result := 0;
  537. Exit;
  538. end;
  539. if p^ <> '?' then
  540. begin
  541. Result := -1;
  542. Exit;
  543. end;
  544. Inc(p);
  545. end;
  546. Result := 0;
  547. Exit;
  548. end;
  549. if c <> n^ then
  550. begin
  551. Result := -1;
  552. Exit;
  553. end;
  554. Inc(n);
  555. end;
  556. end;
  557. if n^ = #0 then
  558. begin
  559. Result := 0;
  560. Exit;
  561. end;
  562. Result := -1;
  563. end;
  564. function MatchFileName(const Mask, FileName: string): Boolean;
  565. begin
  566. if Mask = '*.*' then
  567. begin
  568. Result := True;
  569. Exit;
  570. end;
  571. Result := FileNameMatch(PChar(Mask), PChar(FileName)) = 0;
  572. end;
  573. function _CaseSensitive(const CaseSensitive: Boolean; const S: string): string;
  574. begin
  575. if CaseSensitive then
  576. Result := S
  577. else
  578. Result := AnsiUpperCase(S);
  579. end;
  580. procedure FileMasksToStrings(const FileMasks: string; MaskList: TStrings;
  581. CaseSensitive: Boolean);
  582. var
  583. Masks: string;
  584. i: Integer;
  585. begin
  586. Masks := StringReplace(FileMasks, ';', ',', [rfReplaceAll]);
  587. SetCommaText(Masks, MaskList);
  588. for i := MaskList.Count - 1 downto 0 do
  589. begin
  590. if MaskList[i] = '' then
  591. begin
  592. MaskList.Delete(i);
  593. Continue;
  594. end;
  595. if StrScan(PChar(MaskList[i]), '.') <> nil then
  596. begin
  597. if (MaskList[i] <> '') and (MaskList[i][1] = '.') then
  598. MaskList[i] := '*' + _CaseSensitive(CaseSensitive, MaskList[i])
  599. else
  600. MaskList[i] := _CaseSensitive(CaseSensitive, MaskList[i]);
  601. end
  602. else
  603. begin
  604. MaskList[i] := '*.' + _CaseSensitive(CaseSensitive, MaskList[i]);
  605. end;
  606. if MaskList[i] = '*.*' then
  607. begin
  608. if i > 0 then
  609. MaskList.Exchange(0, i);
  610. Exit;
  611. end;
  612. end;
  613. end;
  614. function FileMatchesMasks(const FileName, FileMasks: string;
  615. CaseSensitive: Boolean = False): Boolean;
  616. var
  617. MaskList: TStrings;
  618. FFileName: string;
  619. i: Integer;
  620. begin
  621. MaskList := TStringList.Create;
  622. try
  623. FileMasksToStrings(FileMasks, MaskList, CaseSensitive);
  624. FFileName := _CaseSensitive(CaseSensitive, ExtractFileName(FileName));
  625. Result := False;
  626. for i := 0 to MaskList.Count - 1 do
  627. begin
  628. if MatchFileName(MaskList[i], FFileName) then
  629. begin
  630. Result := True;
  631. Exit;
  632. end;
  633. end;
  634. finally
  635. MaskList.Free;
  636. end;
  637. end;
  638. function FileMatchesMasks(const FileName: string; MaskList: TStrings;
  639. CaseSensitive: Boolean = False): Boolean;
  640. var
  641. FFileName: string;
  642. i: Integer;
  643. begin
  644. FFileName := _CaseSensitive(CaseSensitive, ExtractFileName(FileName));
  645. Result := False;
  646. for i := 0 to MaskList.Count - 1 do
  647. begin
  648. if MatchFileName(MaskList[i], FFileName) then
  649. begin
  650. Result := True;
  651. Exit;
  652. end;
  653. end;
  654. end;
  655. function RefString(const S: string): Pointer;
  656. var
  657. tmpS: string;
  658. begin
  659. tmpS := S;
  660. Result := Pointer(tmpS);
  661. Pointer(tmpS) := nil;
  662. end;
  663. procedure ReleaseString(P: Pointer);
  664. var
  665. tmpS: string;
  666. begin
  667. Pointer(tmpS) := P;
  668. end;
  669. function PointerToString(P: Pointer): string;
  670. begin
  671. Result := StrPas(PChar(P));
  672. end;
  673. procedure SetCommaText(const s: string; ss: TStrings);
  674. begin
  675. if Assigned(ss) then
  676. begin
  677. if s <> '' then
  678. begin
  679. ss.CommaText := s;
  680. end
  681. else
  682. begin
  683. ss.Clear;
  684. end;
  685. end;
  686. end;
  687. function ExtractFileNames(ssFiles, ssNames, ssDuplicated: TStringList): Boolean;
  688. var
  689. i, idx: Integer;
  690. s: string;
  691. begin
  692. Result := False;
  693. if (not Assigned(ssFiles)) or (not Assigned(ssNames)) then
  694. begin
  695. Exit;
  696. end;
  697. ssNames.Clear;
  698. ssNames.Sorted := True;
  699. Result := True;
  700. for i := 0 to ssFiles.Count - 1 do
  701. begin
  702. s := ExtractFileName(ssFiles[i]);
  703. idx := ssNames.IndexOf(s);
  704. if idx >= 0 then
  705. begin
  706. Result := False;
  707. if Assigned(ssDuplicated) then
  708. begin
  709. ssDuplicated.Add(ssFiles[Integer(ssNames.Objects[idx])]);
  710. ssDuplicated.Add(ssFiles[i]);
  711. ssDuplicated.Add('');
  712. end;
  713. Continue;
  714. end;
  715. ssNames.AddObject(s, Pointer(i));
  716. end;
  717. end;
  718. function DirListExtractFileNames(ssFiles, ssNames, ssDuplicated: TStrings): Boolean;
  719. var
  720. i, idx: Integer;
  721. s: string;
  722. begin
  723. Result := False;
  724. if (not Assigned(ssFiles)) or (not Assigned(ssNames)) then
  725. begin
  726. Exit;
  727. end;
  728. ssNames.Clear;
  729. Result := True;
  730. for i := 0 to ssFiles.Count - 1 do
  731. begin
  732. s := ExtractFileName(ssFiles[i]);
  733. idx := ssNames.IndexOf(s);
  734. if idx >= 0 then
  735. begin
  736. Result := False;
  737. if Assigned(ssDuplicated) then
  738. begin
  739. ssDuplicated.Add(s);
  740. end;
  741. Continue;
  742. end;
  743. ssNames.AddObject(s, Pointer(i));
  744. end;
  745. end;
  746. { TStringObjectList }
  747. function TStringObjectList.AddStringObject(const S: string;
  748. str: string): Integer;
  749. begin
  750. Result := AddObject(S, RefString(str));
  751. end;
  752. procedure TStringObjectList.Clear;
  753. begin
  754. ClearStringObjects;
  755. inherited;
  756. end;
  757. procedure TStringObjectList.ClearStringObjects;
  758. var
  759. i: Integer;
  760. begin
  761. for i := 0 to Count - 1 do
  762. begin
  763. ReleaseString(Objects[i]);
  764. end;
  765. end;
  766. constructor TStringObjectList.Create;
  767. begin
  768. inherited;
  769. end;
  770. procedure TStringObjectList.Delete(Index: Integer);
  771. begin
  772. if (Index < 0) or (Index >= Count) then
  773. begin
  774. Exit;
  775. end;
  776. ReleaseString(Objects[Index]);
  777. inherited;
  778. end;
  779. destructor TStringObjectList.Destroy;
  780. begin
  781. Clear;
  782. inherited;
  783. end;
  784. function TStringObjectList.GetStringObjects(Index: Integer): string;
  785. begin
  786. Result := PointerToString(Objects[Index]);
  787. end;
  788. procedure TStringObjectList.SetStringObjects(Index: Integer;
  789. const Value: string);
  790. begin
  791. if (Index < 0) or (Index >= Count) then
  792. begin
  793. Exit;
  794. end;
  795. ReleaseString(Objects[Index]);
  796. PutObject(Index, RefString(Value));
  797. end;
  798. function DirectoryExists(const Directory: string): Boolean;
  799. var
  800. Code: Integer;
  801. begin
  802. Code := GetFileAttributes(PChar(Directory));
  803. Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0);
  804. end;
  805. function FindFile(obj: TObject;
  806. const Path: string; const FileName: string = '*.*';
  807. FileProc: TFindFileCallBack = nil; DirProc: TFindDirCallBack = nil;
  808. bSub: Boolean = True; DoMsgProc: TProcMsgCallBack = nil): Boolean;
  809. var
  810. FindAbort: Boolean;
  811. procedure DoFindFile(obj: TObject;
  812. const Path, SubPath: string; const FileName: string;
  813. FileProc: TFindFileCallBack; DirProc: TFindDirCallBack;
  814. bSub: Boolean; DoMsgProc: TProcMsgCallBack);
  815. var
  816. APath: string;
  817. Info: TSearchRec;
  818. Succ: Integer;
  819. begin
  820. FindAbort := False;
  821. APath := MakePath(
  822. MakePath(Path) + SubPath);
  823. Succ := FindFirst(APath + FileName, faAnyFile {- faVolumeID}, Info);
  824. try
  825. while Succ = 0 do
  826. begin
  827. if (Info.Name <> '.') and (Info.Name <> '..') then
  828. begin
  829. if (Info.Attr and faDirectory) <> faDirectory then
  830. begin
  831. if Assigned(FileProc) then
  832. begin
  833. FileProc(obj, APath + Info.FindData.cFileName, Info, FindAbort);
  834. end;
  835. end;
  836. end;
  837. if Assigned(DoMsgProc) then
  838. begin
  839. DoMsgProc(obj);
  840. end;
  841. if FindAbort then
  842. begin
  843. Exit;
  844. end;
  845. Succ := FindNext(Info);
  846. end;
  847. finally
  848. SysUtils.FindClose(Info);
  849. end;
  850. if bSub then
  851. begin
  852. Succ := FindFirst(APath + '*.*', faAnyFile {- faVolumeID}, Info);
  853. try
  854. while Succ = 0 do
  855. begin
  856. if (Info.Name <> '.') and (Info.Name <> '..') and
  857. (Info.Attr and faDirectory = faDirectory) then
  858. begin
  859. if Assigned(DirProc) then
  860. begin
  861. DirProc(obj, MakePath(SubPath + Info.Name));
  862. end;
  863. DoFindFile(obj,
  864. Path,
  865. MakePath(SubPath + Info.Name),
  866. FileName,
  867. FileProc,
  868. DirProc,
  869. bSub,
  870. DoMsgProc);
  871. if FindAbort then
  872. begin
  873. Exit;
  874. end;
  875. end;
  876. Succ := FindNext(Info);
  877. end;
  878. finally
  879. SysUtils.FindClose(Info);
  880. end;
  881. end;
  882. end;
  883. begin
  884. Result := False;
  885. if not DirectoryExists(Path) then
  886. begin
  887. Exit;
  888. end;
  889. DoFindFile(obj,
  890. MakePath(Path),
  891. '',
  892. FileName,
  893. FileProc,
  894. DirProc,
  895. bSub,
  896. DoMsgProc);
  897. Result := not FindAbort;
  898. end;
  899. function IsDelimiter(const S: string; Delimiter: Char; Index: Integer): Boolean;
  900. begin
  901. Result := (Index > 0) and (Index <= Length(S)) and
  902. (S[Index] = Delimiter) and (ByteType(S, Index) = mbSingleByte);
  903. end;
  904. function LastCharPos(const s: string; chr: Char): Integer;
  905. var
  906. i: Integer;
  907. begin
  908. i := Length(s);
  909. while (i > 0) and (not IsDelimiter(s, chr, i)) do
  910. begin
  911. Dec(i);
  912. end;
  913. Result := i;
  914. end;
  915. const
  916. shlwapi32 = 'shlwapi.dll';
  917. function PathRelativePathToA(pszPath: PAnsiChar; pszFrom: PAnsiChar; dwAttrFrom: DWORD;
  918. pszTo: PAnsiChar; dwAttrTo: DWORD): BOOL; stdcall; external shlwapi32 name
  919. 'PathRelativePathToA';
  920. function PathRelativePathToW(pszPath: PWideChar; pszFrom: PWideChar; dwAttrFrom: DWORD;
  921. pszTo: PWideChar; dwAttrTo: DWORD): BOOL; stdcall; external shlwapi32 name
  922. 'PathRelativePathToW';
  923. function PathRelativePathTo(pszPath: PChar; pszFrom: PChar; dwAttrFrom: DWORD;
  924. pszTo: PChar; dwAttrTo: DWORD): BOOL; stdcall; external shlwapi32 name
  925. 'PathRelativePathToA';
  926. function RelativePath_API(const aFrom, aTo: string; FromIsDir, ToIsDir: Boolean): string;
  927. function GetAttr(IsDir: Boolean): DWORD;
  928. begin
  929. if IsDir then
  930. Result := FILE_ATTRIBUTE_DIRECTORY
  931. else
  932. Result := FILE_ATTRIBUTE_NORMAL;
  933. end;
  934. var
  935. p: array[0..MAX_PATH] of Char;
  936. begin
  937. PathRelativePathTo(p, PChar(aFrom), GetAttr(FromIsDir), PChar(aTo), GetAttr(ToIsDir));
  938. Result := StrPas(p);
  939. end;
  940. function MatchExt(const ext, FileName: string): Boolean;
  941. begin
  942. if ext = '.*' then
  943. begin
  944. Result := True;
  945. Exit;
  946. end;
  947. Result := FileNameMatch(PChar(ext), PChar(FileName)) = 0;
  948. end;
  949. procedure FileExtsToStrings(const FileExts: string; ExtList: TStrings;
  950. CaseSensitive: Boolean);
  951. var
  952. Exts: string;
  953. i: Integer;
  954. begin
  955. Exts := StringReplace(FileExts, ';', ',', [rfReplaceAll]);
  956. ExtList.CommaText := Exts;
  957. for i := ExtList.Count - 1 downto 0 do
  958. begin
  959. if ExtList[i] = '' then
  960. begin
  961. ExtList.Delete(i);
  962. Continue;
  963. end;
  964. if StrScan(PChar(ExtList[i]), '.') <> nil then
  965. begin
  966. ExtList[i] := _CaseSensitive(CaseSensitive, ExtractFileExt(ExtList[i]));
  967. end
  968. else
  969. begin
  970. ExtList[i] := '.' + _CaseSensitive(CaseSensitive, ExtList[i]);
  971. end;
  972. if ExtList[i] = '.*' then
  973. begin
  974. if i > 0 then
  975. ExtList.Exchange(0, i);
  976. Exit;
  977. end;
  978. end;
  979. end;
  980. procedure FileMasksToStringsStrict(const FileMasks: string; MaskList: TStrings;
  981. CaseSensitive: Boolean);
  982. var
  983. Exts: string;
  984. i: Integer;
  985. begin
  986. Exts := StringReplace(FileMasks, ';', ',', [rfReplaceAll]);
  987. MaskList.CommaText := Exts;
  988. for i := MaskList.Count - 1 downto 0 do
  989. begin
  990. if MaskList[i] = '' then
  991. begin
  992. MaskList.Delete(i);
  993. Continue;
  994. end;
  995. MaskList[i] := _CaseSensitive(CaseSensitive, MaskList[i]);
  996. if MaskList[i] = '*.*' then
  997. begin
  998. if i > 0 then
  999. MaskList.Exchange(0, i);
  1000. Exit;
  1001. end;
  1002. end;
  1003. end;
  1004. function FileMatchesExts(const FileName, FileExts: string;
  1005. CaseSensitive: Boolean = False): Boolean;
  1006. var
  1007. ExtList: TStrings;
  1008. FExt: string;
  1009. i: Integer;
  1010. begin
  1011. ExtList := TStringList.Create;
  1012. try
  1013. FileExtsToStrings(FileExts, ExtList, CaseSensitive);
  1014. FExt := _CaseSensitive(CaseSensitive, ExtractFileExt(FileName));
  1015. Result := False;
  1016. for i := 0 to ExtList.Count - 1 do
  1017. begin
  1018. if MatchExt(ExtList[i], FExt) then
  1019. begin
  1020. Result := True;
  1021. Exit;
  1022. end;
  1023. end;
  1024. finally
  1025. ExtList.Free;
  1026. end;
  1027. end;
  1028. function FileMatchesExts(const FileName: string; ExtList: TStrings;
  1029. CaseSensitive: Boolean = False): Boolean;
  1030. var
  1031. FExt: string;
  1032. i: Integer;
  1033. begin
  1034. FExt := _CaseSensitive(CaseSensitive, ExtractFileExt(FileName));
  1035. Result := False;
  1036. for i := 0 to ExtList.Count - 1 do
  1037. begin
  1038. if MatchExt(ExtList[i], FExt) then
  1039. begin
  1040. Result := True;
  1041. Exit;
  1042. end;
  1043. end;
  1044. end;
  1045. end.