| 1 |
(* FIO.mod provides a simple buffered file input/output library. |
| 2 |
|
| 3 |
Copyright (C) 2001-2023 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
IMPLEMENTATION MODULE FIO ; |
| 28 |
|
| 29 |
(* |
| 30 |
Title : FIO |
| 31 |
Author : Gaius Mulley |
| 32 |
System : UNIX (gm2) |
| 33 |
Date : Thu Sep 2 22:07:21 1999 |
| 34 |
Last edit : Thu Sep 2 22:07:21 1999 |
| 35 |
Description: a complete reimplememtation of FIO.mod |
| 36 |
provides a simple buffered file input/output library. |
| 37 |
*) |
| 38 |
|
| 39 |
FROM SYSTEM IMPORT ADR, TSIZE, SIZE, WORD ; |
| 40 |
FROM ASCII IMPORT nl, nul, tab ; |
| 41 |
FROM StrLib IMPORT StrLen, StrConCat, StrCopy ; |
| 42 |
FROM Storage IMPORT ALLOCATE, DEALLOCATE ; |
| 43 |
FROM NumberIO IMPORT CardToStr ; |
| 44 |
FROM libc IMPORT exit, open, creat, read, write, close, lseek, strncpy, memcpy ; |
| 45 |
FROM Indexing IMPORT Index, InitIndex, InBounds, HighIndice, PutIndice, GetIndice ; |
| 46 |
FROM M2RTS IMPORT InstallTerminationProcedure ; |
| 47 |
|
| 48 |
CONST |
| 49 |
SEEK_SET = 0 ; (* relative from beginning of the file *) |
| 50 |
SEEK_END = 2 ; (* relative to the end of the file *) |
| 51 |
UNIXREADONLY = 0 ; |
| 52 |
UNIXWRITEONLY = 1 ; |
| 53 |
CreatePermissions = 666B; |
| 54 |
MaxBufferLength = 1024*16 ; |
| 55 |
MaxErrorString = 1024* 8 ; |
| 56 |
|
| 57 |
TYPE |
| 58 |
FileUsage = (unused, openedforread, openedforwrite, openedforrandom) ; |
| 59 |
FileStatus = (successful, outofmemory, toomanyfilesopen, failed, connectionfailure, endofline, endoffile) ; |
| 60 |
|
| 61 |
NameInfo = RECORD |
| 62 |
address: ADDRESS ; |
| 63 |
size : CARDINAL ; |
| 64 |
END ; |
| 65 |
|
| 66 |
Buffer = POINTER TO buf ; |
| 67 |
buf = RECORD |
| 68 |
valid : BOOLEAN ; (* are the field valid? *) |
| 69 |
bufstart: LONGINT ; (* the position of buffer in file *) |
| 70 |
position: CARDINAL ; (* where are we through this buffer *) |
| 71 |
address : ADDRESS ; (* dynamic buffer address *) |
| 72 |
filled : CARDINAL ; (* length of the buffer filled *) |
| 73 |
size : CARDINAL ; (* maximum space in this buffer *) |
| 74 |
left : CARDINAL ; (* number of bytes left to read *) |
| 75 |
contents: POINTER TO ARRAY [0..MaxBufferLength] OF CHAR ; |
| 76 |
END ; |
| 77 |
|
| 78 |
FileDescriptor = POINTER TO fds ; |
| 79 |
fds = RECORD |
| 80 |
unixfd: INTEGER ; |
| 81 |
name : NameInfo ; |
| 82 |
state : FileStatus ; |
| 83 |
usage : FileUsage ; |
| 84 |
output: BOOLEAN ; (* is this file going to write data *) |
| 85 |
buffer: Buffer ; |
| 86 |
abspos: LONGINT ; (* absolute position into file. *) |
| 87 |
END ; (* reflects low level reads which *) |
| 88 |
(* means this value will normally *) |
| 89 |
(* be further through the file than *) |
| 90 |
(* bufstart above. *) |
| 91 |
PtrToChar = POINTER TO CHAR ; |
| 92 |
|
| 93 |
|
| 94 |
VAR |
| 95 |
FileInfo: Index ; |
| 96 |
Error : File ; (* not stderr, this is an unused file handle |
| 97 |
which only serves to hold status values |
| 98 |
when we cannot create a new file handle *) |
| 99 |
|
| 100 |
|
| 101 |
(* |
| 102 |
GetUnixFileDescriptor - returns the UNIX file descriptor of a file. |
| 103 |
*) |
| 104 |
|
| 105 |
PROCEDURE GetUnixFileDescriptor (f: File) : INTEGER ; |
| 106 |
VAR |
| 107 |
fd: FileDescriptor ; |
| 108 |
BEGIN |
| 109 |
IF f#Error |
| 110 |
THEN |
| 111 |
fd := GetIndice(FileInfo, f) ; |
| 112 |
IF fd#NIL |
| 113 |
THEN |
| 114 |
RETURN( fd^.unixfd ) |
| 115 |
END |
| 116 |
END ; |
| 117 |
FormatError1('file %d has not been opened or is out of range\n', f) ; |
| 118 |
RETURN( -1 ) |
| 119 |
END GetUnixFileDescriptor ; |
| 120 |
|
| 121 |
|
| 122 |
(* |
| 123 |
WriteString - writes a string to file, f. |
| 124 |
*) |
| 125 |
|
| 126 |
PROCEDURE WriteString (f: File; a: ARRAY OF CHAR) ; |
| 127 |
VAR |
| 128 |
l: CARDINAL ; |
| 129 |
BEGIN |
| 130 |
l := StrLen(a) ; |
| 131 |
IF WriteNBytes(f, l, ADR(a))#l |
| 132 |
THEN |
| 133 |
END |
| 134 |
END WriteString ; |
| 135 |
|
| 136 |
|
| 137 |
(* |
| 138 |
Max - returns the maximum of two values. |
| 139 |
*) |
| 140 |
|
| 141 |
PROCEDURE Max (a, b: CARDINAL) : CARDINAL ; |
| 142 |
BEGIN |
| 143 |
IF a>b |
| 144 |
THEN |
| 145 |
RETURN( a ) |
| 146 |
ELSE |
| 147 |
RETURN( b ) |
| 148 |
END |
| 149 |
END Max ; |
| 150 |
|
| 151 |
|
| 152 |
(* |
| 153 |
Min - returns the minimum of two values. |
| 154 |
*) |
| 155 |
|
| 156 |
PROCEDURE Min (a, b: CARDINAL) : CARDINAL ; |
| 157 |
BEGIN |
| 158 |
IF a<b |
| 159 |
THEN |
| 160 |
RETURN( a ) |
| 161 |
ELSE |
| 162 |
RETURN( b ) |
| 163 |
END |
| 164 |
END Min ; |
| 165 |
|
| 166 |
|
| 167 |
(* |
| 168 |
GetNextFreeDescriptor - returns the index to the FileInfo array indicating |
| 169 |
the next free slot. |
| 170 |
*) |
| 171 |
|
| 172 |
PROCEDURE GetNextFreeDescriptor () : File ; |
| 173 |
VAR |
| 174 |
f, h: File ; |
| 175 |
fd : FileDescriptor ; |
| 176 |
BEGIN |
| 177 |
f := Error+1 ; |
| 178 |
h := HighIndice(FileInfo) ; |
| 179 |
LOOP |
| 180 |
IF f<=h |
| 181 |
THEN |
| 182 |
fd := GetIndice(FileInfo, f) ; |
| 183 |
IF fd=NIL |
| 184 |
THEN |
| 185 |
RETURN( f ) |
| 186 |
END |
| 187 |
END ; |
| 188 |
INC(f) ; |
| 189 |
IF f>h |
| 190 |
THEN |
| 191 |
PutIndice(FileInfo, f, NIL) ; (* create new slot *) |
| 192 |
RETURN( f ) |
| 193 |
END |
| 194 |
END |
| 195 |
END GetNextFreeDescriptor ; |
| 196 |
|
| 197 |
|
| 198 |
(* |
| 199 |
IsNoError - returns a TRUE if no error has occured on file, f. |
| 200 |
*) |
| 201 |
|
| 202 |
PROCEDURE IsNoError (f: File) : BOOLEAN ; |
| 203 |
VAR |
| 204 |
fd: FileDescriptor ; |
| 205 |
BEGIN |
| 206 |
IF f=Error |
| 207 |
THEN |
| 208 |
RETURN( FALSE ) |
| 209 |
ELSE |
| 210 |
fd := GetIndice(FileInfo, f) ; |
| 211 |
RETURN( (fd#NIL) AND ((fd^.state=successful) OR (fd^.state=endoffile) OR (fd^.state=endofline)) ) |
| 212 |
END |
| 213 |
END IsNoError ; |
| 214 |
|
| 215 |
|
| 216 |
(* |
| 217 |
IsActive - returns TRUE if the file, f, is still active. |
| 218 |
*) |
| 219 |
|
| 220 |
PROCEDURE IsActive (f: File) : BOOLEAN ; |
| 221 |
BEGIN |
| 222 |
IF f=Error |
| 223 |
THEN |
| 224 |
RETURN( FALSE ) |
| 225 |
ELSE |
| 226 |
RETURN( GetIndice(FileInfo, f)#NIL ) |
| 227 |
END |
| 228 |
END IsActive ; |
| 229 |
|
| 230 |
|
| 231 |
(* |
| 232 |
openToRead - attempts to open a file, fname, for reading and |
| 233 |
it returns this file. |
| 234 |
The success of this operation can be checked by |
| 235 |
calling IsNoError. |
| 236 |
*) |
| 237 |
|
| 238 |
PROCEDURE openToRead (fname: ADDRESS; flength: CARDINAL) : File ; |
| 239 |
VAR |
| 240 |
f: File ; |
| 241 |
BEGIN |
| 242 |
f := GetNextFreeDescriptor() ; |
| 243 |
IF f=Error |
| 244 |
THEN |
| 245 |
SetState(f, toomanyfilesopen) |
| 246 |
ELSE |
| 247 |
f := InitializeFile(f, fname, flength, successful, openedforread, FALSE, MaxBufferLength) ; |
| 248 |
ConnectToUnix(f, FALSE, FALSE) |
| 249 |
END ; |
| 250 |
RETURN( f ) |
| 251 |
END openToRead ; |
| 252 |
|
| 253 |
|
| 254 |
(* |
| 255 |
openToWrite - attempts to open a file, fname, for write and |
| 256 |
it returns this file. |
| 257 |
The success of this operation can be checked by |
| 258 |
calling IsNoError. |
| 259 |
*) |
| 260 |
|
| 261 |
PROCEDURE openToWrite (fname: ADDRESS; flength: CARDINAL) : File ; |
| 262 |
VAR |
| 263 |
f: File ; |
| 264 |
BEGIN |
| 265 |
f := GetNextFreeDescriptor() ; |
| 266 |
IF f=Error |
| 267 |
THEN |
| 268 |
SetState(f, toomanyfilesopen) |
| 269 |
ELSE |
| 270 |
f := InitializeFile(f, fname, flength, successful, openedforwrite, TRUE, MaxBufferLength) ; |
| 271 |
ConnectToUnix(f, TRUE, TRUE) |
| 272 |
END ; |
| 273 |
RETURN( f ) |
| 274 |
END openToWrite ; |
| 275 |
|
| 276 |
|
| 277 |
(* |
| 278 |
openForRandom - attempts to open a file, fname, for random access |
| 279 |
read or write and it returns this file. |
| 280 |
The success of this operation can be checked by |
| 281 |
calling IsNoError. |
| 282 |
towrite, determines whether the file should be |
| 283 |
opened for writing or reading. |
| 284 |
*) |
| 285 |
|
| 286 |
PROCEDURE openForRandom (fname: ADDRESS; flength: CARDINAL; |
| 287 |
towrite, newfile: BOOLEAN) : File ; |
| 288 |
VAR |
| 289 |
f: File ; |
| 290 |
BEGIN |
| 291 |
f := GetNextFreeDescriptor() ; |
| 292 |
IF f=Error |
| 293 |
THEN |
| 294 |
SetState(f, toomanyfilesopen) |
| 295 |
ELSE |
| 296 |
f := InitializeFile(f, fname, flength, successful, openedforrandom, towrite, MaxBufferLength) ; |
| 297 |
ConnectToUnix(f, towrite, newfile) |
| 298 |
END ; |
| 299 |
RETURN( f ) |
| 300 |
END openForRandom ; |
| 301 |
|
| 302 |
|
| 303 |
(* |
| 304 |
exists - returns TRUE if a file named, fname exists for reading. |
| 305 |
*) |
| 306 |
|
| 307 |
PROCEDURE exists (fname: ADDRESS; flength: CARDINAL) : BOOLEAN ; |
| 308 |
VAR |
| 309 |
f: File ; |
| 310 |
BEGIN |
| 311 |
f := openToRead(fname, flength) ; |
| 312 |
IF IsNoError(f) |
| 313 |
THEN |
| 314 |
Close(f) ; |
| 315 |
RETURN( TRUE ) |
| 316 |
ELSE |
| 317 |
Close(f) ; |
| 318 |
RETURN( FALSE ) |
| 319 |
END |
| 320 |
END exists ; |
| 321 |
|
| 322 |
|
| 323 |
(* |
| 324 |
SetState - sets the field, state, of file, f, to, s. |
| 325 |
*) |
| 326 |
|
| 327 |
PROCEDURE SetState (f: File; s: FileStatus) ; |
| 328 |
VAR |
| 329 |
fd: FileDescriptor ; |
| 330 |
BEGIN |
| 331 |
fd := GetIndice(FileInfo, f) ; |
| 332 |
fd^.state := s |
| 333 |
END SetState ; |
| 334 |
|
| 335 |
|
| 336 |
(* |
| 337 |
InitializeFile - initialize a file descriptor |
| 338 |
*) |
| 339 |
|
| 340 |
PROCEDURE InitializeFile (f: File; fname: ADDRESS; |
| 341 |
flength: CARDINAL; fstate: FileStatus; |
| 342 |
use: FileUsage; |
| 343 |
towrite: BOOLEAN; buflength: CARDINAL) : File ; |
| 344 |
VAR |
| 345 |
p : PtrToChar ; |
| 346 |
fd: FileDescriptor ; |
| 347 |
BEGIN |
| 348 |
NEW(fd) ; |
| 349 |
IF fd=NIL |
| 350 |
THEN |
| 351 |
SetState(Error, outofmemory) ; |
| 352 |
RETURN( Error ) |
| 353 |
ELSE |
| 354 |
PutIndice(FileInfo, f, fd) ; |
| 355 |
WITH fd^ DO |
| 356 |
name.size := flength+1 ; (* need to guarantee the nul for C *) |
| 357 |
usage := use ; |
| 358 |
output := towrite ; |
| 359 |
ALLOCATE(name.address, name.size) ; |
| 360 |
IF name.address=NIL |
| 361 |
THEN |
| 362 |
state := outofmemory ; |
| 363 |
RETURN( f ) |
| 364 |
END ; |
| 365 |
name.address := strncpy(name.address, fname, flength) ; |
| 366 |
(* and assign nul to the last byte *) |
| 367 |
p := name.address ; |
| 368 |
INC(p, flength) ; |
| 369 |
p^ := nul ; |
| 370 |
abspos := 0 ; |
| 371 |
(* now for the buffer *) |
| 372 |
NEW(buffer) ; |
| 373 |
IF buffer=NIL |
| 374 |
THEN |
| 375 |
SetState(Error, outofmemory) ; |
| 376 |
RETURN( Error ) |
| 377 |
ELSE |
| 378 |
WITH buffer^ DO |
| 379 |
valid := FALSE ; |
| 380 |
bufstart := 0 ; |
| 381 |
size := buflength ; |
| 382 |
position := 0 ; |
| 383 |
filled := 0 ; |
| 384 |
IF size=0 |
| 385 |
THEN |
| 386 |
address := NIL |
| 387 |
ELSE |
| 388 |
ALLOCATE(address, size) ; |
| 389 |
IF address=NIL |
| 390 |
THEN |
| 391 |
state := outofmemory ; |
| 392 |
RETURN( f ) |
| 393 |
END |
| 394 |
END ; |
| 395 |
IF towrite |
| 396 |
THEN |
| 397 |
left := size |
| 398 |
ELSE |
| 399 |
left := 0 |
| 400 |
END ; |
| 401 |
contents := address ; (* provides easy access for reading characters *) |
| 402 |
END ; |
| 403 |
state := fstate |
| 404 |
END |
| 405 |
END |
| 406 |
END ; |
| 407 |
RETURN( f ) |
| 408 |
END InitializeFile ; |
| 409 |
|
| 410 |
|
| 411 |
(* |
| 412 |
ConnectToUnix - connects a FIO file to a UNIX file descriptor. |
| 413 |
*) |
| 414 |
|
| 415 |
PROCEDURE ConnectToUnix (f: File; towrite, newfile: BOOLEAN) ; |
| 416 |
VAR |
| 417 |
fd: FileDescriptor ; |
| 418 |
BEGIN |
| 419 |
IF f#Error |
| 420 |
THEN |
| 421 |
fd := GetIndice(FileInfo, f) ; |
| 422 |
IF fd#NIL |
| 423 |
THEN |
| 424 |
WITH fd^ DO |
| 425 |
IF towrite |
| 426 |
THEN |
| 427 |
IF newfile |
| 428 |
THEN |
| 429 |
unixfd := creat(name.address, CreatePermissions) |
| 430 |
ELSE |
| 431 |
unixfd := open(name.address, UNIXWRITEONLY, 0) |
| 432 |
END |
| 433 |
ELSE |
| 434 |
unixfd := open(name.address, UNIXREADONLY, 0) |
| 435 |
END ; |
| 436 |
IF unixfd<0 |
| 437 |
THEN |
| 438 |
state := connectionfailure |
| 439 |
END |
| 440 |
END |
| 441 |
END |
| 442 |
END |
| 443 |
END ConnectToUnix ; |
| 444 |
|
| 445 |
|
| 446 |
(* |
| 447 |
The following functions are wrappers for the above. |
| 448 |
*) |
| 449 |
|
| 450 |
PROCEDURE Exists (fname: ARRAY OF CHAR) : BOOLEAN ; |
| 451 |
BEGIN |
| 452 |
RETURN( exists(ADR(fname), StrLen(fname)) ) |
| 453 |
END Exists ; |
| 454 |
|
| 455 |
|
| 456 |
PROCEDURE OpenToRead (fname: ARRAY OF CHAR) : File ; |
| 457 |
BEGIN |
| 458 |
RETURN( openToRead(ADR(fname), StrLen(fname)) ) |
| 459 |
END OpenToRead ; |
| 460 |
|
| 461 |
|
| 462 |
PROCEDURE OpenToWrite (fname: ARRAY OF CHAR) : File ; |
| 463 |
BEGIN |
| 464 |
RETURN( openToWrite(ADR(fname), StrLen(fname)) ) |
| 465 |
END OpenToWrite ; |
| 466 |
|
| 467 |
|
| 468 |
PROCEDURE OpenForRandom (fname: ARRAY OF CHAR; |
| 469 |
towrite: BOOLEAN; newfile: BOOLEAN) : File ; |
| 470 |
BEGIN |
| 471 |
RETURN( openForRandom(ADR(fname), StrLen(fname), towrite, newfile) ) |
| 472 |
END OpenForRandom ; |
| 473 |
|
| 474 |
|
| 475 |
(* |
| 476 |
Close - close a file which has been previously opened using: |
| 477 |
OpenToRead, OpenToWrite, OpenForRandom. |
| 478 |
It is correct to close a file which has an error status. |
| 479 |
*) |
| 480 |
|
| 481 |
PROCEDURE Close (f: File) ; |
| 482 |
VAR |
| 483 |
fd: FileDescriptor ; |
| 484 |
BEGIN |
| 485 |
IF f#Error |
| 486 |
THEN |
| 487 |
fd := GetIndice(FileInfo, f) ; |
| 488 |
(* |
| 489 |
we allow users to close files which have an error status |
| 490 |
*) |
| 491 |
IF fd#NIL |
| 492 |
THEN |
| 493 |
FlushBuffer(f) ; |
| 494 |
WITH fd^ DO |
| 495 |
IF unixfd>=0 |
| 496 |
THEN |
| 497 |
IF close(unixfd)#0 |
| 498 |
THEN |
| 499 |
FormatError1('failed to close file (%s)\n', name.address) ; |
| 500 |
state := failed (* --fixme-- too late to notify user (unless we return a BOOLEAN) *) |
| 501 |
END |
| 502 |
END ; |
| 503 |
IF name.address#NIL |
| 504 |
THEN |
| 505 |
DEALLOCATE(name.address, name.size) |
| 506 |
END ; |
| 507 |
IF buffer#NIL |
| 508 |
THEN |
| 509 |
WITH buffer^ DO |
| 510 |
IF address#NIL |
| 511 |
THEN |
| 512 |
DEALLOCATE(address, size) |
| 513 |
END |
| 514 |
END ; |
| 515 |
DISPOSE(buffer) ; |
| 516 |
buffer := NIL |
| 517 |
END |
| 518 |
END ; |
| 519 |
DISPOSE(fd) ; |
| 520 |
PutIndice(FileInfo, f, NIL) |
| 521 |
END |
| 522 |
END |
| 523 |
END Close ; |
| 524 |
|
| 525 |
|
| 526 |
(* |
| 527 |
ReadFromBuffer - attempts to read, nBytes, from file, f. |
| 528 |
It firstly consumes the buffer and then performs |
| 529 |
direct unbuffered reads. This should only be used |
| 530 |
when wishing to read large files. |
| 531 |
|
| 532 |
The actual number of bytes read is returned. |
| 533 |
-1 is returned if EOF is reached. |
| 534 |
*) |
| 535 |
|
| 536 |
PROCEDURE ReadFromBuffer (f: File; a: ADDRESS; nBytes: CARDINAL) : INTEGER ; |
| 537 |
VAR |
| 538 |
t : ADDRESS ; |
| 539 |
result: INTEGER ; |
| 540 |
total, |
| 541 |
n : CARDINAL ; |
| 542 |
p : POINTER TO BYTE ; |
| 543 |
fd : FileDescriptor ; |
| 544 |
BEGIN |
| 545 |
IF f#Error |
| 546 |
THEN |
| 547 |
total := 0 ; (* how many bytes have we read *) |
| 548 |
fd := GetIndice(FileInfo, f) ; |
| 549 |
WITH fd^ DO |
| 550 |
(* extract from the buffer first *) |
| 551 |
IF (buffer#NIL) AND (buffer^.valid) |
| 552 |
THEN |
| 553 |
WITH buffer^ DO |
| 554 |
IF left>0 |
| 555 |
THEN |
| 556 |
IF nBytes=1 |
| 557 |
THEN |
| 558 |
(* too expensive to call memcpy for 1 character *) |
| 559 |
p := a ; |
| 560 |
p^ := contents^[position] ; |
| 561 |
DEC(left) ; (* remove consumed bytes *) |
| 562 |
INC(position) ; (* move onwards n bytes *) |
| 563 |
nBytes := 0 ; (* reduce the amount for future direct *) |
| 564 |
(* read *) |
| 565 |
RETURN( 1 ) |
| 566 |
ELSE |
| 567 |
n := Min(left, nBytes) ; |
| 568 |
t := address ; |
| 569 |
INC(t, position) ; |
| 570 |
p := memcpy(a, t, n) ; |
| 571 |
DEC(left, n) ; (* remove consumed bytes *) |
| 572 |
INC(position, n) ; (* move onwards n bytes *) |
| 573 |
(* move onwards ready for direct reads *) |
| 574 |
INC(a, n) ; |
| 575 |
DEC(nBytes, n) ; (* reduce the amount for future direct *) |
| 576 |
(* read *) |
| 577 |
INC(total, n) ; |
| 578 |
RETURN( total ) (* much cleaner to return now, *) |
| 579 |
END (* difficult to record an error if *) |
| 580 |
END (* the read below returns -1 *) |
| 581 |
END |
| 582 |
END ; |
| 583 |
IF nBytes>0 |
| 584 |
THEN |
| 585 |
(* still more to read *) |
| 586 |
result := read(unixfd, a, INTEGER(nBytes)) ; |
| 587 |
IF result>0 |
| 588 |
THEN |
| 589 |
INC(total, result) ; |
| 590 |
INC(abspos, result) ; |
| 591 |
(* now disable the buffer as we read directly into, a. *) |
| 592 |
IF buffer#NIL |
| 593 |
THEN |
| 594 |
buffer^.valid := FALSE |
| 595 |
END ; |
| 596 |
ELSE |
| 597 |
IF result=0 |
| 598 |
THEN |
| 599 |
(* eof reached *) |
| 600 |
state := endoffile |
| 601 |
ELSE |
| 602 |
state := failed |
| 603 |
END ; |
| 604 |
(* indicate buffer is empty *) |
| 605 |
IF buffer#NIL |
| 606 |
THEN |
| 607 |
WITH buffer^ DO |
| 608 |
valid := FALSE ; |
| 609 |
left := 0 ; |
| 610 |
position := 0 ; |
| 611 |
IF address#NIL |
| 612 |
THEN |
| 613 |
contents^[position] := nul |
| 614 |
END |
| 615 |
END |
| 616 |
END ; |
| 617 |
RETURN( -1 ) |
| 618 |
END |
| 619 |
END |
| 620 |
END ; |
| 621 |
RETURN( total ) |
| 622 |
ELSE |
| 623 |
RETURN( -1 ) |
| 624 |
END |
| 625 |
END ReadFromBuffer ; |
| 626 |
|
| 627 |
|
| 628 |
(* |
| 629 |
ReadNBytes - reads nBytes of a file into memory area, dest, returning |
| 630 |
the number of bytes actually read. |
| 631 |
This function will consume from the buffer and then |
| 632 |
perform direct libc reads. It is ideal for large reads. |
| 633 |
*) |
| 634 |
|
| 635 |
PROCEDURE ReadNBytes (f: File; nBytes: CARDINAL; dest: ADDRESS) : CARDINAL ; |
| 636 |
VAR |
| 637 |
n: INTEGER ; |
| 638 |
p: POINTER TO CHAR ; |
| 639 |
BEGIN |
| 640 |
IF f # Error |
| 641 |
THEN |
| 642 |
CheckAccess (f, openedforread, FALSE) ; |
| 643 |
n := ReadFromBuffer (f, dest, nBytes) ; |
| 644 |
IF n <= 0 |
| 645 |
THEN |
| 646 |
RETURN 0 |
| 647 |
ELSE |
| 648 |
p := dest ; |
| 649 |
INC (p, n-1) ; |
| 650 |
SetEndOfLine (f, p^) ; |
| 651 |
RETURN n |
| 652 |
END |
| 653 |
ELSE |
| 654 |
RETURN 0 |
| 655 |
END |
| 656 |
END ReadNBytes ; |
| 657 |
|
| 658 |
|
| 659 |
(* |
| 660 |
BufferedRead - will read, nBytes, through the buffer. |
| 661 |
Similar to ReadFromBuffer, but this function will always |
| 662 |
read into the buffer before copying into memory. |
| 663 |
|
| 664 |
Useful when performing small reads. |
| 665 |
*) |
| 666 |
|
| 667 |
PROCEDURE BufferedRead (f: File; nBytes: CARDINAL; a: ADDRESS) : INTEGER ; |
| 668 |
VAR |
| 669 |
t : ADDRESS ; |
| 670 |
result: INTEGER ; |
| 671 |
total, |
| 672 |
n : INTEGER ; |
| 673 |
p : POINTER TO BYTE ; |
| 674 |
fd : FileDescriptor ; |
| 675 |
BEGIN |
| 676 |
IF f#Error |
| 677 |
THEN |
| 678 |
fd := GetIndice(FileInfo, f) ; |
| 679 |
total := 0 ; (* how many bytes have we read *) |
| 680 |
IF fd#NIL |
| 681 |
THEN |
| 682 |
WITH fd^ DO |
| 683 |
(* extract from the buffer first *) |
| 684 |
IF buffer#NIL |
| 685 |
THEN |
| 686 |
WITH buffer^ DO |
| 687 |
WHILE nBytes>0 DO |
| 688 |
IF (left>0) AND valid |
| 689 |
THEN |
| 690 |
IF nBytes=1 |
| 691 |
THEN |
| 692 |
(* too expensive to call memcpy for 1 character *) |
| 693 |
p := a ; |
| 694 |
p^ := contents^[position] ; |
| 695 |
DEC(left) ; (* remove consumed byte *) |
| 696 |
INC(position) ; (* move onwards n byte *) |
| 697 |
INC(total) ; |
| 698 |
RETURN( total ) |
| 699 |
ELSE |
| 700 |
n := Min(left, nBytes) ; |
| 701 |
t := address ; |
| 702 |
INC(t, position) ; |
| 703 |
p := memcpy(a, t, n) ; |
| 704 |
DEC(left, n) ; (* remove consumed bytes *) |
| 705 |
INC(position, n) ; (* move onwards n bytes *) |
| 706 |
(* move onwards ready for direct reads *) |
| 707 |
INC(a, n) ; |
| 708 |
DEC(nBytes, n) ; (* reduce the amount for future direct *) |
| 709 |
(* read *) |
| 710 |
INC(total, n) |
| 711 |
END |
| 712 |
ELSE |
| 713 |
(* refill buffer *) |
| 714 |
n := read(unixfd, address, size) ; |
| 715 |
IF n>=0 |
| 716 |
THEN |
| 717 |
valid := TRUE ; |
| 718 |
position := 0 ; |
| 719 |
left := n ; |
| 720 |
filled := n ; |
| 721 |
bufstart := abspos ; |
| 722 |
INC(abspos, n) ; |
| 723 |
IF n=0 |
| 724 |
THEN |
| 725 |
(* eof reached *) |
| 726 |
state := endoffile ; |
| 727 |
RETURN( -1 ) |
| 728 |
END |
| 729 |
ELSE |
| 730 |
valid := FALSE ; |
| 731 |
position := 0 ; |
| 732 |
left := 0 ; |
| 733 |
filled := 0 ; |
| 734 |
state := failed ; |
| 735 |
RETURN( total ) |
| 736 |
END |
| 737 |
END |
| 738 |
END |
| 739 |
END ; |
| 740 |
RETURN( total ) |
| 741 |
END |
| 742 |
END |
| 743 |
END |
| 744 |
END ; |
| 745 |
RETURN( -1 ) |
| 746 |
END BufferedRead ; |
| 747 |
|
| 748 |
|
| 749 |
(* |
| 750 |
HandleEscape - translates \n and \t into their respective ascii codes. |
| 751 |
*) |
| 752 |
|
| 753 |
PROCEDURE HandleEscape (VAR dest: ARRAY OF CHAR; src: ARRAY OF CHAR; |
| 754 |
VAR i, j: CARDINAL; HighSrc, HighDest: CARDINAL) ; |
| 755 |
BEGIN |
| 756 |
IF (i+1<HighSrc) AND (src[i]='\') AND (j<HighDest) |
| 757 |
THEN |
| 758 |
IF src[i+1]='n' |
| 759 |
THEN |
| 760 |
(* requires a newline *) |
| 761 |
dest[j] := nl ; |
| 762 |
INC(j) ; |
| 763 |
INC(i, 2) |
| 764 |
ELSIF src[i+1]='t' |
| 765 |
THEN |
| 766 |
(* requires a tab (yuck) tempted to fake this but I better not.. *) |
| 767 |
dest[j] := tab ; |
| 768 |
INC(j) ; |
| 769 |
INC(i, 2) |
| 770 |
ELSE |
| 771 |
(* copy escaped character *) |
| 772 |
INC(i) ; |
| 773 |
dest[j] := src[i] ; |
| 774 |
INC(j) ; |
| 775 |
INC(i) |
| 776 |
END |
| 777 |
END |
| 778 |
END HandleEscape ; |
| 779 |
|
| 780 |
|
| 781 |
(* |
| 782 |
Cast - casts a := b |
| 783 |
*) |
| 784 |
|
| 785 |
PROCEDURE Cast (VAR a: ARRAY OF BYTE; b: ARRAY OF BYTE) ; |
| 786 |
VAR |
| 787 |
i: CARDINAL ; |
| 788 |
BEGIN |
| 789 |
IF HIGH(a)=HIGH(b) |
| 790 |
THEN |
| 791 |
FOR i := 0 TO HIGH(a) DO |
| 792 |
a[i] := b[i] |
| 793 |
END |
| 794 |
ELSE |
| 795 |
FormatError('cast failed') |
| 796 |
END |
| 797 |
END Cast ; |
| 798 |
|
| 799 |
|
| 800 |
(* |
| 801 |
StringFormat1 - converts string, src, into, dest, together with encapsulated |
| 802 |
entity, w. It only formats the first %s or %d with n. |
| 803 |
*) |
| 804 |
|
| 805 |
PROCEDURE StringFormat1 (VAR dest: ARRAY OF CHAR; src: ARRAY OF CHAR; |
| 806 |
w: ARRAY OF BYTE) ; |
| 807 |
VAR |
| 808 |
HighSrc, |
| 809 |
HighDest, |
| 810 |
c, i, j : CARDINAL ; |
| 811 |
str : ARRAY [0..MaxErrorString] OF CHAR ; |
| 812 |
p : POINTER TO CHAR ; |
| 813 |
BEGIN |
| 814 |
HighSrc := StrLen(src) ; |
| 815 |
HighDest := HIGH(dest) ; |
| 816 |
p := NIL ; |
| 817 |
c := 0 ; |
| 818 |
i := 0 ; |
| 819 |
j := 0 ; |
| 820 |
WHILE (i<HighSrc) AND (src[i]#nul) AND (j<HighDest) AND (src[i]#'%') DO |
| 821 |
IF src[i]='\' |
| 822 |
THEN |
| 823 |
HandleEscape(dest, src, i, j, HighSrc, HighDest) |
| 824 |
ELSE |
| 825 |
dest[j] := src[i] ; |
| 826 |
INC(i) ; |
| 827 |
INC(j) |
| 828 |
END |
| 829 |
END ; |
| 830 |
|
| 831 |
IF (i+1<HighSrc) AND (src[i]='%') AND (j<HighDest) |
| 832 |
THEN |
| 833 |
IF src[i+1]='s' |
| 834 |
THEN |
| 835 |
Cast(p, w) ; |
| 836 |
WHILE (j<HighDest) AND (p^#nul) DO |
| 837 |
dest[j] := p^ ; |
| 838 |
INC(j) ; |
| 839 |
INC(p) |
| 840 |
END ; |
| 841 |
IF j<HighDest |
| 842 |
THEN |
| 843 |
dest[j] := nul |
| 844 |
END ; |
| 845 |
j := StrLen(dest) ; |
| 846 |
INC(i, 2) |
| 847 |
ELSIF src[i+1]='d' |
| 848 |
THEN |
| 849 |
dest[j] := nul ; |
| 850 |
Cast(c, w) ; |
| 851 |
CardToStr(c, 0, str) ; |
| 852 |
StrConCat(dest, str, dest) ; |
| 853 |
j := StrLen(dest) ; |
| 854 |
INC(i, 2) |
| 855 |
ELSE |
| 856 |
dest[j] := src[i] ; |
| 857 |
INC(i) ; |
| 858 |
INC(j) |
| 859 |
END |
| 860 |
END ; |
| 861 |
(* and finish off copying src into dest *) |
| 862 |
WHILE (i<HighSrc) AND (src[i]#nul) AND (j<HighDest) DO |
| 863 |
IF src[i]='\' |
| 864 |
THEN |
| 865 |
HandleEscape(dest, src, i, j, HighSrc, HighDest) |
| 866 |
ELSE |
| 867 |
dest[j] := src[i] ; |
| 868 |
INC(i) ; |
| 869 |
INC(j) |
| 870 |
END |
| 871 |
END ; |
| 872 |
IF j<HighDest |
| 873 |
THEN |
| 874 |
dest[j] := nul |
| 875 |
END ; |
| 876 |
END StringFormat1 ; |
| 877 |
|
| 878 |
|
| 879 |
(* |
| 880 |
FormatError - provides a orthoganal counterpart to the procedure below. |
| 881 |
*) |
| 882 |
|
| 883 |
PROCEDURE FormatError (a: ARRAY OF CHAR) ; |
| 884 |
BEGIN |
| 885 |
WriteString (StdErr, a) |
| 886 |
END FormatError ; |
| 887 |
|
| 888 |
|
| 889 |
(* |
| 890 |
FormatError1 - generic error procedure taking standard format string |
| 891 |
and single parameter. |
| 892 |
*) |
| 893 |
|
| 894 |
PROCEDURE FormatError1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) ; |
| 895 |
VAR |
| 896 |
s: ARRAY [0..MaxErrorString] OF CHAR ; |
| 897 |
BEGIN |
| 898 |
StringFormat1 (s, a, w) ; |
| 899 |
FormatError (s) |
| 900 |
END FormatError1 ; |
| 901 |
|
| 902 |
|
| 903 |
(* |
| 904 |
FormatError2 - generic error procedure taking standard format string |
| 905 |
and two parameters. |
| 906 |
*) |
| 907 |
|
| 908 |
PROCEDURE FormatError2 (a: ARRAY OF CHAR; |
| 909 |
w1, w2: ARRAY OF BYTE) ; |
| 910 |
VAR |
| 911 |
s: ARRAY [0..MaxErrorString] OF CHAR ; |
| 912 |
BEGIN |
| 913 |
StringFormat1 (s, a, w1) ; |
| 914 |
FormatError1 (s, w2) |
| 915 |
END FormatError2 ; |
| 916 |
|
| 917 |
|
| 918 |
(* |
| 919 |
CheckAccess - checks to see whether a file f has been |
| 920 |
opened for read/write. |
| 921 |
*) |
| 922 |
|
| 923 |
PROCEDURE CheckAccess (f: File; use: FileUsage; towrite: BOOLEAN) ; |
| 924 |
VAR |
| 925 |
fd: FileDescriptor ; |
| 926 |
BEGIN |
| 927 |
IF f#Error |
| 928 |
THEN |
| 929 |
fd := GetIndice (FileInfo, f) ; |
| 930 |
IF fd=NIL |
| 931 |
THEN |
| 932 |
IF f#StdErr |
| 933 |
THEN |
| 934 |
FormatError ('this file has probably been closed and not reopened successfully or alternatively never opened\n') |
| 935 |
END ; |
| 936 |
HALT |
| 937 |
ELSE |
| 938 |
WITH fd^ DO |
| 939 |
IF (use=openedforwrite) AND (usage=openedforread) |
| 940 |
THEN |
| 941 |
FormatError1 ('this file (%s) has been opened for reading but is now being written\n', |
| 942 |
name.address) ; |
| 943 |
HALT |
| 944 |
ELSIF (use=openedforread) AND (usage=openedforwrite) |
| 945 |
THEN |
| 946 |
FormatError1('this file (%s) has been opened for writing but is now being read\n', |
| 947 |
name.address) ; |
| 948 |
HALT |
| 949 |
ELSIF state=connectionfailure |
| 950 |
THEN |
| 951 |
FormatError1('this file (%s) was not successfully opened\n', |
| 952 |
name.address) ; |
| 953 |
HALT |
| 954 |
ELSIF towrite#output |
| 955 |
THEN |
| 956 |
IF output |
| 957 |
THEN |
| 958 |
FormatError1('this file (%s) was opened for writing but is now being read\n', |
| 959 |
name.address) ; |
| 960 |
HALT |
| 961 |
ELSE |
| 962 |
FormatError1('this file (%s) was opened for reading but is now being written\n', |
| 963 |
name.address) ; |
| 964 |
HALT |
| 965 |
END |
| 966 |
END |
| 967 |
END |
| 968 |
END |
| 969 |
ELSE |
| 970 |
FormatError('this file has not been opened successfully\n') ; |
| 971 |
HALT |
| 972 |
END |
| 973 |
END CheckAccess ; |
| 974 |
|
| 975 |
|
| 976 |
(* |
| 977 |
ReadChar - returns a character read from file f. |
| 978 |
Sensible to check with IsNoError or EOF after calling |
| 979 |
this function. |
| 980 |
*) |
| 981 |
|
| 982 |
PROCEDURE ReadChar (f: File) : CHAR ; |
| 983 |
VAR |
| 984 |
ch: CHAR ; |
| 985 |
BEGIN |
| 986 |
CheckAccess (f, openedforread, FALSE) ; |
| 987 |
IF BufferedRead (f, SIZE (ch), ADR (ch)) = VAL (INTEGER, SIZE (ch)) |
| 988 |
THEN |
| 989 |
SetEndOfLine (f, ch) ; |
| 990 |
RETURN ch |
| 991 |
ELSE |
| 992 |
RETURN nul |
| 993 |
END |
| 994 |
END ReadChar ; |
| 995 |
|
| 996 |
|
| 997 |
(* |
| 998 |
SetEndOfLine - |
| 999 |
*) |
| 1000 |
|
| 1001 |
PROCEDURE SetEndOfLine (f: File; ch: CHAR) ; |
| 1002 |
VAR |
| 1003 |
fd: FileDescriptor ; |
| 1004 |
BEGIN |
| 1005 |
CheckAccess(f, openedforread, FALSE) ; |
| 1006 |
IF f#Error |
| 1007 |
THEN |
| 1008 |
fd := GetIndice(FileInfo, f) ; |
| 1009 |
WITH fd^ DO |
| 1010 |
IF ch=nl |
| 1011 |
THEN |
| 1012 |
state := endofline |
| 1013 |
ELSE |
| 1014 |
state := successful |
| 1015 |
END |
| 1016 |
END |
| 1017 |
END |
| 1018 |
END SetEndOfLine ; |
| 1019 |
|
| 1020 |
|
| 1021 |
(* |
| 1022 |
UnReadChar - replaces a character, ch, back into file f. |
| 1023 |
This character must have been read by ReadChar |
| 1024 |
and it does not allow successive calls. It may |
| 1025 |
only be called if the previous read was successful |
| 1026 |
or end of file was seen. |
| 1027 |
If the state was previously endoffile then it |
| 1028 |
is altered to successful. |
| 1029 |
Otherwise it is left alone. |
| 1030 |
*) |
| 1031 |
|
| 1032 |
PROCEDURE UnReadChar (f: File; ch: CHAR) ; |
| 1033 |
VAR |
| 1034 |
fd : FileDescriptor ; |
| 1035 |
n : CARDINAL ; |
| 1036 |
a, b: ADDRESS ; |
| 1037 |
BEGIN |
| 1038 |
CheckAccess(f, openedforread, FALSE) ; |
| 1039 |
IF f#Error |
| 1040 |
THEN |
| 1041 |
fd := GetIndice(FileInfo, f) ; |
| 1042 |
WITH fd^ DO |
| 1043 |
IF (state=successful) OR (state=endoffile) OR (state=endofline) |
| 1044 |
THEN |
| 1045 |
IF (buffer#NIL) AND (buffer^.valid) |
| 1046 |
THEN |
| 1047 |
WITH buffer^ DO |
| 1048 |
(* we assume that a ReadChar has occurred, we will check just in case. *) |
| 1049 |
IF state=endoffile |
| 1050 |
THEN |
| 1051 |
position := MaxBufferLength ; |
| 1052 |
left := 0 ; |
| 1053 |
filled := 0 ; |
| 1054 |
state := successful |
| 1055 |
END ; |
| 1056 |
IF position>0 |
| 1057 |
THEN |
| 1058 |
DEC(position) ; |
| 1059 |
INC(left) ; |
| 1060 |
contents^[position] := ch ; |
| 1061 |
ELSE |
| 1062 |
(* position=0 *) |
| 1063 |
(* if possible make room and store ch *) |
| 1064 |
IF filled=size |
| 1065 |
THEN |
| 1066 |
FormatError1('performing too many UnReadChar calls on file (%d)\n', f) |
| 1067 |
ELSE |
| 1068 |
n := filled-position ; |
| 1069 |
b := ADR(contents^[position]) ; |
| 1070 |
a := ADR(contents^[position+1]) ; |
| 1071 |
a := memcpy(a, b, n) ; |
| 1072 |
INC(filled) ; |
| 1073 |
contents^[position] := ch ; |
| 1074 |
END |
| 1075 |
END |
| 1076 |
END |
| 1077 |
END |
| 1078 |
ELSE |
| 1079 |
FormatError1('UnReadChar can only be called if the previous read was successful or end of file, error on file (%d)\n', f) |
| 1080 |
END |
| 1081 |
END |
| 1082 |
END |
| 1083 |
END UnReadChar ; |
| 1084 |
|
| 1085 |
|
| 1086 |
(* |
| 1087 |
ReadAny - reads HIGH(a) bytes into, a. All input |
| 1088 |
is fully buffered, unlike ReadNBytes and thus is more |
| 1089 |
suited to small reads. |
| 1090 |
*) |
| 1091 |
|
| 1092 |
PROCEDURE ReadAny (f: File; VAR a: ARRAY OF BYTE) ; |
| 1093 |
BEGIN |
| 1094 |
CheckAccess(f, openedforread, FALSE) ; |
| 1095 |
IF BufferedRead (f, HIGH (a), ADR (a)) = VAL (INTEGER, HIGH (a)) |
| 1096 |
THEN |
| 1097 |
SetEndOfLine(f, a[HIGH(a)]) |
| 1098 |
END |
| 1099 |
END ReadAny ; |
| 1100 |
|
| 1101 |
|
| 1102 |
(* |
| 1103 |
EOF - tests to see whether a file, f, has reached end of file. |
| 1104 |
*) |
| 1105 |
|
| 1106 |
PROCEDURE EOF (f: File) : BOOLEAN ; |
| 1107 |
VAR |
| 1108 |
fd: FileDescriptor ; |
| 1109 |
BEGIN |
| 1110 |
CheckAccess(f, openedforread, FALSE) ; |
| 1111 |
IF f#Error |
| 1112 |
THEN |
| 1113 |
fd := GetIndice(FileInfo, f) ; |
| 1114 |
IF fd#NIL |
| 1115 |
THEN |
| 1116 |
RETURN( fd^.state=endoffile ) |
| 1117 |
END |
| 1118 |
END ; |
| 1119 |
RETURN( TRUE ) |
| 1120 |
END EOF ; |
| 1121 |
|
| 1122 |
|
| 1123 |
(* |
| 1124 |
EOLN - tests to see whether a file, f, is upon a newline. |
| 1125 |
It does NOT consume the newline. |
| 1126 |
*) |
| 1127 |
|
| 1128 |
PROCEDURE EOLN (f: File) : BOOLEAN ; |
| 1129 |
VAR |
| 1130 |
ch: CHAR ; |
| 1131 |
fd: FileDescriptor ; |
| 1132 |
BEGIN |
| 1133 |
CheckAccess(f, openedforread, FALSE) ; |
| 1134 |
(* |
| 1135 |
we will read a character and then push it back onto the input stream, |
| 1136 |
having noted the file status, we also reset the status. |
| 1137 |
*) |
| 1138 |
IF f#Error |
| 1139 |
THEN |
| 1140 |
fd := GetIndice(FileInfo, f) ; |
| 1141 |
IF fd#NIL |
| 1142 |
THEN |
| 1143 |
IF (fd^.state=successful) OR (fd^.state=endofline) |
| 1144 |
THEN |
| 1145 |
ch := ReadChar(f) ; |
| 1146 |
IF (fd^.state=successful) OR (fd^.state=endofline) |
| 1147 |
THEN |
| 1148 |
UnReadChar(f, ch) |
| 1149 |
END ; |
| 1150 |
RETURN( ch=nl ) |
| 1151 |
END |
| 1152 |
END |
| 1153 |
END ; |
| 1154 |
RETURN( FALSE ) |
| 1155 |
END EOLN ; |
| 1156 |
|
| 1157 |
|
| 1158 |
(* |
| 1159 |
WasEOLN - tests to see whether a file, f, has just seen a newline. |
| 1160 |
*) |
| 1161 |
|
| 1162 |
PROCEDURE WasEOLN (f: File) : BOOLEAN ; |
| 1163 |
VAR |
| 1164 |
fd: FileDescriptor ; |
| 1165 |
BEGIN |
| 1166 |
CheckAccess(f, openedforread, FALSE) ; |
| 1167 |
IF f=Error |
| 1168 |
THEN |
| 1169 |
RETURN FALSE |
| 1170 |
ELSE |
| 1171 |
fd := GetIndice(FileInfo, f) ; |
| 1172 |
RETURN( (fd#NIL) AND (fd^.state=endofline) ) |
| 1173 |
END |
| 1174 |
END WasEOLN ; |
| 1175 |
|
| 1176 |
|
| 1177 |
(* |
| 1178 |
WriteLine - writes out a linefeed to file, f. |
| 1179 |
*) |
| 1180 |
|
| 1181 |
PROCEDURE WriteLine (f: File) ; |
| 1182 |
BEGIN |
| 1183 |
WriteChar(f, nl) |
| 1184 |
END WriteLine ; |
| 1185 |
|
| 1186 |
|
| 1187 |
(* |
| 1188 |
WriteNBytes - writes nBytes from memory area src to a file |
| 1189 |
returning the number of bytes actually written. |
| 1190 |
This function will flush the buffer and then |
| 1191 |
write the nBytes using a direct write from libc. |
| 1192 |
It is ideal for large writes. |
| 1193 |
*) |
| 1194 |
|
| 1195 |
PROCEDURE WriteNBytes (f: File; nBytes: CARDINAL; src: ADDRESS) : CARDINAL ; |
| 1196 |
VAR |
| 1197 |
total: INTEGER ; |
| 1198 |
fd : FileDescriptor ; |
| 1199 |
BEGIN |
| 1200 |
CheckAccess(f, openedforwrite, TRUE) ; |
| 1201 |
FlushBuffer(f) ; |
| 1202 |
IF f#Error |
| 1203 |
THEN |
| 1204 |
fd := GetIndice(FileInfo, f) ; |
| 1205 |
IF fd#NIL |
| 1206 |
THEN |
| 1207 |
WITH fd^ DO |
| 1208 |
total := write(unixfd, src, INTEGER(nBytes)) ; |
| 1209 |
IF total<0 |
| 1210 |
THEN |
| 1211 |
state := failed ; |
| 1212 |
RETURN( 0 ) |
| 1213 |
ELSE |
| 1214 |
INC(abspos, CARDINAL(total)) ; |
| 1215 |
IF buffer#NIL |
| 1216 |
THEN |
| 1217 |
buffer^.bufstart := abspos |
| 1218 |
END ; |
| 1219 |
RETURN( CARDINAL(total) ) |
| 1220 |
END |
| 1221 |
END |
| 1222 |
END |
| 1223 |
END ; |
| 1224 |
RETURN( 0 ) |
| 1225 |
END WriteNBytes ; |
| 1226 |
|
| 1227 |
|
| 1228 |
(* |
| 1229 |
BufferedWrite - will write, nBytes, through the buffer. |
| 1230 |
Similar to WriteNBytes, but this function will always |
| 1231 |
write into the buffer before copying into memory. |
| 1232 |
|
| 1233 |
Useful when performing small writes. |
| 1234 |
*) |
| 1235 |
|
| 1236 |
PROCEDURE BufferedWrite (f: File; nBytes: CARDINAL; a: ADDRESS) : INTEGER ; |
| 1237 |
VAR |
| 1238 |
t : ADDRESS ; |
| 1239 |
result: INTEGER ; |
| 1240 |
total, |
| 1241 |
n : INTEGER ; |
| 1242 |
p : POINTER TO BYTE ; |
| 1243 |
fd : FileDescriptor ; |
| 1244 |
BEGIN |
| 1245 |
IF f#Error |
| 1246 |
THEN |
| 1247 |
fd := GetIndice(FileInfo, f) ; |
| 1248 |
IF fd#NIL |
| 1249 |
THEN |
| 1250 |
total := 0 ; (* how many bytes have we read *) |
| 1251 |
WITH fd^ DO |
| 1252 |
IF buffer#NIL |
| 1253 |
THEN |
| 1254 |
WITH buffer^ DO |
| 1255 |
WHILE nBytes>0 DO |
| 1256 |
(* place into the buffer first *) |
| 1257 |
IF left>0 |
| 1258 |
THEN |
| 1259 |
IF nBytes=1 |
| 1260 |
THEN |
| 1261 |
(* too expensive to call memcpy for 1 character *) |
| 1262 |
p := a ; |
| 1263 |
contents^[position] := p^ ; |
| 1264 |
DEC(left) ; (* reduce space *) |
| 1265 |
INC(position) ; (* move onwards n byte *) |
| 1266 |
INC(total) ; |
| 1267 |
RETURN( total ) |
| 1268 |
ELSE |
| 1269 |
n := Min(left, nBytes) ; |
| 1270 |
t := address ; |
| 1271 |
INC(t, position) ; |
| 1272 |
p := memcpy(a, t, CARDINAL(n)) ; |
| 1273 |
DEC(left, n) ; (* remove consumed bytes *) |
| 1274 |
INC(position, n) ; (* move onwards n bytes *) |
| 1275 |
(* move ready for further writes *) |
| 1276 |
INC(a, n) ; |
| 1277 |
DEC(nBytes, n) ; (* reduce the amount for future writes *) |
| 1278 |
INC(total, n) |
| 1279 |
END |
| 1280 |
ELSE |
| 1281 |
FlushBuffer(f) ; |
| 1282 |
IF (state#successful) AND (state#endofline) |
| 1283 |
THEN |
| 1284 |
nBytes := 0 |
| 1285 |
END |
| 1286 |
END |
| 1287 |
END |
| 1288 |
END ; |
| 1289 |
RETURN( total ) |
| 1290 |
END |
| 1291 |
END |
| 1292 |
END |
| 1293 |
END ; |
| 1294 |
RETURN( -1 ) |
| 1295 |
END BufferedWrite ; |
| 1296 |
|
| 1297 |
|
| 1298 |
(* |
| 1299 |
FlushBuffer - flush contents of file, f. |
| 1300 |
*) |
| 1301 |
|
| 1302 |
PROCEDURE FlushBuffer (f: File) ; |
| 1303 |
VAR |
| 1304 |
fd: FileDescriptor ; |
| 1305 |
BEGIN |
| 1306 |
IF f#Error |
| 1307 |
THEN |
| 1308 |
fd := GetIndice(FileInfo, f) ; |
| 1309 |
IF fd#NIL |
| 1310 |
THEN |
| 1311 |
WITH fd^ DO |
| 1312 |
IF output AND (buffer#NIL) |
| 1313 |
THEN |
| 1314 |
WITH buffer^ DO |
| 1315 |
IF (position=0) OR (write(unixfd, address, position)=VAL(INTEGER, position)) |
| 1316 |
THEN |
| 1317 |
INC(abspos, position) ; |
| 1318 |
bufstart := abspos ; |
| 1319 |
position := 0 ; |
| 1320 |
filled := 0 ; |
| 1321 |
left := size |
| 1322 |
ELSE |
| 1323 |
state := failed |
| 1324 |
END |
| 1325 |
END |
| 1326 |
END |
| 1327 |
END |
| 1328 |
END |
| 1329 |
END |
| 1330 |
END FlushBuffer ; |
| 1331 |
|
| 1332 |
|
| 1333 |
(* |
| 1334 |
WriteAny - writes HIGH(a) bytes onto, file, f. All output |
| 1335 |
is fully buffered, unlike WriteNBytes and thus is more |
| 1336 |
suited to small writes. |
| 1337 |
*) |
| 1338 |
|
| 1339 |
PROCEDURE WriteAny (f: File; VAR a: ARRAY OF BYTE) ; |
| 1340 |
BEGIN |
| 1341 |
CheckAccess (f, openedforwrite, TRUE) ; |
| 1342 |
IF BufferedWrite (f, HIGH (a), ADR (a)) = VAL (INTEGER, HIGH (a)) |
| 1343 |
THEN |
| 1344 |
END |
| 1345 |
END WriteAny ; |
| 1346 |
|
| 1347 |
|
| 1348 |
(* |
| 1349 |
WriteChar - writes a single character to file, f. |
| 1350 |
*) |
| 1351 |
|
| 1352 |
PROCEDURE WriteChar (f: File; ch: CHAR) ; |
| 1353 |
BEGIN |
| 1354 |
CheckAccess (f, openedforwrite, TRUE) ; |
| 1355 |
IF BufferedWrite (f, SIZE (ch), ADR (ch)) = VAL (INTEGER, SIZE (ch)) |
| 1356 |
THEN |
| 1357 |
END |
| 1358 |
END WriteChar ; |
| 1359 |
|
| 1360 |
|
| 1361 |
(* |
| 1362 |
WriteCardinal - writes a CARDINAL to file, f. |
| 1363 |
It writes the binary image of the cardinal |
| 1364 |
to file, f. |
| 1365 |
*) |
| 1366 |
|
| 1367 |
PROCEDURE WriteCardinal (f: File; c: CARDINAL) ; |
| 1368 |
BEGIN |
| 1369 |
WriteAny(f, c) |
| 1370 |
END WriteCardinal ; |
| 1371 |
|
| 1372 |
|
| 1373 |
(* |
| 1374 |
ReadCardinal - reads a CARDINAL from file, f. |
| 1375 |
It reads a binary image of a CARDINAL |
| 1376 |
from a file, f. |
| 1377 |
*) |
| 1378 |
|
| 1379 |
PROCEDURE ReadCardinal (f: File) : CARDINAL ; |
| 1380 |
VAR |
| 1381 |
c: CARDINAL ; |
| 1382 |
BEGIN |
| 1383 |
ReadAny(f, c) ; |
| 1384 |
RETURN( c ) |
| 1385 |
END ReadCardinal ; |
| 1386 |
|
| 1387 |
|
| 1388 |
(* |
| 1389 |
ReadString - reads a string from file, f, into string, a. |
| 1390 |
It terminates the string if HIGH is reached or |
| 1391 |
if a newline is seen or an error occurs. |
| 1392 |
*) |
| 1393 |
|
| 1394 |
PROCEDURE ReadString (f: File; VAR a: ARRAY OF CHAR) ; |
| 1395 |
VAR |
| 1396 |
high, |
| 1397 |
i : CARDINAL ; |
| 1398 |
ch : CHAR ; |
| 1399 |
BEGIN |
| 1400 |
CheckAccess(f, openedforread, FALSE) ; |
| 1401 |
high := HIGH(a) ; |
| 1402 |
i := 0 ; |
| 1403 |
REPEAT |
| 1404 |
ch := ReadChar(f) ; |
| 1405 |
IF i<=high |
| 1406 |
THEN |
| 1407 |
IF (ch=nl) OR (NOT IsNoError(f)) OR EOF(f) |
| 1408 |
THEN |
| 1409 |
a[i] := nul ; |
| 1410 |
INC(i) |
| 1411 |
ELSE |
| 1412 |
a[i] := ch ; |
| 1413 |
INC(i) |
| 1414 |
END |
| 1415 |
END |
| 1416 |
UNTIL (ch=nl) OR (i>high) OR (NOT IsNoError(f)) OR EOF(f) |
| 1417 |
END ReadString ; |
| 1418 |
|
| 1419 |
|
| 1420 |
(* |
| 1421 |
SetPositionFromBeginning - sets the position from the beginning of the file. |
| 1422 |
*) |
| 1423 |
|
| 1424 |
PROCEDURE SetPositionFromBeginning (f: File; pos: LONGINT) ; |
| 1425 |
VAR |
| 1426 |
offset: LONGINT ; |
| 1427 |
fd : FileDescriptor ; |
| 1428 |
BEGIN |
| 1429 |
IF f#Error |
| 1430 |
THEN |
| 1431 |
fd := GetIndice(FileInfo, f) ; |
| 1432 |
IF fd#NIL |
| 1433 |
THEN |
| 1434 |
WITH fd^ DO |
| 1435 |
(* always force the lseek, until we are confident that abspos is always correct, |
| 1436 |
basically it needs some hard testing before we should remove the OR TRUE. *) |
| 1437 |
IF (abspos#pos) OR TRUE |
| 1438 |
THEN |
| 1439 |
FlushBuffer(f) ; |
| 1440 |
IF buffer#NIL |
| 1441 |
THEN |
| 1442 |
WITH buffer^ DO |
| 1443 |
IF output |
| 1444 |
THEN |
| 1445 |
left := size |
| 1446 |
ELSE |
| 1447 |
left := 0 |
| 1448 |
END ; |
| 1449 |
position := 0 ; |
| 1450 |
filled := 0 |
| 1451 |
END |
| 1452 |
END ; |
| 1453 |
offset := lseek(unixfd, pos, SEEK_SET) ; |
| 1454 |
IF (offset>=0) AND (pos=offset) |
| 1455 |
THEN |
| 1456 |
abspos := pos |
| 1457 |
ELSE |
| 1458 |
state := failed ; |
| 1459 |
abspos := 0 |
| 1460 |
END ; |
| 1461 |
IF buffer#NIL |
| 1462 |
THEN |
| 1463 |
buffer^.valid := FALSE ; |
| 1464 |
buffer^.bufstart := abspos |
| 1465 |
END |
| 1466 |
END |
| 1467 |
END |
| 1468 |
END |
| 1469 |
END |
| 1470 |
END SetPositionFromBeginning ; |
| 1471 |
|
| 1472 |
|
| 1473 |
(* |
| 1474 |
SetPositionFromEnd - sets the position from the end of the file. |
| 1475 |
*) |
| 1476 |
|
| 1477 |
PROCEDURE SetPositionFromEnd (f: File; pos: LONGINT) ; |
| 1478 |
VAR |
| 1479 |
offset: LONGINT ; |
| 1480 |
fd : FileDescriptor ; |
| 1481 |
BEGIN |
| 1482 |
IF f#Error |
| 1483 |
THEN |
| 1484 |
fd := GetIndice(FileInfo, f) ; |
| 1485 |
IF fd#NIL |
| 1486 |
THEN |
| 1487 |
WITH fd^ DO |
| 1488 |
FlushBuffer(f) ; |
| 1489 |
IF buffer#NIL |
| 1490 |
THEN |
| 1491 |
WITH buffer^ DO |
| 1492 |
IF output |
| 1493 |
THEN |
| 1494 |
left := size |
| 1495 |
ELSE |
| 1496 |
left := 0 |
| 1497 |
END ; |
| 1498 |
position := 0 ; |
| 1499 |
filled := 0 |
| 1500 |
END |
| 1501 |
END ; |
| 1502 |
offset := lseek(unixfd, pos, SEEK_END) ; |
| 1503 |
IF offset>=0 |
| 1504 |
THEN |
| 1505 |
abspos := offset ; |
| 1506 |
ELSE |
| 1507 |
state := failed ; |
| 1508 |
abspos := 0 ; |
| 1509 |
offset := 0 |
| 1510 |
END ; |
| 1511 |
IF buffer#NIL |
| 1512 |
THEN |
| 1513 |
buffer^.valid := FALSE ; |
| 1514 |
buffer^.bufstart := offset |
| 1515 |
END |
| 1516 |
END |
| 1517 |
END |
| 1518 |
END |
| 1519 |
END SetPositionFromEnd ; |
| 1520 |
|
| 1521 |
|
| 1522 |
(* |
| 1523 |
FindPosition - returns the current absolute position in file, f. |
| 1524 |
*) |
| 1525 |
|
| 1526 |
PROCEDURE FindPosition (f: File) : LONGINT ; |
| 1527 |
VAR |
| 1528 |
fd: FileDescriptor ; |
| 1529 |
BEGIN |
| 1530 |
IF f#Error |
| 1531 |
THEN |
| 1532 |
fd := GetIndice(FileInfo, f) ; |
| 1533 |
IF fd#NIL |
| 1534 |
THEN |
| 1535 |
WITH fd^ DO |
| 1536 |
IF (buffer=NIL) OR (NOT buffer^.valid) |
| 1537 |
THEN |
| 1538 |
RETURN( abspos ) |
| 1539 |
ELSE |
| 1540 |
WITH buffer^ DO |
| 1541 |
RETURN( bufstart+VAL(LONGINT, position) ) |
| 1542 |
END |
| 1543 |
END |
| 1544 |
END |
| 1545 |
END |
| 1546 |
END ; |
| 1547 |
RETURN( 0 ) |
| 1548 |
END FindPosition ; |
| 1549 |
|
| 1550 |
|
| 1551 |
(* |
| 1552 |
GetFileName - assigns, a, with the filename associated with, f. |
| 1553 |
*) |
| 1554 |
|
| 1555 |
PROCEDURE GetFileName (f: File; VAR a: ARRAY OF CHAR) ; |
| 1556 |
VAR |
| 1557 |
i : CARDINAL ; |
| 1558 |
p : POINTER TO CHAR ; |
| 1559 |
fd: FileDescriptor ; |
| 1560 |
BEGIN |
| 1561 |
IF f#Error |
| 1562 |
THEN |
| 1563 |
fd := GetIndice(FileInfo, f) ; |
| 1564 |
IF fd=NIL |
| 1565 |
THEN |
| 1566 |
FormatError('this file has probably been closed and not reopened successfully or alternatively never opened\n') ; |
| 1567 |
HALT |
| 1568 |
ELSE |
| 1569 |
WITH fd^.name DO |
| 1570 |
IF address=NIL |
| 1571 |
THEN |
| 1572 |
StrCopy('', a) |
| 1573 |
ELSE |
| 1574 |
p := address ; |
| 1575 |
i := 0 ; |
| 1576 |
WHILE (p^#nul) AND (i<=HIGH(a)) DO |
| 1577 |
a[i] := p^ ; |
| 1578 |
INC(p) ; |
| 1579 |
INC(i) |
| 1580 |
END |
| 1581 |
END |
| 1582 |
END |
| 1583 |
END |
| 1584 |
END |
| 1585 |
END GetFileName ; |
| 1586 |
|
| 1587 |
|
| 1588 |
(* |
| 1589 |
getFileName - returns the address of the filename associated with, f. |
| 1590 |
*) |
| 1591 |
|
| 1592 |
PROCEDURE getFileName (f: File) : ADDRESS ; |
| 1593 |
VAR |
| 1594 |
fd: FileDescriptor ; |
| 1595 |
BEGIN |
| 1596 |
IF f#Error |
| 1597 |
THEN |
| 1598 |
fd := GetIndice(FileInfo, f) ; |
| 1599 |
IF fd=NIL |
| 1600 |
THEN |
| 1601 |
FormatError('this file has probably been closed and not reopened successfully or alternatively never opened\n') ; |
| 1602 |
HALT |
| 1603 |
ELSE |
| 1604 |
RETURN fd^.name.address |
| 1605 |
END |
| 1606 |
END ; |
| 1607 |
RETURN NIL |
| 1608 |
END getFileName ; |
| 1609 |
|
| 1610 |
|
| 1611 |
(* |
| 1612 |
getFileNameLength - returns the number of characters associated with filename, f. |
| 1613 |
*) |
| 1614 |
|
| 1615 |
PROCEDURE getFileNameLength (f: File) : CARDINAL ; |
| 1616 |
VAR |
| 1617 |
fd: FileDescriptor ; |
| 1618 |
BEGIN |
| 1619 |
IF f#Error |
| 1620 |
THEN |
| 1621 |
fd := GetIndice(FileInfo, f) ; |
| 1622 |
IF fd=NIL |
| 1623 |
THEN |
| 1624 |
FormatError('this file has probably been closed and not reopened successfully or alternatively never opened\n') ; |
| 1625 |
HALT |
| 1626 |
ELSE |
| 1627 |
RETURN fd^.name.size |
| 1628 |
END |
| 1629 |
END ; |
| 1630 |
RETURN 0 |
| 1631 |
END getFileNameLength ; |
| 1632 |
|
| 1633 |
|
| 1634 |
(* |
| 1635 |
PreInitialize - preinitialize the file descriptor. |
| 1636 |
*) |
| 1637 |
|
| 1638 |
PROCEDURE PreInitialize (f: File; fname: ARRAY OF CHAR; |
| 1639 |
state: FileStatus; use: FileUsage; |
| 1640 |
towrite: BOOLEAN; osfd: INTEGER; bufsize: CARDINAL) ; |
| 1641 |
VAR |
| 1642 |
fd, fe: FileDescriptor ; |
| 1643 |
BEGIN |
| 1644 |
IF InitializeFile(f, ADR(fname), StrLen(fname), state, use, towrite, bufsize)=f |
| 1645 |
THEN |
| 1646 |
fd := GetIndice(FileInfo, f) ; |
| 1647 |
IF f=Error |
| 1648 |
THEN |
| 1649 |
fe := GetIndice(FileInfo, StdErr) ; |
| 1650 |
IF fe=NIL |
| 1651 |
THEN |
| 1652 |
HALT |
| 1653 |
ELSE |
| 1654 |
fd^.unixfd := fe^.unixfd (* the error channel *) |
| 1655 |
END |
| 1656 |
ELSE |
| 1657 |
fd^.unixfd := osfd |
| 1658 |
END |
| 1659 |
ELSE |
| 1660 |
HALT |
| 1661 |
END |
| 1662 |
END PreInitialize ; |
| 1663 |
|
| 1664 |
|
| 1665 |
(* |
| 1666 |
FlushOutErr - flushes, StdOut, and, StdErr. |
| 1667 |
It is also called when the application calls M2RTS.Terminate. |
| 1668 |
(which is automatically placed in program modules by the GM2 |
| 1669 |
scaffold). |
| 1670 |
*) |
| 1671 |
|
| 1672 |
PROCEDURE FlushOutErr ; |
| 1673 |
BEGIN |
| 1674 |
IF IsNoError(StdOut) |
| 1675 |
THEN |
| 1676 |
FlushBuffer(StdOut) |
| 1677 |
END ; |
| 1678 |
IF IsNoError(StdErr) |
| 1679 |
THEN |
| 1680 |
FlushBuffer(StdErr) |
| 1681 |
END |
| 1682 |
END FlushOutErr ; |
| 1683 |
|
| 1684 |
|
| 1685 |
(* |
| 1686 |
Init - initialize the modules, global variables. |
| 1687 |
*) |
| 1688 |
|
| 1689 |
PROCEDURE Init ; |
| 1690 |
BEGIN |
| 1691 |
FileInfo := InitIndex(0) ; |
| 1692 |
Error := 0 ; |
| 1693 |
PreInitialize(Error , 'error' , toomanyfilesopen, unused , FALSE, -1, 0) ; |
| 1694 |
StdIn := 1 ; |
| 1695 |
PreInitialize(StdIn , '<stdin>' , successful , openedforread , FALSE, 0, MaxBufferLength) ; |
| 1696 |
StdOut := 2 ; |
| 1697 |
PreInitialize(StdOut , '<stdout>', successful , openedforwrite, TRUE, 1, MaxBufferLength) ; |
| 1698 |
StdErr := 3 ; |
| 1699 |
PreInitialize(StdErr , '<stderr>', successful , openedforwrite, TRUE, 2, MaxBufferLength) ; |
| 1700 |
IF NOT InstallTerminationProcedure(FlushOutErr) |
| 1701 |
THEN |
| 1702 |
HALT |
| 1703 |
END |
| 1704 |
END Init ; |
| 1705 |
|
| 1706 |
|
| 1707 |
BEGIN |
| 1708 |
Init |
| 1709 |
FINALLY |
| 1710 |
FlushOutErr |
| 1711 |
END FIO. |