Solved

rearrange file

Posted on 1998-01-11
2
198 Views
Last Modified: 2010-04-16
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
Comment
Question by:Scarhand
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
FuzzyLogic earned 200 total points
ID: 1217096
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
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1217097
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question