When doing personal projects I tend too make some useful functions and procedures.
So I'm gonna post them all here =)
Some of these might already be in SCAR/Simba/SRL (I didn't see it?)
Simba Code:
{*******************************************************************************
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
By: Dgby714, Smartzkid, Sandstorm
Description: Removes the Index'th item from Arr
*******************************************************************************}
procedure RemoveArrIndex(Index: integer; var Arr: TStringArray);
var
I, P: integer;
begin
if (not (InRange(Index, Low(Arr), High(Arr)))) then
Exit;
P := High(Arr);
for I := Index + 1 to P do
Arr[I - 1] := Arr[I];
SetArrayLength(Arr, Length(Arr) - 1);
end;
Simba Code:
{*******************************************************************************
function InArr(const Data, Arr: variant): boolean;
By: Dgby714
Description: Returns True if Data is a item in Arr.
*******************************************************************************}
function InArr(const Data, Arr: variant): boolean;
var
I, P, T: integer;
begin
if (VarType(Arr) = 8204) then
begin
P := High(TVariantArray(Arr));
T := VarType(Data);
for I := 0 to P do
if (T = VarType(Arr[I])) then
if (Data = Arr[I]) then
begin
Result := True;
Exit;
end;
end;
end;
Simba Code:
{*******************************************************************************
function in_string(Text, Data: string): boolean;
By: Dgby714
Description: Returns True if Text is in Data
*******************************************************************************}
function in_string(Text, Data: string): boolean;
begin
Result := (Pos(Lowercase(Text), Lowercase(Data)) > 0);
end;
Simba Code:
{*******************************************************************************
procedure QScreenShot;
By: Dgby714
Description: Saves a screenshot into your AppPath/Screenshots
*******************************************************************************}
procedure QScreenShot;
var
Path, Filename: string;
I: integer;
begin
Path := AppPath + 'Screenshots' + {$IFDEF WINDOWS}'\'{$ELSE}'/'{$ENDIF};
if (not (DirectoryExists(Path))) then
if (not (CreateDirectory(Path))) then
RaiseException(erCustomError, 'Exception in QScreenShot: Cannot create directory "' + Path + '"');
for I := 0 to High(I) do
begin
Filename := 'Screenshot_' + IntToStr(I) + '.bmp';
if (not (FileExists(Path + Filename))) then
Break;
end;
SaveScreenshot(Path + Filename);
DebugLn('QScreenShot: Screenshot saved as "' + Path + Filename + '"');
end;
Simba Code:
{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: integer): string;
var
I: integer;
begin
Result := '';
for I := 1 to Len do
Result := Result + chr(33 + Random(93));
end;
Simba Code:
{*******************************************************************************
function Center(str: string; len: integer): string;
By: Dgby714
Description: Returns a str centered with len Length [Center('hey', 5) = ' hey ']
*******************************************************************************}
function Center(str: string; len: integer): string;
begin
Result := Trim(str);
while (Length(Result) < Len) do
begin
if (Length(Result) = (Len - 1)) then
Result := Result + ' '
else
Result := ' ' + Result + ' ';
end;
end;
Simba Code:
{*******************************************************************************
function TimeStamp: string;
By: Dgby714
Description: Returns the current time in 24 hour HH:MM:SS format
*******************************************************************************}
function TimeStamp: string;
var
Time: TDateTime;
H, M, S, MS: word;
begin
Time := Now;
DecodeTime(Time, H, M, S, MS);
Result := Padz(IntToStr(H), 2) + ':' + Padz(IntToStr(M), 2) + ':' + Padz(IntToStr(S), 2);
end;
Simba Code:
{*******************************************************************************
procedure WaitEx(TimeInMS: integer);
By: Dgby714
Description: Waits TimeInMs + Rand(10%) unless 1000 > TimeInMS > 5000!
*******************************************************************************}
procedure WaitEx(TimeInMS: integer);
var
RandTime: integer;
begin
if (TimeInMS < 1000) then
RandTime := Random(1000)
else if (TimeInMS > 5000) then
RandTime := Random(5000)
else
RandTime := Random(TimeInMS / 10);
Wait(TimeInMS + RandTime);
end;
Simba Code:
{*******************************************************************************
procedure WriteINIArr(Section, Name: string; Arr: TStringArray; Filename: string);
By: Dgby714
Description: Writes Arr to Filename under Scetion (The items are named Name)
*******************************************************************************}
procedure WriteINIArr(Section, Name: string; Arr: TStringArray; Filename: string);
var
I: integer;
begin
WriteINI(Section, 'Name', Name, Filename);
WriteINI(Section, 'Count', IntToStr(Length(Arr)), Filename);
for I := Low(Arr) to High(Arr) do
WriteINI(Section, Name + '[' + IntToStr(I) + ']', Arr[I], Filename);
end;
{*******************************************************************************
function ReadINIArr(Section, Filename: string): TStringArray;
By: Dgby714
Description: Returns a array from Filename under Section (Use WriteINIArr first)
*******************************************************************************}
function ReadINIArr(Section, Filename: string): TStringArray;
var
Name: string;
Count, I: integer;
begin
Name := ReadINI(Section, 'Name', Filename);
Count := StrToIntDef(ReadINI(Section, 'Count', Filename), 0);
SetArrayLength(Result, Count);
for I := 0 to Count - 1 do
Result[I] := ReadINI(Section, Name + '[' + IntToStr(I) + ']', Filename);
end;
Simba Code:
{*******************************************************************************
function ToExtended(Data: variant): extended;
By: Dgby714
Description: Returns Data as a Double/Extended
*******************************************************************************}
function ToExtended(Data: variant): extended;
begin
case VarType(Data) of
varInteger, varInt64, varSmallInt, varShortInt, varWord, varLongWord, varByte: Result := Data * 1.0;
varString, varOleStr: Result := StrToIntDef(Data, 0) * 1.0;
varNull: Result := 0.0;
varDouble: Result := Data;
varBoolean: if (Data) then
Result := 1.0
else
Result := 0.0;
end;
end;
These next ones are some I've found aren't needed =/ Ill give why too.
Simba Code:
{*******************************************************************************
function Space(num: integer): string;
By: Dgby714
Description: Returns num spaces
*******************************************************************************}
function Space(num: integer): string;
var
I: Integer;
begin
Result := '';
for I := 0 to Num do
Result := Result + ' ';
end;
This one can be....