Quick.Commons.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. { ***************************************************************************
  2. Copyright (c) 2016-2020 Kike Pérez
  3. Unit : Quick.Commons
  4. Description : Common functions
  5. Author : Kike Pérez
  6. Version : 2.0
  7. Created : 14/07/2017
  8. Modified : 05/06/2020
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Commons;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. Types,
  28. {$IFDEF MSWINDOWS}
  29. Windows,
  30. ShlObj,
  31. {$ENDIF MSWINDOWS}
  32. {$IFDEF FPC}
  33. Quick.Files,
  34. {$IFDEF LINUX}
  35. FileInfo,
  36. {$ENDIF}
  37. {$ELSE}
  38. IOUtils,
  39. {$ENDIF}
  40. {$IFDEF ANDROID}
  41. Androidapi.JNI.Os,
  42. Androidapi.Helpers,
  43. Androidapi.JNI.JavaTypes,
  44. Androidapi.JNI.GraphicsContentViewText,
  45. {$ENDIF}
  46. {$IFDEF IOS}
  47. iOSapi.UIKit,
  48. Posix.SysSysctl,
  49. Posix.StdDef,
  50. iOSapi.Foundation,
  51. Macapi.ObjectiveC,
  52. Macapi.Helpers,
  53. {$ENDIF}
  54. {$IFDEF OSX}
  55. Macapi.Foundation,
  56. Macapi.Helpers,
  57. FMX.Helpers.Mac,
  58. Macapi.ObjectiveC,
  59. {$ENDIF}
  60. {$IFDEF POSIX}
  61. Posix.Unistd,
  62. {$ENDIF}
  63. DateUtils;
  64. type
  65. TLogEventType = (etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException);
  66. TLogVerbose = set of TLogEventType;
  67. const
  68. LOG_ONLYERRORS = [etInfo,etError];
  69. LOG_ERRORSANDWARNINGS = [etInfo,etWarning,etError];
  70. LOG_TRACE = [etInfo,etError,etWarning,etTrace];
  71. LOG_ALL = [etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException];
  72. LOG_DEBUG = [etInfo,etSuccess,etWarning,etError,etDebug];
  73. {$IFDEF DELPHIXE7_UP}
  74. EventStr : array of string = ['INFO','SUCC','WARN','ERROR','DEBUG','TRACE'];
  75. {$ELSE}
  76. EventStr : array[0..5] of string = ('INFO','SUCC','WARN','ERROR','DEBUG','TRACE');
  77. {$ENDIF}
  78. type
  79. TPasswordComplexity = set of (pfIncludeNumbers,pfIncludeSigns);
  80. TEnvironmentPath = record
  81. EXEPATH : string;
  82. {$IFDEF MSWINDOWS}
  83. WINDOWS : string;
  84. SYSTEM : string;
  85. PROGRAMFILES : string;
  86. COMMONFILES : string;
  87. HOMEDRIVE : string;
  88. TEMP : string;
  89. USERPROFILE : string;
  90. INSTDRIVE : string;
  91. DESKTOP : string;
  92. STARTMENU : string;
  93. DESKTOP_ALLUSERS : string;
  94. STARTMENU_ALLUSERS : string;
  95. STARTUP : string;
  96. APPDATA : String;
  97. PROGRAMDATA : string;
  98. ALLUSERSPROFILE : string;
  99. {$ENDIF MSWINDOWS}
  100. end;
  101. {$IFNDEF FPC}
  102. TFileHelper = record helper for TFile
  103. {$IF DEFINED(MSWINDOWS) OR DEFINED(DELPHILINUX)}
  104. class function IsInUse(const FileName : string) : Boolean; static;
  105. {$ENDIF}
  106. class function GetSize(const FileName: String): Int64; static;
  107. end;
  108. TDirectoryHelper = record helper for TDirectory
  109. class function GetSize(const Path: String): Int64; static;
  110. end;
  111. {$ENDIF}
  112. {$IFDEF FPC}
  113. {$IFDEF LINUX}
  114. UINT = cardinal;
  115. {$ENDIF}
  116. PLASTINPUTINFO = ^LASTINPUTINFO;
  117. tagLASTINPUTINFO = record
  118. cbSize: UINT;
  119. dwTime: DWORD;
  120. end;
  121. LASTINPUTINFO = tagLASTINPUTINFO;
  122. TLastInputInfo = LASTINPUTINFO;
  123. type
  124. TCmdLineSwitchType = (clstValueNextParam, clstValueAppended);
  125. TCmdLineSwitchTypes = set of TCmdLineSwitchType;
  126. {$ENDIF}
  127. TCounter = record
  128. private
  129. fMaxValue : Integer;
  130. fCurrentValue : Integer;
  131. public
  132. property MaxValue : Integer read fMaxValue;
  133. procedure Init(aMaxValue : Integer);
  134. function Count : Integer;
  135. function CountIs(aValue : Integer) : Boolean;
  136. function Check : Boolean;
  137. procedure Reset;
  138. end;
  139. TTimeCounter = record
  140. private
  141. fCurrentTime : TDateTime;
  142. fDoneEvery : Integer;
  143. public
  144. property DoneEvery : Integer read fDoneEvery;
  145. procedure Init(MillisecondsToReach : Integer);
  146. function Check : Boolean;
  147. procedure Reset;
  148. end;
  149. {$IFNDEF FPC}
  150. {$IFNDEF DELPHIXE7_UP}
  151. TArrayUtil<T> = class
  152. class procedure Delete(var aArray : TArray<T>; aIndex : Integer);
  153. end;
  154. {$ENDIF}
  155. TArrayOfStringHelper = record helper for TArray<string>
  156. public
  157. function Any : Boolean; overload;
  158. function Any(const aValue : string) : Boolean; overload;
  159. function Add(const aValue : string) : Integer;
  160. function AddIfNotExists(const aValue : string; aCaseSense : Boolean = False) : Integer;
  161. function Remove(const aValue : string) : Boolean;
  162. function Exists(const aValue : string) : Boolean;
  163. function Count : Integer;
  164. end;
  165. TDelegate<T> = reference to procedure(Value : T);
  166. {$ENDIF}
  167. TPairItem = record
  168. Name : string;
  169. Value : string;
  170. constructor Create(const aName, aValue : string);
  171. end;
  172. TPairList = class
  173. type
  174. TPairEnumerator = class
  175. private
  176. fArray : ^TArray<TPairItem>;
  177. fIndex : Integer;
  178. function GetCurrent: TPairItem;
  179. public
  180. constructor Create(var aArray: TArray<TPairItem>);
  181. property Current : TPairItem read GetCurrent;
  182. function MoveNext: Boolean;
  183. end;
  184. private
  185. fItems : TArray<TPairItem>;
  186. public
  187. function GetEnumerator : TPairEnumerator;
  188. function GetValue(const aName : string) : string;
  189. function GetPair(const aName : string) : TPairItem;
  190. function Add(aPair : TPairItem) : Integer; overload;
  191. function Add(const aName, aValue : string) : Integer; overload;
  192. procedure AddOrUpdate(const aName, aValue : string);
  193. function Exists(const aName : string) : Boolean;
  194. function Remove(const aName : string) : Boolean;
  195. function Count : Integer;
  196. property Items[const aName : string] : string read GetValue write AddOrUpdate;
  197. function ToArray : TArray<TPairItem>;
  198. procedure FromArray(aValue : TArray<TPairItem>);
  199. procedure Clear;
  200. end;
  201. EEnvironmentPath = class(Exception);
  202. EShellError = class(Exception);
  203. //generates a random password with complexity options
  204. function RandomPassword(const PasswordLength : Integer; Complexity : TPasswordComplexity = [pfIncludeNumbers,pfIncludeSigns]) : string;
  205. //generates a random string
  206. function RandomString(const aLength: Integer) : string;
  207. //extracts file extension from a filename
  208. function ExtractFileNameWithoutExt(const FileName: string): string;
  209. //converts a Unix path to Windows path
  210. function UnixToWindowsPath(const UnixPath: string): string;
  211. //converts a Windows path to Unix path
  212. function WindowsToUnixPath(const WindowsPath: string): string;
  213. //corrects malformed urls
  214. function CorrectURLPath(cUrl : string) : string;
  215. //get typical environment paths as temp, desktop, etc
  216. procedure GetEnvironmentPaths;
  217. {$IFDEF MSWINDOWS}
  218. function GetSpecialFolderPath(folderID : Integer) : string;
  219. //checks if running on a 64bit OS
  220. function Is64bitOS : Boolean;
  221. //checks if is a console app
  222. function IsConsole : Boolean;
  223. function HasConsoleOutput : Boolean;
  224. //checks if compiled in debug mode
  225. {$ENDIF}
  226. function IsDebug : Boolean;
  227. {$IFDEF MSWINDOWS}
  228. //checks if running as a service
  229. function IsService : Boolean;
  230. //gets number of seconds without user interaction (mouse, keyboard)
  231. function SecondsIdle: DWord;
  232. //frees process memory not needed
  233. procedure FreeUnusedMem;
  234. //changes screen resolution
  235. function SetScreenResolution(Width, Height: integer): Longint;
  236. {$ENDIF MSWINDOWS}
  237. //returns last day of current month
  238. function LastDayCurrentMonth: TDateTime;
  239. {$IFDEF FPC}
  240. function DateTimeInRange(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
  241. {$ENDIF}
  242. //checks if two datetimes are in same day
  243. function IsSameDay(cBefore, cNow : TDateTime) : Boolean;
  244. //change Time of a DateTime
  245. function ChangeTimeOfADay(aDate : TDateTime; aHour, aMinute, aSecond : Word; aMilliSecond : Word = 0) : TDateTime;
  246. //change Date of a DateTime
  247. function ChangeDateOfADay(aDate : TDateTime; aYear, aMonth, aDay : Word) : TDateTime;
  248. //returns n times a char
  249. function FillStr(const C : Char; const Count : Integer) : string;
  250. //checks if string exists in array of string
  251. function StrInArray(const aValue : string; const aInArray : array of string) : Boolean;
  252. //checks if integer exists in array of integer
  253. function IntInArray(const aValue : Integer; const aInArray : array of Integer) : Boolean;
  254. //check if array is empty
  255. function IsEmptyArray(aArray : TArray<string>) : Boolean; overload;
  256. function IsEmptyArray(aArray : TArray<Integer>) : Boolean; overload;
  257. //returns a number leading zero
  258. function Zeroes(const Number, Len : Int64) : string;
  259. //converts a number to thousand delimeter string
  260. function NumberToStr(const Number : Int64) : string;
  261. //returns n spaces
  262. function Spaces(const Count : Integer) : string;
  263. //returns current date as a string
  264. function NowStr : string;
  265. //returns a new GUID as string
  266. function NewGuidStr : string;
  267. //compare a string with a wildcard pattern (? or *)
  268. function IsLike(cText, Pattern: string) : Boolean;
  269. //Upper case for first letter
  270. function Capitalize(s: string): string;
  271. function CapitalizeWords(s: string): string;
  272. //returns current logged user
  273. function GetLoggedUserName : string;
  274. //returns computer name
  275. function GetComputerName : string;
  276. //Changes incorrect delims in path
  277. function NormalizePathDelim(const cPath : string; const Delim : Char) : string;
  278. //Removes last segment of a path
  279. function RemoveLastPathSegment(cDir : string) : string;
  280. //returns path delimiter if found
  281. function GetPathDelimiter(const aPath : string) : string;
  282. //returns first segment of a path
  283. function GetFirstPathSegment(const aPath : string) : string;
  284. //returns last segment of a path
  285. function GetLastPathSegment(const aPath : string) : string;
  286. //finds swith in commandline params
  287. function ParamFindSwitch(const Switch : string) : Boolean;
  288. //gets value for a switch if exists
  289. function ParamGetSwitch(const Switch : string; var cvalue : string) : Boolean;
  290. //returns app name (filename based)
  291. function GetAppName : string;
  292. //returns app version (major & minor)
  293. function GetAppVersionStr: string;
  294. //returns app version full (major, minor, release & compiled)
  295. function GetAppVersionFullStr: string;
  296. //convert UTC DateTime to Local DateTime
  297. function UTCToLocalTime(GMTTime: TDateTime): TDateTime;
  298. //convert Local DateTime to UTC DateTime
  299. function LocalTimeToUTC(LocalTime : TDateTime): TDateTime;
  300. //convert DateTime to GTM Time string
  301. function DateTimeToGMT(aDate : TDateTime) : string;
  302. //convert GMT Time string to DateTime
  303. function GMTToDateTime(aDate : string) : TDateTime;
  304. //convert DateTime to Json Date format
  305. function DateTimeToJsonDate(aDateTime : TDateTime) : string;
  306. //convert Json Date format to DateTime
  307. function JsonDateToDateTime(const aJsonDate : string) : TDateTime;
  308. //count number of digits of a Integer
  309. function CountDigits(anInt: Cardinal): Cardinal; inline;
  310. //count times a string is present in other string
  311. function CountStr(const aFindStr, aSourceStr : string) : Integer;
  312. //save stream to file
  313. procedure SaveStreamToFile(aStream : TStream; const aFilename : string);
  314. //save stream to string
  315. function StreamToString(aStream : TStream) : string;
  316. function StreamToString2(const aStream: TStream; const aEncoding: TEncoding): string;
  317. //save string to stream
  318. procedure StringToStream(const aStr : string; aStream : TStream);
  319. procedure StringToStream2(const aStr : string; aStream : TStream);
  320. //returns a real comma separated text from stringlist
  321. function CommaText(aList : TStringList) : string; overload;
  322. //returns a real comma separated text from array of string
  323. function CommaText(aArray : TArray<string>) : string; overload;
  324. //returns a string CRLF from array of string
  325. function ArrayToString(aArray : TArray<string>) : string;
  326. //converts TStrings to array
  327. function StringsToArray(aStrings : TStrings) : TArray<string>;
  328. {$IFDEF MSWINDOWS}
  329. //process messages on console applications
  330. procedure ProcessMessages;
  331. //get last error message
  332. function GetLastOSError : String;
  333. {$ENDIF}
  334. {$IF DEFINED(FPC) AND DEFINED(MSWINDOWS)}
  335. function GetLastInputInfo(var plii: TLastInputInfo): BOOL;stdcall; external 'user32' name 'GetLastInputInfo';
  336. {$ENDIF}
  337. function RemoveLastChar(const aText : string) : string;
  338. function DateTimeToSQL(aDateTime : TDateTime) : string;
  339. function IsInteger(const aValue : string) : Boolean;
  340. //extract a substring and deletes from source string
  341. function ExtractStr(var vSource : string; aIndex : Integer; aCount : Integer) : string;
  342. //get first string between string delimiters
  343. function GetSubString(const aSource, aFirstDelimiter, aLastDelimiter : string) : string;
  344. //get double quoted or dequoted string
  345. function DbQuotedStr(const str : string): string;
  346. function UnDbQuotedStr(const str: string) : string;
  347. //get simple quoted or dequoted string
  348. function SpQuotedStr(const str : string): string;
  349. function UnSpQuotedStr(const str : string): string;
  350. //ternary operator
  351. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : string) : string; overload;
  352. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Integer) : Integer; overload;
  353. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Extended) : Extended; overload;
  354. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : TObject) : TObject; overload;
  355. var
  356. path : TEnvironmentPath;
  357. implementation
  358. {TFileHelper}
  359. {$IFNDEF FPC}
  360. {$IFDEF MSWINDOWS}
  361. class function TFileHelper.IsInUse(const FileName : string) : Boolean;
  362. var
  363. HFileRes: HFILE;
  364. begin
  365. Result := False;
  366. if not FileExists(FileName) then Exit;
  367. try
  368. HFileRes := CreateFile(PChar(FileName)
  369. ,GENERIC_READ or GENERIC_WRITE
  370. ,0
  371. ,nil
  372. ,OPEN_EXISTING
  373. ,FILE_ATTRIBUTE_NORMAL
  374. ,0);
  375. Result := (HFileRes = INVALID_HANDLE_VALUE);
  376. if not(Result) then begin
  377. CloseHandle(HFileRes);
  378. end;
  379. except
  380. Result := True;
  381. end;
  382. end;
  383. {$ENDIF}
  384. {$IFDEF DELPHILINUX}
  385. class function TFileHelper.IsInUse(const FileName : string) : Boolean;
  386. var
  387. fs : TFileStream;
  388. begin
  389. try
  390. fs := TFileStream.Create(FileName, fmOpenReadWrite, fmShareExclusive);
  391. Result := True;
  392. fs.Free;
  393. except
  394. Result := False;
  395. end;
  396. end;
  397. {$ENDIF}
  398. {$IFDEF MSWINDOWS}
  399. class function TFileHelper.GetSize(const FileName: String): Int64;
  400. var
  401. info: TWin32FileAttributeData;
  402. begin
  403. Result := -1;
  404. if not GetFileAttributesEx(PWideChar(FileName), GetFileExInfoStandard, @info) then Exit;
  405. Result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
  406. end;
  407. {$ELSE}
  408. class function TFileHelper.GetSize(const FileName: String): Int64;
  409. var
  410. sr : TSearchRec;
  411. begin
  412. if FindFirst(fileName, faAnyFile, sr ) = 0 then Result := sr.Size
  413. else Result := -1;
  414. end;
  415. {$ENDIF}
  416. {TDirectoryHelper}
  417. class function TDirectoryHelper.GetSize(const Path: String): Int64;
  418. var
  419. filename : string;
  420. begin
  421. Result := -1;
  422. for filename in TDirectory.GetFiles(Path) do
  423. begin
  424. Result := Result + TFile.GetSize(filename);
  425. end;
  426. end;
  427. {$ENDIF}
  428. {other functions}
  429. function RandomPassword(const PasswordLength : Integer; Complexity : TPasswordComplexity = [pfIncludeNumbers,pfIncludeSigns]) : string;
  430. const
  431. PassAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  432. PassSigns = '@!&$';
  433. PassNumbers = '1234567890';
  434. var
  435. MinNumbers,
  436. MinSigns : Integer;
  437. NumNumbers,
  438. NumSigns : Integer;
  439. begin
  440. Result := '';
  441. Randomize;
  442. //fill all alfa
  443. repeat
  444. Result := Result + PassAlpha[Random(Length(PassAlpha))+1];
  445. until (Length(Result) = PasswordLength);
  446. //checks if need include numbers
  447. if pfIncludeNumbers in Complexity then
  448. begin
  449. MinNumbers := Round(PasswordLength / 10 * 2);
  450. NumNumbers := 0;
  451. if MinNumbers = 0 then MinNumbers := 1;
  452. repeat
  453. Result[Random(PasswordLength)+1] := PassNumbers[Random(Length(PassNumbers))+1];
  454. Inc(NumNumbers);
  455. until NumNumbers = MinNumbers;
  456. end;
  457. //checks if need include signs
  458. if pfIncludeNumbers in Complexity then
  459. begin
  460. MinSigns := Round(PasswordLength / 10 * 1);
  461. NumSigns := 0;
  462. if MinSigns = 0 then MinSigns := 1;
  463. repeat
  464. Result[Random(PasswordLength)+1] := PassSigns[Random(Length(PassSigns))+1];
  465. Inc(NumSigns);
  466. until NumSigns = MinSigns;
  467. end;
  468. end;
  469. function RandomString(const aLength: Integer) : string;
  470. const
  471. chars : string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  472. var
  473. i : Integer;
  474. clong : Integer;
  475. begin
  476. clong := High(chars);
  477. SetLength(Result, aLength);
  478. for i := 1 to aLength do
  479. begin
  480. Result[i] := chars[Random(clong) + 1];
  481. end;
  482. end;
  483. function ExtractFileNameWithoutExt(const FileName: string): string;
  484. begin
  485. Result := TPath.GetFileNameWithoutExtension(FileName);
  486. end;
  487. function UnixToWindowsPath(const UnixPath: string): string;
  488. begin
  489. Result := StringReplace(UnixPath, '/', '\',[rfReplaceAll, rfIgnoreCase]);
  490. end;
  491. function WindowsToUnixPath(const WindowsPath: string): string;
  492. begin
  493. Result := StringReplace(WindowsPath, '\', '/',[rfReplaceAll, rfIgnoreCase]);
  494. end;
  495. function CorrectURLPath(cUrl : string) : string;
  496. var
  497. nurl : string;
  498. begin
  499. nurl := WindowsToUnixPath(cUrl);
  500. nurl := StringReplace(nurl,'//','/',[rfReplaceAll]);
  501. Result := StringReplace(nurl,' ','%20',[rfReplaceAll]);
  502. //TNetEncoding.Url.Encode()
  503. end;
  504. procedure GetEnvironmentPaths;
  505. begin
  506. //gets path
  507. path.EXEPATH := TPath.GetDirectoryName(ParamStr(0));
  508. {$IFDEF MSWINDOWS}
  509. path.WINDOWS := SysUtils.GetEnvironmentVariable('windir');
  510. path.PROGRAMFILES := SysUtils.GetEnvironmentVariable('ProgramFiles');
  511. path.COMMONFILES := SysUtils.GetEnvironmentVariable('CommonProgramFiles(x86)');
  512. path.HOMEDRIVE := SysUtils.GetEnvironmentVariable('SystemDrive');
  513. path.USERPROFILE := SysUtils.GetEnvironmentVariable('USERPROFILE');
  514. path.PROGRAMDATA := SysUtils.GetEnvironmentVariable('ProgramData');
  515. path.ALLUSERSPROFILE := SysUtils.GetEnvironmentVariable('AllUsersProfile');
  516. path.INSTDRIVE := path.HOMEDRIVE;
  517. path.TEMP := SysUtils.GetEnvironmentVariable('TEMP');
  518. //these paths fail if user is SYSTEM
  519. try
  520. path.SYSTEM := GetSpecialFolderPath(CSIDL_SYSTEM);
  521. path.APPDATA := GetSpecialFolderPath(CSIDL_APPDATA);
  522. path.DESKTOP := GetSpecialFolderPath(CSIDL_DESKTOP);
  523. path.DESKTOP_ALLUSERS := GetSpecialFolderPath(CSIDL_COMMON_DESKTOPDIRECTORY);
  524. path.STARTMENU:=GetSpecialFolderPath(CSIDL_PROGRAMS);
  525. path.STARTMENU_ALLUSERS:=GetSpecialFolderPath(CSIDL_COMMON_PROGRAMS);
  526. path.STARTMENU_ALLUSERS := path.STARTMENU;
  527. path.STARTUP:=GetSpecialFolderPath(CSIDL_STARTUP);
  528. except
  529. //
  530. end;
  531. {$ENDIF}
  532. end;
  533. {$IFDEF MSWINDOWS}
  534. function GetSpecialFolderPath(folderID : Integer) : string;
  535. var
  536. ppidl: PItemIdList;
  537. begin
  538. SHGetSpecialFolderLocation(0, folderID, ppidl);
  539. SetLength(Result, MAX_PATH);
  540. if not SHGetPathFromIDList(ppidl,{$IFDEF FPC}PAnsiChar(Result){$ELSE}PChar(Result){$ENDIF}) then
  541. begin
  542. raise EShellError.create(Format('GetSpecialFolderPath: Invalid PIPL (%d)',[folderID]));
  543. end;
  544. SetLength(Result, lStrLen({$IFDEF FPC}PAnsiChar(Result){$ELSE}PChar(Result){$ENDIF}));
  545. end;
  546. function Is64bitOS : Boolean;
  547. begin
  548. {$IFDEF WIN64}
  549. Result := True;
  550. {$ELSE}
  551. Result := False;
  552. {$ENDIF WIN64}
  553. end;
  554. function IsConsole: Boolean;
  555. begin
  556. {$IFDEF CONSOLE}
  557. Result := True;
  558. {$ELSE}
  559. Result := False;
  560. {$ENDIF CONSOLE}
  561. end;
  562. {$ENDIF}
  563. function HasConsoleOutput : Boolean;
  564. {$IFDEF MSWINDOWS}
  565. var
  566. stout : THandle;
  567. begin
  568. try
  569. stout := GetStdHandle(Std_Output_Handle);
  570. Win32Check(stout <> Invalid_Handle_Value);
  571. Result := stout <> 0;
  572. except
  573. Result := False;
  574. end;
  575. end;
  576. {$ELSE}
  577. begin
  578. Result := IsConsole;
  579. end;
  580. {$ENDIF}
  581. function IsDebug: Boolean;
  582. begin
  583. {$IFDEF DEBUG}
  584. Result := True;
  585. {$ELSE}
  586. Result := False;
  587. {$ENDIF DEBUG}
  588. end;
  589. {$IFDEF MSWINDOWS}
  590. function IsService : Boolean;
  591. begin
  592. //only working with my Quick.AppService unit
  593. try
  594. Result := (IsConsole) and (not HasConsoleOutput);
  595. except
  596. Result := False;
  597. end;
  598. end;
  599. function SecondsIdle: DWord;
  600. var
  601. liInfo: TLastInputInfo;
  602. begin
  603. liInfo.cbSize := SizeOf(TLastInputInfo) ;
  604. GetLastInputInfo(liInfo) ;
  605. Result := (GetTickCount - liInfo.dwTime) DIV 1000;
  606. end;
  607. procedure FreeUnusedMem;
  608. begin
  609. if Win32Platform = VER_PLATFORM_WIN32_NT then SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
  610. end;
  611. function SetScreenResolution(Width, Height: integer): Longint;
  612. var
  613. DeviceMode: TDeviceMode;
  614. begin
  615. with DeviceMode do
  616. begin
  617. dmSize := SizeOf(TDeviceMode);
  618. dmPelsWidth := Width;
  619. dmPelsHeight := Height;
  620. dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
  621. end;
  622. Result := ChangeDisplaySettings(DeviceMode, CDS_UPDATEREGISTRY);
  623. end;
  624. {$ENDIF MSWINDOWS}
  625. function LastDayCurrentMonth: TDateTime;
  626. begin
  627. Result := EncodeDate(YearOf(Now),MonthOf(Now), DaysInMonth(Now));
  628. end;
  629. {$IFDEF FPC}
  630. function DateTimeInRange(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
  631. begin
  632. if aInclusive then
  633. Result := (AStartDateTime <= ADateTime) and (ADateTime <= AEndDateTime)
  634. else
  635. Result := (AStartDateTime < ADateTime) and (ADateTime < AEndDateTime);
  636. end;
  637. {$ENDIF}
  638. function IsSameDay(cBefore, cNow : TDateTime) : Boolean;
  639. begin
  640. //Test: Result := MinutesBetween(cBefore,cNow) < 1;
  641. Result := DateTimeInRange(cNow,StartOfTheDay(cBefore),EndOfTheDay(cBefore),True);
  642. end;
  643. function ChangeTimeOfADay(aDate : TDateTime; aHour, aMinute, aSecond : Word; aMilliSecond : Word = 0) : TDateTime;
  644. var
  645. y, m, d : Word;
  646. begin
  647. DecodeDate(aDate,y,m,d);
  648. Result := EncodeDateTime(y,m,d,aHour,aMinute,aSecond,aMilliSecond);
  649. end;
  650. function ChangeDateOfADay(aDate : TDateTime; aYear, aMonth, aDay : Word) : TDateTime;
  651. var
  652. h, m, s, ms : Word;
  653. begin
  654. DecodeTime(aDate,h,m,s,ms);
  655. Result := EncodeDateTime(aYear,aMonth,aDay,h,m,s,0);
  656. end;
  657. function FillStr(const C : Char; const Count : Integer) : string;
  658. var
  659. i : Integer;
  660. begin
  661. Result := '';
  662. for i := 1 to Count do Result := Result + C;
  663. end;
  664. function StrInArray(const aValue : string; const aInArray : array of string) : Boolean;
  665. var
  666. s : string;
  667. begin
  668. for s in aInArray do
  669. begin
  670. if s = aValue then Exit(True);
  671. end;
  672. Result := False;
  673. end;
  674. function IntInArray(const aValue : Integer; const aInArray : array of Integer) : Boolean;
  675. var
  676. i : Integer;
  677. begin
  678. for i in aInArray do
  679. begin
  680. if i = aValue then Exit(True);
  681. end;
  682. Result := False;
  683. end;
  684. function IsEmptyArray(aArray : TArray<string>) : Boolean;
  685. begin
  686. Result := Length(aArray) = 0;
  687. end;
  688. function IsEmptyArray(aArray : TArray<Integer>) : Boolean;
  689. begin
  690. Result := Length(aArray) = 0;
  691. end;
  692. function Zeroes(const Number, Len : Int64) : string;
  693. begin
  694. if Len > Length(IntToStr(Number)) then Result := FillStr('0',Len - Length(IntToStr(Number))) + IntToStr(Number)
  695. else Result := IntToStr(Number);
  696. end;
  697. function NumberToStr(const Number : Int64) : string;
  698. begin
  699. try
  700. Result := FormatFloat('0,',Number);
  701. except
  702. Result := '#Error';
  703. end;
  704. end;
  705. function Spaces(const Count : Integer) : string;
  706. begin
  707. Result := FillStr(' ',Count);
  708. end;
  709. function NowStr : string;
  710. begin
  711. Result := DateTimeToStr(Now());
  712. end;
  713. function NewGuidStr : string;
  714. var
  715. guid : TGUID;
  716. begin
  717. guid.NewGuid;
  718. Result := guid.ToString
  719. //GUIDToString(guid);
  720. end;
  721. function IsLike(cText, Pattern: string) : Boolean;
  722. var
  723. i, n : Integer;
  724. match : Boolean;
  725. wildcard : Boolean;
  726. CurrentPattern : Char;
  727. begin
  728. Result := False;
  729. wildcard := False;
  730. cText := LowerCase(cText);
  731. Pattern := LowerCase(Pattern);
  732. match := False;
  733. if (Pattern.Length > cText.Length) or (Pattern = '') then Exit;
  734. if Pattern = '*' then
  735. begin
  736. Result := True;
  737. Exit;
  738. end;
  739. for i := 1 to cText.Length do
  740. begin
  741. CurrentPattern := Pattern[i];
  742. if CurrentPattern = '*' then wildcard := True;
  743. if wildcard then
  744. begin
  745. n := Pos(Copy(Pattern,i+1,Pattern.Length),cText);
  746. if (n > i) or (Pattern.Length = i) then
  747. begin
  748. Result := True;
  749. Exit;
  750. end;
  751. end
  752. else
  753. begin
  754. if (cText[i] = CurrentPattern) or (CurrentPattern = '?') then match := True
  755. else match := False;
  756. end;
  757. end;
  758. Result := match;
  759. end;
  760. function Capitalize(s: string): string;
  761. begin
  762. Result := '';
  763. if s.Length = 0 then Exit;
  764. s := LowerCase(s,loUserLocale);
  765. Result := UpperCase(s[1],loUserLocale) + Trim(Copy(s, 2, s.Length));
  766. end;
  767. function CapitalizeWords(s: string): string;
  768. var
  769. cword : string;
  770. begin
  771. Result := '';
  772. if s.Length = 0 then Exit;
  773. s := LowerCase(s,loUserLocale);
  774. for cword in s.Split([' ']) do
  775. begin
  776. if Result = '' then Result := Capitalize(cword)
  777. else Result := Result + ' ' + Capitalize(cword);
  778. end;
  779. end;
  780. function GetLoggedUserName : string;
  781. {$IFDEF MSWINDOWS}
  782. const
  783. cnMaxUserNameLen = 254;
  784. var
  785. sUserName : string;
  786. dwUserNameLen : DWord;
  787. begin
  788. dwUserNameLen := cnMaxUserNameLen-1;
  789. SetLength( sUserName, cnMaxUserNameLen );
  790. GetUserName(PChar( sUserName ),dwUserNameLen );
  791. SetLength( sUserName, dwUserNameLen );
  792. Result := sUserName;
  793. end;
  794. {$ELSE}
  795. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  796. begin
  797. Result := GetEnvironmentVariable('USERNAME');
  798. end;
  799. {$ELSE}
  800. var
  801. {$IFNDEF NEXTGEN}
  802. plogin : PAnsiChar;
  803. {$ELSE}
  804. plogin : MarshaledAString;
  805. {$ENDIF}
  806. begin
  807. {$IFDEF POSIX}
  808. try
  809. plogin := getlogin;
  810. Result := Copy(plogin,1,Length(Trim(plogin)));
  811. except
  812. Result := 'N/A';
  813. end;
  814. {$ELSE}
  815. Result := 'N/A';
  816. {$ENDIF}
  817. //raise ENotImplemented.Create('Not Android GetLoggedUserName implemented!');
  818. end;
  819. {$ENDIF}
  820. {$ENDIF}
  821. {$IFDEF IOS}
  822. function GetDeviceModel : String;
  823. var
  824. size : size_t;
  825. buffer : array of Byte;
  826. begin
  827. sysctlbyname('hw.machine',nil,@size,nil,0);
  828. if size > 0 then
  829. begin
  830. SetLength(buffer, size);
  831. sysctlbyname('hw.machine',@buffer[0],@size,nil,0);
  832. Result := UTF8ToString(MarshaledAString(buffer));
  833. end
  834. else Result := EmptyStr;
  835. end;
  836. {$ENDIF}
  837. function GetComputerName : string;
  838. {$IFDEF MSWINDOWS}
  839. var
  840. dwLength: dword;
  841. begin
  842. dwLength := 253;
  843. SetLength(Result, dwLength+1);
  844. if not Windows.GetComputerName(pchar(result), dwLength) then Result := 'Not detected!';
  845. Result := pchar(result);
  846. end;
  847. {$ELSE}
  848. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  849. begin
  850. Result := GetEnvironmentVariable('COMPUTERNAME');
  851. end;
  852. {$ELSE} //Android gets model name
  853. {$IFDEF NEXTGEN}
  854. begin
  855. {$IFDEF ANDROID}
  856. Result := JStringToString(TJBuild.JavaClass.MODEL);
  857. {$ELSE} //IOS
  858. Result := GetDeviceModel;
  859. {$ENDIF}
  860. end;
  861. {$ELSE}
  862. {$IFDEF DELPHILINUX}
  863. var
  864. phost : PAnsiChar;
  865. begin
  866. try
  867. if gethostname(phost,_SC_HOST_NAME_MAX) = 0 then
  868. begin
  869. {$IFDEF DEBUG}
  870. Result := Copy(Trim(phost),1,Length(Trim(phost)));
  871. {$ELSE}
  872. Result := Copy(phost,1,Length(phost));
  873. {$ENDIF}
  874. end
  875. else Result := 'N/A.';
  876. except
  877. Result := 'N/A';
  878. end;
  879. end;
  880. {$ELSE} //OSX
  881. begin
  882. Result := NSStrToStr(TNSHost.Wrap(TNSHost.OCClass.currentHost).localizedName);
  883. end;
  884. {$ENDIF}
  885. {$ENDIF}
  886. {$ENDIF}
  887. {$ENDIF}
  888. function NormalizePathDelim(const cPath : string; const Delim : Char) : string;
  889. begin
  890. if Delim = '\' then Result := StringReplace(cPath,'/',Delim,[rfReplaceAll])
  891. else Result := StringReplace(cPath,'\',Delim,[rfReplaceAll]);
  892. end;
  893. function RemoveLastPathSegment(cDir : string) : string;
  894. var
  895. posi : Integer;
  896. delim : Char;
  897. EndsWithDelim : Boolean;
  898. begin
  899. if cDir.Contains('\') then delim := '\'
  900. else if cDir.Contains('/') then delim := '/'
  901. else
  902. begin
  903. Result := '';
  904. Exit;
  905. end;
  906. NormalizePathDelim(cDir,delim);
  907. if cDir.EndsWith(delim) then
  908. begin
  909. cDir := Copy(cDir,1,cDir.Length-1);
  910. EndsWithDelim := True;
  911. end
  912. else EndsWithDelim := False;
  913. if cDir.CountChar(delim) > 1 then posi := cDir.LastDelimiter(delim)
  914. else posi := Pos(delim,cDir)-1;
  915. if posi = cDir.Length then posi := 0;
  916. Result := Copy(cDir,1,posi);
  917. if (Result <> '') and (EndsWithDelim) then Result := Result + delim;
  918. end;
  919. function GetPathDelimiter(const aPath : string) : string;
  920. begin
  921. if aPath.Contains('/') then Result := '/'
  922. else if aPath.Contains('\') then Result := '\'
  923. else Result := '';
  924. end;
  925. function GetFirstPathSegment(const aPath : string) : string;
  926. var
  927. delimiter : string;
  928. spath : string;
  929. begin
  930. delimiter := GetPathDelimiter(aPath);
  931. if delimiter.IsEmpty then Exit(aPath);
  932. if aPath.StartsWith(delimiter) then spath := Copy(aPath,2,aPath.Length)
  933. else spath := aPath;
  934. Result := Copy(spath,0,spath.IndexOf(delimiter));
  935. end;
  936. function GetLastPathSegment(const aPath : string) : string;
  937. var
  938. delimiter : string;
  939. spath : string;
  940. begin
  941. delimiter := GetPathDelimiter(aPath);
  942. if delimiter.IsEmpty then Exit(aPath);
  943. if aPath.EndsWith(delimiter) then spath := Copy(aPath,0,aPath.Length - 1)
  944. else spath := aPath;
  945. Result := spath.Substring(spath.LastDelimiter(delimiter)+1);
  946. end;
  947. function ParamFindSwitch(const Switch : string) : Boolean;
  948. begin
  949. Result := FindCmdLineSwitch(Switch,['-', '/'],True);
  950. end;
  951. {$IFDEF FPC}
  952. function FindCmdLineSwitch(const Switch: string; var Value: string; IgnoreCase: Boolean = True;
  953. const SwitchTypes: TCmdLineSwitchTypes = [clstValueNextParam, clstValueAppended]): Boolean; overload;
  954. type
  955. TCompareProc = function(const S1, S2: string): Boolean;
  956. var
  957. Param: string;
  958. I, ValueOfs,
  959. SwitchLen, ParamLen: Integer;
  960. SameSwitch: TCompareProc;
  961. begin
  962. Result := False;
  963. Value := '';
  964. if IgnoreCase then
  965. SameSwitch := SameText else
  966. SameSwitch := SameStr;
  967. SwitchLen := Switch.Length;
  968. for I := 1 to ParamCount do
  969. begin
  970. Param := ParamStr(I);
  971. if CharInSet(Param.Chars[0], SwitchChars) and SameSwitch(Param.SubString(1,SwitchLen), Switch) then
  972. begin
  973. ParamLen := Param.Length;
  974. // Look for an appended value if the param is longer than the switch
  975. if (ParamLen > SwitchLen + 1) then
  976. begin
  977. // If not looking for appended value switches then this is not a matching switch
  978. if not (clstValueAppended in SwitchTypes) then
  979. Continue;
  980. ValueOfs := SwitchLen + 1;
  981. if Param.Chars[ValueOfs] = ':' then
  982. Inc(ValueOfs);
  983. Value := Param.SubString(ValueOfs, MaxInt);
  984. end
  985. // If the next param is not a switch, then treat it as the value
  986. else if (clstValueNextParam in SwitchTypes) and (I < ParamCount) and
  987. not CharInSet(ParamStr(I+1).Chars[0], SwitchChars) then
  988. Value := ParamStr(I+1);
  989. Result := True;
  990. Break;
  991. end;
  992. end;
  993. end;
  994. {$ENDIF}
  995. function ParamGetSwitch(const Switch : string; var cvalue : string) : Boolean;
  996. begin
  997. Result := FindCmdLineSwitch(Switch,cvalue,True,[clstValueAppended]);
  998. end;
  999. function GetAppName : string;
  1000. begin
  1001. Result := ExtractFilenameWithoutExt(ParamStr(0));
  1002. end;
  1003. function GetAppVersionStr: string;
  1004. {$IFDEF MSWINDOWS}
  1005. var
  1006. Rec: LongRec;
  1007. ver : Cardinal;
  1008. begin
  1009. ver := GetFileVersion(ParamStr(0));
  1010. if ver <> Cardinal(-1) then
  1011. begin
  1012. Rec := LongRec(ver);
  1013. Result := Format('%d.%d', [Rec.Hi, Rec.Lo]);
  1014. end
  1015. else Result := '';
  1016. end;
  1017. {$ELSE}
  1018. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  1019. var
  1020. version : TProgramVersion;
  1021. begin
  1022. if GetProgramVersion(version) then Result := Format('%d.%d', [version.Major, version.Minor])
  1023. else Result := '';
  1024. end;
  1025. {$ELSE}
  1026. {$IFDEF NEXTGEN}
  1027. {$IFDEF ANDROID}
  1028. var
  1029. PkgInfo : JPackageInfo;
  1030. begin
  1031. PkgInfo := SharedActivity.getPackageManager.getPackageInfo(SharedActivity.getPackageName,0);
  1032. Result := IntToStr(PkgInfo.VersionCode);
  1033. end;
  1034. {$ELSE} //IOS
  1035. var
  1036. AppKey: Pointer;
  1037. AppBundle: NSBundle;
  1038. BuildStr : NSString;
  1039. begin
  1040. try
  1041. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1042. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1043. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1044. Result := UTF8ToString(BuildStr.UTF8String);
  1045. except
  1046. Result := '';
  1047. end;
  1048. end;
  1049. {$ENDIF}
  1050. {$ELSE} //OSX
  1051. {$IFDEF OSX}
  1052. var
  1053. AppKey: Pointer;
  1054. AppBundle: NSBundle;
  1055. BuildStr : NSString;
  1056. begin
  1057. try
  1058. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1059. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1060. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1061. Result := UTF8ToString(BuildStr.UTF8String);
  1062. except
  1063. Result := '';
  1064. end;
  1065. end;
  1066. {$ELSE}
  1067. begin
  1068. Result := '';
  1069. end;
  1070. {$ENDIF}
  1071. {$ENDIF}
  1072. {$ENDIF}
  1073. {$ENDIF}
  1074. function GetAppVersionFullStr: string;
  1075. {$IFDEF MSWINDOWS}
  1076. var
  1077. Exe: string;
  1078. Size, Handle: DWORD;
  1079. Buffer: TBytes;
  1080. FixedPtr: PVSFixedFileInfo;
  1081. begin
  1082. Result := '';
  1083. Exe := ParamStr(0);
  1084. Size := GetFileVersionInfoSize(PChar(Exe), Handle);
  1085. if Size = 0 then
  1086. begin
  1087. //RaiseLastOSError;
  1088. //no version info in file
  1089. Exit;
  1090. end;
  1091. SetLength(Buffer, Size);
  1092. if not GetFileVersionInfo(PChar(Exe), Handle, Size, Buffer) then
  1093. RaiseLastOSError;
  1094. if not VerQueryValue(Buffer, '\', Pointer(FixedPtr), Size) then
  1095. RaiseLastOSError;
  1096. if (LongRec(FixedPtr.dwFileVersionLS).Hi = 0) and (LongRec(FixedPtr.dwFileVersionLS).Lo = 0) then
  1097. begin
  1098. Result := Format('%d.%d',
  1099. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1100. LongRec(FixedPtr.dwFileVersionMS).Lo]); //minor
  1101. end
  1102. else if (LongRec(FixedPtr.dwFileVersionLS).Lo = 0) then
  1103. begin
  1104. Result := Format('%d.%d.%d',
  1105. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1106. LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
  1107. LongRec(FixedPtr.dwFileVersionLS).Hi]); //release
  1108. end
  1109. else
  1110. begin
  1111. Result := Format('%d.%d.%d.%d',
  1112. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1113. LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
  1114. LongRec(FixedPtr.dwFileVersionLS).Hi, //release
  1115. LongRec(FixedPtr.dwFileVersionLS).Lo]); //build
  1116. end;
  1117. end;
  1118. {$ELSE}
  1119. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  1120. var
  1121. version : TProgramVersion;
  1122. begin
  1123. if GetProgramVersion(version) then Result := Format('%d.%d.%d.%d', [version.Major, version.Minor, version.Revision, version.Build])
  1124. else Result := '';
  1125. end;
  1126. {$ELSE}
  1127. {$IFDEF NEXTGEN}
  1128. {$IFDEF ANDROID}
  1129. var
  1130. PkgInfo : JPackageInfo;
  1131. begin
  1132. PkgInfo := SharedActivity.getPackageManager.getPackageInfo(SharedActivity.getPackageName,0);
  1133. Result := JStringToString(PkgInfo.versionName);
  1134. end;
  1135. {$ELSE} //IOS
  1136. var
  1137. AppKey: Pointer;
  1138. AppBundle: NSBundle;
  1139. BuildStr : NSString;
  1140. begin
  1141. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1142. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1143. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1144. Result := UTF8ToString(BuildStr.UTF8String);
  1145. end;
  1146. {$ENDIF}
  1147. {$ELSE}
  1148. {$IFDEF OSX}
  1149. var
  1150. AppKey: Pointer;
  1151. AppBundle: NSBundle;
  1152. BuildStr : NSString;
  1153. begin
  1154. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1155. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1156. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1157. Result := UTF8ToString(BuildStr.UTF8String);
  1158. end;
  1159. {$ELSE}
  1160. begin
  1161. Result := '';
  1162. end;
  1163. {$ENDIF}
  1164. {$ENDIF}
  1165. {$ENDIF}
  1166. {$ENDIF}
  1167. function UTCToLocalTime(GMTTime: TDateTime): TDateTime;
  1168. begin
  1169. {$IFDEF FPC}
  1170. Result := LocalTimeToUniversal(GMTTime);
  1171. {$ELSE}
  1172. Result := TTimeZone.Local.ToLocalTime(GMTTime);
  1173. {$ENDIF}
  1174. end;
  1175. function LocalTimeToUTC(LocalTime : TDateTime): TDateTime;
  1176. begin
  1177. {$IFDEF FPC}
  1178. Result := UniversalTimeToLocal(Localtime);
  1179. {$ELSE}
  1180. Result := TTimeZone.Local.ToUniversalTime(LocalTime);
  1181. {$ENDIF}
  1182. end;
  1183. function DateTimeToGMT(aDate : TDateTime) : string;
  1184. var
  1185. FmtSettings : TFormatSettings;
  1186. begin
  1187. FmtSettings.DateSeparator := '-';
  1188. FmtSettings.TimeSeparator := ':';
  1189. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ" GMT"';
  1190. Result := DateTimeToStr(aDate,FmtSettings).Trim;
  1191. end;
  1192. function GMTToDateTime(aDate : string) : TDateTime;
  1193. var
  1194. FmtSettings : TFormatSettings;
  1195. begin
  1196. FmtSettings.DateSeparator := '-';
  1197. FmtSettings.TimeSeparator := ':';
  1198. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ" GMT"';
  1199. Result := StrToDateTime(aDate,FmtSettings);
  1200. end;
  1201. function DateTimeToJsonDate(aDateTime : TDateTime) : string;
  1202. {$IFNDEF DELPHIXE7_UP}
  1203. var
  1204. FmtSettings : TFormatSettings;
  1205. {$ENDIF}
  1206. begin
  1207. {$IFDEF DELPHIXE7_UP}
  1208. Result := DateToISO8601(aDateTime);
  1209. {$ELSE}
  1210. FmtSettings.DateSeparator := '-';
  1211. FmtSettings.TimeSeparator := ':';
  1212. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ"Z"';
  1213. Result := DateTimeToStr(aDateTime,FmtSettings).Trim;
  1214. {$ENDIF}
  1215. end;
  1216. function JsonDateToDateTime(const aJsonDate : string) : TDateTime;
  1217. {$IFNDEF DELPHIXE7_UP}
  1218. var
  1219. FmtSettings : TFormatSettings;
  1220. {$ENDIF}
  1221. {$IFDEF FPC}
  1222. var
  1223. jdate : string;
  1224. {$ENDIF}
  1225. begin
  1226. {$IFDEF DELPHIXE7_UP}
  1227. Result := ISO8601ToDate(aJsonDate);
  1228. {$ELSE}
  1229. FmtSettings.DateSeparator := '-';
  1230. FmtSettings.TimeSeparator := ':';
  1231. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ"Z"';
  1232. {$IFDEF FPC}
  1233. jdate := StringReplace(aJsondate,'T',' ',[rfIgnoreCase]);
  1234. jdate := Copy(jdate,1,Pos('.',jdate)-1);
  1235. Result := StrToDateTime(jdate,FmtSettings);
  1236. {$ELSE}
  1237. Result := StrToDateTime(aJsonDate,FmtSettings);
  1238. {$ENDIF}
  1239. {$ENDIF}
  1240. end;
  1241. function CountDigits(anInt: Cardinal): Cardinal; inline;
  1242. var
  1243. cmp: Cardinal;
  1244. begin
  1245. cmp := 10;
  1246. Result := 1;
  1247. while (Result < 10) and (cmp <= anInt) do
  1248. begin
  1249. cmp := cmp*10;
  1250. Inc(Result);
  1251. end;
  1252. end;
  1253. function CountStr(const aFindStr, aSourceStr : string) : Integer;
  1254. var
  1255. i : Integer;
  1256. found : Integer;
  1257. findstr : string;
  1258. mainstr : string;
  1259. begin
  1260. findstr := aFindStr.ToLower;
  1261. mainstr := aSourceStr.ToLower;
  1262. Result := 0;
  1263. i := 0;
  1264. while i < mainstr.Length do
  1265. begin
  1266. found := Pos(findstr,mainstr,i);
  1267. if found > 0 then
  1268. begin
  1269. i := found;
  1270. Inc(Result);
  1271. end
  1272. else Break;
  1273. end;
  1274. end;
  1275. procedure SaveStreamToFile(aStream : TStream; const aFileName : string);
  1276. var
  1277. fs : TFileStream;
  1278. begin
  1279. fs := TFileStream.Create(aFileName,fmCreate);
  1280. try
  1281. aStream.Seek(0,soBeginning);
  1282. fs.CopyFrom(aStream,aStream.Size);
  1283. finally
  1284. fs.Free;
  1285. end;
  1286. end;
  1287. function StreamToString(aStream : TStream) : string;
  1288. var
  1289. ss : TStringStream;
  1290. begin
  1291. aStream.Position := 0;
  1292. if aStream = nil then Exit;
  1293. if aStream is TMemoryStream then
  1294. begin
  1295. SetString(Result, PChar(TMemoryStream(aStream).Memory), TMemoryStream(aStream).Size div SizeOf(Char));
  1296. end
  1297. else if aStream is TStringStream then
  1298. begin
  1299. Result := TStringStream(aStream).DataString;
  1300. end
  1301. else
  1302. begin
  1303. ss := TStringStream.Create;
  1304. try
  1305. aStream.Seek(0,soBeginning);
  1306. ss.CopyFrom(aStream,aStream.Size);
  1307. Result := ss.DataString;
  1308. finally
  1309. ss.Free;
  1310. end;
  1311. end;
  1312. end;
  1313. function StreamToString2(const aStream: TStream; const aEncoding: TEncoding): string;
  1314. var
  1315. sbytes: TBytes;
  1316. begin
  1317. aStream.Position := 0;
  1318. SetLength(sbytes, aStream.Size);
  1319. aStream.ReadBuffer(sbytes,aStream.Size);
  1320. Result := aEncoding.GetString(sbytes);
  1321. end;
  1322. procedure StringToStream(const aStr : string; aStream : TStream);
  1323. begin
  1324. aStream.Seek(0,soBeginning);
  1325. aStream.WriteBuffer(Pointer(aStr)^,aStr.Length * SizeOf(Char));
  1326. end;
  1327. procedure StringToStream2(const aStr : string; aStream : TStream);
  1328. var
  1329. stream : TStringStream;
  1330. begin
  1331. stream := TStringStream.Create(aStr,TEncoding.UTF8);
  1332. try
  1333. aStream.CopyFrom(stream,stream.Size);
  1334. finally
  1335. stream.Free;
  1336. end;
  1337. end;
  1338. function CommaText(aList : TStringList) : string;
  1339. var
  1340. value : string;
  1341. sb : TStringBuilder;
  1342. begin
  1343. if aList.Text = '' then Exit;
  1344. sb := TStringBuilder.Create;
  1345. try
  1346. for value in aList do
  1347. begin
  1348. sb.Append(value);
  1349. sb.Append(',');
  1350. end;
  1351. if sb.Length > 1 then Result := sb.ToString(0, sb.Length - 1);
  1352. finally
  1353. sb.Free;
  1354. end;
  1355. end;
  1356. function CommaText(aArray : TArray<string>) : string;
  1357. var
  1358. value : string;
  1359. sb : TStringBuilder;
  1360. begin
  1361. if High(aArray) < 0 then Exit;
  1362. sb := TStringBuilder.Create;
  1363. try
  1364. for value in aArray do
  1365. begin
  1366. sb.Append(value);
  1367. sb.Append(',');
  1368. end;
  1369. if sb.Length > 1 then Result := sb.ToString(0, sb.Length - 1);
  1370. finally
  1371. sb.Free;
  1372. end;
  1373. end;
  1374. function ArrayToString(aArray : TArray<string>) : string;
  1375. var
  1376. value : string;
  1377. sb : TStringBuilder;
  1378. begin
  1379. if High(aArray) < 0 then Exit;
  1380. sb := TStringBuilder.Create;
  1381. try
  1382. for value in aArray do
  1383. begin
  1384. sb.Append(value);
  1385. sb.Append(#10#13);
  1386. end;
  1387. finally
  1388. sb.Free;
  1389. end;
  1390. end;
  1391. function StringsToArray(aStrings : TStrings) : TArray<string>;
  1392. var
  1393. i : Integer;
  1394. begin
  1395. if aStrings.Count = 0 then Exit;
  1396. SetLength(Result,aStrings.Count);
  1397. for i := 0 to aStrings.Count - 1 do
  1398. begin
  1399. Result[i] := aStrings[i];
  1400. end;
  1401. end;
  1402. { TCounter }
  1403. procedure TCounter.Init(aMaxValue : Integer);
  1404. begin
  1405. fMaxValue := aMaxValue;
  1406. fCurrentValue := 0;
  1407. end;
  1408. function TCounter.Count : Integer;
  1409. begin
  1410. Result := fCurrentValue;
  1411. end;
  1412. function TCounter.CountIs(aValue : Integer) : Boolean;
  1413. begin
  1414. Result := fCurrentValue = aValue;
  1415. end;
  1416. function TCounter.Check : Boolean;
  1417. begin
  1418. if fCurrentValue = fMaxValue then
  1419. begin
  1420. Result := True;
  1421. Reset;
  1422. end
  1423. else
  1424. begin
  1425. Result := False;
  1426. Inc(fCurrentValue);
  1427. end;
  1428. end;
  1429. procedure TCounter.Reset;
  1430. begin
  1431. fCurrentValue := fMaxValue;
  1432. end;
  1433. { TimeCounter }
  1434. procedure TTimeCounter.Init(MillisecondsToReach : Integer);
  1435. begin
  1436. fDoneEvery := MillisecondsToReach;
  1437. end;
  1438. function TTimeCounter.Check : Boolean;
  1439. begin
  1440. if MilliSecondsBetween(fCurrentTime,Now) > fDoneEvery then
  1441. begin
  1442. fCurrentTime := Now();
  1443. Result := True;
  1444. end
  1445. else Result := False;
  1446. end;
  1447. procedure TTimeCounter.Reset;
  1448. begin
  1449. fCurrentTime := Now();
  1450. end;
  1451. { TArrayOfStringHelper}
  1452. {$IFNDEF FPC}
  1453. function TArrayOfStringHelper.Any : Boolean;
  1454. begin
  1455. Result := High(Self) >= 0;
  1456. end;
  1457. function TArrayOfStringHelper.Any(const aValue : string) : Boolean;
  1458. begin
  1459. Result := Exists(aValue);
  1460. end;
  1461. function TArrayOfStringHelper.Add(const aValue : string) : Integer;
  1462. begin
  1463. SetLength(Self,Length(Self)+1);
  1464. Self[High(Self)] := aValue;
  1465. Result := High(Self);
  1466. end;
  1467. function TArrayOfStringHelper.AddIfNotExists(const aValue : string; aCaseSense : Boolean = False) : Integer;
  1468. var
  1469. i : Integer;
  1470. begin
  1471. for i := Low(Self) to High(Self) do
  1472. begin
  1473. if aCaseSense then
  1474. begin
  1475. if Self[i] = aValue then Exit(i);
  1476. end
  1477. else
  1478. begin
  1479. if CompareText(Self[i],aValue) = 0 then Exit(i)
  1480. end;
  1481. end;
  1482. //if not exists add it
  1483. Result := Self.Add(aValue);
  1484. end;
  1485. function TArrayOfStringHelper.Remove(const aValue : string) : Boolean;
  1486. var
  1487. i : Integer;
  1488. begin
  1489. for i := Low(Self) to High(Self) do
  1490. begin
  1491. if CompareText(Self[i],aValue) = 0 then
  1492. begin
  1493. {$IFDEF DELPHIXE7_UP}
  1494. System.Delete(Self,i,1);
  1495. {$ELSE}
  1496. TArrayUtil<string>.Delete(Self,i);
  1497. {$ENDIF}
  1498. Exit(True);
  1499. end;
  1500. end;
  1501. Result := False;
  1502. end;
  1503. function TArrayOfStringHelper.Exists(const aValue : string) : Boolean;
  1504. var
  1505. value : string;
  1506. begin
  1507. Result := False;
  1508. for value in Self do
  1509. begin
  1510. if CompareText(value,aValue) = 0 then Exit(True)
  1511. end;
  1512. end;
  1513. function TArrayOfStringHelper.Count : Integer;
  1514. begin
  1515. Result := High(Self) + 1;
  1516. end;
  1517. {$ENDIF}
  1518. { TPairItem }
  1519. constructor TPairItem.Create(const aName, aValue: string);
  1520. begin
  1521. Name := aName;
  1522. Value := aValue;
  1523. end;
  1524. { TPairList }
  1525. function TPairList.GetEnumerator : TPairEnumerator;
  1526. begin
  1527. Result := TPairEnumerator.Create(fItems);
  1528. end;
  1529. function TPairList.Add(aPair: TPairItem): Integer;
  1530. begin
  1531. SetLength(fItems,Length(fItems)+1);
  1532. fItems[High(fItems)] := aPair;
  1533. Result := High(fItems);
  1534. end;
  1535. function TPairList.Add(const aName, aValue: string): Integer;
  1536. begin
  1537. SetLength(fItems,Length(fItems)+1);
  1538. fItems[High(fItems)].Name := aName;
  1539. fItems[High(fItems)].Value := aValue;
  1540. Result := High(fItems);
  1541. end;
  1542. procedure TPairList.AddOrUpdate(const aName, aValue: string);
  1543. var
  1544. i : Integer;
  1545. begin
  1546. for i := Low(fItems) to High(fItems) do
  1547. begin
  1548. if CompareText(fItems[i].Name,aName) = 0 then
  1549. begin
  1550. fItems[i].Value := aValue;
  1551. Exit;
  1552. end;
  1553. end;
  1554. //if not exists add it
  1555. Self.Add(aName,aValue);
  1556. end;
  1557. function TPairList.Count: Integer;
  1558. begin
  1559. Result := High(fItems) + 1;
  1560. end;
  1561. function TPairList.Exists(const aName: string): Boolean;
  1562. var
  1563. i : Integer;
  1564. begin
  1565. Result := False;
  1566. for i := Low(fItems) to High(fItems) do
  1567. begin
  1568. if CompareText(fItems[i].Name,aName) = 0 then Exit(True)
  1569. end;
  1570. end;
  1571. function TPairList.GetPair(const aName: string): TPairItem;
  1572. var
  1573. i : Integer;
  1574. begin
  1575. for i := Low(fItems) to High(fItems) do
  1576. begin
  1577. if CompareText(fItems[i].Name,aName) = 0 then Exit(fItems[i]);
  1578. end;
  1579. end;
  1580. function TPairList.GetValue(const aName: string): string;
  1581. var
  1582. i : Integer;
  1583. begin
  1584. Result := '';
  1585. for i := Low(fItems) to High(fItems) do
  1586. begin
  1587. if CompareText(fItems[i].Name,aName) = 0 then Exit(fItems[i].Value);
  1588. end;
  1589. end;
  1590. function TPairList.Remove(const aName: string): Boolean;
  1591. var
  1592. i : Integer;
  1593. begin
  1594. for i := Low(fItems) to High(fItems) do
  1595. begin
  1596. if CompareText(fItems[i].Name,aName) = 0 then
  1597. begin
  1598. {$IF Defined(DELPHIXE7_UP) OR Defined(FPC)}
  1599. System.Delete(fItems,i,1);
  1600. {$ELSE}
  1601. TArrayUtil<TPairItem>.Delete(fItems,i);
  1602. {$ENDIF}
  1603. Exit(True);
  1604. end;
  1605. end;
  1606. Result := False;
  1607. end;
  1608. function TPairList.ToArray : TArray<TPairItem>;
  1609. begin
  1610. Result := fItems;
  1611. end;
  1612. procedure TPairList.FromArray(aValue : TArray<TPairItem>);
  1613. begin
  1614. fItems := aValue;
  1615. end;
  1616. procedure TPairList.Clear;
  1617. begin
  1618. SetLength(fItems,0);
  1619. end;
  1620. { TPairList.TPairEnumerator}
  1621. constructor TPairList.TPairEnumerator.Create(var aArray: TArray<TPairItem>);
  1622. begin
  1623. fIndex := -1;
  1624. fArray := @aArray;
  1625. end;
  1626. function TPairList.TPairEnumerator.GetCurrent : TPairItem;
  1627. begin
  1628. Result := TArray<TPairItem>(fArray^)[fIndex];
  1629. end;
  1630. function TPairList.TPairEnumerator.MoveNext: Boolean;
  1631. begin
  1632. Inc(fIndex);
  1633. Result := fIndex < High(TArray<TPairItem>(fArray^))+1;
  1634. end;
  1635. {$IFDEF MSWINDOWS}
  1636. procedure ProcessMessages;
  1637. var
  1638. Msg: TMsg;
  1639. begin
  1640. while integer(PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) <> 0 do
  1641. begin
  1642. TranslateMessage(Msg);
  1643. DispatchMessage(Msg);
  1644. end;
  1645. end;
  1646. function GetLastOSError: String;
  1647. begin
  1648. Result := SysErrorMessage(Windows.GetLastError);
  1649. end;
  1650. {$ENDIF}
  1651. function RemoveLastChar(const aText : string) : string;
  1652. begin
  1653. Result := aText.Remove(aText.Length - 1);
  1654. end;
  1655. function DateTimeToSQL(aDateTime : TDateTime) : string;
  1656. begin
  1657. Result := FormatDateTime('YYYYMMDD hh:mm:ss',aDateTime);
  1658. end;
  1659. function IsInteger(const aValue : string) : Boolean;
  1660. var
  1661. i : Integer;
  1662. begin
  1663. Result := TryStrToInt(aValue,i);
  1664. end;
  1665. function ExtractStr(var vSource : string; aIndex : Integer; aCount : Integer) : string;
  1666. begin
  1667. if aIndex > vSource.Length then Exit('');
  1668. Result := Copy(vSource,aIndex,aCount);
  1669. Delete(vSource,aIndex,aCount);
  1670. end;
  1671. function GetSubString(const aSource, aFirstDelimiter, aLastDelimiter : string) : string;
  1672. var
  1673. i : Integer;
  1674. begin
  1675. i := Pos(aFirstDelimiter,aSource);
  1676. if i > -1 then Result := Copy(aSource, i + aFirstDelimiter.Length, Pos(aLastDelimiter, aSource, i + aFirstDelimiter.Length) - i - aFirstDelimiter.Length)
  1677. else Result := '';
  1678. end;
  1679. function DbQuotedStr(const str : string): string;
  1680. var
  1681. i : Integer;
  1682. begin
  1683. Result := str;
  1684. for i := Result.Length - 1 downto 0 do
  1685. begin
  1686. if Result.Chars[i] = '"' then Result := Result.Insert(i, '"');
  1687. end;
  1688. Result := '"' + Result + '"';
  1689. end;
  1690. function UnDbQuotedStr(const str: string) : string;
  1691. begin
  1692. Result := Trim(str);
  1693. if not Result.IsEmpty then
  1694. begin
  1695. if Result.StartsWith('"') then Result := Copy(Result, 2, Result.Length - 2);
  1696. end;
  1697. end;
  1698. function SpQuotedStr(const str : string): string;
  1699. begin
  1700. Result := '''' + str + '''';
  1701. end;
  1702. function UnSpQuotedStr(const str: string) : string;
  1703. begin
  1704. Result := Trim(str);
  1705. if not Result.IsEmpty then
  1706. begin
  1707. if Result.StartsWith('''') then Result := Copy(Result, 2, Result.Length - 2);
  1708. end;
  1709. end;
  1710. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : string) : string;
  1711. begin
  1712. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1713. end;
  1714. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Integer) : Integer;
  1715. begin
  1716. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1717. end;
  1718. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Extended) : Extended;
  1719. begin
  1720. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1721. end;
  1722. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : TObject) : TObject;
  1723. begin
  1724. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1725. end;
  1726. {$IFNDEF FPC}
  1727. {$IFNDEF DELPHIXE7_UP}
  1728. class procedure TArrayUtil<T>.Delete(var aArray : TArray<T>; aIndex : Integer);
  1729. var
  1730. n : Integer;
  1731. len : Integer;
  1732. begin
  1733. len := Length(aArray);
  1734. if (len > 0) and (aIndex < len) then
  1735. begin
  1736. for n := aIndex + 1 to len - 1 do aArray[n - 1] := aArray[n];
  1737. SetLength(aArray, len - 1);
  1738. end;
  1739. end;
  1740. {$ENDIF}
  1741. {$ENDIF}
  1742. {$IFNDEF NEXTGEN}
  1743. initialization
  1744. try
  1745. GetEnvironmentPaths;
  1746. except
  1747. {$IFDEF SHOW_ENVIRONMENTPATH_ERRORS}
  1748. on E : Exception do
  1749. begin
  1750. if not IsService then
  1751. begin
  1752. if HasConsoleOutput then Writeln(Format('[WARN] GetEnvironmentPaths: %s',[E.Message]))
  1753. else MessageBox(0,PWideChar(Format('Get environment path error: %s',[E.Message])),'GetEnvironmentPaths',MB_ICONEXCLAMATION);
  1754. end;
  1755. end;
  1756. {$ENDIF}
  1757. end;
  1758. {$ENDIF}
  1759. end.