Author Topic: function sprintf(): string + function GetSequentialFilename(): string  (Read 2097 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Hello everyone, while scripting today i came across the need for sprintf() and as far as i know there is no sprintf() style function in pascal, so here is my rendition. It should be pretty efficient, and its pretty easy to add new keywords/types.

Usage:
Code: [Select]
var finalstring: string;

finalstring := sprintf('my int=%i%, my float=%f%, my string=%s%',[15,1.2345,'abcd']);

// finalstring = 'my int=15, my float=1.2345, my string=abcd

Warning:
You must have the same amount of item tokens('%i%') as array elements.

The Code:
Code: [Select]
function sprintf(Mask: string; Input: array of variant): string;
var cur,add: string; i,ii: integer;
begin
result := '';
i := 0;
ii := 0;
cur := getpiece(mask,'%',i);
while (cur <> nil) do begin
if i mod 2 = 0 then begin
add := cur;
end else begin
cur := lowercase(cur);
add := 'ERROR';
case cur of
's': add := input[ii];
'i': add := inttostr(input[ii]);
'd': add := floattostr(input[ii]);
'f': add := floattostr(input[ii]);
end;
ii := ii + 1;
end;
i := i + 1;
cur := getpiece(mask,'%',i);
result := result + add;
end;
end;

EDIT:
Also ill post this, it gets filenames that ascend in order, like backup_0,backup_1,backup_2 ect. It uses sprintf() above.

Usage:
Code: [Select]
var myfilename: string;

myfilename := getsequentialfilename('scripts/'+scriptname+'/gamesave_%i%.txt',5);
The above usage code will result in available filenames like this:
'scripts/<scriptname>/gamesave_0.txt'
'scripts/<scriptname>/gamesave_1.txt'
'scripts/<scriptname>/gamesave_2.txt'
'scripts/<scriptname>/gamesave_3.txt'
'scripts/<scriptname>/gamesave_4.txt'

If there are no available filenames in the rage provided the function returns a null string, '', or nil.

The Code:
Code: [Select]
function GetSequentialFilename(Mask: string; Max: integer): string;
var i: integer; newname: string;
begin
result := '';
i := 0;
while true do begin
newname := sprintf(mask,[i]);
if not fileexists(newname) then break;
if i >= max-1 then exit;
i := i + 1;
end;
result := newname;
end;

What do you think, usefull or not?
« Last Edit: November 08, 2010, 10:55:16 pm by Hacktank »


Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: function sprintf(): string + function GetSequentialFilename(): string
« Reply #1 on: November 09, 2010, 08:15:13 am »
This code might be handy

Code: (Pascal) [Select]
function GetTypeOF(Value: variant): string;
begin
  case VarType(Value) of
    3  : Result:= IntToStr(Value);
    5  : Result:= FloatToStr(Value);
    11 : Result:= iif(Value, 'true', 'false');
    256: Result:= Value;
  end;
end;
Soldat Global Account System: #soldat.sgas @ quakenet

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: function sprintf(): string + function GetSequentialFilename(): string
« Reply #2 on: November 09, 2010, 10:26:42 am »
This sprintf stuff is not really useful here, where would you like to use it?
If you want a string with some values, normal method such as 'abc ' + inttostr(x) + ' def' + floattostr(y) is way more efficient than using such function.