UtilMethods.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. unit UtilMethods;
  2. interface
  3. uses
  4. Controls, ZhAPI, ActnList, ZjIDTree, DB, ZjGridDBA, ZjGrid, Windows, Messages,
  5. sdDB, VCLZip, VCLUnZip, Dialogs, Forms, ShlObj, Classes, StrUtils, Math, ADODB,
  6. IdGlobal;
  7. type
  8. TRoundMode = (rmNearest, rmUp, rmDown);
  9. TBookmarkRefreshEvent = procedure (AExpandFrame: Boolean) of object;
  10. {RoundTo}
  11. function GetCompareDigitValue(ADigit: Integer): Double;
  12. function QuantityRoundTo(AValue: Double): Double;
  13. function PriceRoundTo(AValue: Double): Double;
  14. function TotalPriceRoundTo(AValue: Double): Double;
  15. function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
  16. {Interface Control}
  17. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  18. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  19. {DataBase Rela}
  20. function GetsdDataSetNewID(ADataSet: TsdDataSet; const AIndex: string): Integer;
  21. procedure ExecuteSql(AConnection: TADOConnection; const ASql: string);
  22. function QueryData(AConnection: TADOConnection; const ASql: string): TADOQuery;
  23. {Message}
  24. procedure WarningMessage(const AMsg: string; AHandle: THandle = 0);
  25. procedure ErrorMessage(const AMsg: string; AHandle: THandle = 0);
  26. function QuestMessage(const AMsg: string; AHandle: THandle = 0): Boolean;
  27. function QuestMessageYesNo(const AMsg: string; AHandle: THandle = 0): Boolean;
  28. procedure TipMessage(const AMsg: string; AHandle: THandle = 0);
  29. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  30. {Get Common Path}
  31. function GetAppFilePath: string;
  32. function GetMyProjectsFilePath: string;
  33. function GetEmptyDataBaseFileName: string;
  34. function GetTemplateBillsFileName: string;
  35. function GetTemplateXlsFileName: string;
  36. function GetBackupFilePath: string;
  37. function GetReportTemplatePath: string;
  38. function GetAppTempPath: string;
  39. {Select & Save File Choose}
  40. function GetFilter(AExt: string): string;
  41. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  42. function SelectFiles(AFiles: TStrings; const AExt: string): Boolean;
  43. function SaveFile(var FileName: string; const AExt: string): Boolean;
  44. function SelectExcelFile(var AFileName: string): Boolean;
  45. function SaveExcelFile(var FileName: string): Boolean;
  46. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  47. AParentHandle: THandle = 0; AHasNewFolderBtn: Boolean = True): Boolean;
  48. function FixPathByAppPath(AFileName: string): string;
  49. function BrowseFolder(var APath: string; const ATitle: string;
  50. AParentHandle: THandle; AHasNewFolderBtn: Boolean = True): Boolean;
  51. {CheckStrings}
  52. function CheckPeg(const AStr: string): Boolean;
  53. function CheckValidPassword(APassword: string): Boolean;
  54. function ValidInteger(var AKey: Char): Boolean;
  55. {MergeStrings}
  56. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  57. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  58. {CodeTransform}
  59. function B_CodeToIndexCode(const AB_Code: string): string;
  60. function ChinessNum(const ADigitNum: Integer): string;
  61. function Num2Peg(ANum: Double): string;
  62. {Compare Code}
  63. //function CompareCodeWithChar(const ACode1, ACode2: string): Integer;
  64. {Tree Relative}
  65. function CreateTree: TZjIDTree;
  66. procedure DisConnectTree(ATree: TZjIDTree);
  67. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  68. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  69. {Generate Name/Directory/Path}
  70. function GetTempFileDir: string;
  71. function GetTempFilePath: string;
  72. function GetTempName(ALength: Integer = 8): string;
  73. function GetTempFileName: string; overload;
  74. function GetTempFileName(const APath, AExt: string): string; overload;
  75. function GenerateTempFolder(AGeneratePath: string): string;
  76. function GetNewGUIDFileName(const AGeneratePath: string): string;
  77. {Progress bar Control}
  78. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  79. procedure UpdateProgress(APosition: Integer; const AHint: string);
  80. procedure DisableSysProgress;
  81. procedure EnableSysProgress;
  82. {Interface RePaint Control}
  83. procedure BeginUpdateWindow(AHandle: THandle);
  84. procedure EndUpdateWindow(AHandle: THandle);
  85. {Zip}
  86. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  87. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  88. {Copy By Stream}
  89. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  90. procedure AppendTestLog(const AFileName, ALog: string);
  91. // Add by chenshilong, 2014-04-11
  92. function GetVersion(APartCount: Integer = 4): string;
  93. function ExtractFileNameWithoutExt(const AFileName: string): string;
  94. function ShortText(AText: string; AWidth: Integer): string; // 文本缩略显示
  95. function CustomWidthText(AText: string; AWidth: Integer): string;
  96. function ReplaceCharsForJson(AText: string): string; // 替换Json文本中的特殊字符
  97. function RecoverCharsFromJson(AText: string): string; // 下载得到的Json文本恢复成用户需要的文本
  98. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  99. function DeleteFolder(const FolderStr: string): Boolean;
  100. function HasExt(const AFileName: string): Boolean;
  101. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  102. function FileCount(APath: string): Integer;
  103. implementation
  104. uses
  105. SysUtils, dxBar, MainFrm, ConstUnit, Globals, StdCtrls, ShellAPI;
  106. var
  107. SysProgressDisabled: Boolean;
  108. {RoundTo}
  109. function InnerRoundTo(const AValue: Extended; const ADigit: Integer; RoundMode: TRoundMode): Extended;
  110. var
  111. LFactor, Offset, HFactor: Extended;
  112. begin
  113. LFactor := IntPower(10, ADigit);
  114. HFactor := IntPower(10, abs(ADigit));
  115. Result := AValue;
  116. case RoundMode of
  117. rmNearest:
  118. begin
  119. if AValue >= 0 then
  120. Offset := 0.5
  121. else
  122. Offset := -0.5;
  123. Result := Trunc((AValue * HFactor) + Offset) * LFactor;
  124. end;
  125. rmUP:
  126. begin
  127. if Frac(AValue / LFactor) > 0 then
  128. Result := Trunc(AValue * HFactor + 1) * LFactor
  129. else
  130. Result := Trunc(AValue * HFactor) * LFactor;
  131. end;
  132. rmDown:
  133. begin
  134. Result := Trunc(AValue * HFactor) * LFactor;
  135. end;
  136. end;
  137. end;
  138. function GetCompareDigitValue(ADigit: Integer): Double;
  139. begin
  140. if ADigit < 0 then
  141. Result := IntPower(10, ADigit - 1)
  142. else
  143. Result := 0.1;
  144. end;
  145. function QuantityRoundTo(AValue: Double): Double;
  146. begin
  147. if Assigned(OpenProjectManager.CurProjectData) then
  148. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.QuantityDigit)
  149. else
  150. Result := CommonRoundTo(AValue, iQuantityDigit);
  151. end;
  152. function PriceRoundTo(AValue: Double): Double;
  153. begin
  154. if Assigned(OpenProjectManager.CurProjectData) then
  155. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.PriceDigit)
  156. else
  157. Result := CommonRoundTo(AValue, iPriceDigit);
  158. end;
  159. function TotalPriceRoundTo(AValue: Double): Double;
  160. begin
  161. if Assigned(OpenProjectManager.CurProjectData) then
  162. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.TotalPriceDigit)
  163. else
  164. Result := CommonRoundTo(AValue, iTotalPriceDigit);
  165. end;
  166. function GetTrueDigit(AValue: Double): Integer;
  167. const
  168. sFormat12 = '0.############';
  169. var
  170. sValue, sDigitValue: string;
  171. iPointPos: Integer;
  172. begin
  173. sValue := FormatFloat(sFormat12, AValue);
  174. iPointPos := Pos('.', sValue);
  175. Result := Min(0, -(Length(sValue) - iPointPos));
  176. end;
  177. function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
  178. var
  179. X: Double;
  180. P: Pointer;
  181. I: Integer;
  182. Buf: array [0..7] of Byte;
  183. begin
  184. P := @AValue;
  185. CopyMemory(@Buf[0], P, SizeOf(AValue));
  186. // 进位处理,从后往前,如果当前byte为$FF,则往前一byte进1
  187. // 注意到尾数开始的位置停止
  188. // 这里说的前后是二进制串,实际存储前后相反
  189. for I := 0 to 6 do
  190. if (I < 6) and (Buf[I] = $FF) then
  191. Buf[I] := 0
  192. else
  193. begin
  194. Buf[I] := Buf[I] + 1;
  195. Break;
  196. end;
  197. P := @X;
  198. CopyMemory(P, @Buf[0], SizeOf(X));
  199. if (ADigit < 0) and (ADigit < GetTrueDigit(AValue)) then
  200. Result := AValue
  201. else
  202. Result := InnerRoundTo(X, ADigit, RoundMode);
  203. end;
  204. {Interface Control}
  205. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  206. begin
  207. if Assigned(AControl) then
  208. begin
  209. if Assigned(AControl.Parent) then
  210. AControl.Parent.RemoveControl(AControl);
  211. AControl.Parent := AParent;
  212. AControl.Align := AAlign;
  213. end;
  214. end;
  215. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  216. begin
  217. if Assigned(ADxBtn) then
  218. if ADxBtn is TdxBarButton then
  219. TdxBarButton(ADxBtn).Action := AAction;
  220. end;
  221. {DataBase Rela}
  222. function GetsdDataSetNewID(ADataSet: TsdDataSet; const AIndex: string): Integer;
  223. var
  224. idx: TsdIndex;
  225. begin
  226. idx := ADataSet.FindIndex(AIndex);
  227. if idx.RecordCount > 0 then
  228. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  229. else
  230. Result := 1;
  231. end;
  232. procedure ExecuteSql(AConnection: TADOConnection; const ASql: string);
  233. var
  234. vQuery: TADOQuery;
  235. begin
  236. vQuery := TADOQuery.Create(nil);
  237. try
  238. vQuery.Connection := AConnection;
  239. vQuery.SQL.Add(ASql);
  240. vQuery.ExecSQL;
  241. finally
  242. vQuery.Free;
  243. end;
  244. end;
  245. function QueryData(AConnection: TADOConnection; const ASql: string): TADOQuery;
  246. begin
  247. Result := TADOQuery.Create(nil);
  248. Result.Connection := AConnection;
  249. Result.SQL.Clear;
  250. Result.SQL.Add(ASql);
  251. Result.Open;
  252. end;
  253. {Message}
  254. function GetValidHandle(AHandle: THandle = 0): THandle;
  255. begin
  256. if AHandle = 0 then
  257. begin
  258. if (Screen.ActiveForm <> nil) and (Screen.ActiveForm.HandleAllocated) then
  259. Result := Screen.ActiveForm.Handle
  260. else
  261. Result := Application.Handle;
  262. end
  263. else
  264. Result := AHandle;
  265. end;
  266. procedure WarningMessage(const AMsg: string; AHandle: THandle = 0);
  267. begin
  268. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('警告'), MB_OK or MB_ICONWARNING);
  269. end;
  270. procedure ErrorMessage(const AMsg: string; AHandle: THandle = 0);
  271. begin
  272. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('错误'), MB_OK or MB_ICONERROR);
  273. end;
  274. function QuestMessage(const AMsg: string; AHandle: THandle = 0): Boolean;
  275. begin
  276. Result := MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('询问'), MB_OKCANCEL or MB_ICONQUESTION) = IDOK;
  277. end;
  278. function QuestMessageYesNo(const AMsg: string; AHandle: THandle = 0): Boolean;
  279. begin
  280. Result := MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('询问'), MB_YESNO or MB_ICONQUESTION) = IDYes;
  281. end;
  282. procedure TipMessage(const AMsg: string; AHandle: THandle = 0);
  283. begin
  284. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('提示'), MB_OK or MB_ICONINFORMATION);
  285. end;
  286. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  287. begin
  288. Allow := False;
  289. ErrorMessage(AMsg);
  290. end;
  291. {Get Common Path}
  292. function GetAppFilePath: string;
  293. begin
  294. Result := ExtractFilePath(ParamStr(0));
  295. end;
  296. function GetMyProjectsFilePath: string;
  297. begin
  298. Result := GetAppFilePath + '我的项目\';
  299. end;
  300. function GetEmptyDataBaseFileName: string;
  301. begin
  302. Result := GetAppFilePath + 'Data\Base.dat';
  303. end;
  304. function GetTemplateBillsFileName: string;
  305. begin
  306. Result := GetAppFilePath + 'Data\Template.xls';
  307. end;
  308. function GetTemplateXlsFileName: string;
  309. begin
  310. Result := GetAppFilePath + 'Data\Basic.xls';
  311. end;
  312. function GetBackupFilePath: string;
  313. begin
  314. Result := GetAppFilePath + 'TenderBackup\';
  315. end;
  316. function GetReportTemplatePath: string;
  317. begin
  318. Result := GetAppFilePath + 'ReportTemplates\';
  319. end;
  320. function GetAppTempPath: string;
  321. begin
  322. Result := GetAppFilePath + 'Temp\';
  323. if DirectoryExists(Result) then
  324. CreateDirectoryInDeep(Result);
  325. end;
  326. {Select & Save File Choose}
  327. function GetFilter(AExt: string): string;
  328. begin
  329. if AExt = '' then
  330. Result := '所有文件(*.*)|*.*'
  331. else
  332. begin
  333. if AExt[1] <> '.' then
  334. AExt := '.' + AExt;
  335. Result := Format('(*%s)|*%s', [AExt, AExt]);
  336. end;
  337. end;
  338. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  339. var
  340. odFile: TOpenDialog;
  341. begin
  342. odFile := TOpenDialog.Create(nil);
  343. try
  344. odFile.Filter := GetFilter(AExt);
  345. if odFile.Execute then
  346. begin
  347. Application.ProcessMessages;
  348. AFileName := odFile.FileName;
  349. Result := True;
  350. end
  351. else
  352. Result := False;
  353. finally
  354. odFile.Free;
  355. end;
  356. end;
  357. function SelectFiles(AFiles: TStrings; const AExt: string): Boolean;
  358. var
  359. odFile: TOpenDialog;
  360. begin
  361. odFile := TOpenDialog.Create(nil);
  362. try
  363. odFile.Options := odFile.Options + [ofAllowMultiSelect];
  364. odFile.Filter := GetFilter(AExt);
  365. if odFile.Execute then
  366. begin
  367. Application.ProcessMessages;
  368. AFiles.Assign(odFile.Files);
  369. Result := True;
  370. end
  371. else
  372. Result := False;
  373. finally
  374. odFile.Free;
  375. end;
  376. end;
  377. function SaveFile(var FileName: string; const AExt: string): Boolean;
  378. var
  379. sdFile: TSaveDialog;
  380. begin
  381. sdFile := TSaveDialog.Create(nil);
  382. try
  383. sdFile.FileName := FileName;
  384. sdFile.DefaultExt := AExt;
  385. sdFile.Filter := GetFilter(AExt);
  386. Result := sdFile.Execute;
  387. if Result then
  388. FileName := sdFile.FileName;
  389. Application.ProcessMessages;
  390. finally
  391. sdFile.Free;
  392. end;
  393. end;
  394. function SelectExcelFile(var AFileName: string): Boolean;
  395. var
  396. odFile: TOpenDialog;
  397. begin
  398. odFile := TOpenDialog.Create(nil);
  399. try
  400. odFile.Filter := 'Excel文件(*.xls)|*.xls;|Excel文件(*.xlsx)|*.xlsx;';
  401. if odFile.Execute then
  402. begin
  403. Application.ProcessMessages;
  404. AFileName := odFile.FileName;
  405. Result := True;
  406. end
  407. else
  408. Result := False;
  409. finally
  410. odFile.Free;
  411. end;
  412. end;
  413. function SaveExcelFile(var FileName: string): Boolean;
  414. function CheckFileName(AFileName: string; AExt: string): string;
  415. begin
  416. if SameText(ExtractFileExt(AFileName), AExt) then
  417. Result := AFileName
  418. else
  419. Result := ExtractFilePath(AFileName) + ExtractFileName(AFileName) + AExt;
  420. end;
  421. var
  422. sdFile: TSaveDialog;
  423. begin
  424. sdFile := TSaveDialog.Create(nil);
  425. try
  426. sdFile.FileName := FileName;
  427. sdFile.Filter := 'Excel文件(*.xls)|*.xls;|Excel文件(*.xlsx)|*.xlsx;';
  428. Result := sdFile.Execute;
  429. if Result then
  430. begin
  431. case sdFile.FilterIndex of
  432. 1: FileName := CheckFileName(sdFile.FileName, '.xls');
  433. 2: FileName := CheckFileName(sdFile.FileName, '.xlsx');
  434. end;
  435. end;
  436. finally
  437. sdFile.Free;
  438. end;
  439. end;
  440. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  441. AParentHandle: THandle; AHasNewFolderBtn: Boolean): Boolean;
  442. var
  443. pID: PItemIDList;
  444. bInfo: TBrowseInfo;
  445. AHandle: THandle;
  446. PStr: array[0..1023] of Char;
  447. sPath: string;
  448. function BFCallBack(Hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
  449. begin
  450. if uMsg = BFFM_INITIALIZED then
  451. begin
  452. SendMessage(Hwnd, BFFM_SETSELECTION, 1, lpData);
  453. end;
  454. Result := 0;
  455. end;
  456. begin
  457. Result := False;
  458. if AParentHandle = 0 then
  459. AHandle := Screen.ActiveForm.Handle
  460. else
  461. AHandle := AParentHandle;
  462. if ADirectory = '' then
  463. sPath := GetAppFilePath
  464. else
  465. sPath := ADirectory;
  466. with bInfo do
  467. begin
  468. hwndOwner := AParentHandle;
  469. iImage := 0;
  470. lParam := Integer(PChar(sPath));
  471. lpszTitle := PChar(ATitle);
  472. pidlRoot := nil;
  473. pszDisplayName := nil;
  474. if AHasNewFolderBtn then
  475. ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI
  476. else
  477. ulFlags := BIF_RETURNONLYFSDIRS;
  478. lpfn := @BFCallBack;
  479. end;
  480. pID := SHBrowseForFolder(bInfo);
  481. if pID <> nil then
  482. begin
  483. SHGetPathFromIDList(pID, PStr);
  484. ADirectory := StrPas(PStr);
  485. if (ADirectory <> '') and (ADirectory[Length(ADirectory)] <> '\') then
  486. ADirectory := ADirectory + '\';
  487. Result := True;
  488. end;
  489. end;
  490. function FixPathByAppPath(AFileName: string): string;
  491. begin
  492. Result := AFileName;
  493. if AnsiPos(':\', Result) = 0 then
  494. begin
  495. if (Result <> '') and (Result[1] = '\') then
  496. Delete(Result, 1, 1);
  497. Result := ExtractFilePath(Application.ExeName) + Result;
  498. end;
  499. end;
  500. var
  501. PathStr: string;
  502. function BrowseFolder(var APath: string; const ATitle: string; AParentHandle: THandle;
  503. AHasNewFolderBtn: Boolean): Boolean;
  504. function BFCallBackFunc(hwnd: HWND; uMsg: UINT; lParam: LPARAM; lpData: LPARAM): Integer; stdcall;
  505. begin
  506. Result := 0;
  507. case uMsg of
  508. BFFM_INITIALIZED : // 初始化设置目录。
  509. begin
  510. SendMessage(Hwnd,BFFM_SETSELECTION,-1,Integer(PathStr));
  511. end;
  512. end;
  513. end;
  514. var
  515. pID: PItemIDList;
  516. bInfo: TBrowseInfo;
  517. PStr: array[0..1023] of Char;
  518. sPath: string;
  519. Hdl: THandle;
  520. begin
  521. Result := False;
  522. PathStr := '';
  523. if AParentHandle = 0 then
  524. Hdl := Screen.ActiveForm.Handle
  525. else
  526. Hdl := AParentHandle;
  527. sPath := FixPathByAppPath(APath);
  528. if DirectoryExists(sPath) then
  529. PathStr := sPath;
  530. bInfo.hwndOwner := Hdl;
  531. bInfo.iImage := 0;
  532. bInfo.lParam := 0;
  533. bInfo.lpszTitle := PChar(ATitle);
  534. bInfo.pidlRoot := nil;
  535. bInfo.pszDisplayName := nil;
  536. if (not AHasNewFolderBtn) then
  537. bInfo.ulFlags := BIF_RETURNONLYFSDIRS
  538. else
  539. bInfo.ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI;
  540. bInfo.lpfn := @BFCallBackFunc;
  541. pID := SHBrowseForFolder(bInfo);
  542. if pID <> nil then
  543. begin
  544. SHGetPathFromIDList(pID,pStr);
  545. APath := StrPas(pStr);
  546. if (APath <> '') and (APath[Length(APath)] <> '\') then
  547. APath := APath + '\';
  548. Result := True;
  549. end;
  550. end;
  551. {CheckStrings}
  552. function CheckPeg(const AStr: string): Boolean;
  553. function GetPosition(const AName, AStr, AStrSpare: string): Integer;
  554. begin
  555. Result := Pos(AStr, AName);
  556. if Result = 0 then
  557. Result := Pos(AStrSpare, AName);
  558. end;
  559. var
  560. iPosK, iPosPlus: Integer;
  561. fNum: Double;
  562. begin
  563. Result := False;
  564. iPosK := GetPosition(AStr, 'K', 'k');
  565. iPosPlus := GetPosition(AStr, '+', '+');
  566. if (iPosK = 0) or (iPosPlus = 0) or (iPosPlus < iPosK) then Exit;
  567. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosPlus - iPosK - 1), fNum);
  568. end;
  569. function CheckValidPassword(APassword: string): Boolean;
  570. var
  571. iIndex, iLength: Integer;
  572. begin
  573. Result := True;
  574. if APassword = '' then Exit;
  575. iIndex := 1;
  576. iLength := Length(APassword);
  577. Result := (iLength >= 6) and (iLength <= 16);
  578. while Result and (iIndex < iLength) do
  579. begin
  580. Result := APassword[iIndex] in ['A'..'Z', 'a'..'z', '0'..'9'];
  581. Inc(iIndex);
  582. end;
  583. end;
  584. function ValidInteger(var AKey: Char): Boolean;
  585. begin
  586. if AKey in ['+', '-', '0'..'9', #8, #13, #35, #36,
  587. #37, #38, #39, #40, #46] then
  588. begin
  589. Result := True;
  590. end
  591. else
  592. begin
  593. AKey := #0;
  594. Result := False;
  595. end;
  596. end;
  597. {MergeStrings}
  598. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  599. var
  600. sgs1, sgs2: TStrings;
  601. i1, i2: Integer;
  602. bExist: Boolean;
  603. begin
  604. Result := ABGLCode1;
  605. sgs1 := TStringList.Create;
  606. sgs2 := TStringList.Create;
  607. try
  608. sgs1.Delimiter := ';';
  609. sgs2.Delimiter := ';';
  610. sgs1.DelimitedText := ABGLCode1;
  611. sgs2.DelimitedText := ABGLCode2;
  612. for i2 := 0 to sgs2.Count - 1 do
  613. begin
  614. bExist := False;
  615. for i1 := 0 to sgs1.Count - 1 do
  616. if SameText(sgs2[i2], sgs1[i1]) then
  617. begin
  618. bExist := True;
  619. Break;
  620. end;
  621. if not bExist then
  622. sgs1.Add(sgs2[i2]);
  623. end;
  624. Result := sgs1.DelimitedText;
  625. finally
  626. sgs1.Free;
  627. sgs2.Free;
  628. end;
  629. end;
  630. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  631. var
  632. sgsC1, sgsC2, sgsN1, sgsN2: TStrings;
  633. i1, i2: Integer;
  634. bExist: Boolean;
  635. begin
  636. sgsC1 := TStringList.Create;
  637. sgsC2 := TStringList.Create;
  638. sgsN1 := TStringList.Create;
  639. sgsN2 := TStringList.Create;
  640. try
  641. sgsC1.Delimiter := ';';
  642. sgsC2.Delimiter := ';';
  643. sgsC1.DelimitedText := ABGLCode1;
  644. sgsC2.DelimitedText := ABGLCode2;
  645. sgsN1.Delimiter := ';';
  646. sgsN2.Delimiter := ';';
  647. sgsN1.DelimitedText := ABGLNum1;
  648. sgsN2.DelimitedText := ABGLNum2;
  649. for i2 := 0 to sgsC2.Count - 1 do
  650. begin
  651. bExist := False;
  652. for i1 := 0 to sgsC1.Count - 1 do
  653. if SameText(sgsC2[i2], sgsC1[i1]) then
  654. begin
  655. bExist := True;
  656. Break;
  657. end;
  658. if bExist then
  659. begin
  660. sgsN1[i1] := FloatToStr(StrToFloatDef(sgsN1[i1], 0) + StrToFloatDef(sgsN2[i2], 0))
  661. end
  662. else
  663. begin
  664. sgsC1.Add(sgsC2[i2]);
  665. sgsN1.Add(sgsN2[i2]);
  666. end;
  667. end;
  668. ABGLCode1 := sgsC1.DelimitedText;
  669. ABGLNum1 := sgsN1.DelimitedText;
  670. finally
  671. sgsC1.Free;
  672. sgsC2.Free;
  673. sgsN1.Free;
  674. sgsN2.Free;
  675. end;
  676. end;
  677. function B_CodeToIndexCode(const AB_Code: string): string;
  678. var
  679. sgs: TStrings;
  680. i, iNum, iError: Integer;
  681. sError: string;
  682. begin
  683. sgs := TStringList.Create;
  684. try
  685. Result := '';
  686. sgs.Delimiter := '-';
  687. sgs.DelimitedText := AB_Code;
  688. for i := 0 to sgs.Count - 1 do
  689. begin
  690. Val(sgs[i], iNum, iError);
  691. if iError = 0 then
  692. sError := ''
  693. else
  694. sError := Copy(sgs[i], iError, Length(sgs[i])-iError+1);
  695. if iError = 0 then
  696. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError])
  697. else if iNum = 0 then
  698. Result := Result + '9999' + Format('%-4s', [sError])
  699. else
  700. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError]);
  701. end;
  702. finally
  703. sgs.Free;
  704. end;
  705. end;
  706. function ChinessNum(const ADigitNum: Integer): string;
  707. function TransChineseNum(const ANum, AZeroNum: Integer): string;
  708. begin
  709. Result := '';
  710. case ANum of
  711. 1: if AZeroNum <> 1 then Result := '一';
  712. 2: Result := '二';
  713. 3: Result := '三';
  714. 4: Result := '四';
  715. 5: Result := '五';
  716. 6: Result := '六';
  717. 7: Result := '七';
  718. 8: Result := '八';
  719. 9: Result := '九';
  720. end;
  721. if (Result = '') and ((AZeroNum <> 1) or (ANum = 0)) then Exit;
  722. case AZeroNum of
  723. 0: Result := Result;
  724. 1: Result := Result + '十';
  725. 2: Result := Result + '百';
  726. 3: Result := Result + '千';
  727. 4: Result := Result + '万';
  728. end;
  729. end;
  730. var
  731. iBai, iShi, iGe: Integer;
  732. begin
  733. Result := '';
  734. if (ADigitNum < 0) and (ADigitNum > 10000) then Exit;
  735. iBai := ADigitNum div 100;
  736. iShi := (ADigitNum mod 100) div 10;
  737. iGe := (ADigitNum mod 100) mod 10;
  738. Result := TransChineseNum(iBai, 2) + TransChineseNum(iShi, 1) + TransChineseNum(iGe, 0);
  739. end;
  740. function CheckDigit(ANum: Double): Integer;
  741. begin
  742. if abs(ANum - advRoundTo(ANum, -2)) > 0.0001 then
  743. Result := 3
  744. else if abs(ANum - advRoundTo(ANum, -1)) > 0.001 then
  745. Result := 2
  746. else if abs(ANum - AdvRoundTo(ANum, 0)) > 0.01 then
  747. Result := 1
  748. else
  749. Result := 0;
  750. end;
  751. function Num2Peg(ANum: Double): string;
  752. var
  753. fMod: Double;
  754. iK, iDigit: Integer;
  755. sDigit: string;
  756. begin
  757. iK := Trunc(ANum/1000);
  758. fMod := Frac(ANum/1000)*1000;
  759. iDigit := CheckDigit(fMod);
  760. case iDigit of
  761. 3: sDigit := FormatFloat('000.000', AdvRoundTo(fMod, -3));
  762. 2: sDigit := FormatFloat('000.00', AdvRoundTo(fMod, -2));
  763. 1: sDigit := FormatFloat('000.0', AdvRoundTo(fMod, -1));
  764. 0: sDigit := FormatFloat('000', Trunc(AdvRoundTo(fMod, 0)));
  765. end;
  766. { case iDigit of
  767. 3: Result := Format('K%d+%3.3f', [iK, ]);
  768. 2: Result := Format('K%d+%3.2f', [iK, AdvRoundTo(fMod, -2)]);
  769. 1: Result := Format('K%d+%3.1f', [iK, AdvRoundTo(fMod, -1)]);
  770. 0: Result := Format('K%d+%s', [iK, FormatFloat('000', [])]);
  771. end; }
  772. Result := Format('K%d+%s', [iK, sDigit]);
  773. end;
  774. {Tree Relative}
  775. function CreateTree: TZjIDTree;
  776. begin
  777. Result := TZjIDTree.Create;
  778. Result.KeyFieldName := 'ID';
  779. Result.ParentFieldName := 'ParentID';
  780. Result.NextSiblingFieldName := 'NextSiblingID';
  781. Result.AutoCreateKeyID := True;
  782. Result.AutoExpand := True;
  783. end;
  784. procedure DisConnectTree(ATree: TZjIDTree);
  785. begin
  786. ATree.DataSet := nil;
  787. ATree.Active := False;
  788. end;
  789. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  790. begin
  791. ATree.DataSet := ADataset;
  792. ATree.Active := True;
  793. end;
  794. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  795. var
  796. I: Integer;
  797. begin
  798. for I := 0 to AGridDBA.Columns.Count - 1 do
  799. AGrid.Cells[I + 1, 0].Text := AGridDBA.Columns[I].Title.Caption;
  800. end;
  801. {Generate Name/Path}
  802. function GetTempFileDir: string;
  803. var
  804. TempPath: string;
  805. begin
  806. TempPath := GetEnvironmentVariable('TMP');
  807. if TempPath = '' then
  808. TempPath := GetEnvironmentVariable('TEMP');
  809. if TempPath = '' then
  810. begin
  811. if not DirectoryExists(ExtractFileDir(ParamStr(0)) + '\Temp') then
  812. CreateDir(ExtractFileDir(ParamStr(0)) + '\Temp');
  813. TempPath := ExtractFileDir(ParamStr(0)) + '\Temp';
  814. end;
  815. Result := TempPath;
  816. end;
  817. function GetTempFilePath: string;
  818. begin
  819. Result := GetTempFileDir + '\';
  820. end;
  821. function GetTempName(ALength: Integer): string;
  822. const
  823. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  824. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  825. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  826. 'W', 'X', 'Y', 'Z');
  827. var
  828. Temp: string;
  829. Num: Integer;
  830. begin
  831. Randomize;
  832. Temp := '';
  833. while Length(Temp) < ALength do
  834. begin
  835. Num := Random(37);
  836. If Num <> 36 Then
  837. Temp := Temp + CodedBuf[Num]; // 0..35
  838. end;
  839. Result := Temp;
  840. end;
  841. function GetTempFileName: string;
  842. var
  843. TempExt: string;
  844. begin
  845. Result := GetTempFilePath + GetTempName;
  846. TempExt := '.' + GetTempName(3);
  847. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  848. SameText(TempExt, '.ldb') do
  849. TempExt := '.' + GetTempName(3);
  850. Result := Result + TempExt;
  851. while FileExists(Result) do
  852. begin
  853. Result := GetTempFilePath + GetTempName;
  854. TempExt := '.' + GetTempName(3);
  855. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  856. SameText(TempExt, '.ldb') do
  857. TempExt := '.' + GetTempName(3);
  858. Result := Result + TempExt;
  859. end;
  860. end;
  861. function GetTempFileName(const APath, AExt: string): string;
  862. begin
  863. Result := APath + GetTempName + AExt;
  864. while FileExists(Result) do
  865. Result := APath + GetTempName + AExt;
  866. end;
  867. function GenerateTempFolder(AGeneratePath: string): string;
  868. begin
  869. Result := AGeneratePath + GetTempName;
  870. while DirectoryExists(Result) do
  871. Result := AGeneratePath + GetTempName;
  872. CreateDirectoryInDeep(Result);
  873. end;
  874. function GetNewGUIDFileName(const AGeneratePath: string): string;
  875. var
  876. gFile: TGUID;
  877. begin
  878. repeat
  879. CreateGUID(gFile);
  880. Result := AGeneratePath + GUIDToString(gFile);
  881. until not FileExists(Result);
  882. end;
  883. {Progress bar Control}
  884. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  885. begin
  886. if not SysProgressDisabled then
  887. UpdateProgress(APosition, AHint);
  888. end;
  889. procedure UpdateProgress(APosition: Integer; const AHint: string);
  890. begin
  891. MainForm.UpdateProgress(APosition, AHint);
  892. end;
  893. procedure DisableSysProgress;
  894. begin
  895. SysProgressDisabled := True;
  896. end;
  897. procedure EnableSysProgress;
  898. begin
  899. SysProgressDisabled := False;
  900. end;
  901. {Interface RePaint Control}
  902. procedure BeginUpdateWindow(AHandle: THandle);
  903. begin
  904. SendMessage(AHandle, WM_SETREDRAW, 0, 0);
  905. end;
  906. procedure EndUpdateWindow(AHandle: THandle);
  907. begin
  908. SendMessage(AHandle, WM_SETREDRAW, 1, 0);
  909. end;
  910. {Zip}
  911. function ZipComplete(const AFileName: string; ACheckMode: Integer): Boolean;
  912. const
  913. i4k = 4096;
  914. var
  915. iSize1, iSize2: Integer;
  916. begin
  917. iSize1 := FileSizeByName(AFileName);
  918. Sleep(300);
  919. iSize2 := FileSizeByName(AFileName);
  920. Result := (iSize1 = iSize2);
  921. if ACheckMode = 0 then
  922. Result := Result and (iSize1 > i4k);
  923. end;
  924. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  925. procedure AppendLog(const ALog: string);
  926. begin
  927. if SupportManager.ConfigInfo.IsLog then
  928. MeasureLog.AppendLogTo(ALog);
  929. end;
  930. var
  931. sTempFile: string;
  932. vZip: TVCLZip;
  933. iCount: Integer;
  934. begin
  935. AppendLog('Zip');
  936. sTempFile := GetTempFileName;
  937. vZip := TVCLZip.Create(nil);
  938. iCount := 0;
  939. try
  940. vZip.FilesList.Clear;
  941. vZip.ZipName := sTempFile;
  942. vzip.RootDir := AFileFolder;
  943. vZip.OverwriteMode := Always;
  944. //vZip.AddDirEntriesOnRecurse:=True;
  945. vZip.RelativePaths := True;
  946. //vZip.RecreateDirs := True;
  947. vZip.FilesList.Add(AFileFolder + '\*.*');
  948. vZip.Zip;
  949. AppendLog(Format('Check Zip File, FileSize - %d', [FileSizeByName(sTempFile)]));
  950. while not ZipComplete(sTempFile, ACheckMode) and (iCount < 10) do
  951. begin
  952. Sleep(200);
  953. Inc(iCount);
  954. AppendLog(Format('Check Zip File - %d, FileSize - %d', [iCount + 1, FileSizeByName(sTempFile)]));
  955. end;
  956. AppendLog('Check Zip File --> Pass');
  957. CopyFileOrFolder(sTempFile, AFileName);
  958. AppendLog('Zip --> Pass');
  959. finally
  960. vZip.Free;
  961. if FileExists(sTempFile) then
  962. DeleteFile(sTempFile);
  963. end;
  964. end;
  965. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  966. var
  967. vUnZip: TVCLZip;
  968. begin
  969. Result := True;
  970. vUnZip := TVCLZip.Create(nil);
  971. try
  972. vUnZip.FilesList.Clear;
  973. vUnZip.ZipName := AFileName;
  974. vUnZip.ReadZip;
  975. vUnZip.DestDir := AFileFolder;
  976. vUnZip.OverwriteMode := Always;
  977. vUnZip.RecreateDirs := True;
  978. vUnZip.RelativePaths := True;
  979. vUnZip.DoAll := True;
  980. vUnZip.FilesList.Add('*.*');
  981. try
  982. vUnZip.UnZip;
  983. except
  984. Result := False;
  985. end;
  986. finally
  987. vUnZip.Free;
  988. end;
  989. end;
  990. {Copy By Stream}
  991. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  992. var
  993. MS: TMemoryStream;
  994. begin
  995. MS := TMemoryStream.Create;
  996. try
  997. MS.LoadFromFile(ASourceFile);
  998. if FileExists(ADestFile) then
  999. DeleteFile(ADestFile);
  1000. MS.SaveToFile(ADestFile);
  1001. finally
  1002. MS.Free;
  1003. end;
  1004. end;
  1005. procedure AppendTestLog(const AFileName, ALog: string);
  1006. var
  1007. f: TextFile;
  1008. begin
  1009. try
  1010. if FileExists(AFileName) then
  1011. begin
  1012. AssignFile(f, AFileName);
  1013. Append(f);
  1014. Writeln(f, ALog);
  1015. end
  1016. else
  1017. begin
  1018. AssignFile(f, AFileName);
  1019. Rewrite(f);
  1020. Writeln(f, ALog);
  1021. end;
  1022. finally
  1023. CloseFile(f);
  1024. end;
  1025. end;
  1026. function GetVersion(APartCount: Integer = 4): string;
  1027. var
  1028. V1, V2, V3, V4: Word;
  1029. FInfoSize, FF: Cardinal;
  1030. FInfo: Pointer;
  1031. FFixed: PVSFIXEDFILEINFO;
  1032. begin
  1033. FInfoSize := GetFileVersionInfoSize(PChar(Application.ExeName), FF);
  1034. FInfo := AllocMem(FInfoSize);
  1035. try
  1036. GetFileVersionInfo(PChar(Application.ExeName), FF, FInfoSize, FInfo);
  1037. VerQueryValue(FInfo, '\', Pointer(FFixed), FInfoSize);
  1038. V1 := FFixed.dwFileVersionMS shr 16;
  1039. V2 := FFixed.dwFileVersionMS and $FFFF;
  1040. V3 := FFixed.dwFileVersionLS shr 16;
  1041. V4 := FFixed.dwFileVersionLS and $FFFF;
  1042. finally
  1043. Dispose(FInfo);
  1044. end;
  1045. Result := '';
  1046. case APartCount of
  1047. 1:
  1048. Result := Format('%d', [V1]);
  1049. 2:
  1050. Result := Format('%d.%d', [V1, V2]);
  1051. 3:
  1052. Result := Format('%d.%d.%d', [V1, V2, V3]);
  1053. 4:
  1054. Result := Format('%d.%d.%d.%d', [V1, V2, V3, V4]);
  1055. end;
  1056. end;
  1057. function ExtractFileNameWithoutExt(const AFileName: string): string;
  1058. var
  1059. sFileName, Ext: string;
  1060. begin
  1061. Result := '';
  1062. if AFileName = '' then
  1063. Exit;
  1064. sFileName := AFileName;
  1065. if sFileName[Length(sFileName)] = '\' then
  1066. Delete(sFileName, Length(sFileName), 1);
  1067. Result := ExtractFileName(sFileName);
  1068. Ext := ExtractFileExt(sFileName);
  1069. Delete(Result, Length(Result) - Length(Ext) + 1, Length(Ext));
  1070. end;
  1071. function ShortText(AText: string; AWidth: Integer): string;
  1072. var vMM: TMemo;
  1073. begin
  1074. if Trim(AText) = '' then
  1075. begin
  1076. Result := '';
  1077. Exit;
  1078. end;
  1079. vMM := TMemo.Create(nil);
  1080. try
  1081. vMM.Visible := False;
  1082. vMM.WordWrap := True;
  1083. vMM.parent := Application.MainForm;
  1084. vMM.Width := AWidth - 10; // 10像素留给3个小点
  1085. vMM.Text := AText;
  1086. if vMM.Lines.Count > 1 then
  1087. Result := vMM.Lines[0] + '...'
  1088. else
  1089. Result := AText;
  1090. finally
  1091. vMM.Free;
  1092. end;
  1093. end;
  1094. function CustomWidthText(AText: string; AWidth: Integer): string;
  1095. var vMM: TMemo;
  1096. i: Integer;
  1097. begin
  1098. if Trim(AText) = '' then
  1099. begin
  1100. Result := '';
  1101. Exit;
  1102. end;
  1103. vMM := TMemo.Create(nil);
  1104. try
  1105. vMM.Visible := False;
  1106. vMM.WordWrap := True;
  1107. vMM.parent := Application.MainForm;
  1108. vMM.Width := AWidth;
  1109. vMM.Text := AText;
  1110. for i := 0 to vMM.Lines.Count - 1 do
  1111. begin
  1112. if i = 0 then
  1113. Result := vMM.Lines[i]
  1114. else
  1115. Result := Result + #10#13 + vMM.Lines[i];
  1116. end;
  1117. finally
  1118. vMM.Free;
  1119. end;
  1120. end;
  1121. function ReplaceCharsForJson(AText: string): string;
  1122. const
  1123. BefChar: array [0..7] of Char = ('{', '}', ',', ':', '"', '[', ']', '%');
  1124. AftChar: array [0..7] of string = ('{', '}', ',', ':', '"', '【', '】', '♂');
  1125. var I: Integer;
  1126. begin
  1127. AText := Trim(AText);
  1128. Result := AText;
  1129. if AText = '' then Exit;
  1130. for I := low(BefChar) to High(BefChar) do
  1131. begin
  1132. if Pos(BefChar[I], AText) > 0 then
  1133. AText := StringReplace(AText, BefChar[I], AftChar[I], [rfReplaceAll]);
  1134. end;
  1135. Result := AText;
  1136. end;
  1137. function RecoverCharsFromJson(AText: string): string;
  1138. const
  1139. BefStr: array [0..3] of string = ('♂', '\r\n', '\r', '\n');
  1140. AftStr: array [0..3] of string = ('%', '', '', '');
  1141. var I: Integer;
  1142. begin
  1143. AText := Trim(AText);
  1144. Result := AText;
  1145. if AText = '' then Exit;
  1146. for I := low(BefStr) to High(BefStr) do
  1147. begin
  1148. if Pos(BefStr[I], AText) > 0 then
  1149. AText := StringReplace(AText, BefStr[I], AftStr[I], [rfReplaceAll]);
  1150. end;
  1151. Result := AText;
  1152. end;
  1153. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  1154. var
  1155. SRec: TSearchRec;
  1156. retval: Integer;
  1157. begin
  1158. AFileList.Clear;
  1159. retval := FindFirst(APath + AExtName, faAnyFile, sRec);
  1160. try
  1161. while retval = 0 do
  1162. begin
  1163. if (SRec.Attr and faDirectory) = 0 then
  1164. AFileList.Add(Srec.Name);
  1165. retval := FindNext(SRec);
  1166. end;
  1167. finally
  1168. FindClose(SRec);
  1169. end;
  1170. end;
  1171. function DeleteFolder(const FolderStr: string): Boolean;
  1172. var
  1173. fos: SHFILEOPSTRUCT;
  1174. begin
  1175. ZeroMemory(@fos, SizeOf(fos));
  1176. fos.Wnd := HWND_DESKTOP;
  1177. fos.wFunc := FO_DELETE;
  1178. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO OR FOF_NOCONFIRMATION;
  1179. fos.pFrom := PChar(FolderStr + #0);
  1180. Result := SHFileOperation(fos) <> 0;
  1181. end;
  1182. function HasExt(const AFileName: string): Boolean;
  1183. begin
  1184. Result := ExtractFileExt(AFileName) <> '';
  1185. end;
  1186. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  1187. var
  1188. fos: SHFILEOPSTRUCT;
  1189. begin
  1190. ZeroMemory(@fos, SizeOf(fos));
  1191. fos.Wnd := HWND_DESKTOP;
  1192. fos.wFunc := FO_COPY;
  1193. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO;
  1194. fos.pFrom := PChar(SrcFolder + #0);
  1195. fos.pTo := PChar(DstFolder + #0);
  1196. Result := SHFileOperation(fos) <> 0;
  1197. end;
  1198. function FileCount(APath: string): Integer;
  1199. var
  1200. vSR: TSearchRec;
  1201. iRetval: Integer;
  1202. vSL: TStringList;
  1203. begin
  1204. vSL := TStringList.Create;
  1205. iRetval := FindFirst(APath + '*.*', faAnyFile, vSR);
  1206. try
  1207. while iRetval = 0 do
  1208. begin
  1209. if (vSR.Attr and faDirectory) = 0 then
  1210. vSL.Add(vSR.Name);
  1211. iRetval := FindNext(vSR);
  1212. end;
  1213. Result := vSL.Count;
  1214. finally
  1215. FindClose(vSR);
  1216. vSL.Free;
  1217. end;
  1218. end;
  1219. end.