Quick.Commons.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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 : 14/07/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. {$IFDEF DELPHIRX10_UP}
  720. Result := TGUID.NewGuid.ToString;
  721. {$ELSE}
  722. guid.NewGuid;
  723. Result := guid.ToString
  724. {$ENDIF}
  725. end;
  726. function IsLike(cText, Pattern: string) : Boolean;
  727. var
  728. i, n : Integer;
  729. match : Boolean;
  730. wildcard : Boolean;
  731. CurrentPattern : Char;
  732. begin
  733. Result := False;
  734. wildcard := False;
  735. cText := LowerCase(cText);
  736. Pattern := LowerCase(Pattern);
  737. match := False;
  738. if (Pattern.Length > cText.Length) or (Pattern = '') then Exit;
  739. if Pattern = '*' then
  740. begin
  741. Result := True;
  742. Exit;
  743. end;
  744. for i := 1 to cText.Length do
  745. begin
  746. CurrentPattern := Pattern[i];
  747. if CurrentPattern = '*' then wildcard := True;
  748. if wildcard then
  749. begin
  750. n := Pos(Copy(Pattern,i+1,Pattern.Length),cText);
  751. if (n > i) or (Pattern.Length = i) then
  752. begin
  753. Result := True;
  754. Exit;
  755. end;
  756. end
  757. else
  758. begin
  759. if (cText[i] = CurrentPattern) or (CurrentPattern = '?') then match := True
  760. else match := False;
  761. end;
  762. end;
  763. Result := match;
  764. end;
  765. function Capitalize(s: string): string;
  766. begin
  767. Result := '';
  768. if s.Length = 0 then Exit;
  769. s := LowerCase(s,loUserLocale);
  770. Result := UpperCase(s[1],loUserLocale) + Trim(Copy(s, 2, s.Length));
  771. end;
  772. function CapitalizeWords(s: string): string;
  773. var
  774. cword : string;
  775. begin
  776. Result := '';
  777. if s.Length = 0 then Exit;
  778. s := LowerCase(s,loUserLocale);
  779. for cword in s.Split([' ']) do
  780. begin
  781. if Result = '' then Result := Capitalize(cword)
  782. else Result := Result + ' ' + Capitalize(cword);
  783. end;
  784. end;
  785. function GetLoggedUserName : string;
  786. {$IFDEF MSWINDOWS}
  787. const
  788. cnMaxUserNameLen = 254;
  789. var
  790. sUserName : string;
  791. dwUserNameLen : DWord;
  792. begin
  793. dwUserNameLen := cnMaxUserNameLen-1;
  794. SetLength( sUserName, cnMaxUserNameLen );
  795. GetUserName(PChar( sUserName ),dwUserNameLen );
  796. SetLength( sUserName, dwUserNameLen );
  797. Result := sUserName;
  798. end;
  799. {$ELSE}
  800. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  801. begin
  802. Result := GetEnvironmentVariable('USERNAME');
  803. end;
  804. {$ELSE}
  805. var
  806. {$IFNDEF NEXTGEN}
  807. plogin : PAnsiChar;
  808. {$ELSE}
  809. plogin : MarshaledAString;
  810. {$ENDIF}
  811. begin
  812. {$IFDEF POSIX}
  813. try
  814. plogin := getlogin;
  815. Result := Copy(plogin,1,Length(Trim(plogin)));
  816. except
  817. Result := 'N/A';
  818. end;
  819. {$ELSE}
  820. Result := 'N/A';
  821. {$ENDIF}
  822. //raise ENotImplemented.Create('Not Android GetLoggedUserName implemented!');
  823. end;
  824. {$ENDIF}
  825. {$ENDIF}
  826. {$IFDEF IOS}
  827. function GetDeviceModel : String;
  828. var
  829. size : size_t;
  830. buffer : array of Byte;
  831. begin
  832. sysctlbyname('hw.machine',nil,@size,nil,0);
  833. if size > 0 then
  834. begin
  835. SetLength(buffer, size);
  836. sysctlbyname('hw.machine',@buffer[0],@size,nil,0);
  837. Result := UTF8ToString(MarshaledAString(buffer));
  838. end
  839. else Result := EmptyStr;
  840. end;
  841. {$ENDIF}
  842. function GetComputerName : string;
  843. {$IFDEF MSWINDOWS}
  844. var
  845. dwLength: dword;
  846. begin
  847. dwLength := 253;
  848. SetLength(Result, dwLength+1);
  849. if not Windows.GetComputerName(pchar(result), dwLength) then Result := 'Not detected!';
  850. Result := pchar(result);
  851. end;
  852. {$ELSE}
  853. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  854. begin
  855. Result := GetEnvironmentVariable('COMPUTERNAME');
  856. end;
  857. {$ELSE} //Android gets model name
  858. {$IFDEF NEXTGEN}
  859. begin
  860. {$IFDEF ANDROID}
  861. Result := JStringToString(TJBuild.JavaClass.MODEL);
  862. {$ELSE} //IOS
  863. Result := GetDeviceModel;
  864. {$ENDIF}
  865. end;
  866. {$ELSE}
  867. {$IFDEF DELPHILINUX}
  868. var
  869. phost : PAnsiChar;
  870. begin
  871. try
  872. phost := AllocMem(256);
  873. try
  874. if gethostname(phost,_SC_HOST_NAME_MAX) = 0 then
  875. begin
  876. {$IFDEF DEBUG}
  877. Result := Copy(Trim(phost),1,Length(Trim(phost)));
  878. {$ELSE}
  879. Result := Copy(phost,1,Length(phost));
  880. {$ENDIF}
  881. end
  882. else Result := 'N/A.';
  883. finally
  884. FreeMem(phost);
  885. end;
  886. except
  887. Result := 'N/A';
  888. end;
  889. end;
  890. {$ELSE} //OSX
  891. begin
  892. Result := NSStrToStr(TNSHost.Wrap(TNSHost.OCClass.currentHost).localizedName);
  893. end;
  894. {$ENDIF}
  895. {$ENDIF}
  896. {$ENDIF}
  897. {$ENDIF}
  898. function NormalizePathDelim(const cPath : string; const Delim : Char) : string;
  899. begin
  900. if Delim = '\' then Result := StringReplace(cPath,'/',Delim,[rfReplaceAll])
  901. else Result := StringReplace(cPath,'\',Delim,[rfReplaceAll]);
  902. end;
  903. function RemoveLastPathSegment(cDir : string) : string;
  904. var
  905. posi : Integer;
  906. delim : Char;
  907. EndsWithDelim : Boolean;
  908. begin
  909. if cDir.Contains('\') then delim := '\'
  910. else if cDir.Contains('/') then delim := '/'
  911. else
  912. begin
  913. Result := '';
  914. Exit;
  915. end;
  916. NormalizePathDelim(cDir,delim);
  917. if cDir.EndsWith(delim) then
  918. begin
  919. cDir := Copy(cDir,1,cDir.Length-1);
  920. EndsWithDelim := True;
  921. end
  922. else EndsWithDelim := False;
  923. if cDir.CountChar(delim) > 1 then posi := cDir.LastDelimiter(delim)
  924. else posi := Pos(delim,cDir)-1;
  925. if posi = cDir.Length then posi := 0;
  926. Result := Copy(cDir,1,posi);
  927. if (Result <> '') and (EndsWithDelim) then Result := Result + delim;
  928. end;
  929. function GetPathDelimiter(const aPath : string) : string;
  930. begin
  931. if aPath.Contains('/') then Result := '/'
  932. else if aPath.Contains('\') then Result := '\'
  933. else Result := '';
  934. end;
  935. function GetFirstPathSegment(const aPath : string) : string;
  936. var
  937. delimiter : string;
  938. spath : string;
  939. begin
  940. delimiter := GetPathDelimiter(aPath);
  941. if delimiter.IsEmpty then Exit(aPath);
  942. if aPath.StartsWith(delimiter) then spath := Copy(aPath,2,aPath.Length)
  943. else spath := aPath;
  944. Result := Copy(spath,0,spath.IndexOf(delimiter));
  945. end;
  946. function GetLastPathSegment(const aPath : string) : string;
  947. var
  948. delimiter : string;
  949. spath : string;
  950. begin
  951. delimiter := GetPathDelimiter(aPath);
  952. if delimiter.IsEmpty then Exit(aPath);
  953. if aPath.EndsWith(delimiter) then spath := Copy(aPath,0,aPath.Length - 1)
  954. else spath := aPath;
  955. Result := spath.Substring(spath.LastDelimiter(delimiter)+1);
  956. end;
  957. function ParamFindSwitch(const Switch : string) : Boolean;
  958. begin
  959. Result := FindCmdLineSwitch(Switch,['-', '/'],True);
  960. end;
  961. {$IFDEF FPC}
  962. function FindCmdLineSwitch(const Switch: string; var Value: string; IgnoreCase: Boolean = True;
  963. const SwitchTypes: TCmdLineSwitchTypes = [clstValueNextParam, clstValueAppended]): Boolean; overload;
  964. type
  965. TCompareProc = function(const S1, S2: string): Boolean;
  966. var
  967. Param: string;
  968. I, ValueOfs,
  969. SwitchLen, ParamLen: Integer;
  970. SameSwitch: TCompareProc;
  971. begin
  972. Result := False;
  973. Value := '';
  974. if IgnoreCase then
  975. SameSwitch := SameText else
  976. SameSwitch := SameStr;
  977. SwitchLen := Switch.Length;
  978. for I := 1 to ParamCount do
  979. begin
  980. Param := ParamStr(I);
  981. if CharInSet(Param.Chars[0], SwitchChars) and SameSwitch(Param.SubString(1,SwitchLen), Switch) then
  982. begin
  983. ParamLen := Param.Length;
  984. // Look for an appended value if the param is longer than the switch
  985. if (ParamLen > SwitchLen + 1) then
  986. begin
  987. // If not looking for appended value switches then this is not a matching switch
  988. if not (clstValueAppended in SwitchTypes) then
  989. Continue;
  990. ValueOfs := SwitchLen + 1;
  991. if Param.Chars[ValueOfs] = ':' then
  992. Inc(ValueOfs);
  993. Value := Param.SubString(ValueOfs, MaxInt);
  994. end
  995. // If the next param is not a switch, then treat it as the value
  996. else if (clstValueNextParam in SwitchTypes) and (I < ParamCount) and
  997. not CharInSet(ParamStr(I+1).Chars[0], SwitchChars) then
  998. Value := ParamStr(I+1);
  999. Result := True;
  1000. Break;
  1001. end;
  1002. end;
  1003. end;
  1004. {$ENDIF}
  1005. function ParamGetSwitch(const Switch : string; var cvalue : string) : Boolean;
  1006. begin
  1007. Result := FindCmdLineSwitch(Switch,cvalue,True,[clstValueAppended]);
  1008. end;
  1009. function GetAppName : string;
  1010. begin
  1011. Result := ExtractFilenameWithoutExt(ParamStr(0));
  1012. end;
  1013. function GetAppVersionStr: string;
  1014. {$IFDEF MSWINDOWS}
  1015. var
  1016. Rec: LongRec;
  1017. ver : Cardinal;
  1018. begin
  1019. ver := GetFileVersion(ParamStr(0));
  1020. if ver <> Cardinal(-1) then
  1021. begin
  1022. Rec := LongRec(ver);
  1023. Result := Format('%d.%d', [Rec.Hi, Rec.Lo]);
  1024. end
  1025. else Result := '';
  1026. end;
  1027. {$ELSE}
  1028. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  1029. var
  1030. version : TProgramVersion;
  1031. begin
  1032. if GetProgramVersion(version) then Result := Format('%d.%d', [version.Major, version.Minor])
  1033. else Result := '';
  1034. end;
  1035. {$ELSE}
  1036. {$IFDEF NEXTGEN}
  1037. {$IFDEF ANDROID}
  1038. var
  1039. PkgInfo : JPackageInfo;
  1040. begin
  1041. PkgInfo := SharedActivity.getPackageManager.getPackageInfo(SharedActivity.getPackageName,0);
  1042. Result := IntToStr(PkgInfo.VersionCode);
  1043. end;
  1044. {$ELSE} //IOS
  1045. var
  1046. AppKey: Pointer;
  1047. AppBundle: NSBundle;
  1048. BuildStr : NSString;
  1049. begin
  1050. try
  1051. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1052. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1053. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1054. Result := UTF8ToString(BuildStr.UTF8String);
  1055. except
  1056. Result := '';
  1057. end;
  1058. end;
  1059. {$ENDIF}
  1060. {$ELSE} //OSX
  1061. {$IFDEF OSX}
  1062. var
  1063. AppKey: Pointer;
  1064. AppBundle: NSBundle;
  1065. BuildStr : NSString;
  1066. begin
  1067. try
  1068. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1069. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1070. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1071. Result := UTF8ToString(BuildStr.UTF8String);
  1072. except
  1073. Result := '';
  1074. end;
  1075. end;
  1076. {$ELSE}
  1077. begin
  1078. Result := '';
  1079. end;
  1080. {$ENDIF}
  1081. {$ENDIF}
  1082. {$ENDIF}
  1083. {$ENDIF}
  1084. function GetAppVersionFullStr: string;
  1085. {$IFDEF MSWINDOWS}
  1086. var
  1087. Exe: string;
  1088. Size, Handle: DWORD;
  1089. Buffer: TBytes;
  1090. FixedPtr: PVSFixedFileInfo;
  1091. begin
  1092. Result := '';
  1093. Exe := ParamStr(0);
  1094. Size := GetFileVersionInfoSize(PChar(Exe), Handle);
  1095. if Size = 0 then
  1096. begin
  1097. //RaiseLastOSError;
  1098. //no version info in file
  1099. Exit;
  1100. end;
  1101. SetLength(Buffer, Size);
  1102. if not GetFileVersionInfo(PChar(Exe), Handle, Size, Buffer) then
  1103. RaiseLastOSError;
  1104. if not VerQueryValue(Buffer, '\', Pointer(FixedPtr), Size) then
  1105. RaiseLastOSError;
  1106. if (LongRec(FixedPtr.dwFileVersionLS).Hi = 0) and (LongRec(FixedPtr.dwFileVersionLS).Lo = 0) then
  1107. begin
  1108. Result := Format('%d.%d',
  1109. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1110. LongRec(FixedPtr.dwFileVersionMS).Lo]); //minor
  1111. end
  1112. else if (LongRec(FixedPtr.dwFileVersionLS).Lo = 0) then
  1113. begin
  1114. Result := Format('%d.%d.%d',
  1115. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1116. LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
  1117. LongRec(FixedPtr.dwFileVersionLS).Hi]); //release
  1118. end
  1119. else
  1120. begin
  1121. Result := Format('%d.%d.%d.%d',
  1122. [LongRec(FixedPtr.dwFileVersionMS).Hi, //major
  1123. LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
  1124. LongRec(FixedPtr.dwFileVersionLS).Hi, //release
  1125. LongRec(FixedPtr.dwFileVersionLS).Lo]); //build
  1126. end;
  1127. end;
  1128. {$ELSE}
  1129. {$IF DEFINED(FPC) AND DEFINED(LINUX)}
  1130. var
  1131. version : TProgramVersion;
  1132. begin
  1133. if GetProgramVersion(version) then Result := Format('%d.%d.%d.%d', [version.Major, version.Minor, version.Revision, version.Build])
  1134. else Result := '';
  1135. end;
  1136. {$ELSE}
  1137. {$IFDEF NEXTGEN}
  1138. {$IFDEF ANDROID}
  1139. var
  1140. PkgInfo : JPackageInfo;
  1141. begin
  1142. PkgInfo := SharedActivity.getPackageManager.getPackageInfo(SharedActivity.getPackageName,0);
  1143. Result := JStringToString(PkgInfo.versionName);
  1144. end;
  1145. {$ELSE} //IOS
  1146. var
  1147. AppKey: Pointer;
  1148. AppBundle: NSBundle;
  1149. BuildStr : NSString;
  1150. begin
  1151. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1152. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1153. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1154. Result := UTF8ToString(BuildStr.UTF8String);
  1155. end;
  1156. {$ENDIF}
  1157. {$ELSE}
  1158. {$IFDEF OSX}
  1159. var
  1160. AppKey: Pointer;
  1161. AppBundle: NSBundle;
  1162. BuildStr : NSString;
  1163. begin
  1164. AppKey := (StrToNSStr('CFBundleVersion') as ILocalObject).GetObjectID;
  1165. AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  1166. BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  1167. Result := UTF8ToString(BuildStr.UTF8String);
  1168. end;
  1169. {$ELSE}
  1170. begin
  1171. Result := '';
  1172. end;
  1173. {$ENDIF}
  1174. {$ENDIF}
  1175. {$ENDIF}
  1176. {$ENDIF}
  1177. function UTCToLocalTime(GMTTime: TDateTime): TDateTime;
  1178. begin
  1179. {$IFDEF FPC}
  1180. Result := LocalTimeToUniversal(GMTTime);
  1181. {$ELSE}
  1182. Result := TTimeZone.Local.ToLocalTime(GMTTime);
  1183. {$ENDIF}
  1184. end;
  1185. function LocalTimeToUTC(LocalTime : TDateTime): TDateTime;
  1186. begin
  1187. {$IFDEF FPC}
  1188. Result := UniversalTimeToLocal(Localtime);
  1189. {$ELSE}
  1190. Result := TTimeZone.Local.ToUniversalTime(LocalTime);
  1191. {$ENDIF}
  1192. end;
  1193. function DateTimeToGMT(aDate : TDateTime) : string;
  1194. var
  1195. FmtSettings : TFormatSettings;
  1196. begin
  1197. FmtSettings.DateSeparator := '-';
  1198. FmtSettings.TimeSeparator := ':';
  1199. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ" GMT"';
  1200. Result := DateTimeToStr(aDate,FmtSettings).Trim;
  1201. end;
  1202. function GMTToDateTime(aDate : string) : TDateTime;
  1203. var
  1204. FmtSettings : TFormatSettings;
  1205. begin
  1206. FmtSettings.DateSeparator := '-';
  1207. FmtSettings.TimeSeparator := ':';
  1208. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ" GMT"';
  1209. Result := StrToDateTime(aDate,FmtSettings);
  1210. end;
  1211. function DateTimeToJsonDate(aDateTime : TDateTime) : string;
  1212. {$IFNDEF DELPHIXE7_UP}
  1213. var
  1214. FmtSettings : TFormatSettings;
  1215. {$ENDIF}
  1216. begin
  1217. {$IFDEF DELPHIXE7_UP}
  1218. Result := DateToISO8601(aDateTime);
  1219. {$ELSE}
  1220. FmtSettings.DateSeparator := '-';
  1221. FmtSettings.TimeSeparator := ':';
  1222. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ"Z"';
  1223. Result := DateTimeToStr(aDateTime,FmtSettings).Trim;
  1224. {$ENDIF}
  1225. end;
  1226. function JsonDateToDateTime(const aJsonDate : string) : TDateTime;
  1227. {$IFNDEF DELPHIXE7_UP}
  1228. var
  1229. FmtSettings : TFormatSettings;
  1230. {$ENDIF}
  1231. {$IFDEF FPC}
  1232. var
  1233. jdate : string;
  1234. {$ENDIF}
  1235. begin
  1236. {$IFDEF DELPHIXE7_UP}
  1237. Result := ISO8601ToDate(aJsonDate);
  1238. {$ELSE}
  1239. FmtSettings.DateSeparator := '-';
  1240. FmtSettings.TimeSeparator := ':';
  1241. FmtSettings.ShortDateFormat := 'YYYY-MM-DD"T"HH:NN:SS.ZZZ"Z"';
  1242. {$IFDEF FPC}
  1243. jdate := StringReplace(aJsondate,'T',' ',[rfIgnoreCase]);
  1244. jdate := Copy(jdate,1,Pos('.',jdate)-1);
  1245. Result := StrToDateTime(jdate,FmtSettings);
  1246. {$ELSE}
  1247. Result := StrToDateTime(aJsonDate,FmtSettings);
  1248. {$ENDIF}
  1249. {$ENDIF}
  1250. end;
  1251. function CountDigits(anInt: Cardinal): Cardinal; inline;
  1252. var
  1253. cmp: Cardinal;
  1254. begin
  1255. cmp := 10;
  1256. Result := 1;
  1257. while (Result < 10) and (cmp <= anInt) do
  1258. begin
  1259. cmp := cmp*10;
  1260. Inc(Result);
  1261. end;
  1262. end;
  1263. function CountStr(const aFindStr, aSourceStr : string) : Integer;
  1264. var
  1265. i : Integer;
  1266. found : Integer;
  1267. findstr : string;
  1268. mainstr : string;
  1269. begin
  1270. findstr := aFindStr.ToLower;
  1271. mainstr := aSourceStr.ToLower;
  1272. Result := 0;
  1273. i := 0;
  1274. while i < mainstr.Length do
  1275. begin
  1276. found := Pos(findstr,mainstr,i);
  1277. if found > 0 then
  1278. begin
  1279. i := found;
  1280. Inc(Result);
  1281. end
  1282. else Break;
  1283. end;
  1284. end;
  1285. procedure SaveStreamToFile(aStream : TStream; const aFileName : string);
  1286. var
  1287. fs : TFileStream;
  1288. begin
  1289. fs := TFileStream.Create(aFileName,fmCreate);
  1290. try
  1291. aStream.Seek(0,soBeginning);
  1292. fs.CopyFrom(aStream,aStream.Size);
  1293. finally
  1294. fs.Free;
  1295. end;
  1296. end;
  1297. function StreamToString(aStream : TStream) : string;
  1298. var
  1299. ss : TStringStream;
  1300. begin
  1301. aStream.Position := 0;
  1302. if aStream = nil then Exit;
  1303. if aStream is TMemoryStream then
  1304. begin
  1305. SetString(Result, PChar(TMemoryStream(aStream).Memory), TMemoryStream(aStream).Size div SizeOf(Char));
  1306. end
  1307. else if aStream is TStringStream then
  1308. begin
  1309. Result := TStringStream(aStream).DataString;
  1310. end
  1311. else
  1312. begin
  1313. ss := TStringStream.Create;
  1314. try
  1315. aStream.Seek(0,soBeginning);
  1316. ss.CopyFrom(aStream,aStream.Size);
  1317. Result := ss.DataString;
  1318. finally
  1319. ss.Free;
  1320. end;
  1321. end;
  1322. end;
  1323. function StreamToString2(const aStream: TStream; const aEncoding: TEncoding): string;
  1324. var
  1325. sbytes: TBytes;
  1326. begin
  1327. aStream.Position := 0;
  1328. SetLength(sbytes, aStream.Size);
  1329. aStream.ReadBuffer(sbytes,aStream.Size);
  1330. Result := aEncoding.GetString(sbytes);
  1331. end;
  1332. procedure StringToStream(const aStr : string; aStream : TStream);
  1333. begin
  1334. aStream.Seek(0,soBeginning);
  1335. aStream.WriteBuffer(Pointer(aStr)^,aStr.Length * SizeOf(Char));
  1336. end;
  1337. procedure StringToStream2(const aStr : string; aStream : TStream);
  1338. var
  1339. stream : TStringStream;
  1340. begin
  1341. stream := TStringStream.Create(aStr,TEncoding.UTF8);
  1342. try
  1343. aStream.CopyFrom(stream,stream.Size);
  1344. finally
  1345. stream.Free;
  1346. end;
  1347. end;
  1348. function CommaText(aList : TStringList) : string;
  1349. var
  1350. value : string;
  1351. sb : TStringBuilder;
  1352. begin
  1353. if aList.Text = '' then Exit;
  1354. sb := TStringBuilder.Create;
  1355. try
  1356. for value in aList do
  1357. begin
  1358. sb.Append(value);
  1359. sb.Append(',');
  1360. end;
  1361. if sb.Length > 1 then Result := sb.ToString(0, sb.Length - 1);
  1362. finally
  1363. sb.Free;
  1364. end;
  1365. end;
  1366. function CommaText(aArray : TArray<string>) : string;
  1367. var
  1368. value : string;
  1369. sb : TStringBuilder;
  1370. begin
  1371. if High(aArray) < 0 then Exit;
  1372. sb := TStringBuilder.Create;
  1373. try
  1374. for value in aArray do
  1375. begin
  1376. sb.Append(value);
  1377. sb.Append(',');
  1378. end;
  1379. if sb.Length > 1 then Result := sb.ToString(0, sb.Length - 1);
  1380. finally
  1381. sb.Free;
  1382. end;
  1383. end;
  1384. function ArrayToString(aArray : TArray<string>) : string;
  1385. var
  1386. value : string;
  1387. sb : TStringBuilder;
  1388. begin
  1389. if High(aArray) < 0 then Exit;
  1390. sb := TStringBuilder.Create;
  1391. try
  1392. for value in aArray do
  1393. begin
  1394. sb.Append(value);
  1395. sb.Append(#10#13);
  1396. end;
  1397. finally
  1398. sb.Free;
  1399. end;
  1400. end;
  1401. function StringsToArray(aStrings : TStrings) : TArray<string>;
  1402. var
  1403. i : Integer;
  1404. begin
  1405. if aStrings.Count = 0 then Exit;
  1406. SetLength(Result,aStrings.Count);
  1407. for i := 0 to aStrings.Count - 1 do
  1408. begin
  1409. Result[i] := aStrings[i];
  1410. end;
  1411. end;
  1412. { TCounter }
  1413. procedure TCounter.Init(aMaxValue : Integer);
  1414. begin
  1415. fMaxValue := aMaxValue;
  1416. fCurrentValue := 0;
  1417. end;
  1418. function TCounter.Count : Integer;
  1419. begin
  1420. Result := fCurrentValue;
  1421. end;
  1422. function TCounter.CountIs(aValue : Integer) : Boolean;
  1423. begin
  1424. Result := fCurrentValue = aValue;
  1425. end;
  1426. function TCounter.Check : Boolean;
  1427. begin
  1428. if fCurrentValue = fMaxValue then
  1429. begin
  1430. Result := True;
  1431. Reset;
  1432. end
  1433. else
  1434. begin
  1435. Result := False;
  1436. Inc(fCurrentValue);
  1437. end;
  1438. end;
  1439. procedure TCounter.Reset;
  1440. begin
  1441. fCurrentValue := fMaxValue;
  1442. end;
  1443. { TimeCounter }
  1444. procedure TTimeCounter.Init(MillisecondsToReach : Integer);
  1445. begin
  1446. fDoneEvery := MillisecondsToReach;
  1447. end;
  1448. function TTimeCounter.Check : Boolean;
  1449. begin
  1450. if MilliSecondsBetween(fCurrentTime,Now) > fDoneEvery then
  1451. begin
  1452. fCurrentTime := Now();
  1453. Result := True;
  1454. end
  1455. else Result := False;
  1456. end;
  1457. procedure TTimeCounter.Reset;
  1458. begin
  1459. fCurrentTime := Now();
  1460. end;
  1461. { TArrayOfStringHelper}
  1462. {$IFNDEF FPC}
  1463. function TArrayOfStringHelper.Any : Boolean;
  1464. begin
  1465. Result := High(Self) >= 0;
  1466. end;
  1467. function TArrayOfStringHelper.Any(const aValue : string) : Boolean;
  1468. begin
  1469. Result := Exists(aValue);
  1470. end;
  1471. function TArrayOfStringHelper.Add(const aValue : string) : Integer;
  1472. begin
  1473. SetLength(Self,Length(Self)+1);
  1474. Self[High(Self)] := aValue;
  1475. Result := High(Self);
  1476. end;
  1477. function TArrayOfStringHelper.AddIfNotExists(const aValue : string; aCaseSense : Boolean = False) : Integer;
  1478. var
  1479. i : Integer;
  1480. begin
  1481. for i := Low(Self) to High(Self) do
  1482. begin
  1483. if aCaseSense then
  1484. begin
  1485. if Self[i] = aValue then Exit(i);
  1486. end
  1487. else
  1488. begin
  1489. if CompareText(Self[i],aValue) = 0 then Exit(i)
  1490. end;
  1491. end;
  1492. //if not exists add it
  1493. Result := Self.Add(aValue);
  1494. end;
  1495. function TArrayOfStringHelper.Remove(const aValue : string) : Boolean;
  1496. var
  1497. i : Integer;
  1498. begin
  1499. for i := Low(Self) to High(Self) do
  1500. begin
  1501. if CompareText(Self[i],aValue) = 0 then
  1502. begin
  1503. {$IFDEF DELPHIXE7_UP}
  1504. System.Delete(Self,i,1);
  1505. {$ELSE}
  1506. TArrayUtil<string>.Delete(Self,i);
  1507. {$ENDIF}
  1508. Exit(True);
  1509. end;
  1510. end;
  1511. Result := False;
  1512. end;
  1513. function TArrayOfStringHelper.Exists(const aValue : string) : Boolean;
  1514. var
  1515. value : string;
  1516. begin
  1517. Result := False;
  1518. for value in Self do
  1519. begin
  1520. if CompareText(value,aValue) = 0 then Exit(True)
  1521. end;
  1522. end;
  1523. function TArrayOfStringHelper.Count : Integer;
  1524. begin
  1525. Result := High(Self) + 1;
  1526. end;
  1527. {$ENDIF}
  1528. { TPairItem }
  1529. constructor TPairItem.Create(const aName, aValue: string);
  1530. begin
  1531. Name := aName;
  1532. Value := aValue;
  1533. end;
  1534. { TPairList }
  1535. function TPairList.GetEnumerator : TPairEnumerator;
  1536. begin
  1537. Result := TPairEnumerator.Create(fItems);
  1538. end;
  1539. function TPairList.Add(aPair: TPairItem): Integer;
  1540. begin
  1541. SetLength(fItems,Length(fItems)+1);
  1542. fItems[High(fItems)] := aPair;
  1543. Result := High(fItems);
  1544. end;
  1545. function TPairList.Add(const aName, aValue: string): Integer;
  1546. begin
  1547. SetLength(fItems,Length(fItems)+1);
  1548. fItems[High(fItems)].Name := aName;
  1549. fItems[High(fItems)].Value := aValue;
  1550. Result := High(fItems);
  1551. end;
  1552. procedure TPairList.AddOrUpdate(const aName, aValue: string);
  1553. var
  1554. i : Integer;
  1555. begin
  1556. for i := Low(fItems) to High(fItems) do
  1557. begin
  1558. if CompareText(fItems[i].Name,aName) = 0 then
  1559. begin
  1560. fItems[i].Value := aValue;
  1561. Exit;
  1562. end;
  1563. end;
  1564. //if not exists add it
  1565. Self.Add(aName,aValue);
  1566. end;
  1567. function TPairList.Count: Integer;
  1568. begin
  1569. Result := High(fItems) + 1;
  1570. end;
  1571. function TPairList.Exists(const aName: string): Boolean;
  1572. var
  1573. i : Integer;
  1574. begin
  1575. Result := False;
  1576. for i := Low(fItems) to High(fItems) do
  1577. begin
  1578. if CompareText(fItems[i].Name,aName) = 0 then Exit(True)
  1579. end;
  1580. end;
  1581. function TPairList.GetPair(const aName: string): TPairItem;
  1582. var
  1583. i : Integer;
  1584. begin
  1585. for i := Low(fItems) to High(fItems) do
  1586. begin
  1587. if CompareText(fItems[i].Name,aName) = 0 then Exit(fItems[i]);
  1588. end;
  1589. end;
  1590. function TPairList.GetValue(const aName: string): string;
  1591. var
  1592. i : Integer;
  1593. begin
  1594. Result := '';
  1595. for i := Low(fItems) to High(fItems) do
  1596. begin
  1597. if CompareText(fItems[i].Name,aName) = 0 then Exit(fItems[i].Value);
  1598. end;
  1599. end;
  1600. function TPairList.Remove(const aName: string): Boolean;
  1601. var
  1602. i : Integer;
  1603. begin
  1604. for i := Low(fItems) to High(fItems) do
  1605. begin
  1606. if CompareText(fItems[i].Name,aName) = 0 then
  1607. begin
  1608. {$IF Defined(DELPHIXE7_UP) OR Defined(FPC)}
  1609. System.Delete(fItems,i,1);
  1610. {$ELSE}
  1611. TArrayUtil<TPairItem>.Delete(fItems,i);
  1612. {$ENDIF}
  1613. Exit(True);
  1614. end;
  1615. end;
  1616. Result := False;
  1617. end;
  1618. function TPairList.ToArray : TArray<TPairItem>;
  1619. begin
  1620. Result := fItems;
  1621. end;
  1622. procedure TPairList.FromArray(aValue : TArray<TPairItem>);
  1623. begin
  1624. fItems := aValue;
  1625. end;
  1626. procedure TPairList.Clear;
  1627. begin
  1628. SetLength(fItems,0);
  1629. end;
  1630. { TPairList.TPairEnumerator}
  1631. constructor TPairList.TPairEnumerator.Create(var aArray: TArray<TPairItem>);
  1632. begin
  1633. fIndex := -1;
  1634. fArray := @aArray;
  1635. end;
  1636. function TPairList.TPairEnumerator.GetCurrent : TPairItem;
  1637. begin
  1638. Result := TArray<TPairItem>(fArray^)[fIndex];
  1639. end;
  1640. function TPairList.TPairEnumerator.MoveNext: Boolean;
  1641. begin
  1642. Inc(fIndex);
  1643. Result := fIndex < High(TArray<TPairItem>(fArray^))+1;
  1644. end;
  1645. {$IFDEF MSWINDOWS}
  1646. procedure ProcessMessages;
  1647. var
  1648. Msg: TMsg;
  1649. begin
  1650. while integer(PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) <> 0 do
  1651. begin
  1652. TranslateMessage(Msg);
  1653. DispatchMessage(Msg);
  1654. end;
  1655. end;
  1656. function GetLastOSError: String;
  1657. begin
  1658. Result := SysErrorMessage(Windows.GetLastError);
  1659. end;
  1660. {$ENDIF}
  1661. function RemoveLastChar(const aText : string) : string;
  1662. begin
  1663. Result := aText.Remove(aText.Length - 1);
  1664. end;
  1665. function DateTimeToSQL(aDateTime : TDateTime) : string;
  1666. begin
  1667. Result := FormatDateTime('YYYYMMDD hh:mm:ss',aDateTime);
  1668. end;
  1669. function IsInteger(const aValue : string) : Boolean;
  1670. var
  1671. i : Integer;
  1672. begin
  1673. Result := TryStrToInt(aValue,i);
  1674. end;
  1675. function IsFloat(const aValue : string) : Boolean;
  1676. var
  1677. e : Extended;
  1678. begin
  1679. Result := TryStrToFloat(aValue,e);
  1680. end;
  1681. function IsBoolean(const aValue : string) : Boolean;
  1682. var
  1683. b : Boolean;
  1684. begin
  1685. Result := TryStrToBool(aValue,b);
  1686. end;
  1687. function ExtractStr(var vSource : string; aIndex : Integer; aCount : Integer) : string;
  1688. begin
  1689. if aIndex > vSource.Length then Exit('');
  1690. Result := Copy(vSource,aIndex,aCount);
  1691. Delete(vSource,aIndex,aCount);
  1692. end;
  1693. function GetSubString(const aSource, aFirstDelimiter, aLastDelimiter : string) : string;
  1694. var
  1695. i : Integer;
  1696. begin
  1697. i := Pos(aFirstDelimiter,aSource);
  1698. if i > -1 then Result := Copy(aSource, i + aFirstDelimiter.Length, Pos(aLastDelimiter, aSource, i + aFirstDelimiter.Length) - i - aFirstDelimiter.Length)
  1699. else Result := '';
  1700. end;
  1701. function DbQuotedStr(const str : string): string;
  1702. var
  1703. i : Integer;
  1704. begin
  1705. Result := str;
  1706. for i := Result.Length - 1 downto 0 do
  1707. begin
  1708. if Result.Chars[i] = '"' then Result := Result.Insert(i, '"');
  1709. end;
  1710. Result := '"' + Result + '"';
  1711. end;
  1712. function UnDbQuotedStr(const str: string) : string;
  1713. begin
  1714. Result := Trim(str);
  1715. if not Result.IsEmpty then
  1716. begin
  1717. if Result.StartsWith('"') then Result := Copy(Result, 2, Result.Length - 2);
  1718. end;
  1719. end;
  1720. function SpQuotedStr(const str : string): string;
  1721. begin
  1722. Result := '''' + str + '''';
  1723. end;
  1724. function UnSpQuotedStr(const str: string) : string;
  1725. begin
  1726. Result := Trim(str);
  1727. if not Result.IsEmpty then
  1728. begin
  1729. if Result.StartsWith('''') then Result := Copy(Result, 2, Result.Length - 2);
  1730. end;
  1731. end;
  1732. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : string) : string;
  1733. begin
  1734. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1735. end;
  1736. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Integer) : Integer;
  1737. begin
  1738. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1739. end;
  1740. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : Extended) : Extended;
  1741. begin
  1742. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1743. end;
  1744. function Ifx(aCondition : Boolean; const aIfIsTrue, aIfIsFalse : TObject) : TObject;
  1745. begin
  1746. if aCondition then Result := aIfIsTrue else Result := aIfIsFalse;
  1747. end;
  1748. {$IFNDEF FPC}
  1749. {$IFNDEF DELPHIXE7_UP}
  1750. class procedure TArrayUtil<T>.Delete(var aArray : TArray<T>; aIndex : Integer);
  1751. var
  1752. n : Integer;
  1753. len : Integer;
  1754. begin
  1755. len := Length(aArray);
  1756. if (len > 0) and (aIndex < len) then
  1757. begin
  1758. for n := aIndex + 1 to len - 1 do aArray[n - 1] := aArray[n];
  1759. SetLength(aArray, len - 1);
  1760. end;
  1761. end;
  1762. {$ENDIF}
  1763. {$ENDIF}
  1764. {$IFNDEF NEXTGEN}
  1765. initialization
  1766. try
  1767. GetEnvironmentPaths;
  1768. except
  1769. {$IFDEF SHOW_ENVIRONMENTPATH_ERRORS}
  1770. on E : Exception do
  1771. begin
  1772. if not IsService then
  1773. begin
  1774. if HasConsoleOutput then Writeln(Format('[WARN] GetEnvironmentPaths: %s',[E.Message]))
  1775. else MessageBox(0,PWideChar(Format('Get environment path error: %s',[E.Message])),'GetEnvironmentPaths',MB_ICONEXCLAMATION);
  1776. end;
  1777. end;
  1778. {$ENDIF}
  1779. end;
  1780. {$ENDIF}
  1781. end.