{----------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is: JvStrings.PAS, released on 2002-06-15. The Initial Developer of the Original Code is Jan Verhoeven [jan1 dott verhoeven att wxs dott nl] Portions created by Jan Verhoeven are Copyright (C) 2002 Jan Verhoeven. All Rights Reserved. Contributor(s): Robert Love [rlove att slcdug dott org]. You may retrieve the latest version of this file at the Project JEDI's JVCL home page, located at http://jvcl.sourceforge.net Known Issues: Should be merged with JCL -----------------------------------------------------------------------------} // $Id: JvStrings.pas 10612 2006-05-19 19:04:09Z jfudickar $ unit JvStrings; {$I jvcl.inc} {$I crossplatform.inc} interface uses {$IFDEF UNITVERSIONING} JclUnitVersioning, {$ENDIF UNITVERSIONING} {$IFDEF MSWINDOWS} Windows, {$ENDIF MSWINDOWS} {$IFDEF HAS_UNIT_LIBC} Libc, {$ENDIF HAS_UNIT_LIBC} Graphics, SysUtils, Classes; {regular expressions} {template functions} function ReplaceFirst(const SourceStr, FindStr, ReplaceStr: string): string; function ReplaceLast(const SourceStr, FindStr, ReplaceStr: string): string; function InsertLastBlock(var SourceStr: string; BlockStr: string): Boolean; function RemoveMasterBlocks(const SourceStr: string): string; function RemoveFields(const SourceStr: string): string; {http functions} function URLEncode(const Value: string): string; // Converts string To A URLEncoded string function URLDecode(const Value: string): string; // Converts string From A URLEncoded string {set functions} procedure SplitSet(AText: string; AList: TStringList); function JoinSet(AList: TStringList): string; function FirstOfSet(const AText: string): string; function LastOfSet(const AText: string): string; function CountOfSet(const AText: string): Integer; function SetRotateRight(const AText: string): string; function SetRotateLeft(const AText: string): string; function SetPick(const AText: string; AIndex: Integer): string; function SetSort(const AText: string): string; function SetUnion(const Set1, Set2: string): string; function SetIntersect(const Set1, Set2: string): string; function SetExclude(const Set1, Set2: string): string; {replace any <,> etc by < >} function XMLSafe(const AText: string): string; {simple hash, Result can be used in Encrypt} function Hash(const AText: string): Integer; { Base64 encode and decode a string } function B64Encode(const S: string): string; function B64Decode(const S: string): string; {Basic encryption from a Borland Example} function Encrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string; function Decrypt(const InString: string; StartKey, MultKey, AddKey: Integer): string; {Using Encrypt and Decrypt in combination with B64Encode and B64Decode} function EncryptB64(const InString: string; StartKey, MultKey, AddKey: Integer): string; function DecryptB64(const InString: string; StartKey, MultKey, AddKey: Integer): string; procedure CSVToTags(Src, Dst: TStringList); // converts a csv list to a tagged string list procedure TagsToCSV(Src, Dst: TStringList); // converts a tagged string list to a csv list // only fieldnames from the first record are scanned ib the other records procedure ListSelect(Src, Dst: TStringList; const AKey, AValue: string); {selects akey=avalue from Src and returns recordset in Dst} procedure ListFilter(Src: TStringList; const AKey, AValue: string); {filters Src for akey=avalue} procedure ListOrderBy(Src: TStringList; const AKey: string; Numeric: Boolean); {orders a tagged Src list by akey} function PosStr(const FindString, SourceString: string; StartPos: Integer = 1): Integer; { PosStr searches the first occurrence of a substring FindString in a string given by SourceString with case sensitivity (upper and lower case characters are differed). This function returns the index value of the first character of a specified substring from which it occurs in a given string starting with StartPos character index. If a specified substring is not found Q_PosStr returns zero. The author of algorithm is Peter Morris (UK) (Faststrings unit from www.torry.ru). } function PosStrLast(const FindString, SourceString: string): Integer; {finds the last occurance} function LastPosChar(const FindChar: Char; SourceString: string): Integer; function PosText(const FindString, SourceString: string; StartPos: Integer = 1): Integer; { PosText searches the first occurrence of a substring FindString in a string given by SourceString without case sensitivity (upper and lower case characters are not differed). This function returns the index value of the first character of a specified substring from which it occurs in a given string starting with StartPos character index. If a specified substring is not found Q_PosStr returns zero. The author of algorithm is Peter Morris (UK) (Faststrings unit from www.torry.ru). } function PosTextLast(const FindString, SourceString: string): Integer; {finds the last occurance} function NameValuesToXML(const AText: string): string; {$IFDEF MSWINDOWS} procedure LoadResourceFile(AFile: string; MemStream: TMemoryStream); {$ENDIF MSWINDOWS} procedure DirFiles(const ADir, AMask: string; AFileList: TStringList); procedure RecurseDirFiles(const ADir: string; var AFileList: TStringList); procedure RecurseDirProgs(const ADir: string; var AFileList: TStringList); procedure SaveString(const AFile, AText: string); function LoadString(const AFile: string): string; function HexToColor(const AText: string): TColor; function UppercaseHTMLTags(const AText: string): string; function LowercaseHTMLTags(const AText: string): string; procedure GetHTMLAnchors(const AFile: string; AList: TStringList); function RelativePath(const ASrc, ADst: string): string; function GetToken(var Start: Integer; const SourceText: string): string; function PosNonSpace(Start: Integer; const SourceText: string): Integer; function PosEscaped(Start: Integer; const SourceText, FindText: string; EscapeChar: Char): Integer; function DeleteEscaped(const SourceText: string; EscapeChar: Char): string; function BeginOfAttribute(Start: Integer; const SourceText: string): Integer; // parses the beginning of an attribute: space + alpha character function ParseAttribute(var Start: Integer; const SourceText: string; var AName, AValue: string): Boolean; // parses a name="value" attribute from Start; returns 0 when not found or else the position behind the attribute procedure ParseAttributes(const SourceText: string; Attributes: TStrings); // parses all name=value attributes to the attributes TStringList function HasStrValue(const AText, AName: string; var AValue: string): Boolean; // checks if a name="value" pair exists and returns any value function GetStrValue(const AText, AName, ADefault: string): string; // retrieves string value from a line like: // name="jan verhoeven" email="jan1 dott verhoeven att wxs dott nl" // returns ADefault when not found function GetHTMLColorValue(const AText, AName: string; ADefault: TColor): TColor; // same for a color function GetIntValue(const AText, AName: string; ADefault: Integer): Integer; // same for an Integer function GetFloatValue(const AText, AName: string; ADefault: Extended): Extended; // same for a float function GetBoolValue(const AText, AName: string): Boolean; // same for Boolean but without default function GetValue(const AText, AName: string): string; // retrieves string value from a line like: // name="jan verhoeven" email="jan1 dott verhoeven att wxs dott nl" procedure SetValue(var AText: string; const AName, AValue: string); // sets a string value in a line procedure DeleteValue(var AText: string; const AName: string); // deletes a AName="value" pair from AText procedure GetNames(AText: string; AList: TStringList); // get a list of names from a string with name="value" pairs function GetHTMLColor(AColor: TColor): string; // converts a color value to the HTML hex value function BackPosStr(Start: Integer; const FindString, SourceString: string): Integer; // finds a string backward case sensitive function BackPosText(Start: Integer; const FindString, SourceString: string): Integer; // finds a string backward case insensitive function PosRangeStr(Start: Integer; const HeadString, TailString, SourceString: string; var RangeBegin: Integer; var RangeEnd: Integer): Boolean; // finds a text range, e.g.