Google
 

Trailing-Edge - PDP-10 Archives - decuslib10-05 - 43,50337/21/filed.sim
There is 1 other file named filed.sim in the archive. Click here to see a list.
00040	COMMENT INPUTTING AND OUTPUTTING DEC 10 TEXT FILES:   (May 1977)
00080	--------------------------------------------------
00120	
00160	     By Jacob Palme, Swedish National Defense Research Institute,
00200	     S-104 50 Stockholm 80, Sweden.
00240	
00280	     This program contains a class FILED (which can be separately
00320	     compiled) to input and output DEC 10 ASCII-7-formatted text
00360	     files in SIMULA. The program can handle both unnumbered and
00400	     line numbered text files. For numbered files, the program will
00440	     ensure that the output file has the format on line numbers
00480	     required by other DEC 10 text handling programs like for
00520	     example SOS. Lines lacking line numbers will be given such
00560	     numbers.
00600	
00640	     The good facilities for text handling in SIMULA makes it a
00680	     useful language for text handling programs like editors,
00720	     programming language translators, compilers, pre-compilers etc.
00760	
00800	     However, the reading and writing of text files on the DEC 10,
00840	     especially line numbered files, is rather tricky in SIMULA. By
00880	     using this program, you avoid those problems.
00920	
00930	     The program also contains facilities to protect you
00933	     from inputting and outputting the same file and for over-
00936	     writing the output file. If the output file already
00939	     exists, the existing file is renamed to a backup name
00942	     with an extension beginning with the letter Q.
00957	
00960	end of comment;
     
01000	OPTIONS(/c);
01040	BEGIN
01080	EXTERNAL TEXT PROCEDURE from, tagord, storbokstav, scanto, conc;
01120	EXTERNAL TEXT PROCEDURE compress, front;
01160	EXTERNAL REF (infile) PROCEDURE findinfile;
01200	EXTERNAL REF (outfile) PROCEDURE findoutfile;
01240	EXTERNAL BOOLEAN PROCEDURE numbered, rescan;
01280	EXTERNAL CHARACTER PROCEDURE fetchar;
01320	EXTERNAL INTEGER PROCEDURE rename;
01360	
01400	CLASS filed;
01440	BEGIN
01480	  CHARACTER carriagereturn, formfeed, tab;
01520	  BOOLEAN top_of_page; ! TRUE IF next output line starts new page;
01560	  BOOLEAN got_formfeed; ! TRUE IF last input line ended a page;
01600	  BOOLEAN first_line_read; ! TRUE only after first input line;
01640	  BOOLEAN line_numbered; ! The input file is line numbered;
01680	  TEXT sixdigits; ! Used in procedure make_five_digits;
01720	  TEXT editout_image; ! Image for output file;
01760	  TEXT editin_image_strip; ! Stripped input line from file;
01800	  TEXT five_sp; ! Text with just five blank characters;
01840	  TEXT five_sp_tab; ! Text with five blanks and a tab;
01880	  INTEGER last_line_number; ! Number on previous input line;
01920	  INTEGER this_line_number; ! Number on current input line;
01960	  REF (infile) editin; ! The input text file;
02000	  REF (outfile) editout; ! The output text file;
     
02040	  TEXT PROCEDURE make_five_digits(line_number);
02080	    COMMENT: This procedure produces a five-character long string
02120	    containing the parameter number in ASCII format with leading
02160	    zeroes (for use as line number on the output file);
02200	  INTEGER line_number;
02240	  BEGIN
02280	    sixdigits.putint(line_number+100000);
02320	    make_five_digits:- sixdigits.sub(2,5);
02360	  END;
     
02400	  BOOLEAN PROCEDURE five_digits(t); TEXT t;
02440	    COMMENT: This procedure checks if the first five characters of
02480	    the parameter string t are all digits, which is required for a
02520	    correct line number;
02560	  BEGIN
02600	    IF t.length > 4 THEN
02640	    BEGIN
02680	      t.setpos(1);
02720	      IF digit(t.getchar) THEN
02760	      BEGIN IF digit(t.getchar) THEN
02800	        BEGIN IF digit(t.getchar) THEN
02840	          BEGIN IF digit(t.getchar) THEN
02880	            five_digits:= digit(t.getchar);
02920	          END;
02960	        END;
03000	      END;
03040	    END;
03080	  END;
     
03120	  PROCEDURE editinimage;
03160	    COMMENT: This procedure inputs a line from the input text file.
03200	    The input line is stripped into the text "editin_image_strip".
03240	    If the input line is the first line on a new page, then the
03280	    BOOLEAN "top_of_page" becomes TRUE. Page delimiter mark is
03320	    removed from the input line;
03360	  INSPECT editin DO
03400	  BEGIN
03440	    IF first_line_read THEN first_line_read:= FALSE ELSE
03480	    BEGIN
03520	      getline:
03560	      top_of_page:= top_of_page OR got_formfeed;
03600	      inimage;
03640	    END;
03680	    editin_image_strip:- image.strip;
03720	    got_formfeed:= IF editin_image_strip == NOTEXT
03760	    THEN FALSE ELSE fetchar(editin_image_strip,
03800	    editin_image_strip.length) = formfeed;
03840	    IF got_formfeed THEN
03880	    BEGIN
03920	      COMMENT remove form feed from input line;
03960	      editin_image_strip:- editin_image_strip.sub
04000	      (1,editin_image_strip.length-1);
04040	COMMENT bypass input lines containing nothing put a proper
04080	page delimiter mark;
04120	      IF editin_image_strip == NOTEXT THEN GOTO getline;
04160	      IF editin_image_strip = five_sp THEN GOTO getline;
04200	      IF editin_image_strip = five_sp_tab THEN GOTO getline;
04240	    END;
04280	  END;
     
04320	  PROCEDURE editoutimage(t); TEXT t;
04360	
04400	    COMMENT: This procedure outputs the parameter text "t" as a line
04440	    for the output file. If the BOOLEAN "line_numbered" is TRUE, but
04480	    "t" does not contain a correct line number, then a number is
04520	    added or an incorrect number is replaced.
04560	
04600	    IF the BOOLEAN "top_of_page" is TRUE, then a proper page mark is
04640	    inserted into the output file in front of the output line, and
04680	    "top_of_page" is made FALSE at the same time;
04720	
04760	  BEGIN
04800	    t:- t.strip;
04840	    IF line_numbered THEN
04880	    BEGIN
04920	      IF top_of_page THEN
04960	      BEGIN COMMENT output proper page mark;
05000	        editout.image:- editout_image;
05040	        editout.image.setpos(6);
05080	        editout.outchar(carriagereturn);
05120	        editout.outchar(formfeed);
05160	        editout.breakoutimage; top_of_page:= FALSE;
05200	      END;
05240	      IF five_digits(t) THEN
05280	      BEGIN
05320	        this_line_number:= t.sub(1,5).getint;
05360	        IF t.length = 5 THEN GOTO goodnumbered;
05400	        IF fetchar(t,6) = tab THEN GOTO goodnumbered;
05440	      END;
05480	COMMENT The line had no line number, concatate line number
05520	in front of the line;
05560	      last_line_number:= last_line_number+1;
05600	      editout.image:- editout_image;
05640	      editout.outtext(make_five_digits( last_line_number));
05680	      editout.outchar(tab); editout.outtext(t);
05720	      IF FALSE THEN goodnumbered:
05760	      BEGIN
05800	        COMMENT The line began with a line number;
05840	        IF this_line_number <= last_line_number THEN
05880	        BEGIN
05920	    COMMENT: The line number was lower than on the previous line,
05960	    a higher line number is substituted;
06000	          last_line_number:= last_line_number+1;
06040	          t.sub(1,5):= make_five_digits(last_line_number);
06080	        END ELSE last_line_number:= this_line_number;
06120	        editout.image:- t;
06160	      END;
06200	      editout.outimage;
06240	    END ELSE
06280	    BEGIN COMMENT the output file should NOT be line numbered;
06320	      IF top_of_page THEN
06360	      BEGIN COMMENT Output proper page mark;
06400	        editout.image:- editout_image;
06440	        editout.outchar(formfeed);
06480	        editout.breakoutimage; top_of_page:= FALSE;
06520	      END;
06560	      editout.image:- t; editout.outimage;
06600	    END;
06640	  END;
     
06680	    COMMENT initialize constants and dummy variables used by the
06720	    program;
06760	
06800	  carriagereturn:= char(13); formfeed:= char(12);
06840	  tab:= char(9);
06880	
06920	  sixdigits:- blanks(6); five_sp:- blanks(5);
06960	  five_sp_tab:- blanks(6); five_sp_tab.setpos(5);
07000	  five_sp_tab.putchar(tab);
07040	END of class edit;
     
07080	filed BEGIN
07120	    COMMENT: This section is usually replaced by your own code,
07160	    which may for example contain more advanced interpretation of
07200	    input from the user terminal using the DECOM, SCAN, SCAN2 och
07240	    SAFEIO utility packages;
07280	
07320	  TEXT inf, outf, backf, command, after_number;
07440	  sysout.image:- blanks(140);
07480	  outtext("FILED - Inputting and outputting a DEC10-formatted"
07520	  " file - version 78-07:");
07560	  outimage;
07600	
07640	  COMMENT Read initial command string;
07680	  IF rescan THEN
07720	  BEGIN
07760	    inimage; IF sysin.endfile THEN GOTO finalexit;
07800	    command:- sysin.image; scanto(command,'-');
07840	    command:- from(command,command.pos).strip;
07880	    IF command == NOTEXT THEN GOTO prompter;
07920	  END ELSE prompter:
07960	  BEGIN
08000	    outchar('>'); breakoutimage;
08040	    IF sysin.endfile THEN GOTO finalexit;
08055	    COMMENT-------------------------------------------;
08068	    COMMENT here insert any zeroing of data necessary
08081	    after a faulty command string;
08107	    COMMENT-------------------------------------------;
08120	    backf:-
08160	    inf:- outf:- NOTEXT;
08200	    inimage; IF sysin.endfile THEN GOTO finalexit;
08240	    command:- compress(storbokstav(sysin.image.strip),' ');
08280	  END;
08320	  IF command == NOTEXT THEN
08360	  BEGIN outtext("Type ? for help."); outimage;
08400	    GOTO prompter;
08440	  END;
08480	  IF command.getchar = '?' THEN
08520	  BEGIN
08560	    outtext(
08600	    "help message is to be output here");
08640	    outimage;
08680	    outimage;
08720	    GOTO prompter;
08760	  END;
08800	  command:- compress(storbokstav(command),' '); command.setpos(1);
08840	  outf:- copy(scanto(command,'=').strip);
08880	  inf:- copy(scanto(command,'/').strip);
08920	  IF inf == NOTEXT THEN
08960	  BEGIN inf:- outf;
09000	    outtext("Assumming input file = "); outtext(inf); outimage;
09040	    outtext("Give output file name: "); outimage;
09080	    outchar('>'); breakoutimage;
09120	    inimage; command:- compress(storbokstav(sysin.image),' ');
09160	    outf:- copy(scanto(command,'/').strip);
09200	  END;
09240	  outf.setpos(1); inf.setpos(1);
09280	  IF scanto(outf,'/')  = scanto(inf,'/') THEN
09320	  BEGIN
09360	    outtext("?FILED - infile = outfile."); outimage;
09400	    GOTO prompter;
09440	  END;
09480	
09520	  BEGIN
09560	    COMMENT Insert code here to interpret switches in the input
09600	    command string, e.g. using SCAN or SSCAN from LIBSIM;
09640	  END;
09680	
09720	  COMMENT Opening of the input text file. You may prefer longer
09760	  than 80 characters in the images, will occur if input text
09800	  lines are longer than the image size;
09840	  editin:- findinfile(inf);
09880	  IF editin == NONE THEN
09920	  BEGIN outtext("?FILED - cannot find input file: "); outtext(inf);
09960	    outimage; GOTO prompter;
10000	  END;
10040	  editin.open(blanks(140));
10080	  COMMENT First line must be read here to check if the input file is
10120	  line numbered;
10160	  editin.inimage; first_line_read:= TRUE;
10200	  line_numbered:= numbered;
10240	
10280	  INSPECT findinfile(outf) DO
10320	  BEGIN
10360	    backf:- copy(outf);
10400	    IF scanto(backf,'.') == backf THEN backf:- conc(backf,".Q")
10440	    ELSE IF backf.more THEN backf.putchar('Q') ELSE
10480	    backf:- conc(backf,"Q");
10520	    outtext("%FILED renaming previous output file:");
10560	    outimage; outchar('"');
10600	    outtext(outf); outtext(""" to backup file name """);
10640	    outtext(backf); outtext("""."); outimage;
10680	    IF rename(backf,NOTEXT,TRUE) > 0 THEN
10720	    BEGIN outtext("%FILED - cannot delete file: "); outtext(backf);
10760	      outimage; GOTO prompter;
10800	    END;
10840	    IF rename(THIS infile,backf,FALSE) > 0 THEN
10880	    BEGIN outtext("%FILED - cannot rename file """);
10920	      outtext(outf); outtext(""" to backup name """);
10960	      outtext(backf); outtext("""."); outimage;
11000	      GOTO prompter;
11040	    END;
11080	  END;
11120	
11160	  COMMENT Opening of the output text file;
11200	  COMMENT The output file is made line numbered if the input file was
11240	  line numbered. You may prefer to add other options;
11280	  IF line_numbered THEN outf:- conc(outf,"/NUMBERED");
11320	  editout:- findoutfile(outf);
11360	  IF editout == NONE THEN
11400	  BEGIN outtext("?FILED - cannot open output file: "); outtext(outf);
11440	    outimage; GOTO prompter;
11480	  END;
11520	  editout_image:- blanks(140); editout.open(editout_image);
11560	
11600	  after_number:- editin.image;
11640	  IF line_numbered THEN after_number:- after_number.sub(7,133);
11680	  WHILE TRUE DO
11720	  BEGIN COMMENT Loop of reading successive lines in the file;
11760	    editinimage;
11800	    IF editin.endfile THEN GOTO close;
11840	    after_number.setpos(1);
11880	    COMMENT --------------------------------------------------
11920	    ! Here insert code to do something with the input line   !
11960	    ! "after_number" contains the input line without number  !
12000	    ! editin.image the whole line, editing_image_strip the   !
12040	    ! stripped line.                                         !
12080	    ----------------------------------------------------------;
12120	    editoutimage(editin_image_strip);
12160	    after_print:
12200	  END;
12240	  close:
12280	  editin.close;
12320	  editout.close;
12360	  finalexit:
12400	END;
12440	END of the whole FILED program;