Quick.Commons.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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 : 22/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. function IsFloat(const aValue : string) : Boolean;
  341. function IsBoolean(const aValue : string) : Boolean;
  342. //extract a substring and deletes from source string
  343. function ExtractStr(var vSource : string; aIndex : Integer; aCount : Integer) : string;
  344. //get first string between string delimiters
  345. function GetSubString(const aSource, aFirstDelimiter, aLastDelimiter : string) : string;
  346. //get double quoted or dequoted string
  347. function DbQuotedStr(const str : string): string;
  348. function UnDbQuotedStr(const str: string) : string;
  349. //get simple quoted or dequoted string
  350. function SpQuotedStr(const str : string): string;
  351. function UnSpQuotedStr(const str : string): string;
  352. //ternary operator
  353. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : string) : string; overload;
  354. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Integer) : Integer; overload;
  355. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Extended) : Extended; overload;
  356. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : TObject) : TObject; overload;
  357. var
  358. path : TEnvironmentPath;
  359. implementation
  360. {TFileHelper}
  361. {$IFNDEF FPC}
  362. {$IFDEF MSWINDOWS}
  363. class function TFileHelper.IsInUse(const FileName : string) : Boolean;
  364. var
  365. HFileRes: HFILE;
  366. begin
  367. Result := False;
  368. if not FileExists(FileName) then Exit;
  369. try
  370. HFileRes := CreateFile(PChar(FileName)
  371. ,GENERIC_READ or GENERIC_WRITE
  372. ,0
  373. ,nil
  374. ,OPEN_EXISTING
  375. ,FILE_ATTRIBUTE_NORMAL
  376. ,0);
  377. Result := (HFileRes = INVALID_HANDLE_VALUE);
  378. if not(Result) then begin
  379. CloseHandle(HFileRes);
  380. end;
  381. except
  382. Result := True;
  383. end;
  384. end;
  385. {$ENDIF}
  386. {$IFDEF DELPHILINUX}
  387. class function TFileHelper.IsInUse(const FileName : string) : Boolean;
  388. var
  389. fs : TFileStream;
  390. begin
  391. try
  392. fs := TFileStream.Create(FileName, fmOpenReadWrite, fmShareExclusive);
  393. Result := True;
  394. fs.Free;
  395. except
  396. Result := False;
  397. end;
  398. end;
  399. {$ENDIF}
  400. {$IFDEF MSWINDOWS}
  401. class function TFileHelper.GetSize(const FileName: String): Int64;
  402. var
  403. info: TWin32FileAttributeData;
  404. begin
  405. Result := -1;
  406. if not GetFileAttributesEx(PWideChar(FileName), GetFileExInfoStandard, @info) then Exit;
  407. Result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
  408. end;
  409. {$ELSE}
  410. class function TFileHelper.GetSize(const FileName: String): Int64;
  411. var
  412. sr : TSearchRec;
  413. begin
  414. if FindFirst(fileName, faAnyFile, sr ) = 0 then Result := sr.Size
  415. else Result := -1;
  416. end;
  417. {$ENDIF}
  418. {TDirectoryHelper}
  419. class function TDirectoryHelper.GetSize(const Path: String): Int64;
  420. var
  421. filename : string;
  422. begin
  423. Result := -1;
  424. for filename in TDirectory.GetFiles(Path) do
  425. begin
  426. Result := Result + TFile.GetSize(filename);
  427. end;
  428. end;
  429. {$ENDIF}
  430. {other functions}
  431. function RandomPassword(const PasswordLength : Integer; Complexity : TPasswordComplexity = [pfIncludeNumbers,pfIncludeSigns]) : string;
  432. const
  433. PassAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  434. PassSigns = '@!&$';
  435. PassNumbers = '1234567890';
  436. var
  437. MinNumbers,
  438. MinSigns : Integer;
  439. NumNumbers,
  440. NumSigns : Integer;
  441. begin
  442. Result := '';
  443. Randomize;
  444. //fill all alfa
  445. repeat
  446. Result := Result + PassAlpha[Random(Length(PassAlpha))+1];
  447. until (Length(Result) = PasswordLength);
  448. //checks if need include numbers
  449. if pfIncludeNumbers in Complexity then
  450. begin
  451. MinNumbers := Round(PasswordLength / 10 * 2);
  452. NumNumbers := 0;
  453. if MinNumbers = 0 then MinNumbers := 1;
  454. repeat
  455. Result[Random(PasswordLength)+1] := PassNumbers[Random(Length(PassNumbers))+1];
  456. Inc(NumNumbers);
  457. until NumNumbers = MinNumbers;
  458. end;
  459. //checks if need include signs
  460. if pfIncludeNumbers in Complexity then
  461. begin
  462. MinSigns := Round(PasswordLength / 10 * 1);
  463. NumSigns := 0;
  464. if MinSigns = 0 then MinSigns := 1;
  465. repeat
  466. Result[Random(PasswordLength)+1] := PassSigns[Random(Length(PassSigns))+1];
  467. Inc(NumSigns);
  468. until NumSigns = MinSigns;
  469. end;
  470. end;
  471. function RandomString(const aLength: Integer) : string;
  472. const
  473. chars : string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  474. var
  475. i : Integer;
  476. clong : Integer;
  477. begin
  478. clong := High(chars);
  479. SetLength(Result, aLength);
  480. for i := 1 to aLength do
  481. begin
  482. Result[i] := chars[Random(clong) + 1];
  483. end;
  484. end;
  485. function ExtractFileNameWithoutExt(const FileName: string): string;
  486. begin
  487. Result := TPath.GetFileNameWithoutExtension(FileName);
  488. end;
  489. function UnixToWindowsPath(const UnixPath: string): string;
  490. begin
  491. Result := StringReplace(UnixPath, '/', '\',[rfReplaceAll, rfIgnoreCase]);
  492. end;
  493. function WindowsToUnixPath(const WindowsPath: string): string;
  494. begin
  495. Result := StringReplace(WindowsPath, '\', '/',[rfReplaceAll, rfIgnoreCase]);
  496. end;
  497. function CorrectURLPath(cUrl : string) : string;
  498. var
  499. nurl : string;
  500. begin
  501. nurl := WindowsToUnixPath(cUrl);
  502. nurl := StringReplace(nurl,'//','/',[rfReplaceAll]);
  503. Result := StringReplace(nurl,' ','%20',[rfReplaceAll]);
  504. //TNetEncoding.Url.Encode()
  505. end;
  506. procedure GetEnvironmentPaths;
  507. begin
  508. //gets path
  509. path.EXEPATH := TPath.GetDirectoryName(ParamStr(0));
  510. {$IFDEF MSWINDOWS}
  511. path.WINDOWS := SysUtils.GetEnvironmentVariable('windir');
  512. path.PROGRAMFILES := SysUtils.GetEnvironmentVariable('ProgramFiles');
  513. path.COMMONFILES := SysUtils.GetEnvironmentVariable('CommonProgramFiles(x86)');
  514. path.HOMEDRIVE := SysUtils.GetEnvironmentVariable('SystemDrive');
  515. path.USERPROFILE := SysUtils.GetEnvironmentVariable('USERPROFILE');
  516. path.PROGRAMDATA := SysUtils.GetEnvironmentVariable('ProgramData');
  517. path.ALLUSERSPROFILE := SysUtils.GetEnvironmentVariable('AllUsersProfile');
  518. path.INSTDRIVE := path.HOMEDRIVE;
  519. path.TEMP := SysUtils.GetEnvironmentVariable('TEMP');
  520. //these paths fail if user is SYSTEM
  521. try
  522. path.SYSTEM := GetSpecialFolderPath(CSIDL_SYSTEM);
  523. path.APPDATA := GetSpecialFolderPath(CSIDL_APPDATA);
  524. path.DESKTOP := GetSpecialFolderPath(CSIDL_DESKTOP);
  525. path.DESKTOP_ALLUSERS := GetSpecialFolderPath(CSIDL_COMMON_DESKTOPDIRECTORY);
  526. path.STARTMENU:=GetSpecialFolderPath(CSIDL_PROGRAMS);
  527. path.STARTMENU_ALLUSERS:=GetSpecialFolderPath(CSIDL_COMMON_PROGRAMS);
  528. path.STARTMENU_ALLUSERS := path.STARTMENU;
  529. path.STARTUP:=GetSpecialFolderPath(CSIDL_STARTUP);
  530. except
  531. //
  532. end;
  533. {$ENDIF}
  534. end;
  535. {$IFDEF MSWINDOWS}
  536. function GetSpecialFolderPath(folderID : Integer) : string;
  537. var
  538. ppidl: PItemIdList;
  539. begin
  540. SHGetSpecialFolderLocation(0, folderID, ppidl);
  541. SetLength(Result, MAX_PATH);
  542. if not SHGetPathFromIDList(ppidl,{$IFDEF FPC}PAnsiChar(Result){$ELSE}PChar(Result){$ENDIF}) then
  543. begin
  544. raise EShellError.create(Format('GetSpecialFolderPath: Invalid PIPL (%d)',[folderID]));
  545. end;
  546. SetLength(Result, lStrLen({$IFDEF FPC}PAnsiChar(Result){$ELSE}PChar(Result){$ENDIF}));
  547. end;
  548. function Is64bitOS : Boolean;
  549. begin
  550. {$IFDEF WIN64}
  551. Result := True;
  552. {$ELSE}
  553. Result := False;
  554. {$ENDIF WIN64}
  555. end;
  556. function IsConsole: Boolean;
  557. begin
  558. {$IFDEF CONSOLE}
  559. Result := True;
  560. {$ELSE}
  561. Result := False;
  562. {$ENDIF CONSOLE}
  563. end;
  564. {$ENDIF}
  565. function HasConsoleOutput : Boolean;
  566. {$IFDEF MSWINDOWS}
  567. var
  568. stout : THandle;
  569. begin
  570. try
  571. stout := GetStdHandle(Std_Output_Handle);
  572. Win32Check(stout <> Invalid_Handle_Value);
  573. Result := stout <> 0;
  574. except
  575. Result := False;
  576. end;
  577. end;
  578. {$ELSE}
  579. begin
  580. Result := IsConsole;
  581. end;
  582. {$ENDIF}
  583. function IsDebug: Boolean;
  584. begin
  585. {$IFDEF DEBUG}
  586. Result := True;
  587. {$ELSE}
  588. Result := False;
  589. {$ENDIF DEBUG}
  590. end;
  591. {$IFDEF MSWINDOWS}
  592. function IsService : Boolean;
  593. begin
  594. //only working with my Quick.AppService unit
  595. try
  596. Result := (IsConsole) and (not HasConsoleOutput);
  597. except
  598. Result := False;
  599. end;
  600. end;
  601. function SecondsIdle: DWord;
  602. var
  603. liInfo: TLastInputInfo;
  604. begin
  605. liInfo.cbSize := SizeOf(TLastInputInfo) ;
  606. GetLastInputInfo(liInfo) ;
  607. Result := (GetTickCount - liInfo.dwTime) DIV 1000;
  608. end;
  609. procedure FreeUnusedMem;
  610. begin
  611. if Win32Platform = VER_PLATFORM_WIN32_NT then SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
  612. end;
  613. function SetScreenResolution(Width, Height: integer): Longint;
  614. var
  615. DeviceMode: TDeviceMode;
  616. begin
  617. with DeviceMode do
  618. begin
  619. dmSize := SizeOf(TDeviceMode);
  620. dmPelsWidth := Width;
  621. dmPelsHeight := Height;
  622. dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
  623. end;
  624. Result := ChangeDisplaySettings(DeviceMode, CDS_UPDATEREGISTRY);
  625. end;
  626. {$ENDIF MSWINDOWS}
  627. function LastDayCurrentMonth: TDateTime;
  628. begin
  629. Result := EncodeDate(YearOf(Now),MonthOf(Now), DaysInMonth(Now));
  630. end;
  631. {$IFDEF FPC}
  632. function DateTimeInRange(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
  633. begin
  634. if aInclusive then
  635. Result := (AStartDateTime <= ADateTime) and (ADateTime <= AEndDateTime)
  636. else
  637. Result := (AStartDateTime < ADateTime) and (ADateTime < AEndDateTime);
  638. end;
  639. {$ENDIF}
  640. function IsSameDay(cBefore, cNow : TDateTime) : Boolean;
  641. begin
  642. //Test: Result := MinutesBetween(cBefore,cNow) < 1;
  643. Result := DateTimeInRange(cNow,StartOfTheDay(cBefore),EndOfTheDay(cBefore),True);
  644. end;
  645. function ChangeTimeOfADay(aDate : TDateTime; aHour, aMinute, aSecond : Word; aMilliSecond : Word = 0) : TDateTime;
  646. var
  647. y, m, d : Word;
  648. begin
  649. DecodeDate(aDate,y,m,d);
  650. Result := EncodeDateTime(y,m,d,aHour,aMinute,aSecond,aMilliSecond);
  651. end;
  652. function ChangeDateOfADay(aDate : TDateTime; aYear, aMonth, aDay : Word) : TDateTime;
  653. var
  654. h, m, s, ms : Word;
  655. begin
  656. DecodeTime(aDate,h,m,s,ms);
  657. Result := EncodeDateTime(aYear,aMonth,aDay,h,m,s,0);
  658. end;
  659. function FillStr(const C : Char; const Count : Integer) : string;
  660. var
  661. i : Integer;
  662. begin
  663. Result := '';
  664. for i := 1 to Count do Result := Result + C;
  665. end;
  666. function StrInArray(const aValue : string; const aInArray : array of string) : Boolean;
  667. var
  668. s : string;
  669. begin
  670. for s in aInArray do
  671. begin
  672. if s = aValue then Exit(True);
  673. end;
  674. Result := False;
  675. end;
  676. function IntInArray(const aValue : Integer; const aInArray : array of Integer) : Boolean;
  677. var
  678. i : Integer;
  679. begin
  680. for i in aInArray do
  681. begin
  682. if i = aValue then Exit(True);
  683. end;
  684. Result := False;
  685. end;
  686. function IsEmptyArray(aArray : TArray<string>) : Boolean;
  687. begin
  688. Result := Length(aArray) = 0;
  689. end;
  690. function IsEmptyArray(aArray : TArray<Integer>) : Boolean;
  691. begin
  692. Result := Length(aArray) = 0;
  693. end;
  694. function Zeroes(const Number, Len : Int64) : string;
  695. begin
  696. if Len > Length(IntToStr(Number)) then Result := FillStr('0',Len - Length(IntToStr(Number))) + IntToStr(Number)
  697. else Result := IntToStr(Number);
  698. end;
  699. function NumberToStr(const Number : Int64) : string;
  700. begin
  701. try
  702. Result := FormatFloat('0,',Number);
  703. except
  704. Result := '#Error';
  705. end;
  706. end;
  707. function Spaces(const Count : Integer) : string;
  708. begin
  709. Result := FillStr(' ',Count);
  710. end;
  711. function NowStr : string;
  712. begin
  713. Result := DateTimeToStr(Now());
  714. end;
  715. function NewGuidStr : string;
  716. var
  717. guid : TGUID;
  718. begin
  719. guid.NewGuid;
  720. Result := guid.ToString
  721. //GUIDToString(guid);
  722. end;
  723. function IsLike(cText, Pattern: string) : Boolean;
  724. var
  725. i, n : Integer;
  726. match : Boolean;
  727. wildcard : Boolean;
  728. CurrentPattern : Char;
  729. begin
  730. Result := False;
  731. wildcard := False;
  732. cText := LowerCase(cText);
  733. Pattern := LowerCase(Pattern);
  734. match := False;
  735. if (Pattern.Length > cText.Length) or (Pattern = '') then Exit;
  736. if Pattern = '*' then
  737. begin
  738. Result := True;
  739. Exit;
  740. end;
  741. for i := 1 to cText.Length do
  742. begin
  743. CurrentPattern := Pattern[i];
  744. if CurrentPattern = '*' then wildcard := True;
  745. if wildcard then
  746. begin
  747. n := Pos(Copy(Pattern,i+1,Pattern.Length),cText);
  748. if (n > i) or (Pattern.Length = i) then
  749. begin
  750. Result := True;
  751. Exit;
  752. end;
  753. end
  754. else
  755. begin
  756. if (cText[i] = CurrentPattern) or (CurrentPattern = '?') then match := True
  757. else match := False;
  758. end;
  759. end;
  760. Result := match;
  761. end;
  762. function Capitalize(s: string): string;
  763. begin
  764. Result := '';
  765. if s.Length = 0 then Exit;
  766. s := LowerCase(s,loUserLocale);
  767. Result := UpperCase(s[1],loUserLocale) + Trim(Copy(s, 2, s.Length));
  768. end;
  769. function CapitalizeWords(s: string): string;
  770. var
  771. cword : string;
  772. begin
  773. Result := '';
  774. if s.Length = 0 then Exit;
  775. s := LowerCase(s,loUserLocale);
  776. for cword in s.Split([' ']) do
  777. begin
  778. if Result = '' then Result := Capitalize(cword)
  779. else Result := Result + ' ' + Capitalize(cword);
  780. end;
  781. end;
  782. function GetLoggedUserName : string;
  783. {$IFDEF MSWINDOWS}
  784. const
  785. cnMaxUserNameLen = 254;
  786. var
  787. sUserName : string;
  788. dwUserNameLen : DWord;
  789. begin
  790. dwUserNameLen := cnMaxUserNameLen-1;
  791. SetLength( sUserName, cnMaxUserNameLen );
  792. GetUserName(PChar( sUserName ),dwUserNameLen );
  793. SetLength( sUserName, dwUserNameLen );
  794. Result := sUserName;
  795. end;
  796. {$ELSE}
  797. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  798. begin
  799. Result := GetEnvironmentVariable('USERNAME');
  800. end;
  801. {$ELSE}
  802. var
  803. {$IFNDEF NEXTGEN}
  804. plogin : PAnsiChar;
  805. {$ELSE}
  806. plogin : MarshaledAString;
  807. {$ENDIF}
  808. begin
  809. {$IFDEF POSIX}
  810. try
  811. plogin := getlogin;
  812. Result := Copy(plogin,1,Length(Trim(plogin)));
  813. except
  814. Result := 'N/A';
  815. end;
  816. {$ELSE}
  817. Result := 'N/A';
  818. {$ENDIF}
  819. //raise ENotImplemented.Create('Not Android GetLoggedUserName implemented!');
  820. end;
  821. {$ENDIF}
  822. {$ENDIF}
  823. {$IFDEF IOS}
  824. function GetDeviceModel : String;
  825. var
  826. size : size_t;
  827. buffer : array of Byte;
  828. begin
  829. sysctlbyname('hw.machine',nil,@size,nil,0);
  830. if size > 0 then
  831. begin
  832. SetLength(buffer, size);
  833. sysctlbyname('hw.machine',@buffer[0],@size,nil,0);
  834. Result := UTF8ToString(MarshaledAString(buffer));
  835. end
  836. else Result := EmptyStr;
  837. end;
  838. {$ENDIF}
  839. function GetComputerName : string;
  840. {$IFDEF MSWINDOWS}
  841. var
  842. dwLength: dword;
  843. begin
  844. dwLength := 253;
  845. SetLength(Result, dwLength+1);
  846. if not Windows.GetComputerName(pchar(result), dwLength) then Result := 'Not detected!';
  847. Result := pchar(result);
  848. end;
  849. {$ELSE}
  850. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  851. begin
  852. Result := GetEnvironmentVariable('COMPUTERNAME');
  853. end;
  854. {$ELSE} //Android gets model name
  855. {$IFDEF NEXTGEN}
  856. begin
  857. {$IFDEF ANDROID}
  858. Result := JStringToString(TJBuild.JavaClass.MODEL);
  859. {$ELSE} //IOS
  860. Result := GetDeviceModel;
  861. {$ENDIF}
  862. end;
  863. {$ELSE}
  864. {$IFDEF DELPHILINUX}
  865. var
  866. phost : PAnsiChar;
  867. begin
  868. try
  869. phost := AllocMem(256);
  870. try
  871. if gethostname(phost,_SC_HOST_NAME_MAX) = 0 then
  872. begin
  873. {$IFDEF DEBUG}
  874. Result := Copy(Trim(phost),1,Length(Trim(phost)));
  875. {$ELSE}
  876. Result := Copy(phost,1,Length(phost));
  877. {$ENDIF}
  878. end
  879. else Result := 'N/A.';
  880. finally
  881. FreeMem(phost);
  882. end;
  883. except
  884. Result := 'N/A';
  885. end;
  886. end;
  887. {$ELSE} //OSX
  888. begin
  889. Result := NSStrToStr(TNSHost.Wrap(TNSHost.OCClass.currentHost).localizedName);
  890. end;
  891. {$ENDIF}
  892. {$ENDIF}
  893. {$ENDIF}
  894. {$ENDIF}
  895. function NormalizePathDelim(const cPath : string; const Delim : Char) : string;
  896. begin
  897. if Delim = '\' then Result := StringReplace(cPath,'/',Delim,[rfReplaceAll])
  898. else Result := StringReplace(cPath,'\',Delim,[rfReplaceAll]);
  899. end;
  900. function RemoveLastPathSegment(cDir : string) : string;
  901. var
  902. posi : Integer;
  903. delim : Char;
  904. EndsWithDelim : Boolean;
  905. begin
  906. if cDir.Contains('\') then delim := '\'
  907. else if cDir.Contains('/') then delim := '/'
  908. else
  909. begin
  910. Result := '';
  911. Exit;
  912. end;
  913. NormalizePathDelim(cDir,delim);
  914. if cDir.EndsWith(delim) then
  915. begin
  916. cDir := Copy(cDir,1,cDir.Length-1);
  917. EndsWithDelim := True;
  918. end
  919. else EndsWithDelim := False;
  920. if cDir.CountChar(delim) > 1 then posi := cDir.LastDelimiter(delim)
  921. else posi := Pos(delim,cDir)-1;
  922. if posi = cDir.Length then posi := 0;
  923. Result := Copy(cDir,1,posi);
  924. if (Result <> '') and (EndsWithDelim) then Result := Result + delim;
  925. end;
  926. function GetPathDelimiter(const aPath : string) : string;
  927. begin
  928. if aPath.Contains('/') then Result := '/'
  929. else if aPath.Contains('\') then Result := '\'
  930. else Result := '';
  931. end;
  932. function GetFirstPathSegment(const aPath : string) : string;
  933. var
  934. delimiter : string;
  935. spath : string;
  936. begin
  937. delimiter := GetPathDelimiter(aPath);
  938. if delimiter.IsEmpty then Exit(aPath);
  939. if aPath.StartsWith(delimiter) then spath := Copy(aPath,2,aPath.Length)
  940. else spath := aPath;
  941. Result := Copy(spath,0,spath.IndexOf(delimiter));
  942. end;
  943. function GetLastPathSegment(const aPath : string) : string;
  944. var
  945. delimiter : string;
  946. spath : string;
  947. begin
  948. delimiter := GetPathDelimiter(aPath);
  949. if delimiter.IsEmpty then Exit(aPath);
  950. if aPath.EndsWith(delimiter) then spath := Copy(aPath,0,aPath.Length - 1)
  951. else spath := aPath;
  952. Result := spath.Substring(spath.LastDelimiter(delimiter)+1);
  953. end;
  954. function ParamFindSwitch(const Switch : string) : Boolean;
  955. begin
  956. Result := FindCmdLineSwitch(Switch,['-', '/'],True);
  957. end;
  958. {$IFDEF FPC}
  959. function FindCmdLineSwitch(const Switch: string; var Value: string; IgnoreCase: Boolean = True;
  960. const SwitchTypes: TCmdLineSwitchTypes = [clstValueNextParam, clstValueAppended]): Boolean; overload;
  961. type
  962. TCompareProc = function(const S1, S2: string): Boolean;
  963. var
  964. Param: string;
  965. I, ValueOfs,
  966. SwitchLen, ParamLen: Integer;
  967. SameSwitch: TCompareProc;
  968. begin
  969. Result := False;
  970. Value := '';
  971. if IgnoreCase then
  972. SameSwitch := SameText else
  973. SameSwitch := SameStr;
  974. SwitchLen := Switch.Length;
  975. for I := 1 to ParamCount do
  976. begin
  977. Param := ParamStr(I);
  978. if CharInSet(Param.Chars[0], SwitchChars) and SameSwitch(Param.SubString(1,SwitchLen), Switch) then
  979. begin
  980. ParamLen := Param.Length;
  981. // Look for an appended value if the param is longer than the switch
  982. if (ParamLen > SwitchLen + 1) then
  983. begin
  984. // If not looking for appended value switches then this is not a matching switch
  985. if not (clstValueAppended in SwitchTypes) then
  986. Continue;
  987. ValueOfs := SwitchLen + 1;
  988. if Param.Chars[ValueOfs] = ':' then
  989. Inc(ValueOfs);
  990. Value := Param.SubString(ValueOfs, MaxInt);
  991. end
  992. // If the next param is not a switch, then treat it as the value
  993. else if (clstValueNextParam in SwitchTypes) and (I < ParamCount) and
  994. not CharInSet(ParamStr(I+1).Chars[0], SwitchChars) then
  995. Value := ParamStr(I+1);
  996. Result := True;
  997. Break;
  998. end;
  999. end;
  1000. end;
  1001. {$ENDIF}
  1002. function ParamGetSwitch(const Switch : string; var cvalue : string) : Boolean;
  1003. begin
  1004. Result := FindCmdLineSwitch(Switch,cvalue,True,[clstValueAppended]);
  1005. end;
  1006. function GetAppName : string;
  1007. begin
  1008. Result := ExtractFilenameWithoutExt(ParamStr(0));
  1009. end;
  1010. function GetAppVersionStr: string;
  1011. {$IFDEF MSWINDOWS}
  1012. var
  1013. Rec: LongRec;
  1014. ver : Cardinal;
  1015. begin
  1016. ver := GetFileVersion(ParamStr(0));
  1017. if ver <> Cardinal(-1) then
  1018. begin
  1019. Rec := LongRec(ver);
  1020. Result := Format('%d.%d', [Rec.Hi, Rec.Lo]);
  1021. end
  1022. else Result := '';
  1023. end;
  1024. {$ELSE}
  1025. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  1026. var
  1027. version : TProgramVersion;
  1028. begin
  1029. if GetProgramVersion(version) then Result := Format('%d.%d', [version.Major, version.Minor])
  1030. else Result := '';
  1031. end;
  1032. {$ELSE}
  1033. {$IFDEF NEXTGEN}
  1034. {$IFDEF ANDROID}
  1035. var
  1036. PkgInfo : JPackageInfo;
  1037. begin
  1038. PkgInfo := SharedActivity.getPackageManager.getPackageInfo(SharedActivity.getPackageName,0);
  1039. Result := IntToStr(PkgInfo.VersionCode);
  1040. end;
  1041. {$ELSE} //IOS
  1042. var
  1043. AppKey: Pointer;
  1044. AppBundle: NSBundle;
  1045. BuildStr : NSString;
  1046. begin
  1047. try
  1048. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1049. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1050. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1051. Result := UTF8ToString(BuildStr.UTF8String);
  1052. except
  1053. Result := '';
  1054. end;
  1055. end;
  1056. {$ENDIF}
  1057. {$ELSE} //OSX
  1058. {$IFDEF OSX}
  1059. var
  1060. AppKey: Pointer;
  1061. AppBundle: NSBundle;
  1062. BuildStr : NSString;
  1063. begin
  1064. try
  1065. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1066. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1067. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1068. Result := UTF8ToString(BuildStr.UTF8String);
  1069. except
  1070. Result := '';
  1071. end;
  1072. end;
  1073. {$ELSE}
  1074. begin
  1075. Result := '';
  1076. end;
  1077. {$ENDIF}
  1078. {$ENDIF}
  1079. {$ENDIF}
  1080. {$ENDIF}
  1081. function GetAppVersionFullStr: string;
  1082. {$IFDEF MSWINDOWS}
  1083. var
  1084. Exe: string;
  1085. Size, Handle: DWORD;
  1086. Buffer: TBytes;
  1087. FixedPtr: PVSFixedFileInfo;
  1088. begin
  1089. Result := '';
  1090. Exe := ParamStr(0);
  1091. Size := GetFileVersionInfoSize(PChar(Exe), Handle);
  1092. if Size = 0 then
  1093. begin
  1094. //RaiseLastOSError;
  1095. //no version info in file
  1096. Exit;
  1097. end;
  1098. SetLength(Buffer, Size);
  1099. if not GetFileVersionInfo(PChar(Exe), Handle, Size, Buffer) then
  1100. RaiseLastOSError;
  1101. if not VerQueryValue(Buffer, '\', Pointer(FixedPtr), Size) then
  1102. RaiseLastOSError;
  1103. if (LongRec(FixedPtr.dwFileVersionLS).Hi = 0) and (LongRec(FixedPtr.dwFileVersionLS).Lo = 0) then
  1104. begin
  1105. Result := Format('%d.%d',
  1106. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1107. LongRec(FixedPtr.dwFileVersionMS).Lo]); //minor
  1108. end
  1109. else if (LongRec(FixedPtr.dwFileVersionLS).Lo = 0) then
  1110. begin
  1111. Result := Format('%d.%d.%d',
  1112. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1113. LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
  1114. LongRec(FixedPtr.dwFileVersionLS).Hi]); //release
  1115. end
  1116. else
  1117. begin
  1118. Result := Format('%d.%d.%d.%d',
  1119. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1120. LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
  1121. LongRec(FixedPtr.dwFileVersionLS).Hi, //release
  1122. LongRec(FixedPtr.dwFileVersionLS).Lo]); //build
  1123. end;
  1124. end;
  1125. {$ELSE}
  1126. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  1127. var
  1128. version : TProgramVersion;
  1129. begin
  1130. if GetProgramVersion(version) then Result := Format('%d.%d.%d.%d', [version.Major, version.Minor, version.Revision, version.Build])
  1131. else Result := '';
  1132. end;
  1133. {$ELSE}
  1134. {$IFDEF NEXTGEN}
  1135. {$IFDEF ANDROID}
  1136. var
  1137. PkgInfo : JPackageInfo;
  1138. begin
  1139. PkgInfo := SharedActivity.getPackageManager.getPackageInfo(SharedActivity.getPackageName,0);
  1140. Result := JStringToString(PkgInfo.versionName);
  1141. end;
  1142. {$ELSE} //IOS
  1143. var
  1144. AppKey: Pointer;
  1145. AppBundle: NSBundle;
  1146. BuildStr : NSString;
  1147. begin
  1148. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1149. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1150. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1151. Result := UTF8ToString(BuildStr.UTF8String);
  1152. end;
  1153. {$ENDIF}
  1154. {$ELSE}
  1155. {$IFDEF OSX}
  1156. var
  1157. AppKey: Pointer;
  1158. AppBundle: NSBundle;
  1159. BuildStr : NSString;
  1160. begin
  1161. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1162. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1163. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1164. Result := UTF8ToString(BuildStr.UTF8String);
  1165. end;
  1166. {$ELSE}
  1167. begin
  1168. Result := '';
  1169. end;
  1170. {$ENDIF}
  1171. {$ENDIF}
  1172. {$ENDIF}
  1173. {$ENDIF}
  1174. function UTCToLocalTime(GMTTime: TDateTime): TDateTime;
  1175. begin
  1176. {$IFDEF FPC}
  1177. Result := LocalTimeToUniversal(GMTTime);
  1178. {$ELSE}
  1179. Result := TTimeZone.Local.ToLocalTime(GMTTime);
  1180. {$ENDIF}
  1181. end;
  1182. function LocalTimeToUTC(LocalTime : TDateTime): TDateTime;
  1183. begin
  1184. {$IFDEF FPC}
  1185. Result := UniversalTimeToLocal(Localtime);
  1186. {$ELSE}
  1187. Result := TTimeZone.Local.ToUniversalTime(LocalTime);
  1188. {$ENDIF}
  1189. end;
  1190. function DateTimeToGMT(aDate : TDateTime) : string;
  1191. var
  1192. FmtSettings : TFormatSettings;
  1193. begin
  1194. FmtSettings.DateSeparator := '-';
  1195. FmtSettings.TimeSeparator := ':';
  1196. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ" GMT"';
  1197. Result := DateTimeToStr(aDate,FmtSettings).Trim;
  1198. end;
  1199. function GMTToDateTime(aDate : string) : TDateTime;
  1200. var
  1201. FmtSettings : TFormatSettings;
  1202. begin
  1203. FmtSettings.DateSeparator := '-';
  1204. FmtSettings.TimeSeparator := ':';
  1205. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ" GMT"';
  1206. Result := StrToDateTime(aDate,FmtSettings);
  1207. end;
  1208. function DateTimeToJsonDate(aDateTime : TDateTime) : string;
  1209. {$IFNDEF DELPHIXE7_UP}
  1210. var
  1211. FmtSettings : TFormatSettings;
  1212. {$ENDIF}
  1213. begin
  1214. {$IFDEF DELPHIXE7_UP}
  1215. Result := DateToISO8601(aDateTime);
  1216. {$ELSE}
  1217. FmtSettings.DateSeparator := '-';
  1218. FmtSettings.TimeSeparator := ':';
  1219. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ"Z"';
  1220. Result := DateTimeToStr(aDateTime,FmtSettings).Trim;
  1221. {$ENDIF}
  1222. end;
  1223. function JsonDateToDateTime(const aJsonDate : string) : TDateTime;
  1224. {$IFNDEF DELPHIXE7_UP}
  1225. var
  1226. FmtSettings : TFormatSettings;
  1227. {$ENDIF}
  1228. {$IFDEF FPC}
  1229. var
  1230. jdate : string;
  1231. {$ENDIF}
  1232. begin
  1233. {$IFDEF DELPHIXE7_UP}
  1234. Result := ISO8601ToDate(aJsonDate);
  1235. {$ELSE}
  1236. FmtSettings.DateSeparator := '-';
  1237. FmtSettings.TimeSeparator := ':';
  1238. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ"Z"';
  1239. {$IFDEF FPC}
  1240. jdate := StringReplace(aJsondate,'T',' ',[rfIgnoreCase]);
  1241. jdate := Copy(jdate,1,Pos('.',jdate)-1);
  1242. Result := StrToDateTime(jdate,FmtSettings);
  1243. {$ELSE}
  1244. Result := StrToDateTime(aJsonDate,FmtSettings);
  1245. {$ENDIF}
  1246. {$ENDIF}
  1247. end;
  1248. function CountDigits(anInt: Cardinal): Cardinal; inline;
  1249. var
  1250. cmp: Cardinal;
  1251. begin
  1252. cmp := 10;
  1253. Result := 1;
  1254. while (Result < 10) and (cmp <= anInt) do
  1255. begin
  1256. cmp := cmp*10;
  1257. Inc(Result);
  1258. end;
  1259. end;
  1260. function CountStr(const aFindStr, aSourceStr : string) : Integer;
  1261. var
  1262. i : Integer;
  1263. found : Integer;
  1264. findstr : string;
  1265. mainstr : string;
  1266. begin
  1267. findstr := aFindStr.ToLower;
  1268. mainstr := aSourceStr.ToLower;
  1269. Result := 0;
  1270. i := 0;
  1271. while i < mainstr.Length do
  1272. begin
  1273. found := Pos(findstr,mainstr,i);
  1274. if found > 0 then
  1275. begin
  1276. i := found;
  1277. Inc(Result);
  1278. end
  1279. else Break;
  1280. end;
  1281. end;
  1282. procedure SaveStreamToFile(aStream : TStream; const aFileName : string);
  1283. var
  1284. fs : TFileStream;
  1285. begin
  1286. fs := TFileStream.Create(aFileName,fmCreate);
  1287. try
  1288. aStream.Seek(0,soBeginning);
  1289. fs.CopyFrom(aStream,aStream.Size);
  1290. finally
  1291. fs.Free;
  1292. end;
  1293. end;
  1294. function StreamToString(aStream : TStream) : string;
  1295. var
  1296. ss : TStringStream;
  1297. begin
  1298. aStream.Position := 0;
  1299. if aStream = nil then Exit;
  1300. if aStream is TMemoryStream then
  1301. begin
  1302. SetString(Result, PChar(TMemoryStream(aStream).Memory), TMemoryStream(aStream).Size div SizeOf(Char));
  1303. end
  1304. else if aStream is TStringStream then
  1305. begin
  1306. Result := TStringStream(aStream).DataString;
  1307. end
  1308. else
  1309. begin
  1310. ss := TStringStream.Create;
  1311. try
  1312. aStream.Seek(0,soBeginning);
  1313. ss.CopyFrom(aStream,aStream.Size);
  1314. Result := ss.DataString;
  1315. finally
  1316. ss.Free;
  1317. end;
  1318. end;
  1319. end;
  1320. function StreamToString2(const aStream: TStream; const aEncoding: TEncoding): string;
  1321. var
  1322. sbytes: TBytes;
  1323. begin
  1324. aStream.Position := 0;
  1325. SetLength(sbytes, aStream.Size);
  1326. aStream.ReadBuffer(sbytes,aStream.Size);
  1327. Result := aEncoding.GetString(sbytes);
  1328. end;
  1329. procedure StringToStream(const aStr : string; aStream : TStream);
  1330. begin
  1331. aStream.Seek(0,soBeginning);
  1332. aStream.WriteBuffer(Pointer(aStr)^,aStr.Length * SizeOf(Char));
  1333. end;
  1334. procedure StringToStream2(const aStr : string; aStream : TStream);
  1335. var
  1336. stream : TStringStream;
  1337. begin
  1338. stream := TStringStream.Create(aStr,TEncoding.UTF8);
  1339. try
  1340. aStream.CopyFrom(stream,stream.Size);
  1341. finally
  1342. stream.Free;
  1343. end;
  1344. end;
  1345. function CommaText(aList : TStringList) : string;
  1346. var
  1347. value : string;
  1348. sb : TStringBuilder;
  1349. begin
  1350. if aList.Text = '' then Exit;
  1351. sb := TStringBuilder.Create;
  1352. try
  1353. for value in aList do
  1354. begin
  1355. sb.Append(value);
  1356. sb.Append(',');
  1357. end;
  1358. if sb.Length > 1 then Result := sb.ToString(0, sb.Length - 1);
  1359. finally
  1360. sb.Free;
  1361. end;
  1362. end;
  1363. function CommaText(aArray : TArray<string>) : string;
  1364. var
  1365. value : string;
  1366. sb : TStringBuilder;
  1367. begin
  1368. if High(aArray) < 0 then Exit;
  1369. sb := TStringBuilder.Create;
  1370. try
  1371. for value in aArray do
  1372. begin
  1373. sb.Append(value);
  1374. sb.Append(',');
  1375. end;
  1376. if sb.Length > 1 then Result := sb.ToString(0, sb.Length - 1);
  1377. finally
  1378. sb.Free;
  1379. end;
  1380. end;
  1381. function ArrayToString(aArray : TArray<string>) : string;
  1382. var
  1383. value : string;
  1384. sb : TStringBuilder;
  1385. begin
  1386. if High(aArray) < 0 then Exit;
  1387. sb := TStringBuilder.Create;
  1388. try
  1389. for value in aArray do
  1390. begin
  1391. sb.Append(value);
  1392. sb.Append(#10#13);
  1393. end;
  1394. finally
  1395. sb.Free;
  1396. end;
  1397. end;
  1398. function StringsToArray(aStrings : TStrings) : TArray<string>;
  1399. var
  1400. i : Integer;
  1401. begin
  1402. if aStrings.Count = 0 then Exit;
  1403. SetLength(Result,aStrings.Count);
  1404. for i := 0 to aStrings.Count - 1 do
  1405. begin
  1406. Result[i] := aStrings[i];
  1407. end;
  1408. end;
  1409. { TCounter }
  1410. procedure TCounter.Init(aMaxValue : Integer);
  1411. begin
  1412. fMaxValue := aMaxValue;
  1413. fCurrentValue := 0;
  1414. end;
  1415. function TCounter.Count : Integer;
  1416. begin
  1417. Result := fCurrentValue;
  1418. end;
  1419. function TCounter.CountIs(aValue : Integer) : Boolean;
  1420. begin
  1421. Result := fCurrentValue = aValue;
  1422. end;
  1423. function TCounter.Check : Boolean;
  1424. begin
  1425. if fCurrentValue = fMaxValue then
  1426. begin
  1427. Result := True;
  1428. Reset;
  1429. end
  1430. else
  1431. begin
  1432. Result := False;
  1433. Inc(fCurrentValue);
  1434. end;
  1435. end;
  1436. procedure TCounter.Reset;
  1437. begin
  1438. fCurrentValue := fMaxValue;
  1439. end;
  1440. { TimeCounter }
  1441. procedure TTimeCounter.Init(MillisecondsToReach : Integer);
  1442. begin
  1443. fDoneEvery := MillisecondsToReach;
  1444. end;
  1445. function TTimeCounter.Check : Boolean;
  1446. begin
  1447. if MilliSecondsBetween(fCurrentTime,Now) > fDoneEvery then
  1448. begin
  1449. fCurrentTime := Now();
  1450. Result := True;
  1451. end
  1452. else Result := False;
  1453. end;
  1454. procedure TTimeCounter.Reset;
  1455. begin
  1456. fCurrentTime := Now();
  1457. end;
  1458. { TArrayOfStringHelper}
  1459. {$IFNDEF FPC}
  1460. function TArrayOfStringHelper.Any : Boolean;
  1461. begin
  1462. Result := High(Self) >= 0;
  1463. end;
  1464. function TArrayOfStringHelper.Any(const aValue : string) : Boolean;
  1465. begin
  1466. Result := Exists(aValue);
  1467. end;
  1468. function TArrayOfStringHelper.Add(const aValue : string) : Integer;
  1469. begin
  1470. SetLength(Self,Length(Self)+1);
  1471. Self[High(Self)] := aValue;
  1472. Result := High(Self);
  1473. end;
  1474. function TArrayOfStringHelper.AddIfNotExists(const aValue : string; aCaseSense : Boolean = False) : Integer;
  1475. var
  1476. i : Integer;
  1477. begin
  1478. for i := Low(Self) to High(Self) do
  1479. begin
  1480. if aCaseSense then
  1481. begin
  1482. if Self[i] = aValue then Exit(i);
  1483. end
  1484. else
  1485. begin
  1486. if CompareText(Self[i],aValue) = 0 then Exit(i)
  1487. end;
  1488. end;
  1489. //if not exists add it
  1490. Result := Self.Add(aValue);
  1491. end;
  1492. function TArrayOfStringHelper.Remove(const aValue : string) : Boolean;
  1493. var
  1494. i : Integer;
  1495. begin
  1496. for i := Low(Self) to High(Self) do
  1497. begin
  1498. if CompareText(Self[i],aValue) = 0 then
  1499. begin
  1500. {$IFDEF DELPHIXE7_UP}
  1501. System.Delete(Self,i,1);
  1502. {$ELSE}
  1503. TArrayUtil<string>.Delete(Self,i);
  1504. {$ENDIF}
  1505. Exit(True);
  1506. end;
  1507. end;
  1508. Result := False;
  1509. end;
  1510. function TArrayOfStringHelper.Exists(const aValue : string) : Boolean;
  1511. var
  1512. value : string;
  1513. begin
  1514. Result := False;
  1515. for value in Self do
  1516. begin
  1517. if CompareText(value,aValue) = 0 then Exit(True)
  1518. end;
  1519. end;
  1520. function TArrayOfStringHelper.Count : Integer;
  1521. begin
  1522. Result := High(Self) + 1;
  1523. end;
  1524. {$ENDIF}
  1525. { TPairItem }
  1526. constructor TPairItem.Create(const aName, aValue: string);
  1527. begin
  1528. Name := aName;
  1529. Value := aValue;
  1530. end;
  1531. { TPairList }
  1532. function TPairList.GetEnumerator : TPairEnumerator;
  1533. begin
  1534. Result := TPairEnumerator.Create(fItems);
  1535. end;
  1536. function TPairList.Add(aPair: TPairItem): Integer;
  1537. begin
  1538. SetLength(fItems,Length(fItems)+1);
  1539. fItems[High(fItems)] := aPair;
  1540. Result := High(fItems);
  1541. end;
  1542. function TPairList.Add(const aName, aValue: string): Integer;
  1543. begin
  1544. SetLength(fItems,Length(fItems)+1);
  1545. fItems[High(fItems)].Name := aName;
  1546. fItems[High(fItems)].Value := aValue;
  1547. Result := High(fItems);
  1548. end;
  1549. procedure TPairList.AddOrUpdate(const aName, aValue: string);
  1550. var
  1551. i : Integer;
  1552. begin
  1553. for i := Low(fItems) to High(fItems) do
  1554. begin
  1555. if CompareText(fItems[i].Name,aName) = 0 then
  1556. begin
  1557. fItems[i].Value := aValue;
  1558. Exit;
  1559. end;
  1560. end;
  1561. //if not exists add it
  1562. Self.Add(aName,aValue);
  1563. end;
  1564. function TPairList.Count: Integer;
  1565. begin
  1566. Result := High(fItems) + 1;
  1567. end;
  1568. function TPairList.Exists(const aName: string): Boolean;
  1569. var
  1570. i : Integer;
  1571. begin
  1572. Result := False;
  1573. for i := Low(fItems) to High(fItems) do
  1574. begin
  1575. if CompareText(fItems[i].Name,aName) = 0 then Exit(True)
  1576. end;
  1577. end;
  1578. function TPairList.GetPair(const aName: string): TPairItem;
  1579. var
  1580. i : Integer;
  1581. begin
  1582. for i := Low(fItems) to High(fItems) do
  1583. begin
  1584. if CompareText(fItems[i].Name,aName) = 0 then Exit(fItems[i]);
  1585. end;
  1586. end;
  1587. function TPairList.GetValue(const aName: string): string;
  1588. var
  1589. i : Integer;
  1590. begin
  1591. Result := '';
  1592. for i := Low(fItems) to High(fItems) do
  1593. begin
  1594. if CompareText(fItems[i].Name,aName) = 0 then Exit(fItems[i].Value);
  1595. end;
  1596. end;
  1597. function TPairList.Remove(const aName: string): Boolean;
  1598. var
  1599. i : Integer;
  1600. begin
  1601. for i := Low(fItems) to High(fItems) do
  1602. begin
  1603. if CompareText(fItems[i].Name,aName) = 0 then
  1604. begin
  1605. {$IF Defined(DELPHIXE7_UP) OR Defined(FPC)}
  1606. System.Delete(fItems,i,1);
  1607. {$ELSE}
  1608. TArrayUtil<TPairItem>.Delete(fItems,i);
  1609. {$ENDIF}
  1610. Exit(True);
  1611. end;
  1612. end;
  1613. Result := False;
  1614. end;
  1615. function TPairList.ToArray : TArray<TPairItem>;
  1616. begin
  1617. Result := fItems;
  1618. end;
  1619. procedure TPairList.FromArray(aValue : TArray<TPairItem>);
  1620. begin
  1621. fItems := aValue;
  1622. end;
  1623. procedure TPairList.Clear;
  1624. begin
  1625. SetLength(fItems,0);
  1626. end;
  1627. { TPairList.TPairEnumerator}
  1628. constructor TPairList.TPairEnumerator.Create(var aArray: TArray<TPairItem>);
  1629. begin
  1630. fIndex := -1;
  1631. fArray := @aArray;
  1632. end;
  1633. function TPairList.TPairEnumerator.GetCurrent : TPairItem;
  1634. begin
  1635. Result := TArray<TPairItem>(fArray^)[fIndex];
  1636. end;
  1637. function TPairList.TPairEnumerator.MoveNext: Boolean;
  1638. begin
  1639. Inc(fIndex);
  1640. Result := fIndex < High(TArray<TPairItem>(fArray^))+1;
  1641. end;
  1642. {$IFDEF MSWINDOWS}
  1643. procedure ProcessMessages;
  1644. var
  1645. Msg: TMsg;
  1646. begin
  1647. while integer(PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) <> 0 do
  1648. begin
  1649. TranslateMessage(Msg);
  1650. DispatchMessage(Msg);
  1651. end;
  1652. end;
  1653. function GetLastOSError: String;
  1654. begin
  1655. Result := SysErrorMessage(Windows.GetLastError);
  1656. end;
  1657. {$ENDIF}
  1658. function RemoveLastChar(const aText : string) : string;
  1659. begin
  1660. Result := aText.Remove(aText.Length - 1);
  1661. end;
  1662. function DateTimeToSQL(aDateTime : TDateTime) : string;
  1663. begin
  1664. Result := FormatDateTime('YYYYMMDD hh:mm:ss',aDateTime);
  1665. end;
  1666. function IsInteger(const aValue : string) : Boolean;
  1667. var
  1668. i : Integer;
  1669. begin
  1670. Result := TryStrToInt(aValue,i);
  1671. end;
  1672. function IsFloat(const aValue : string) : Boolean;
  1673. var
  1674. e : Extended;
  1675. begin
  1676. Result := TryStrToFloat(aValue,e);
  1677. end;
  1678. function IsBoolean(const aValue : string) : Boolean;
  1679. var
  1680. b : Boolean;
  1681. begin
  1682. Result := TryStrToBool(aValue,b);
  1683. end;
  1684. function ExtractStr(var vSource : string; aIndex : Integer; aCount : Integer) : string;
  1685. begin
  1686. if aIndex > vSource.Length then Exit('');
  1687. Result := Copy(vSource,aIndex,aCount);
  1688. Delete(vSource,aIndex,aCount);
  1689. end;
  1690. function GetSubString(const aSource, aFirstDelimiter, aLastDelimiter : string) : string;
  1691. var
  1692. i : Integer;
  1693. begin
  1694. i := Pos(aFirstDelimiter,aSource);
  1695. if i > -1 then Result := Copy(aSource, i + aFirstDelimiter.Length, Pos(aLastDelimiter, aSource, i + aFirstDelimiter.Length) - i - aFirstDelimiter.Length)
  1696. else Result := '';
  1697. end;
  1698. function DbQuotedStr(const str : string): string;
  1699. var
  1700. i : Integer;
  1701. begin
  1702. Result := str;
  1703. for i := Result.Length - 1 downto 0 do
  1704. begin
  1705. if Result.Chars[i] = '"' then Result := Result.Insert(i, '"');
  1706. end;
  1707. Result := '"' + Result + '"';
  1708. end;
  1709. function UnDbQuotedStr(const str: string) : string;
  1710. begin
  1711. Result := Trim(str);
  1712. if not Result.IsEmpty then
  1713. begin
  1714. if Result.StartsWith('"') then Result := Copy(Result, 2, Result.Length - 2);
  1715. end;
  1716. end;
  1717. function SpQuotedStr(const str : string): string;
  1718. begin
  1719. Result := '''' + str + '''';
  1720. end;
  1721. function UnSpQuotedStr(const str: string) : string;
  1722. begin
  1723. Result := Trim(str);
  1724. if not Result.IsEmpty then
  1725. begin
  1726. if Result.StartsWith('''') then Result := Copy(Result, 2, Result.Length - 2);
  1727. end;
  1728. end;
  1729. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : string) : string;
  1730. begin
  1731. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1732. end;
  1733. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Integer) : Integer;
  1734. begin
  1735. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1736. end;
  1737. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Extended) : Extended;
  1738. begin
  1739. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1740. end;
  1741. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : TObject) : TObject;
  1742. begin
  1743. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1744. end;
  1745. {$IFNDEF FPC}
  1746. {$IFNDEF DELPHIXE7_UP}
  1747. class procedure TArrayUtil<T>.Delete(var aArray : TArray<T>; aIndex : Integer);
  1748. var
  1749. n : Integer;
  1750. len : Integer;
  1751. begin
  1752. len := Length(aArray);
  1753. if (len > 0) and (aIndex < len) then
  1754. begin
  1755. for n := aIndex + 1 to len - 1 do aArray[n - 1] := aArray[n];
  1756. SetLength(aArray, len - 1);
  1757. end;
  1758. end;
  1759. {$ENDIF}
  1760. {$ENDIF}
  1761. {$IFNDEF NEXTGEN}
  1762. initialization
  1763. try
  1764. GetEnvironmentPaths;
  1765. except
  1766. {$IFDEF SHOW_ENVIRONMENTPATH_ERRORS}
  1767. on E : Exception do
  1768. begin
  1769. if not IsService then
  1770. begin
  1771. if HasConsoleOutput then Writeln(Format('[WARN] GetEnvironmentPaths: %s',[E.Message]))
  1772. else MessageBox(0,PWideChar(Format('Get environment path error: %s',[E.Message])),'GetEnvironmentPaths',MB_ICONEXCLAMATION);
  1773. end;
  1774. end;
  1775. {$ENDIF}
  1776. end;
  1777. {$ENDIF}
  1778. end.