Solved

rearrange file

Posted on 1998-01-11
2
187 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now