home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / UTIL / SRC / PPMISC.MOD < prev   
Text File  |  1996-08-03  |  12KB  |  344 lines

  1. IMPLEMENTATION MODULE PPMisc;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*      Miscellaneous procedures for preprocessor.      *)
  6.         (*                                                      *)
  7.         (*      The purpose of this module is to collect        *)
  8.         (*      together the compiler-dependent of the          *)
  9.         (*      preprocessor.  The module consists mostly       *)
  10.         (*      of file and string operations.                  *)
  11.         (*                                                      *)
  12.         (*  Programmer:         P. Moylan                       *)
  13.         (*  Last edited:        3 August 1996                   *)
  14.         (*  Status:             Working with FST, TopSpeed 1.17 *)
  15.         (*                        TopSpeed 3.10, and XDS        *)
  16.         (*                                                      *)
  17.         (*  Rowley version is untested and probably wrong.      *)
  18.         (*                                                      *)
  19.         (********************************************************)
  20.  
  21. (*<FST
  22. IMPORT System, InOut, FileSystem, Directories, PPTextIO, Strings;
  23. >*)
  24.  
  25. (*<Rowley
  26. IMPORT Environment, InOut, FileSystem, Directories, TextIO, Strings;
  27. >*)
  28.  
  29. (*<TopSpeed
  30. IMPORT SYSTEM, FIO, IO, Lib, Str;
  31. >*)
  32.  
  33. (*<XDS*)
  34. IMPORT SYSTEM, Strings, IOChan, STextIO, ProgramArgs, TextIO, FileSys,
  35.        StreamFile, IOConsts;
  36. (*>*)
  37.  
  38. (************************************************************************)
  39.  
  40. (*<TopSpeed
  41.  
  42. (* Warning: the code used for TopSpeed allocates file buffers on the    *)
  43. (* assumption that we never have more than one input file and one       *)
  44. (* output file open at any one time.                                    *)
  45.  
  46. VAR InFileBuffer, OutFileBuffer: ARRAY [1..2048+FIO.BufferOverhead] OF BYTE;
  47. >*)
  48.  
  49. (*<XDS*) VAR argchannel: IOChan.ChanId; (*>*)
  50.  
  51. (************************************************************************)
  52. (*                         STRING OPERATIONS                            *)
  53. (************************************************************************)
  54.  
  55. PROCEDURE Length (s: ARRAY OF CHAR): CARDINAL;
  56.  
  57.     (* Returns the length of string s. *)
  58.  
  59.     BEGIN
  60.         RETURN (*<TopSpeed Str.Length(s); >*)
  61.                (*<FST|Rowley|XDS*) Strings.Length(s); (*>*)
  62.     END Length;
  63.  
  64. (************************************************************************)
  65.  
  66. PROCEDURE StringMatch (s1, s2: ARRAY OF CHAR): BOOLEAN;
  67.  
  68.     (* Returns TRUE iff the two strings are equal. *)
  69.  
  70.     BEGIN
  71.         (*<FST RETURN (Strings.CompareStr (s1, s2) = 0); >*)
  72.         (*<TopSpeed RETURN (Str.Compare (s1, s2) = 0); >*)
  73.         (*<Rowley RETURN (Strings.Compare (s1, s2) = 0); >*)
  74.         (*<XDS*) RETURN (Strings.Compare (s1, s2) = Strings.equal); (*>*)
  75.     END StringMatch;
  76.  
  77. (************************************************************************)
  78.  
  79. PROCEDURE CopyString (src: ARRAY OF CHAR; VAR (*OUT*) dst: ARRAY OF CHAR);
  80.  
  81.     (* Copies src to dst. *)
  82.  
  83.     BEGIN
  84.         (*<FST|Rowley|XDS*) Strings.Assign (src, dst); (*>*)
  85.         (*<TopSpeed Str.Copy (dst, src); >*)
  86.     END CopyString;
  87.  
  88. (************************************************************************)
  89.  
  90. PROCEDURE Pos (pattern, string: ARRAY OF CHAR): CARDINAL;
  91.  
  92.     (* If pattern is a substring of string, returns the index in        *)
  93.     (* string of the start of the first occurrence of pattern.  If      *)
  94.     (* no match then the value returned is beyond the end of string.    *)
  95.  
  96.     (*<XDS*) VAR found: BOOLEAN;  position: CARDINAL; (*>*)
  97.  
  98.     BEGIN
  99.         (*<FST|Rowley RETURN Strings.Pos (pattern, string); >*)
  100.         (*<TopSpeed
  101.         (* A trap for the unwary: note the order of the parameters. *)
  102.         RETURN Str.Pos (string, pattern);
  103.         >*)
  104.         (*<XDS*)
  105.         Strings.FindNext (pattern, string, 0, found, position);
  106.         IF found THEN RETURN position
  107.         ELSE RETURN Strings.Length(string)+1;
  108.         END (*IF*);
  109.         (*>*)
  110.     END Pos;
  111.  
  112. (************************************************************************)
  113. (*                              TERMINAL I/O                            *)
  114. (************************************************************************)
  115.  
  116. PROCEDURE Message (mess: ARRAY OF CHAR);
  117.  
  118.     (* Writes a string to the screen. *)
  119.  
  120.     BEGIN
  121.         (*<FST|Rowley InOut.WriteString (mess); >*)
  122.         (*<TopSpeed IO.WrStr (mess); >*)
  123.         (*<XDS*) STextIO.WriteString (mess); (*>*)
  124.     END Message;
  125.  
  126. (************************************************************************)
  127.  
  128. PROCEDURE EndOfMessage;
  129.  
  130.     (* Goes to the next line on the screen. *)
  131.  
  132.     BEGIN
  133.         (*<FST|Rowley InOut.WriteLn; >*)
  134.         (*<TopSpeed IO.WrLn; >*)
  135.         (*<XDS*) STextIO.WriteLn; (*>*)
  136.     END EndOfMessage;
  137.  
  138. (************************************************************************)
  139.  
  140. PROCEDURE CommandLine (argNr: CARDINAL;  VAR (*OUT*) strg : ARRAY OF CHAR;
  141.                                         VAR (*OUT*) arglen : CARDINAL);
  142.  
  143.     (* Picks up argument number argNr from the command line.  If there  *)
  144.     (* is no such argument then arglen is returned as zero.             *)
  145.  
  146. (*<FST
  147.     BEGIN
  148.         System.GetArg (strg, arglen);
  149.     END CommandLine;
  150. >*)
  151.  
  152. (*<TopSpeed
  153.     BEGIN
  154.         Lib.ParamStr (strg, argNr);
  155.         arglen := Str.Length (strg);
  156.     END CommandLine;
  157. >*)
  158.  
  159. (*<XDS*)
  160.     BEGIN
  161.         (* In this version argNr is ignored, and we assume that the arguments   *)
  162.         (* are read in order.  I could improve this, but for the PP application *)
  163.         (* this is good enough.                                                 *)
  164.         IF ProgramArgs.IsArgPresent() THEN
  165.             TextIO.ReadString (argchannel, strg);
  166.             arglen := Strings.Length (strg);
  167.             ProgramArgs.NextArg;
  168.         ELSE
  169.             Strings.Assign ("", strg);
  170.             arglen := 0;
  171.         END (*IF*);
  172.     END CommandLine;
  173. (*>*)
  174.  
  175. (*<Rowley
  176.     VAR OK : BOOLEAN;
  177.     BEGIN
  178.         Environment.GetArg (argNr, strg, OK);
  179.         IF NOT OK THEN
  180.             arglen := 0;
  181.         ELSE
  182.             arglen := Strings.Length (strg);
  183.         END;
  184.     END CommandLine;
  185. >*)
  186.  
  187. (************************************************************************)
  188. (*                           FILE OPERATIONS                            *)
  189. (************************************************************************)
  190.  
  191. PROCEDURE OpenFile (VAR (*OUT*) f: File;  filename: ARRAY OF CHAR;
  192.                                                 create: BOOLEAN): BOOLEAN;
  193.  
  194.     (* Opens the file specified as "filename".  We open it for input if *)
  195.     (* create=FALSE, or create a new file for output if create=TRUE.    *)
  196.     (* The function result indicates success.                           *)
  197.  
  198.     (*<TopSpeed VAR temp: BOOLEAN; >*)
  199.     (*<XDS*) VAR status: StreamFile.OpenResults; (*>*)
  200.  
  201.     BEGIN
  202. (*<TopSpeed
  203.         temp := FIO.IOcheck;  FIO.IOcheck := FALSE;
  204.         IF create THEN
  205.             f := FIO.Create (filename);
  206.         ELSE
  207.             f := FIO.Open (filename);
  208.         END (*IF*);
  209.         FIO.IOcheck := temp;
  210.         IF f < MAX(CARDINAL) THEN
  211.             IF create THEN
  212.                 FIO.AssignBuffer (f, OutFileBuffer);
  213.             ELSE
  214.                 FIO.AssignBuffer (f, InFileBuffer);
  215.             END (*IF*);
  216.             RETURN TRUE;
  217.         ELSE
  218.             RETURN FALSE;
  219.         END (*IF*);
  220. >*)
  221. (*<XDS*)
  222.         IF create THEN
  223.             StreamFile.Open (f, filename, StreamFile.write, status);
  224.         ELSE
  225.             StreamFile.Open (f, filename, StreamFile.read, status);
  226.         END (*IF*);
  227.         RETURN status = StreamFile.opened;
  228. (*>*)
  229. (*<FST|Rowley
  230.         FileSystem.Lookup (f, filename, create);
  231.         RETURN (f.res = FileSystem.done);
  232. >*)
  233.     END OpenFile;
  234.  
  235. (************************************************************************)
  236.  
  237. PROCEDURE CloseFile (VAR (*INOUT*) f: File);
  238.  
  239.     (* Closes file f. *)
  240.  
  241.     BEGIN
  242.         (*<FST|Rowley FileSystem.Close(f); >*)
  243.         (*<TopSpeed FIO.Close(f); >*)
  244.         (*<XDS*) StreamFile.Close(f); (*>*)
  245.     END CloseFile;
  246.  
  247. (************************************************************************)
  248.  
  249. PROCEDURE EndOfFile (VAR (*INOUT*) f: File): BOOLEAN;
  250.  
  251.     (* Returns TRUE iff the end of file f has been reached.             *)
  252.     (* Remark: in the TopSpeed case the library procedure FIO.EOF is    *)
  253.     (* inadequate for this purpose, because (a) it is only updated      *)
  254.     (* after a read, and (b) there is no feedback about WHICH file has  *)
  255.     (* reached its end.                                                 *)
  256.  
  257.     (* The ISO libraries have the same fault; but for the present       *)
  258.     (* application it probably doesn't matter.                          *)
  259.  
  260.     BEGIN
  261.         (*<FST|Rowley RETURN f.eof; >*)
  262.         (*<TopSpeed RETURN FIO.GetPos(f) >= FIO.Size(f); >*)
  263.         (*<XDS*) RETURN IOChan.ReadResult(f) = IOConsts.endOfInput; (*>*)
  264.     END EndOfFile;
  265.  
  266. (************************************************************************)
  267.  
  268. PROCEDURE WriteToFile (VAR (*INOUT*) f: File;
  269.                                 VAR (*IN*) str: ARRAY OF CHAR);
  270.  
  271.     (* Writes a text string to file f. *)
  272.  
  273.     BEGIN
  274.         (*<FST PPTextIO.WriteString (f, str); >*)
  275.         (*<Rowley|XDS*) TextIO.WriteString (f, str); (*>*)
  276.         (*<TopSpeed FIO.WrStr (f, str); >*)
  277.     END WriteToFile;
  278.  
  279. (************************************************************************)
  280.  
  281. PROCEDURE TerminateLine (VAR (*INOUT*) f: File);
  282.  
  283.     (* Writes an end-of-line to file f. *)
  284.  
  285.     BEGIN
  286.         (*<FST PPTextIO.WriteLn (f); >*)
  287.         (*<Rowley|XDS*) TextIO.WriteLn (f); (*>*)
  288.         (*<TopSpeed FIO.WrLn (f); >*)
  289.     END TerminateLine;
  290.  
  291. (************************************************************************)
  292.  
  293. PROCEDURE ReadLine (VAR (*INOUT*) f: File;  VAR (*OUT*) strg: ARRAY OF CHAR);
  294.  
  295.     (* Reads a line of text from file f. *)
  296.  
  297.     BEGIN
  298.         (*<FST PPTextIO.ReadString (f,strg); >*)
  299.         (*<Rowley TextIO.ReadString (f,strg);  TextIO.ReadLn (f); >*)
  300.         (*<TopSpeed FIO.RdStr (f,strg); >*)
  301.         (*<XDS*) TextIO.ReadString (f,strg);  TextIO.SkipLine (f); (*>*)
  302.     END ReadLine;
  303.  
  304. (************************************************************************)
  305.  
  306. PROCEDURE DeleteFile (name: ARRAY OF CHAR);
  307.  
  308.     (* Deletes a file, if it exists.  The file must not be open. *)
  309.  
  310.     (*<XDS*) VAR done: BOOLEAN; (*>*)
  311.  
  312.     BEGIN
  313.         (*<FST|Rowley
  314.         Directories.Delete(name);
  315.         >*)
  316.         (*<TopSpeed
  317.         IF FIO.Exists(name) THEN FIO.Erase(name); END (*IF*);
  318.         >*)
  319.         (*<XDS*)
  320.         IF FileSys.Exists(name) THEN FileSys.Remove(name, done); END (*IF*);
  321.         (*>*)
  322.     END DeleteFile;
  323.  
  324. (************************************************************************)
  325.  
  326. PROCEDURE RenameFile (originalname, newname: ARRAY OF CHAR);
  327.  
  328.     (* Renames an existing file.  The file should not be open. *)
  329.  
  330.     (*<XDS*) VAR done: BOOLEAN; (*>*)
  331.  
  332.     BEGIN
  333.         (*<FST|Rowley Directories.Rename(originalname, newname); >*)
  334.         (*<TopSpeed FIO.Rename(originalname, newname); >*)
  335.         (*<XDS*) FileSys.Rename(originalname, newname, done); (*>*)
  336.     END RenameFile;
  337.  
  338. (************************************************************************)
  339.  
  340. BEGIN
  341.     (*<XDS*) argchannel := ProgramArgs.ArgChan(); (*>*)
  342. END PPMisc.
  343.  
  344.