home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / O3 < prev    next >
Encoding:
Text File  |  1992-12-09  |  10.5 KB  |  254 lines

  1. { (C) Copyright  1986-1992 MetaWare Incorporated;  Santa Cruz, CA 95060. }
  2.  
  3. {
  4.    Portable I/O Routines as Adapted to MetaWare Pascal
  5. }
  6.  
  7. pragma c_include('filep.pf');
  8. pragma c_include('fileh.pf');
  9. pragma c_include('implement.pf');
  10. pragma C_include('SEEK.pf');
  11.  
  12. package Portio;
  13. pragma Routine_aliasing_convention(Implement.RTE_ALIASING);
  14. with Loopholes:[Address];
  15. with Filep_type Transmitted;
  16. with Seek_method_type Transmitted;
  17. with Implement:[Byte_count] Transmitted;
  18. with Implement;
  19. var
  20.    stdin,                  {Standard input}
  21.    stdout,           {Standard output}
  22.    stderr: Filep;          {Standard error file}
  23.    IO_count: Byte_count;   {Number of bytes read on last call to Fread}
  24.    Line_incomplete: Boolean; {Set by Fgetline/Getline if EOL not encountered}
  25.    BUFSIZE: Byte_count; -- Default buffer size:  512.  
  26.                -- File opens use this to allocate a buffer.
  27.                -- Use powers of two for efficiency.
  28.    stdstrg: Filep;      -- We use this to implement readstr/writestr in the RTL.
  29.  
  30. const
  31.    EOFC = Chr(255);        {End of file character indicator for fgetc}
  32.    EOFW = -1;              {End of file character for fgetw}
  33.    
  34. type Real = Standard.Real;      -- In case of VS Pascal, where real is redefined.
  35.  
  36. #if 0
  37.     Fopen - opens a file and returns a descriptor for it.
  38.         Fname is the name of the file
  39.         Mode is a sequence of one or more characters with the following
  40.         meaning:
  41.           'w' = for writing
  42.               'r' = for reading
  43.               'a' = appending (mutually exclusive with 'w')
  44.               'u' = unbufferred
  45.         Note: if the file is opened for both reading and writing ('rw'), then
  46.               unbufferred will be assumed.
  47.         If any character appears in the mode string other than those 
  48.         mentioned above, then Error_invalid_function will be returned in the
  49.         imported variable Errno.
  50. #endif
  51.  
  52. function Fopen(const Fname: String; const Mode: String): Filep; external;
  53.  
  54. {
  55.     Fclose - closes the specified file.
  56. }
  57. procedure Fclose(FP: Filep);                                    external;
  58.  
  59. {
  60.    Fgetc - returns the next character from the input stream.
  61.            EOFC is returned if EOF is encountered; however, since this is
  62.        a perfectly valid (non-ASCII) character, Feof should be called
  63.        to confirm true end-of-file.
  64. }
  65. function Fgetc(FP: Filep): Char;                external;
  66. function Getc(): Char;                                          external;
  67.    {Equivalent to Fgetc(Default_input)}
  68.  
  69. procedure Fflush(FP: Filep);                                    external;
  70.    {Flushes the buffer of a buffered output file}
  71. procedure Flush();                        external;
  72.     {Equivalent to Fflush(Default_output)}
  73. {
  74.    Fputc - writes a character to a file.
  75. }
  76. procedure Fputc(FP: Filep; C: Char);                external;
  77. procedure Putc(C: Char);                                        external;
  78.    {Equivalent to Fputc(Default_output,C)}
  79.  
  80. {
  81.    Fgetw - reads one 2-byte word from file. If end of file occurs, then
  82.            the function returns EOFW. However, since this value is a valid
  83.        integer, Feof should be confirmed to verify true end-of-file.
  84. }
  85. function Fgetw(FP: Filep): Integer;                             external;
  86. function Getw(): Integer;                    external;
  87.  
  88. {
  89.    Fputw - writes one word.
  90. }
  91. procedure Fputw(FP: Filep; I: Integer);                           external;
  92. procedure Putw(I: Integer);                    external;
  93.  
  94. {
  95.    Fread - reads "Len" number of bytes into buffer
  96.            Number of bytes actually read returned in IO_count
  97. }
  98. procedure Fread(FP: Filep; BP: Address; Bytes: Byte_count);external;
  99.    
  100. {
  101.    Fwrite - writes "Len" bytes of data from Buf into "fp"
  102. }
  103. procedure Fwrite(FP: Filep; BP: Address; Bytes: Byte_count);      external;
  104.     
  105. procedure Fseek(FP: Filep; Offset: Longint; Method: Seek_method);external;
  106. {The location of the next byte in the stream FP is adjusted.}
  107.  
  108. function Filepos(FP: Filep): Longint;                external;
  109. { Returns the byte position of the next byte to be read/written. }
  110. {  The position is origined such that 0 references the beginning of the file}
  111.  
  112. function Feof(fp: Filep): Boolean;                         external;
  113. {Returns true if FP is positioned at the end of the file}
  114.  
  115. function Getchar(): Char;                       external;
  116.   {Same as Fgetc(Stdin)}
  117.  
  118. procedure Putchar(C: Char);                    external;
  119.   {Same as Fputc(Stdout,C)}
  120.  
  121. procedure Fgets(fp: Filep; var S: String);                     external;
  122.   {Read bytes into a string until end-of-line or until string filled to max}
  123. procedure Gets(var S:String);                    external;
  124.   {Equivalent to Fgets(Default_input,S) }
  125.  
  126. function Fgetline(FP: Filep; BP: Address; Bytes: Cardinal): Integer; external;
  127. function Getline(BP: Address; Bytes: Cardinal): Integer; external;
  128.   {Reads bytes into a buffer until end-of-line encountered. The length}
  129.   {of the line is returned. A value of -1 indicates end-of-file.    }
  130.   {The line terminator is not included in the length returned.        }
  131.   {For this reason, 0 is a valid line length.                }
  132.   {If there are more bytes on the line than Bytes, the boolean variable    }
  133.   {"Line_incomplete" (defined above) is set to true.            }
  134.  
  135. procedure Fputs(fp: Filep; const S: String);                    external;
  136. procedure Puts(const S: String);                external;
  137.   {Write contents of string}
  138.  
  139. procedure Fprintl(fp: Filep; I: Longint);                       external;
  140. procedure Printl(I: Longint);                              external;
  141.   {Print longint I in minimum field (no padding)}
  142.  
  143. procedure Fprinti(fp: Filep; I: Integer);                       external;
  144. procedure Printi(I:Integer);                                   external;
  145.   {Print single word integer I}
  146. procedure FPrintu(fp: Filep; V: Cardinal);                      external;
  147. procedure Printu(V: Cardinal);                           external;
  148.   {Print unsigned word integer}
  149.  
  150. procedure FPrintll(fp: Filep; I: Longint; Len: Integer); external;
  151. procedure Printll(I: Longint; Len: Integer); external;
  152.   {Print longint I in a field of minimum length |Len|. If Len > 0 then
  153.    I will be right justified in the field. If Len < 0 then I will be left
  154.    justified}
  155.  
  156. procedure Fprintllr(fp: Filep; I: Longint; Len,Base: Integer);  external;
  157. procedure Printllr(I:Longint; Len,Base: Integer); external;
  158.   {Prints integer I in base |Base| in a field of at least |Len| characters}
  159.   {   Base must be within -16..16. A negative base will cause leading }
  160.   {   zeroes to be printed.                                           }
  161.  
  162. procedure Printr(R:real; FW, After_dot: Integer);external;
  163. procedure Fprintr(F:filep; R:real; FW, After_dot:Integer);external;
  164. #if 0
  165. Print real number R in FW:After_dot format, right-adjusted in field
  166. of at least FW (more chrs may be used.)  Format is one of:
  167. After_dot < 0:    (-/space) Digit.Digit More_digits e +/- Digit Digit
  168. After_dot >= 0:    (- if negative) Digits . Digits
  169. where there are After_dot digits after . (no . if After_dot = 0).
  170. If FW is too small in latter case, first format is used.
  171. #endif
  172. procedure Printd(R:longreal; FW, After_dot: Integer);external;
  173. procedure Fprintd(F:filep; R:longreal; FW, After_dot:Integer);external;
  174.   { Print longreal. }
  175. procedure Printx(R:extreal; FW, After_dot: Integer);external;
  176. procedure Fprintx(F:filep; R:extreal; FW, After_dot:Integer);external;
  177.   { Print extreal. }
  178.  
  179. procedure Ungetc(C: Char);                        external;
  180. procedure FUngetc(Fp: Filep; C: Char);                        external;
  181.   {Inserts 'C' back into input buffer so that subsequent Fgetc will reread it}
  182.   {Note: Guaranteed only to work once prior to calling Fgetc or Getchar}
  183.  
  184. procedure Fputtext(Fp: Filep; BP: Address; Len: Cardinal; Field_width: Integer);
  185.                                 external;
  186. procedure Puttext(BP:Address; Len: cardinal; Field_width: INteger); external;
  187.    {Writes out an array of "Len" characters in a field of length }
  188.    {  Abs(Field_width). If field_width < 0 then the chars will be }
  189.    {  left justified instead of right justified                  }
  190.  
  191. procedure Fnewline(Fp: Filep);                             external;
  192. procedure Newline();                       external;
  193.    {Terminates the current line.   }
  194.    {Depending on the host system, this routine will either write out a}
  195.    {   CR followed by LF, a LF only, or terminate the current logical record}
  196.  
  197. function Fscani(Fp: Filep; Len: Cardinal): Longint;      external;
  198. function Scani(Len: Cardinal): Longint;            external;
  199.    {Reads decimal integer from an Ascii file. Leading blanks are skipped}
  200.    {Len is the number of bytes to scan. If Len=0 then leading blanks and}
  201.    {  CR/LF will be skipped until '+', '-', or digit detected; the bytes}
  202.    { are then scanned until a non-digit is detected.                    }
  203. function Scanr(Len: Cardinal):real;            external;
  204. function Fscanr(FP: Filep; Len: cardinal):real;        external;
  205.    {Like Fscani, but reads a real number.                }
  206. function Scand(Len: Cardinal):Extreal;    external;
  207. function Fscand(FP: Filep; Len: cardinal):Extreal;    external;
  208.    {Like Fscanr, but reads a long real.        }
  209.  
  210. procedure Fclose_all;                       external;
  211.    {Closes all open files that where opened with Fopen}
  212.  
  213. procedure FStackDump(F: Filep); external;
  214.    {Produce stack dump of currently active procedures.    }
  215.  
  216. procedure Set_default_input(F: Filep);             external;
  217.    {Sets a new default input file}
  218.  
  219. procedure Set_default_output(F: Filep);         external;
  220.    {Sets a new default output}
  221.  
  222. function Default_input(): Filep;            external;
  223.    {Returns current default input}
  224.  
  225. function Default_output(): Filep;            external;
  226.    {Returns current default output}
  227.  
  228. procedure Restore_default_output();            external;
  229.    {Restores default output prior to last call to "Set_default_output"}
  230.  
  231. procedure Restore_default_input();                      external;
  232.    {Restores default output prior to last call to "Set_default_output"}
  233.  
  234. function Handle(F: Filep): Fileh_type.File_handle;        external;
  235.     {Returns DOS file handle associated with F}
  236.  
  237. procedure Make_buffered(F: Filep);                external;
  238. procedure Make_unbuffered(F: Filep);                external;
  239.  
  240. type Temp_file_name = string(20);    -- Handles most OSs.
  241. { Make a unique temporary name, typically used for temp files.    }
  242. function Make_temp(const Prefix:String):Temp_file_name;        external;
  243.  
  244. pragma Alias(FStackDump,RTE || 'stackdump'); 
  245.    {Alias so that StackDump(T:Text) in Debug_aids is same routine.    }
  246. pragma Alias(Set_default_input,RTE || 'sdefin');
  247. pragma Alias(Set_default_output,RTE || 'sdefout');
  248. pragma Alias(Default_input,RTE || 'defin');
  249. pragma Alias(Default_output,RTE || 'defout');
  250. pragma Alias(Restore_default_output,RTE || 'rdefout');
  251. pragma Alias(Restore_default_input,RTE || 'rdefin');
  252. end;
  253. pragma alias(Portio,Implement.RTE || 'portio');
  254.