Author Topic: Function: retrievepropertext  (Read 1268 times)

0 Members and 1 Guest are viewing this topic.

Offline squiddy

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 333
  • Flagger assassin
    • SoldatX
Function: retrievepropertext
« on: February 24, 2013, 09:27:14 pm »
Input a series of lines with data to be separated and the function will output an array of string with proper text (including it all boxed up). Ispiration came from Hacktank's TextBox heheh :p

Code: [Select]
function retrievepropertext(text, linesplitter, datasplitter, nds, textontop: string; fillwith, corner, fillx, filly: char): array of string; //nds = new data splitter
var s, q, u, y, datalen: byte; bef, af: array of string; i: array of integer; d: array of array of string; //bef and af are SUPPOSED to be "before" and "after" shorts, but their meanings have been altered along the function, sadly.
begin
text := text + linesplitter; //this MUST be done
bef := explode(text,linesplitter);
while bef[getarraylength(bef)-1] = nil do setarraylength(bef,getarraylength(bef)-1); //this ALSO MUST be done
for s := 0 to getarraylength(bef)-1 do bef[s] := bef[s] + datasplitter; //this too. hehehe
af := explode(bef[0],datasplitter); //just to spare variables.. because I'm poor, ya know
datalen := getarraylength(af);
setarraylength(i,datalen);
setarraylength(d,getarraylength(bef));
setarraylength(af,getarraylength(bef));
setarraylength(result,getarraylength(bef));
for s := 0 to getarraylength(d)-1 do setarraylength(d[s],datalen);
for s := 0 to getarraylength(af)-1 do begin //main loop
    af[s] := ''; //clean the variable used temporarily up there heheh
    for q := 0 to datalen do begin //in this loop, remove the old datasplitter and insert the new one instead (nds)
        u := pos(datasplitter,bef[s]);
        af[s] := af[s] + copy(bef[s],1,u-1) + iif(q = datalen,'',nds);
        delete(bef[s],1,u + length(datasplitter) - 1);
        end;
    bef[s] := af[s]; //reciclying variables hehe :p
    for q := 0 to datalen-1 do begin //in this loop, check which are the lenghthiest data in each line
        u := pos(nds,bef[s]);
        if u-1 > i[q] then i[q] := u-1;
        delete(bef[s],1,u + length(nds) - 1);
        end;
    end;
for s := 0 to getarraylength(af)-1 do for q := 0 to datalen-1 do begin //in this loop, add the chars (corner etc.) to the result array
    u := pos(nds,af[s]);
    for y := u-1 to i[q] do d[s][q] := d[s][q] + iif(y = i[q],'',fillwith);
    result[s] := result[s] + copy(af[s],1,u-1) + d[s][q] + iif(q = datalen-1,'',nds);
    delete(af[s],1,u + length(nds) - 1);
    end;
setarraylength(result,getarraylength(result)+2); //add two "blanks" to the result array
for s := getarraylength(result)-2 downto 1 do result[s] := result[s-1]; //and now move all text one index up
result[0] := '';
i[0] := 0; //again, reciclying variables
for s := 0 to getarraylength(result)-1 do if length(result[s])+2 > i[0] then i[0] := length(result[s])+2; //i[0] is now the lenghtiest motherfucking line
for s := 1 to i[0]-2 do result[getarraylength(result)-1] := result[getarraylength(result)-1] + fillx; //put some filling
for s := 1 to (((i[0]-2) div 2) - (length(textontop) div 2)) do result[0] := result[0] + fillx; //fill before the text on top
result[0] := result[0] + textontop; //put the text on top
for s := length(result[0]) to i[0]-3 do result[0] := result[0] + fillx; //fill after the text on top
result[0] := corner + result[0] + corner; //put some motherfucking corners
result[getarraylength(result)-1] := corner + result[getarraylength(result)-1] + corner; //put some more motherfucking corners
for s := 1 to getarraylength(result)-2 do result[s] := filly + result[s] + filly; //and box it up to finish it!!
end;


Example. Let's say you want to do a command box with three simple commands.

Code: [Select]
var text: string; propertext: array of string; s: byte;
begin
text := '!Help - Shows how to play the game' + #13#10 + '!Nades - Grants you grenades' + #13#10 + '!Ragequit - Quits the server';

propertext := retrievepropertext(text,#13#10,'-','-->',' Commands ',' ','+','-','|');

for s := 0 to getarraylength(propertext)-1 do writeln(propertext[s]);

Result:

Code: [Select]
+--------------- Commands ---------------+
|!Help     --> Shows how to play the game|
|!Nades    --> Grants you grenades       |
|!Ragequit --> Quits the server          |
+----------------------------------------+

:)

edit: yay, 250 posts! lol, now I'm a camper

P.S.: Requires Explode (http://forums.soldat.pl/index.php?topic=35698). Too lazy to do it independently.
« Last Edit: February 24, 2013, 09:35:34 pm by squiddy »
www.soldatx.com.br - The brazilian Soldat community.

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Function: retrievepropertext
« Reply #1 on: February 25, 2013, 03:14:14 am »
You could accept two dimensional array of string instead of text with dashes and new lines. If scripter has it in such form, he can split it himself.

Good job on this, i'm sure many will find it handy.
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.