Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

rearrange file

After my last never ending question i have managed to suss out how to write the coding to find a file on a computer.
Now i have to write some coding which will re-write the messy coding of the program and put in the right amount of indentations after 'begins' and 'ends'.
my tutor has told us that we will not affect the amount of lines in the program we just have to take each line strip out any spaces at the begining of each line and then re-write it. But when we find a 'begin' we have to indent so many spaces and when we find an 'end' we have to go back the same amount of spaces so the program looks neat.
He reckons it involves an indentation variable which increments when the program finds a 'begin' and decrements when it finds an 'end' it also has another variable which is the amount of spaces to increment.(i think when the indent count goes up it is multiplied on the space amount so if there is a loop in a loop it doubles the spaces until it finds an 'end'). I think each line is stored in a string and the ASCI end of line code is used.
Any help you can give me will be much appreciated and save me from being put in an asylum.
Cheers
Scarhand.
0
Scarhand
Asked:
Scarhand
  • 2
1 Solution
 
FuzzyLogicCommented:
Here some code for you.

The procedure I wrote gets the Source filename, the Target filename and the tab size.
It then fixes the lines as you asked.
However, it will ignore the 'var', 'const', 'type' etc keywords, so that the varibles after 'var' won't be shifted.
If you want, I can fix this too, but then the procedure will be more compicated.

Hoping this will solve your problem.
Fuzzy
0
 
FuzzyLogicCommented:
function UpStr(S:string):string;
  var
    a:byte;
  begin;
    for a:=1 to length(S) do S[a]:=UpCase(S[a]);
    UpStr:=S;
  end;

procedure Format(SourceName,TargetName:string; size:byte);
  var
    Source,Target:text;
    Line,LineUp,Spaces:string;
    len:byte absolute Line;    { shortcut }
    a:byte;
    tabs:byte;
  begin;
    FillChar(Spaces,byte(' '),255);
    tabs:=0;
    assign(Source,SourceName);
    reset(Source);
    assign(Target,TargetName);
    rewrite(Target);

    while not(eof(Source)) do begin;
      readln(Source,Line);                 { #9 is Tab }
      while (len>0) and ((Line[1]=' ') or (Line[1]=#9)) do delete(Line,1,1);
      LineUp:=UpStr(Line);
      if pos('END',LineUp)=1 then if (tabs>0) then dec(tabs);
      Spaces[0]:=char(tabs*size);   { String of leading spaces }
      Line:=Spaces+Line;
      if pos('BEGIN',LineUp)=1 then inc(tabs);
      writeln(Target,Line);
    end;

    close(Source);
    close(Target);

  end;

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now