| 1 |
/* do not edit automatically generated by mc from FIO. */ |
| 2 |
/* FIO.mod provides a simple buffered file input/output library. |
| 3 |
|
| 4 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 9 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
Under Section 7 of GPL version 3, you are granted additional |
| 20 |
permissions described in the GCC Runtime Library Exception, version |
| 21 |
3.1, as published by the Free Software Foundation. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License and |
| 24 |
a copy of the GCC Runtime Library Exception along with this program; |
| 25 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 26 |
<http://www.gnu.org/licenses/>. */ |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
#include "system.h" |
| 30 |
#include <stdbool.h> |
| 31 |
# if !defined (PROC_D) |
| 32 |
# define PROC_D |
| 33 |
typedef void (*PROC_t) (void); |
| 34 |
typedef struct { PROC_t proc; } PROC; |
| 35 |
# endif |
| 36 |
|
| 37 |
# if !defined (TRUE) |
| 38 |
# define TRUE (1==1) |
| 39 |
# endif |
| 40 |
|
| 41 |
# if !defined (FALSE) |
| 42 |
# define FALSE (1==0) |
| 43 |
# endif |
| 44 |
|
| 45 |
# include "GStorage.h" |
| 46 |
# include "Gmcrts.h" |
| 47 |
#if defined(__cplusplus) |
| 48 |
# undef NULL |
| 49 |
# define NULL 0 |
| 50 |
#endif |
| 51 |
#define _FIO_C |
| 52 |
|
| 53 |
#include "GFIO.h" |
| 54 |
# include "GSYSTEM.h" |
| 55 |
# include "GASCII.h" |
| 56 |
# include "GStrLib.h" |
| 57 |
# include "GStorage.h" |
| 58 |
# include "GNumberIO.h" |
| 59 |
# include "GIndexing.h" |
| 60 |
# include "GM2RTS.h" |
| 61 |
# include "Glibc.h" |
| 62 |
# include "Gwrapc.h" |
| 63 |
|
| 64 |
typedef unsigned int FIO_File; |
| 65 |
|
| 66 |
# define MaxBufferLength (1024*16) |
| 67 |
# define MaxErrorString (1024*8) |
| 68 |
# define CreatePermissions 0666 |
| 69 |
typedef struct FIO_NameInfo_r FIO_NameInfo; |
| 70 |
|
| 71 |
typedef struct FIO_buf_r FIO_buf; |
| 72 |
|
| 73 |
typedef FIO_buf *FIO_Buffer; |
| 74 |
|
| 75 |
typedef struct FIO_fds_r FIO_fds; |
| 76 |
|
| 77 |
typedef FIO_fds *FIO_FileDescriptor; |
| 78 |
|
| 79 |
typedef struct FIO__T7_a FIO__T7; |
| 80 |
|
| 81 |
typedef char *FIO_PtrToChar; |
| 82 |
|
| 83 |
typedef enum {FIO_successful, FIO_outofmemory, FIO_toomanyfilesopen, FIO_failed, FIO_connectionfailure, FIO_endofline, FIO_endoffile} FIO_FileStatus; |
| 84 |
|
| 85 |
typedef enum {FIO_unused, FIO_openedforread, FIO_openedforwrite, FIO_openedforrandom} FIO_FileUsage; |
| 86 |
|
| 87 |
struct FIO_NameInfo_r { |
| 88 |
void * address; |
| 89 |
unsigned int size; |
| 90 |
}; |
| 91 |
|
| 92 |
struct FIO_buf_r { |
| 93 |
bool valid; |
| 94 |
long int bufstart; |
| 95 |
unsigned int position; |
| 96 |
void * address; |
| 97 |
unsigned int filled; |
| 98 |
unsigned int size; |
| 99 |
unsigned int left; |
| 100 |
FIO__T7 * contents; |
| 101 |
}; |
| 102 |
|
| 103 |
struct FIO__T7_a { char array[MaxBufferLength+1]; }; |
| 104 |
struct FIO_fds_r { |
| 105 |
int unixfd; |
| 106 |
FIO_NameInfo name; |
| 107 |
FIO_FileStatus state; |
| 108 |
FIO_FileUsage usage; |
| 109 |
bool output; |
| 110 |
FIO_Buffer buffer; |
| 111 |
long int abspos; |
| 112 |
}; |
| 113 |
|
| 114 |
static Indexing_Index FileInfo; |
| 115 |
static FIO_File Error; |
| 116 |
|
| 117 |
/* |
| 118 |
IsNoError - returns a TRUE if no error has occured on file, f. |
| 119 |
*/ |
| 120 |
|
| 121 |
extern "C" bool FIO_IsNoError (FIO_File f); |
| 122 |
|
| 123 |
/* |
| 124 |
IsActive - returns TRUE if the file, f, is still active. |
| 125 |
*/ |
| 126 |
|
| 127 |
extern "C" bool FIO_IsActive (FIO_File f); |
| 128 |
extern "C" bool FIO_Exists (const char *fname_, unsigned int _fname_high); |
| 129 |
extern "C" FIO_File FIO_OpenToRead (const char *fname_, unsigned int _fname_high); |
| 130 |
extern "C" FIO_File FIO_OpenToWrite (const char *fname_, unsigned int _fname_high); |
| 131 |
extern "C" FIO_File FIO_OpenForRandom (const char *fname_, unsigned int _fname_high, bool towrite, bool newfile); |
| 132 |
|
| 133 |
/* |
| 134 |
Close - close a file which has been previously opened using: |
| 135 |
OpenToRead, OpenToWrite, OpenForRandom. |
| 136 |
It is correct to close a file which has an error status. |
| 137 |
*/ |
| 138 |
|
| 139 |
extern "C" void FIO_Close (FIO_File f); |
| 140 |
|
| 141 |
/* |
| 142 |
exists - returns TRUE if a file named, fname exists for reading. |
| 143 |
*/ |
| 144 |
|
| 145 |
extern "C" bool FIO_exists (void * fname, unsigned int flength); |
| 146 |
|
| 147 |
/* |
| 148 |
openToRead - attempts to open a file, fname, for reading and |
| 149 |
it returns this file. |
| 150 |
The success of this operation can be checked by |
| 151 |
calling IsNoError. |
| 152 |
*/ |
| 153 |
|
| 154 |
extern "C" FIO_File FIO_openToRead (void * fname, unsigned int flength); |
| 155 |
|
| 156 |
/* |
| 157 |
openToWrite - attempts to open a file, fname, for write and |
| 158 |
it returns this file. |
| 159 |
The success of this operation can be checked by |
| 160 |
calling IsNoError. |
| 161 |
*/ |
| 162 |
|
| 163 |
extern "C" FIO_File FIO_openToWrite (void * fname, unsigned int flength); |
| 164 |
|
| 165 |
/* |
| 166 |
openForRandom - attempts to open a file, fname, for random access |
| 167 |
read or write and it returns this file. |
| 168 |
The success of this operation can be checked by |
| 169 |
calling IsNoError. |
| 170 |
towrite, determines whether the file should be |
| 171 |
opened for writing or reading. |
| 172 |
*/ |
| 173 |
|
| 174 |
extern "C" FIO_File FIO_openForRandom (void * fname, unsigned int flength, bool towrite, bool newfile); |
| 175 |
|
| 176 |
/* |
| 177 |
FlushBuffer - flush contents of file, f. |
| 178 |
*/ |
| 179 |
|
| 180 |
extern "C" void FIO_FlushBuffer (FIO_File f); |
| 181 |
|
| 182 |
/* |
| 183 |
ReadNBytes - reads nBytes of a file into memory area, dest, returning |
| 184 |
the number of bytes actually read. |
| 185 |
This function will consume from the buffer and then |
| 186 |
perform direct libc reads. It is ideal for large reads. |
| 187 |
*/ |
| 188 |
|
| 189 |
extern "C" unsigned int FIO_ReadNBytes (FIO_File f, unsigned int nBytes, void * dest); |
| 190 |
|
| 191 |
/* |
| 192 |
ReadAny - reads HIGH (a) + 1 bytes into, a. All input |
| 193 |
is fully buffered, unlike ReadNBytes and thus is more |
| 194 |
suited to small reads. |
| 195 |
*/ |
| 196 |
|
| 197 |
extern "C" void FIO_ReadAny (FIO_File f, unsigned char *a, unsigned int _a_high); |
| 198 |
|
| 199 |
/* |
| 200 |
WriteNBytes - writes nBytes from memory area src to a file |
| 201 |
returning the number of bytes actually written. |
| 202 |
This function will flush the buffer and then |
| 203 |
write the nBytes using a direct write from libc. |
| 204 |
It is ideal for large writes. |
| 205 |
*/ |
| 206 |
|
| 207 |
extern "C" unsigned int FIO_WriteNBytes (FIO_File f, unsigned int nBytes, void * src); |
| 208 |
|
| 209 |
/* |
| 210 |
WriteAny - writes HIGH (a) + 1 bytes onto, file, f. All output |
| 211 |
is fully buffered, unlike WriteNBytes and thus is more |
| 212 |
suited to small writes. |
| 213 |
*/ |
| 214 |
|
| 215 |
extern "C" void FIO_WriteAny (FIO_File f, unsigned char *a, unsigned int _a_high); |
| 216 |
|
| 217 |
/* |
| 218 |
WriteChar - writes a single character to file, f. |
| 219 |
*/ |
| 220 |
|
| 221 |
extern "C" void FIO_WriteChar (FIO_File f, char ch); |
| 222 |
|
| 223 |
/* |
| 224 |
EOF - tests to see whether a file, f, has reached end of file. |
| 225 |
*/ |
| 226 |
|
| 227 |
extern "C" bool FIO_EOF (FIO_File f); |
| 228 |
|
| 229 |
/* |
| 230 |
EOLN - tests to see whether a file, f, is upon a newline. |
| 231 |
It does NOT consume the newline. |
| 232 |
*/ |
| 233 |
|
| 234 |
extern "C" bool FIO_EOLN (FIO_File f); |
| 235 |
|
| 236 |
/* |
| 237 |
WasEOLN - tests to see whether a file, f, has just seen a newline. |
| 238 |
*/ |
| 239 |
|
| 240 |
extern "C" bool FIO_WasEOLN (FIO_File f); |
| 241 |
|
| 242 |
/* |
| 243 |
ReadChar - returns a character read from file f. |
| 244 |
Sensible to check with IsNoError or EOF after calling |
| 245 |
this function. |
| 246 |
*/ |
| 247 |
|
| 248 |
extern "C" char FIO_ReadChar (FIO_File f); |
| 249 |
|
| 250 |
/* |
| 251 |
UnReadChar - replaces a character, ch, back into file f. |
| 252 |
This character must have been read by ReadChar |
| 253 |
and it does not allow successive calls. It may |
| 254 |
only be called if the previous read was successful |
| 255 |
or end of file was seen. |
| 256 |
If the state was previously endoffile then it |
| 257 |
is altered to successful. |
| 258 |
Otherwise it is left alone. |
| 259 |
*/ |
| 260 |
|
| 261 |
extern "C" void FIO_UnReadChar (FIO_File f, char ch); |
| 262 |
|
| 263 |
/* |
| 264 |
WriteLine - writes out a linefeed to file, f. |
| 265 |
*/ |
| 266 |
|
| 267 |
extern "C" void FIO_WriteLine (FIO_File f); |
| 268 |
|
| 269 |
/* |
| 270 |
WriteString - writes a string to file, f. |
| 271 |
*/ |
| 272 |
|
| 273 |
extern "C" void FIO_WriteString (FIO_File f, const char *a_, unsigned int _a_high); |
| 274 |
|
| 275 |
/* |
| 276 |
ReadString - reads a string from file, f, into string, a. |
| 277 |
It terminates the string if HIGH is reached or |
| 278 |
if a newline is seen or an error occurs. |
| 279 |
*/ |
| 280 |
|
| 281 |
extern "C" void FIO_ReadString (FIO_File f, char *a, unsigned int _a_high); |
| 282 |
|
| 283 |
/* |
| 284 |
WriteCardinal - writes a CARDINAL to file, f. |
| 285 |
It writes the binary image of the cardinal |
| 286 |
to file, f. |
| 287 |
*/ |
| 288 |
|
| 289 |
extern "C" void FIO_WriteCardinal (FIO_File f, unsigned int c); |
| 290 |
|
| 291 |
/* |
| 292 |
ReadCardinal - reads a CARDINAL from file, f. |
| 293 |
It reads a binary image of a CARDINAL |
| 294 |
from a file, f. |
| 295 |
*/ |
| 296 |
|
| 297 |
extern "C" unsigned int FIO_ReadCardinal (FIO_File f); |
| 298 |
|
| 299 |
/* |
| 300 |
GetUnixFileDescriptor - returns the UNIX file descriptor of a file. |
| 301 |
*/ |
| 302 |
|
| 303 |
extern "C" int FIO_GetUnixFileDescriptor (FIO_File f); |
| 304 |
|
| 305 |
/* |
| 306 |
SetPositionFromBeginning - sets the position from the beginning of the file. |
| 307 |
*/ |
| 308 |
|
| 309 |
extern "C" void FIO_SetPositionFromBeginning (FIO_File f, long int pos); |
| 310 |
|
| 311 |
/* |
| 312 |
SetPositionFromEnd - sets the position from the end of the file. |
| 313 |
*/ |
| 314 |
|
| 315 |
extern "C" void FIO_SetPositionFromEnd (FIO_File f, long int pos); |
| 316 |
|
| 317 |
/* |
| 318 |
FindPosition - returns the current absolute position in file, f. |
| 319 |
*/ |
| 320 |
|
| 321 |
extern "C" long int FIO_FindPosition (FIO_File f); |
| 322 |
|
| 323 |
/* |
| 324 |
GetFileName - assigns, a, with the filename associated with, f. |
| 325 |
*/ |
| 326 |
|
| 327 |
extern "C" void FIO_GetFileName (FIO_File f, char *a, unsigned int _a_high); |
| 328 |
|
| 329 |
/* |
| 330 |
getFileName - returns the address of the filename associated with, f. |
| 331 |
*/ |
| 332 |
|
| 333 |
extern "C" void * FIO_getFileName (FIO_File f); |
| 334 |
|
| 335 |
/* |
| 336 |
getFileNameLength - returns the number of characters associated with filename, f. |
| 337 |
*/ |
| 338 |
|
| 339 |
extern "C" unsigned int FIO_getFileNameLength (FIO_File f); |
| 340 |
|
| 341 |
/* |
| 342 |
FlushOutErr - flushes, StdOut, and, StdErr. |
| 343 |
It is also called when the application calls M2RTS.Terminate. |
| 344 |
(which is automatically placed in program modules by the GM2 |
| 345 |
scaffold). |
| 346 |
*/ |
| 347 |
|
| 348 |
extern "C" void FIO_FlushOutErr (void); |
| 349 |
|
| 350 |
/* |
| 351 |
Max - returns the maximum of two values. |
| 352 |
*/ |
| 353 |
|
| 354 |
static unsigned int Max (unsigned int a, unsigned int b); |
| 355 |
|
| 356 |
/* |
| 357 |
Min - returns the minimum of two values. |
| 358 |
*/ |
| 359 |
|
| 360 |
static unsigned int Min (unsigned int a, unsigned int b); |
| 361 |
|
| 362 |
/* |
| 363 |
GetNextFreeDescriptor - returns the index to the FileInfo array indicating |
| 364 |
the next free slot. |
| 365 |
*/ |
| 366 |
|
| 367 |
static FIO_File GetNextFreeDescriptor (void); |
| 368 |
|
| 369 |
/* |
| 370 |
SetState - sets the field, state, of file, f, to, s. |
| 371 |
*/ |
| 372 |
|
| 373 |
static void SetState (FIO_File f, FIO_FileStatus s); |
| 374 |
|
| 375 |
/* |
| 376 |
InitializeFile - initialize a file descriptor |
| 377 |
*/ |
| 378 |
|
| 379 |
static FIO_File InitializeFile (FIO_File f, void * fname, unsigned int flength, FIO_FileStatus fstate, FIO_FileUsage use, bool towrite, unsigned int buflength); |
| 380 |
|
| 381 |
/* |
| 382 |
ConnectToUnix - connects a FIO file to a UNIX file descriptor. |
| 383 |
*/ |
| 384 |
|
| 385 |
static void ConnectToUnix (FIO_File f, bool towrite, bool newfile); |
| 386 |
|
| 387 |
/* |
| 388 |
ReadFromBuffer - attempts to read, nBytes, from file, f. |
| 389 |
It firstly consumes the buffer and then performs |
| 390 |
direct unbuffered reads. This should only be used |
| 391 |
when wishing to read large files. |
| 392 |
|
| 393 |
The actual number of bytes read is returned. |
| 394 |
-1 is returned if EOF is reached. |
| 395 |
*/ |
| 396 |
|
| 397 |
static int ReadFromBuffer (FIO_File f, void * a, unsigned int nBytes); |
| 398 |
|
| 399 |
/* |
| 400 |
BufferedRead - will read, nBytes, through the buffer. |
| 401 |
Similar to ReadFromBuffer, but this function will always |
| 402 |
read into the buffer before copying into memory. |
| 403 |
|
| 404 |
Useful when performing small reads. |
| 405 |
*/ |
| 406 |
|
| 407 |
static int BufferedRead (FIO_File f, unsigned int nBytes, void * dest); |
| 408 |
|
| 409 |
/* |
| 410 |
HandleEscape - translates |
| 411 |
and \t into their respective ascii codes. |
| 412 |
*/ |
| 413 |
|
| 414 |
static void HandleEscape (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, unsigned int *i, unsigned int *j, unsigned int HighSrc, unsigned int HighDest); |
| 415 |
|
| 416 |
/* |
| 417 |
Cast - casts a := b |
| 418 |
*/ |
| 419 |
|
| 420 |
static void Cast (unsigned char *a, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high); |
| 421 |
|
| 422 |
/* |
| 423 |
StringFormat1 - converts string, src, into, dest, together with encapsulated |
| 424 |
entity, w. It only formats the first %s or %d with n. |
| 425 |
*/ |
| 426 |
|
| 427 |
static void StringFormat1 (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, const unsigned char *w_, unsigned int _w_high); |
| 428 |
|
| 429 |
/* |
| 430 |
FormatError - provides a orthoganal counterpart to the procedure below. |
| 431 |
*/ |
| 432 |
|
| 433 |
static void FormatError (const char *a_, unsigned int _a_high); |
| 434 |
|
| 435 |
/* |
| 436 |
FormatError1 - generic error procedure taking standard format string |
| 437 |
and single parameter. |
| 438 |
*/ |
| 439 |
|
| 440 |
static void FormatError1 (const char *a_, unsigned int _a_high, const unsigned char *w_, unsigned int _w_high); |
| 441 |
|
| 442 |
/* |
| 443 |
FormatError2 - generic error procedure taking standard format string |
| 444 |
and two parameters. |
| 445 |
*/ |
| 446 |
|
| 447 |
static void FormatError2 (const char *a_, unsigned int _a_high, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high); |
| 448 |
|
| 449 |
/* |
| 450 |
CheckAccess - checks to see whether a file f has been |
| 451 |
opened for read/write. |
| 452 |
*/ |
| 453 |
|
| 454 |
static void CheckAccess (FIO_File f, FIO_FileUsage use, bool towrite); |
| 455 |
|
| 456 |
/* |
| 457 |
SetEndOfLine - |
| 458 |
*/ |
| 459 |
|
| 460 |
static void SetEndOfLine (FIO_File f, char ch); |
| 461 |
|
| 462 |
/* |
| 463 |
BufferedWrite - will write, nBytes, through the buffer. |
| 464 |
Similar to WriteNBytes, but this function will always |
| 465 |
write into the buffer before copying into memory. |
| 466 |
|
| 467 |
Useful when performing small writes. |
| 468 |
*/ |
| 469 |
|
| 470 |
static int BufferedWrite (FIO_File f, unsigned int nBytes, void * src); |
| 471 |
|
| 472 |
/* |
| 473 |
PreInitialize - preinitialize the file descriptor. |
| 474 |
*/ |
| 475 |
|
| 476 |
static void PreInitialize (FIO_File f, const char *fname_, unsigned int _fname_high, FIO_FileStatus state, FIO_FileUsage use, bool towrite, int osfd, unsigned int bufsize); |
| 477 |
|
| 478 |
/* |
| 479 |
Init - initialize the modules, global variables. |
| 480 |
*/ |
| 481 |
|
| 482 |
static void Init (void); |
| 483 |
|
| 484 |
|
| 485 |
/* |
| 486 |
Max - returns the maximum of two values. |
| 487 |
*/ |
| 488 |
|
| 489 |
static unsigned int Max (unsigned int a, unsigned int b) |
| 490 |
{ |
| 491 |
if (a > b) |
| 492 |
{ |
| 493 |
return a; |
| 494 |
} |
| 495 |
else |
| 496 |
{ |
| 497 |
return b; |
| 498 |
} |
| 499 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 500 |
__builtin_unreachable (); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
/* |
| 505 |
Min - returns the minimum of two values. |
| 506 |
*/ |
| 507 |
|
| 508 |
static unsigned int Min (unsigned int a, unsigned int b) |
| 509 |
{ |
| 510 |
if (a < b) |
| 511 |
{ |
| 512 |
return a; |
| 513 |
} |
| 514 |
else |
| 515 |
{ |
| 516 |
return b; |
| 517 |
} |
| 518 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 519 |
__builtin_unreachable (); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
/* |
| 524 |
GetNextFreeDescriptor - returns the index to the FileInfo array indicating |
| 525 |
the next free slot. |
| 526 |
*/ |
| 527 |
|
| 528 |
static FIO_File GetNextFreeDescriptor (void) |
| 529 |
{ |
| 530 |
FIO_File f; |
| 531 |
FIO_File h; |
| 532 |
FIO_FileDescriptor fd; |
| 533 |
|
| 534 |
f = Error+1; |
| 535 |
h = Indexing_HighIndice (FileInfo); |
| 536 |
for (;;) |
| 537 |
{ |
| 538 |
if (f <= h) |
| 539 |
{ |
| 540 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 541 |
if (fd == NULL) |
| 542 |
{ |
| 543 |
return f; |
| 544 |
} |
| 545 |
} |
| 546 |
f += 1; |
| 547 |
if (f > h) |
| 548 |
{ |
| 549 |
Indexing_PutIndice (FileInfo, f, NULL); /* create new slot */ |
| 550 |
return f; /* create new slot */ |
| 551 |
} |
| 552 |
} |
| 553 |
ReturnException ("../../gcc/m2/gm2-libs/FIO.def", 25, 1); |
| 554 |
__builtin_unreachable (); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
/* |
| 559 |
SetState - sets the field, state, of file, f, to, s. |
| 560 |
*/ |
| 561 |
|
| 562 |
static void SetState (FIO_File f, FIO_FileStatus s) |
| 563 |
{ |
| 564 |
FIO_FileDescriptor fd; |
| 565 |
|
| 566 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 567 |
fd->state = s; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
/* |
| 572 |
InitializeFile - initialize a file descriptor |
| 573 |
*/ |
| 574 |
|
| 575 |
static FIO_File InitializeFile (FIO_File f, void * fname, unsigned int flength, FIO_FileStatus fstate, FIO_FileUsage use, bool towrite, unsigned int buflength) |
| 576 |
{ |
| 577 |
FIO_PtrToChar p; |
| 578 |
FIO_FileDescriptor fd; |
| 579 |
|
| 580 |
Storage_ALLOCATE ((void **) &fd, sizeof (FIO_fds)); |
| 581 |
if (fd == NULL) |
| 582 |
{ |
| 583 |
SetState (Error, FIO_outofmemory); |
| 584 |
return Error; |
| 585 |
} |
| 586 |
else |
| 587 |
{ |
| 588 |
Indexing_PutIndice (FileInfo, f, reinterpret_cast <void *> (fd)); |
| 589 |
fd->name.size = flength+1; /* need to guarantee the nul for C */ |
| 590 |
fd->usage = use; /* need to guarantee the nul for C */ |
| 591 |
fd->output = towrite; |
| 592 |
Storage_ALLOCATE (&fd->name.address, fd->name.size); |
| 593 |
if (fd->name.address == NULL) |
| 594 |
{ |
| 595 |
fd->state = FIO_outofmemory; |
| 596 |
return f; |
| 597 |
} |
| 598 |
fd->name.address = libc_strncpy (fd->name.address, fname, flength); |
| 599 |
/* and assign nul to the last byte */ |
| 600 |
p = static_cast<FIO_PtrToChar> (fd->name.address); |
| 601 |
p += flength; |
| 602 |
(*p) = ASCII_nul; |
| 603 |
fd->abspos = 0; |
| 604 |
/* now for the buffer */ |
| 605 |
Storage_ALLOCATE ((void **) &fd->buffer, sizeof (FIO_buf)); |
| 606 |
if (fd->buffer == NULL) |
| 607 |
{ |
| 608 |
SetState (Error, FIO_outofmemory); |
| 609 |
return Error; |
| 610 |
} |
| 611 |
else |
| 612 |
{ |
| 613 |
fd->buffer->valid = false; |
| 614 |
fd->buffer->bufstart = 0; |
| 615 |
fd->buffer->size = buflength; |
| 616 |
fd->buffer->position = 0; |
| 617 |
fd->buffer->filled = 0; |
| 618 |
if (fd->buffer->size == 0) |
| 619 |
{ |
| 620 |
fd->buffer->address = NULL; |
| 621 |
} |
| 622 |
else |
| 623 |
{ |
| 624 |
Storage_ALLOCATE (&fd->buffer->address, fd->buffer->size); |
| 625 |
if (fd->buffer->address == NULL) |
| 626 |
{ |
| 627 |
fd->state = FIO_outofmemory; |
| 628 |
return f; |
| 629 |
} |
| 630 |
} |
| 631 |
if (towrite) |
| 632 |
{ |
| 633 |
fd->buffer->left = fd->buffer->size; |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
fd->buffer->left = 0; |
| 638 |
} |
| 639 |
fd->buffer->contents = static_cast<FIO__T7 *> (fd->buffer->address); /* provides easy access for reading characters */ |
| 640 |
fd->state = fstate; /* provides easy access for reading characters */ |
| 641 |
} |
| 642 |
} |
| 643 |
return f; |
| 644 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 645 |
__builtin_unreachable (); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
/* |
| 650 |
ConnectToUnix - connects a FIO file to a UNIX file descriptor. |
| 651 |
*/ |
| 652 |
|
| 653 |
static void ConnectToUnix (FIO_File f, bool towrite, bool newfile) |
| 654 |
{ |
| 655 |
FIO_FileDescriptor fd; |
| 656 |
|
| 657 |
if (f != Error) |
| 658 |
{ |
| 659 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 660 |
if (fd != NULL) |
| 661 |
{ |
| 662 |
if (towrite) |
| 663 |
{ |
| 664 |
if (newfile) |
| 665 |
{ |
| 666 |
fd->unixfd = libc_creat (fd->name.address, CreatePermissions); |
| 667 |
} |
| 668 |
else |
| 669 |
{ |
| 670 |
fd->unixfd = libc_open (fd->name.address, (int ) (wrapc_WriteOnly ()), 0); |
| 671 |
} |
| 672 |
} |
| 673 |
else |
| 674 |
{ |
| 675 |
fd->unixfd = libc_open (fd->name.address, (int ) (wrapc_ReadOnly ()), 0); |
| 676 |
} |
| 677 |
if (fd->unixfd < 0) |
| 678 |
{ |
| 679 |
fd->state = FIO_connectionfailure; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
/* |
| 687 |
ReadFromBuffer - attempts to read, nBytes, from file, f. |
| 688 |
It firstly consumes the buffer and then performs |
| 689 |
direct unbuffered reads. This should only be used |
| 690 |
when wishing to read large files. |
| 691 |
|
| 692 |
The actual number of bytes read is returned. |
| 693 |
-1 is returned if EOF is reached. |
| 694 |
*/ |
| 695 |
|
| 696 |
static int ReadFromBuffer (FIO_File f, void * a, unsigned int nBytes) |
| 697 |
{ |
| 698 |
typedef unsigned char *ReadFromBuffer__T1; |
| 699 |
|
| 700 |
void * t; |
| 701 |
int result; |
| 702 |
unsigned int total; |
| 703 |
unsigned int n; |
| 704 |
ReadFromBuffer__T1 p; |
| 705 |
FIO_FileDescriptor fd; |
| 706 |
|
| 707 |
if (f != Error) |
| 708 |
{ |
| 709 |
total = 0; /* how many bytes have we read */ |
| 710 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); /* how many bytes have we read */ |
| 711 |
/* extract from the buffer first */ |
| 712 |
if ((fd->buffer != NULL) && fd->buffer->valid) |
| 713 |
{ |
| 714 |
if (fd->buffer->left > 0) |
| 715 |
{ |
| 716 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 717 |
if (nBytes == 1) |
| 718 |
{ |
| 719 |
/* too expensive to call memcpy for 1 character */ |
| 720 |
p = static_cast<ReadFromBuffer__T1> (a); |
| 721 |
(*p) = static_cast<unsigned char> ((*fd->buffer->contents).array[fd->buffer->position]); |
| 722 |
fd->buffer->left -= 1; /* remove consumed bytes */ |
| 723 |
fd->buffer->position += 1; /* move onwards n bytes */ |
| 724 |
nBytes = 0; /* reduce the amount for future direct */ |
| 725 |
/* read */ |
| 726 |
return 1; |
| 727 |
} |
| 728 |
else |
| 729 |
{ |
| 730 |
n = Min (fd->buffer->left, nBytes); |
| 731 |
t = fd->buffer->address; |
| 732 |
t = reinterpret_cast<void *> (reinterpret_cast<char *> (t)+fd->buffer->position); |
| 733 |
p = static_cast<ReadFromBuffer__T1> (libc_memcpy (a, t, static_cast<size_t> (n))); |
| 734 |
fd->buffer->left -= n; /* remove consumed bytes */ |
| 735 |
fd->buffer->position += n; /* move onwards n bytes */ |
| 736 |
/* move onwards ready for direct reads */ |
| 737 |
a = reinterpret_cast<void *> (reinterpret_cast<char *> (a)+n); |
| 738 |
nBytes -= n; /* reduce the amount for future direct */ |
| 739 |
/* read */ |
| 740 |
total += n; |
| 741 |
return total; /* much cleaner to return now, */ |
| 742 |
} |
| 743 |
/* difficult to record an error if */ |
| 744 |
} |
| 745 |
/* the read below returns -1 */ |
| 746 |
} |
| 747 |
if (nBytes > 0) |
| 748 |
{ |
| 749 |
/* still more to read */ |
| 750 |
result = static_cast<int> (libc_read (fd->unixfd, a, static_cast<size_t> ((int ) (nBytes)))); |
| 751 |
if (result > 0) |
| 752 |
{ |
| 753 |
/* avoid dangling else. */ |
| 754 |
total += result; |
| 755 |
fd->abspos += result; |
| 756 |
/* now disable the buffer as we read directly into, a. */ |
| 757 |
if (fd->buffer != NULL) |
| 758 |
{ |
| 759 |
fd->buffer->valid = false; |
| 760 |
} |
| 761 |
} |
| 762 |
else |
| 763 |
{ |
| 764 |
if (result == 0) |
| 765 |
{ |
| 766 |
/* eof reached */ |
| 767 |
fd->state = FIO_endoffile; |
| 768 |
} |
| 769 |
else |
| 770 |
{ |
| 771 |
fd->state = FIO_failed; |
| 772 |
} |
| 773 |
/* indicate buffer is empty */ |
| 774 |
if (fd->buffer != NULL) |
| 775 |
{ |
| 776 |
fd->buffer->valid = false; |
| 777 |
fd->buffer->left = 0; |
| 778 |
fd->buffer->position = 0; |
| 779 |
if (fd->buffer->address != NULL) |
| 780 |
{ |
| 781 |
(*fd->buffer->contents).array[fd->buffer->position] = ASCII_nul; |
| 782 |
} |
| 783 |
} |
| 784 |
return -1; |
| 785 |
} |
| 786 |
} |
| 787 |
return total; |
| 788 |
} |
| 789 |
else |
| 790 |
{ |
| 791 |
return -1; |
| 792 |
} |
| 793 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 794 |
__builtin_unreachable (); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
/* |
| 799 |
BufferedRead - will read, nBytes, through the buffer. |
| 800 |
Similar to ReadFromBuffer, but this function will always |
| 801 |
read into the buffer before copying into memory. |
| 802 |
|
| 803 |
Useful when performing small reads. |
| 804 |
*/ |
| 805 |
|
| 806 |
static int BufferedRead (FIO_File f, unsigned int nBytes, void * dest) |
| 807 |
{ |
| 808 |
typedef unsigned char *BufferedRead__T3; |
| 809 |
|
| 810 |
void * src; |
| 811 |
int total; |
| 812 |
int n; |
| 813 |
BufferedRead__T3 p; |
| 814 |
FIO_FileDescriptor fd; |
| 815 |
|
| 816 |
if (f != Error) |
| 817 |
{ |
| 818 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 819 |
total = 0; /* how many bytes have we read */ |
| 820 |
if (fd != NULL) /* how many bytes have we read */ |
| 821 |
{ |
| 822 |
/* extract from the buffer first */ |
| 823 |
if (fd->buffer != NULL) |
| 824 |
{ |
| 825 |
while (nBytes > 0) |
| 826 |
{ |
| 827 |
if ((fd->buffer->left > 0) && fd->buffer->valid) |
| 828 |
{ |
| 829 |
if (nBytes == 1) |
| 830 |
{ |
| 831 |
/* too expensive to call memcpy for 1 character */ |
| 832 |
p = static_cast<BufferedRead__T3> (dest); |
| 833 |
(*p) = static_cast<unsigned char> ((*fd->buffer->contents).array[fd->buffer->position]); |
| 834 |
fd->buffer->left -= 1; /* remove consumed byte */ |
| 835 |
fd->buffer->position += 1; /* move onwards n byte */ |
| 836 |
total += 1; /* move onwards n byte */ |
| 837 |
return total; |
| 838 |
} |
| 839 |
else |
| 840 |
{ |
| 841 |
n = Min (fd->buffer->left, nBytes); |
| 842 |
src = fd->buffer->address; |
| 843 |
src = reinterpret_cast<void *> (reinterpret_cast<char *> (src)+fd->buffer->position); |
| 844 |
p = static_cast<BufferedRead__T3> (libc_memcpy (dest, src, static_cast<size_t> (n))); |
| 845 |
fd->buffer->left -= n; /* remove consumed bytes */ |
| 846 |
fd->buffer->position += n; /* move onwards n bytes */ |
| 847 |
/* move onwards ready for direct reads */ |
| 848 |
dest = reinterpret_cast<void *> (reinterpret_cast<char *> (dest)+n); |
| 849 |
nBytes -= n; /* reduce the amount for future direct */ |
| 850 |
/* read */ |
| 851 |
total += n; |
| 852 |
} |
| 853 |
} |
| 854 |
else |
| 855 |
{ |
| 856 |
/* refill buffer */ |
| 857 |
n = static_cast<int> (libc_read (fd->unixfd, fd->buffer->address, static_cast<size_t> (fd->buffer->size))); |
| 858 |
if (n >= 0) |
| 859 |
{ |
| 860 |
/* avoid dangling else. */ |
| 861 |
fd->buffer->valid = true; |
| 862 |
fd->buffer->position = 0; |
| 863 |
fd->buffer->left = n; |
| 864 |
fd->buffer->filled = n; |
| 865 |
fd->buffer->bufstart = fd->abspos; |
| 866 |
fd->abspos += n; |
| 867 |
if (n == 0) |
| 868 |
{ |
| 869 |
/* eof reached */ |
| 870 |
fd->state = FIO_endoffile; |
| 871 |
return -1; |
| 872 |
} |
| 873 |
} |
| 874 |
else |
| 875 |
{ |
| 876 |
fd->buffer->valid = false; |
| 877 |
fd->buffer->position = 0; |
| 878 |
fd->buffer->left = 0; |
| 879 |
fd->buffer->filled = 0; |
| 880 |
fd->state = FIO_failed; |
| 881 |
return total; |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
return total; |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
return -1; |
| 890 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 891 |
__builtin_unreachable (); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
/* |
| 896 |
HandleEscape - translates |
| 897 |
and \t into their respective ascii codes. |
| 898 |
*/ |
| 899 |
|
| 900 |
static void HandleEscape (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, unsigned int *i, unsigned int *j, unsigned int HighSrc, unsigned int HighDest) |
| 901 |
{ |
| 902 |
char src[_src_high+1]; |
| 903 |
|
| 904 |
/* make a local copy of each unbounded array. */ |
| 905 |
memcpy (src, src_, _src_high+1); |
| 906 |
|
| 907 |
if (((((*i)+1) < HighSrc) && (src[(*i)] == '\\')) && ((*j) < HighDest)) |
| 908 |
{ |
| 909 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 910 |
if (src[(*i)+1] == 'n') |
| 911 |
{ |
| 912 |
/* requires a newline */ |
| 913 |
const_cast<char *>(dest)[(*j)] = ASCII_nl; |
| 914 |
(*j) += 1; |
| 915 |
(*i) += 2; |
| 916 |
} |
| 917 |
else if (src[(*i)+1] == 't') |
| 918 |
{ |
| 919 |
/* avoid dangling else. */ |
| 920 |
/* requires a tab (yuck) tempted to fake this but I better not.. */ |
| 921 |
const_cast<char *>(dest)[(*j)] = ASCII_tab; |
| 922 |
(*j) += 1; |
| 923 |
(*i) += 2; |
| 924 |
} |
| 925 |
else |
| 926 |
{ |
| 927 |
/* avoid dangling else. */ |
| 928 |
/* copy escaped character */ |
| 929 |
(*i) += 1; |
| 930 |
const_cast<char *>(dest)[(*j)] = src[(*i)]; |
| 931 |
(*j) += 1; |
| 932 |
(*i) += 1; |
| 933 |
} |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
/* |
| 939 |
Cast - casts a := b |
| 940 |
*/ |
| 941 |
|
| 942 |
static void Cast (unsigned char *a, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high) |
| 943 |
{ |
| 944 |
unsigned int i; |
| 945 |
unsigned char b[_b_high+1]; |
| 946 |
|
| 947 |
/* make a local copy of each unbounded array. */ |
| 948 |
memcpy (b, b_, _b_high+1); |
| 949 |
|
| 950 |
if (_a_high == _b_high) |
| 951 |
{ |
| 952 |
for (i=0; i<=_a_high; i++) |
| 953 |
{ |
| 954 |
const_cast<unsigned char *>(a)[i] = b[i]; |
| 955 |
} |
| 956 |
} |
| 957 |
else |
| 958 |
{ |
| 959 |
FormatError ((const char *) "cast failed", 11); |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
/* |
| 965 |
StringFormat1 - converts string, src, into, dest, together with encapsulated |
| 966 |
entity, w. It only formats the first %s or %d with n. |
| 967 |
*/ |
| 968 |
|
| 969 |
static void StringFormat1 (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, const unsigned char *w_, unsigned int _w_high) |
| 970 |
{ |
| 971 |
typedef struct StringFormat1__T8_a StringFormat1__T8; |
| 972 |
|
| 973 |
typedef char *StringFormat1__T4; |
| 974 |
|
| 975 |
struct StringFormat1__T8_a { char array[MaxErrorString+1]; }; |
| 976 |
unsigned int HighSrc; |
| 977 |
unsigned int HighDest; |
| 978 |
unsigned int c; |
| 979 |
unsigned int i; |
| 980 |
unsigned int j; |
| 981 |
StringFormat1__T8 str; |
| 982 |
StringFormat1__T4 p; |
| 983 |
char src[_src_high+1]; |
| 984 |
unsigned char w[_w_high+1]; |
| 985 |
|
| 986 |
/* make a local copy of each unbounded array. */ |
| 987 |
memcpy (src, src_, _src_high+1); |
| 988 |
memcpy (w, w_, _w_high+1); |
| 989 |
|
| 990 |
HighSrc = StrLib_StrLen ((const char *) src, _src_high); |
| 991 |
HighDest = _dest_high; |
| 992 |
p = NULL; |
| 993 |
c = 0; |
| 994 |
i = 0; |
| 995 |
j = 0; |
| 996 |
while ((((i < HighSrc) && (src[i] != ASCII_nul)) && (j < HighDest)) && (src[i] != '%')) |
| 997 |
{ |
| 998 |
if (src[i] == '\\') |
| 999 |
{ |
| 1000 |
HandleEscape ((char *) dest, _dest_high, (const char *) src, _src_high, &i, &j, HighSrc, HighDest); |
| 1001 |
} |
| 1002 |
else |
| 1003 |
{ |
| 1004 |
const_cast<char *>(dest)[j] = src[i]; |
| 1005 |
i += 1; |
| 1006 |
j += 1; |
| 1007 |
} |
| 1008 |
} |
| 1009 |
if ((((i+1) < HighSrc) && (src[i] == '%')) && (j < HighDest)) |
| 1010 |
{ |
| 1011 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 1012 |
if (src[i+1] == 's') |
| 1013 |
{ |
| 1014 |
Cast ((unsigned char *) &p, (sizeof (p)-1), (const unsigned char *) w, _w_high); |
| 1015 |
while ((j < HighDest) && ((*p) != ASCII_nul)) |
| 1016 |
{ |
| 1017 |
const_cast<char *>(dest)[j] = (*p); |
| 1018 |
j += 1; |
| 1019 |
p += 1; |
| 1020 |
} |
| 1021 |
if (j < HighDest) |
| 1022 |
{ |
| 1023 |
const_cast<char *>(dest)[j] = ASCII_nul; |
| 1024 |
} |
| 1025 |
j = StrLib_StrLen ((const char *) dest, _dest_high); |
| 1026 |
i += 2; |
| 1027 |
} |
| 1028 |
else if (src[i+1] == 'd') |
| 1029 |
{ |
| 1030 |
/* avoid dangling else. */ |
| 1031 |
const_cast<char *>(dest)[j] = ASCII_nul; |
| 1032 |
Cast ((unsigned char *) &c, (sizeof (c)-1), (const unsigned char *) w, _w_high); |
| 1033 |
NumberIO_CardToStr (c, 0, (char *) &str.array[0], MaxErrorString); |
| 1034 |
StrLib_StrConCat ((const char *) dest, _dest_high, (const char *) &str.array[0], MaxErrorString, (char *) dest, _dest_high); |
| 1035 |
j = StrLib_StrLen ((const char *) dest, _dest_high); |
| 1036 |
i += 2; |
| 1037 |
} |
| 1038 |
else |
| 1039 |
{ |
| 1040 |
/* avoid dangling else. */ |
| 1041 |
const_cast<char *>(dest)[j] = src[i]; |
| 1042 |
i += 1; |
| 1043 |
j += 1; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
/* and finish off copying src into dest */ |
| 1047 |
while (((i < HighSrc) && (src[i] != ASCII_nul)) && (j < HighDest)) |
| 1048 |
{ |
| 1049 |
if (src[i] == '\\') |
| 1050 |
{ |
| 1051 |
HandleEscape ((char *) dest, _dest_high, (const char *) src, _src_high, &i, &j, HighSrc, HighDest); |
| 1052 |
} |
| 1053 |
else |
| 1054 |
{ |
| 1055 |
const_cast<char *>(dest)[j] = src[i]; |
| 1056 |
i += 1; |
| 1057 |
j += 1; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
if (j < HighDest) |
| 1061 |
{ |
| 1062 |
const_cast<char *>(dest)[j] = ASCII_nul; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
/* |
| 1068 |
FormatError - provides a orthoganal counterpart to the procedure below. |
| 1069 |
*/ |
| 1070 |
|
| 1071 |
static void FormatError (const char *a_, unsigned int _a_high) |
| 1072 |
{ |
| 1073 |
char a[_a_high+1]; |
| 1074 |
|
| 1075 |
/* make a local copy of each unbounded array. */ |
| 1076 |
memcpy (a, a_, _a_high+1); |
| 1077 |
|
| 1078 |
FIO_WriteString (FIO_StdErr, (const char *) a, _a_high); |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
/* |
| 1083 |
FormatError1 - generic error procedure taking standard format string |
| 1084 |
and single parameter. |
| 1085 |
*/ |
| 1086 |
|
| 1087 |
static void FormatError1 (const char *a_, unsigned int _a_high, const unsigned char *w_, unsigned int _w_high) |
| 1088 |
{ |
| 1089 |
typedef struct FormatError1__T9_a FormatError1__T9; |
| 1090 |
|
| 1091 |
struct FormatError1__T9_a { char array[MaxErrorString+1]; }; |
| 1092 |
FormatError1__T9 s; |
| 1093 |
char a[_a_high+1]; |
| 1094 |
unsigned char w[_w_high+1]; |
| 1095 |
|
| 1096 |
/* make a local copy of each unbounded array. */ |
| 1097 |
memcpy (a, a_, _a_high+1); |
| 1098 |
memcpy (w, w_, _w_high+1); |
| 1099 |
|
| 1100 |
StringFormat1 ((char *) &s.array[0], MaxErrorString, (const char *) a, _a_high, (const unsigned char *) w, _w_high); |
| 1101 |
FormatError ((const char *) &s.array[0], MaxErrorString); |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
/* |
| 1106 |
FormatError2 - generic error procedure taking standard format string |
| 1107 |
and two parameters. |
| 1108 |
*/ |
| 1109 |
|
| 1110 |
static void FormatError2 (const char *a_, unsigned int _a_high, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high) |
| 1111 |
{ |
| 1112 |
typedef struct FormatError2__T10_a FormatError2__T10; |
| 1113 |
|
| 1114 |
struct FormatError2__T10_a { char array[MaxErrorString+1]; }; |
| 1115 |
FormatError2__T10 s; |
| 1116 |
char a[_a_high+1]; |
| 1117 |
unsigned char w1[_w1_high+1]; |
| 1118 |
unsigned char w2[_w2_high+1]; |
| 1119 |
|
| 1120 |
/* make a local copy of each unbounded array. */ |
| 1121 |
memcpy (a, a_, _a_high+1); |
| 1122 |
memcpy (w1, w1_, _w1_high+1); |
| 1123 |
memcpy (w2, w2_, _w2_high+1); |
| 1124 |
|
| 1125 |
StringFormat1 ((char *) &s.array[0], MaxErrorString, (const char *) a, _a_high, (const unsigned char *) w1, _w1_high); |
| 1126 |
FormatError1 ((const char *) &s.array[0], MaxErrorString, (const unsigned char *) w2, _w2_high); |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
/* |
| 1131 |
CheckAccess - checks to see whether a file f has been |
| 1132 |
opened for read/write. |
| 1133 |
*/ |
| 1134 |
|
| 1135 |
static void CheckAccess (FIO_File f, FIO_FileUsage use, bool towrite) |
| 1136 |
{ |
| 1137 |
FIO_FileDescriptor fd; |
| 1138 |
|
| 1139 |
if (f != Error) |
| 1140 |
{ |
| 1141 |
/* avoid dangling else. */ |
| 1142 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1143 |
if (fd == NULL) |
| 1144 |
{ |
| 1145 |
if (f != FIO_StdErr) |
| 1146 |
{ |
| 1147 |
FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96); |
| 1148 |
} |
| 1149 |
M2RTS_HALT (-1); |
| 1150 |
__builtin_unreachable (); |
| 1151 |
} |
| 1152 |
else |
| 1153 |
{ |
| 1154 |
if ((use == FIO_openedforwrite) && (fd->usage == FIO_openedforread)) |
| 1155 |
{ |
| 1156 |
FormatError1 ((const char *) "this file (%s) has been opened for reading but is now being written\\n", 69, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1)); |
| 1157 |
M2RTS_HALT (-1); |
| 1158 |
__builtin_unreachable (); |
| 1159 |
} |
| 1160 |
else if ((use == FIO_openedforread) && (fd->usage == FIO_openedforwrite)) |
| 1161 |
{ |
| 1162 |
/* avoid dangling else. */ |
| 1163 |
FormatError1 ((const char *) "this file (%s) has been opened for writing but is now being read\\n", 66, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1)); |
| 1164 |
M2RTS_HALT (-1); |
| 1165 |
__builtin_unreachable (); |
| 1166 |
} |
| 1167 |
else if (fd->state == FIO_connectionfailure) |
| 1168 |
{ |
| 1169 |
/* avoid dangling else. */ |
| 1170 |
FormatError1 ((const char *) "this file (%s) was not successfully opened\\n", 44, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1)); |
| 1171 |
M2RTS_HALT (-1); |
| 1172 |
__builtin_unreachable (); |
| 1173 |
} |
| 1174 |
else if (towrite != fd->output) |
| 1175 |
{ |
| 1176 |
/* avoid dangling else. */ |
| 1177 |
if (fd->output) |
| 1178 |
{ |
| 1179 |
FormatError1 ((const char *) "this file (%s) was opened for writing but is now being read\\n", 61, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1)); |
| 1180 |
M2RTS_HALT (-1); |
| 1181 |
__builtin_unreachable (); |
| 1182 |
} |
| 1183 |
else |
| 1184 |
{ |
| 1185 |
FormatError1 ((const char *) "this file (%s) was opened for reading but is now being written\\n", 64, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1)); |
| 1186 |
M2RTS_HALT (-1); |
| 1187 |
__builtin_unreachable (); |
| 1188 |
} |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |
| 1192 |
else |
| 1193 |
{ |
| 1194 |
FormatError ((const char *) "this file has not been opened successfully\\n", 44); |
| 1195 |
M2RTS_HALT (-1); |
| 1196 |
__builtin_unreachable (); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
/* |
| 1202 |
SetEndOfLine - |
| 1203 |
*/ |
| 1204 |
|
| 1205 |
static void SetEndOfLine (FIO_File f, char ch) |
| 1206 |
{ |
| 1207 |
FIO_FileDescriptor fd; |
| 1208 |
|
| 1209 |
CheckAccess (f, FIO_openedforread, false); |
| 1210 |
if (f != Error) |
| 1211 |
{ |
| 1212 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1213 |
if (ch == ASCII_nl) |
| 1214 |
{ |
| 1215 |
fd->state = FIO_endofline; |
| 1216 |
} |
| 1217 |
else |
| 1218 |
{ |
| 1219 |
fd->state = FIO_successful; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
/* |
| 1226 |
BufferedWrite - will write, nBytes, through the buffer. |
| 1227 |
Similar to WriteNBytes, but this function will always |
| 1228 |
write into the buffer before copying into memory. |
| 1229 |
|
| 1230 |
Useful when performing small writes. |
| 1231 |
*/ |
| 1232 |
|
| 1233 |
static int BufferedWrite (FIO_File f, unsigned int nBytes, void * src) |
| 1234 |
{ |
| 1235 |
typedef unsigned char *BufferedWrite__T5; |
| 1236 |
|
| 1237 |
void * dest; |
| 1238 |
int total; |
| 1239 |
int n; |
| 1240 |
BufferedWrite__T5 p; |
| 1241 |
FIO_FileDescriptor fd; |
| 1242 |
|
| 1243 |
if (f != Error) |
| 1244 |
{ |
| 1245 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1246 |
if (fd != NULL) |
| 1247 |
{ |
| 1248 |
total = 0; /* how many bytes have we read */ |
| 1249 |
if (fd->buffer != NULL) /* how many bytes have we read */ |
| 1250 |
{ |
| 1251 |
/* place into the buffer first */ |
| 1252 |
while (nBytes > 0) |
| 1253 |
{ |
| 1254 |
if (fd->buffer->left > 0) |
| 1255 |
{ |
| 1256 |
if (nBytes == 1) |
| 1257 |
{ |
| 1258 |
/* too expensive to call memcpy for 1 character */ |
| 1259 |
p = static_cast<BufferedWrite__T5> (src); |
| 1260 |
(*fd->buffer->contents).array[fd->buffer->position] = static_cast<char> ((*p)); |
| 1261 |
fd->buffer->left -= 1; /* reduce space */ |
| 1262 |
fd->buffer->position += 1; /* move onwards n byte */ |
| 1263 |
total += 1; /* move onwards n byte */ |
| 1264 |
return total; |
| 1265 |
} |
| 1266 |
else |
| 1267 |
{ |
| 1268 |
n = Min (fd->buffer->left, nBytes); |
| 1269 |
dest = fd->buffer->address; |
| 1270 |
dest = reinterpret_cast<void *> (reinterpret_cast<char *> (dest)+fd->buffer->position); |
| 1271 |
p = static_cast<BufferedWrite__T5> (libc_memcpy (dest, src, static_cast<size_t> ((unsigned int ) (n)))); |
| 1272 |
fd->buffer->left -= n; /* remove consumed bytes */ |
| 1273 |
fd->buffer->position += n; /* move onwards n bytes */ |
| 1274 |
/* move ready for further writes */ |
| 1275 |
src = reinterpret_cast<void *> (reinterpret_cast<char *> (src)+n); |
| 1276 |
nBytes -= n; /* reduce the amount for future writes */ |
| 1277 |
total += n; /* reduce the amount for future writes */ |
| 1278 |
} |
| 1279 |
} |
| 1280 |
else |
| 1281 |
{ |
| 1282 |
FIO_FlushBuffer (f); |
| 1283 |
if ((fd->state != FIO_successful) && (fd->state != FIO_endofline)) |
| 1284 |
{ |
| 1285 |
nBytes = 0; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
return total; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |
| 1293 |
return -1; |
| 1294 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1295 |
__builtin_unreachable (); |
| 1296 |
} |
| 1297 |
|
| 1298 |
|
| 1299 |
/* |
| 1300 |
PreInitialize - preinitialize the file descriptor. |
| 1301 |
*/ |
| 1302 |
|
| 1303 |
static void PreInitialize (FIO_File f, const char *fname_, unsigned int _fname_high, FIO_FileStatus state, FIO_FileUsage use, bool towrite, int osfd, unsigned int bufsize) |
| 1304 |
{ |
| 1305 |
FIO_FileDescriptor fd; |
| 1306 |
FIO_FileDescriptor fe; |
| 1307 |
char fname[_fname_high+1]; |
| 1308 |
|
| 1309 |
/* make a local copy of each unbounded array. */ |
| 1310 |
memcpy (fname, fname_, _fname_high+1); |
| 1311 |
|
| 1312 |
if ((InitializeFile (f, const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high), state, use, towrite, bufsize)) == f) |
| 1313 |
{ |
| 1314 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1315 |
if (f == Error) |
| 1316 |
{ |
| 1317 |
fe = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, FIO_StdErr)); |
| 1318 |
if (fe == NULL) |
| 1319 |
{ |
| 1320 |
M2RTS_HALT (-1); |
| 1321 |
__builtin_unreachable (); |
| 1322 |
} |
| 1323 |
else |
| 1324 |
{ |
| 1325 |
fd->unixfd = fe->unixfd; /* the error channel */ |
| 1326 |
} |
| 1327 |
} |
| 1328 |
else |
| 1329 |
{ |
| 1330 |
fd->unixfd = osfd; |
| 1331 |
} |
| 1332 |
} |
| 1333 |
else |
| 1334 |
{ |
| 1335 |
M2RTS_HALT (-1); |
| 1336 |
__builtin_unreachable (); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
|
| 1341 |
/* |
| 1342 |
Init - initialize the modules, global variables. |
| 1343 |
*/ |
| 1344 |
|
| 1345 |
static void Init (void) |
| 1346 |
{ |
| 1347 |
FileInfo = Indexing_InitIndex (0); |
| 1348 |
Error = 0; |
| 1349 |
PreInitialize (Error, (const char *) "error", 5, FIO_toomanyfilesopen, FIO_unused, false, -1, 0); |
| 1350 |
FIO_StdIn = 1; |
| 1351 |
PreInitialize (FIO_StdIn, (const char *) "<stdin>", 7, FIO_successful, FIO_openedforread, false, 0, MaxBufferLength); |
| 1352 |
FIO_StdOut = 2; |
| 1353 |
PreInitialize (FIO_StdOut, (const char *) "<stdout>", 8, FIO_successful, FIO_openedforwrite, true, 1, MaxBufferLength); |
| 1354 |
FIO_StdErr = 3; |
| 1355 |
PreInitialize (FIO_StdErr, (const char *) "<stderr>", 8, FIO_successful, FIO_openedforwrite, true, 2, MaxBufferLength); |
| 1356 |
if (! (M2RTS_InstallTerminationProcedure ((PROC ) {(PROC_t) FIO_FlushOutErr}))) |
| 1357 |
{ |
| 1358 |
M2RTS_HALT (-1); |
| 1359 |
__builtin_unreachable (); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
/* |
| 1365 |
IsNoError - returns a TRUE if no error has occured on file, f. |
| 1366 |
*/ |
| 1367 |
|
| 1368 |
extern "C" bool FIO_IsNoError (FIO_File f) |
| 1369 |
{ |
| 1370 |
FIO_FileDescriptor fd; |
| 1371 |
|
| 1372 |
if (f == Error) |
| 1373 |
{ |
| 1374 |
return false; |
| 1375 |
} |
| 1376 |
else |
| 1377 |
{ |
| 1378 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1379 |
return (fd != NULL) && (((fd->state == FIO_successful) || (fd->state == FIO_endoffile)) || (fd->state == FIO_endofline)); |
| 1380 |
} |
| 1381 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1382 |
__builtin_unreachable (); |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
/* |
| 1387 |
IsActive - returns TRUE if the file, f, is still active. |
| 1388 |
*/ |
| 1389 |
|
| 1390 |
extern "C" bool FIO_IsActive (FIO_File f) |
| 1391 |
{ |
| 1392 |
if (f == Error) |
| 1393 |
{ |
| 1394 |
return false; |
| 1395 |
} |
| 1396 |
else |
| 1397 |
{ |
| 1398 |
return (Indexing_GetIndice (FileInfo, f)) != NULL; |
| 1399 |
} |
| 1400 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1401 |
__builtin_unreachable (); |
| 1402 |
} |
| 1403 |
|
| 1404 |
extern "C" bool FIO_Exists (const char *fname_, unsigned int _fname_high) |
| 1405 |
{ |
| 1406 |
char fname[_fname_high+1]; |
| 1407 |
|
| 1408 |
/* make a local copy of each unbounded array. */ |
| 1409 |
memcpy (fname, fname_, _fname_high+1); |
| 1410 |
|
| 1411 |
/* |
| 1412 |
The following functions are wrappers for the above. |
| 1413 |
*/ |
| 1414 |
return FIO_exists (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high)); |
| 1415 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1416 |
__builtin_unreachable (); |
| 1417 |
} |
| 1418 |
|
| 1419 |
extern "C" FIO_File FIO_OpenToRead (const char *fname_, unsigned int _fname_high) |
| 1420 |
{ |
| 1421 |
char fname[_fname_high+1]; |
| 1422 |
|
| 1423 |
/* make a local copy of each unbounded array. */ |
| 1424 |
memcpy (fname, fname_, _fname_high+1); |
| 1425 |
|
| 1426 |
return FIO_openToRead (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high)); |
| 1427 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1428 |
__builtin_unreachable (); |
| 1429 |
} |
| 1430 |
|
| 1431 |
extern "C" FIO_File FIO_OpenToWrite (const char *fname_, unsigned int _fname_high) |
| 1432 |
{ |
| 1433 |
char fname[_fname_high+1]; |
| 1434 |
|
| 1435 |
/* make a local copy of each unbounded array. */ |
| 1436 |
memcpy (fname, fname_, _fname_high+1); |
| 1437 |
|
| 1438 |
return FIO_openToWrite (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high)); |
| 1439 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1440 |
__builtin_unreachable (); |
| 1441 |
} |
| 1442 |
|
| 1443 |
extern "C" FIO_File FIO_OpenForRandom (const char *fname_, unsigned int _fname_high, bool towrite, bool newfile) |
| 1444 |
{ |
| 1445 |
char fname[_fname_high+1]; |
| 1446 |
|
| 1447 |
/* make a local copy of each unbounded array. */ |
| 1448 |
memcpy (fname, fname_, _fname_high+1); |
| 1449 |
|
| 1450 |
return FIO_openForRandom (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high), towrite, newfile); |
| 1451 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1452 |
__builtin_unreachable (); |
| 1453 |
} |
| 1454 |
|
| 1455 |
|
| 1456 |
/* |
| 1457 |
Close - close a file which has been previously opened using: |
| 1458 |
OpenToRead, OpenToWrite, OpenForRandom. |
| 1459 |
It is correct to close a file which has an error status. |
| 1460 |
*/ |
| 1461 |
|
| 1462 |
extern "C" void FIO_Close (FIO_File f) |
| 1463 |
{ |
| 1464 |
FIO_FileDescriptor fd; |
| 1465 |
|
| 1466 |
if (f != Error) |
| 1467 |
{ |
| 1468 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1469 |
/* |
| 1470 |
we allow users to close files which have an error status |
| 1471 |
*/ |
| 1472 |
if (fd != NULL) |
| 1473 |
{ |
| 1474 |
FIO_FlushBuffer (f); |
| 1475 |
if (fd->unixfd >= 0) |
| 1476 |
{ |
| 1477 |
if ((libc_close (fd->unixfd)) != 0) |
| 1478 |
{ |
| 1479 |
FormatError1 ((const char *) "failed to close file (%s)\\n", 27, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1)); |
| 1480 |
fd->state = FIO_failed; /* --fixme-- too late to notify user (unless we return a BOOLEAN) */ |
| 1481 |
} |
| 1482 |
} |
| 1483 |
if (fd->name.address != NULL) |
| 1484 |
{ |
| 1485 |
Storage_DEALLOCATE (&fd->name.address, fd->name.size); |
| 1486 |
} |
| 1487 |
if (fd->buffer != NULL) |
| 1488 |
{ |
| 1489 |
if (fd->buffer->address != NULL) |
| 1490 |
{ |
| 1491 |
Storage_DEALLOCATE (&fd->buffer->address, fd->buffer->size); |
| 1492 |
} |
| 1493 |
Storage_DEALLOCATE ((void **) &fd->buffer, sizeof (FIO_buf)); |
| 1494 |
fd->buffer = NULL; |
| 1495 |
} |
| 1496 |
Storage_DEALLOCATE ((void **) &fd, sizeof (FIO_fds)); |
| 1497 |
Indexing_PutIndice (FileInfo, f, NULL); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
} |
| 1501 |
|
| 1502 |
|
| 1503 |
/* |
| 1504 |
exists - returns TRUE if a file named, fname exists for reading. |
| 1505 |
*/ |
| 1506 |
|
| 1507 |
extern "C" bool FIO_exists (void * fname, unsigned int flength) |
| 1508 |
{ |
| 1509 |
FIO_File f; |
| 1510 |
|
| 1511 |
f = FIO_openToRead (fname, flength); |
| 1512 |
if (FIO_IsNoError (f)) |
| 1513 |
{ |
| 1514 |
FIO_Close (f); |
| 1515 |
return true; |
| 1516 |
} |
| 1517 |
else |
| 1518 |
{ |
| 1519 |
FIO_Close (f); |
| 1520 |
return false; |
| 1521 |
} |
| 1522 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1523 |
__builtin_unreachable (); |
| 1524 |
} |
| 1525 |
|
| 1526 |
|
| 1527 |
/* |
| 1528 |
openToRead - attempts to open a file, fname, for reading and |
| 1529 |
it returns this file. |
| 1530 |
The success of this operation can be checked by |
| 1531 |
calling IsNoError. |
| 1532 |
*/ |
| 1533 |
|
| 1534 |
extern "C" FIO_File FIO_openToRead (void * fname, unsigned int flength) |
| 1535 |
{ |
| 1536 |
FIO_File f; |
| 1537 |
|
| 1538 |
f = GetNextFreeDescriptor (); |
| 1539 |
if (f == Error) |
| 1540 |
{ |
| 1541 |
SetState (f, FIO_toomanyfilesopen); |
| 1542 |
} |
| 1543 |
else |
| 1544 |
{ |
| 1545 |
f = InitializeFile (f, fname, flength, FIO_successful, FIO_openedforread, false, MaxBufferLength); |
| 1546 |
ConnectToUnix (f, false, false); |
| 1547 |
} |
| 1548 |
return f; |
| 1549 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1550 |
__builtin_unreachable (); |
| 1551 |
} |
| 1552 |
|
| 1553 |
|
| 1554 |
/* |
| 1555 |
openToWrite - attempts to open a file, fname, for write and |
| 1556 |
it returns this file. |
| 1557 |
The success of this operation can be checked by |
| 1558 |
calling IsNoError. |
| 1559 |
*/ |
| 1560 |
|
| 1561 |
extern "C" FIO_File FIO_openToWrite (void * fname, unsigned int flength) |
| 1562 |
{ |
| 1563 |
FIO_File f; |
| 1564 |
|
| 1565 |
f = GetNextFreeDescriptor (); |
| 1566 |
if (f == Error) |
| 1567 |
{ |
| 1568 |
SetState (f, FIO_toomanyfilesopen); |
| 1569 |
} |
| 1570 |
else |
| 1571 |
{ |
| 1572 |
f = InitializeFile (f, fname, flength, FIO_successful, FIO_openedforwrite, true, MaxBufferLength); |
| 1573 |
ConnectToUnix (f, true, true); |
| 1574 |
} |
| 1575 |
return f; |
| 1576 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1577 |
__builtin_unreachable (); |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
/* |
| 1582 |
openForRandom - attempts to open a file, fname, for random access |
| 1583 |
read or write and it returns this file. |
| 1584 |
The success of this operation can be checked by |
| 1585 |
calling IsNoError. |
| 1586 |
towrite, determines whether the file should be |
| 1587 |
opened for writing or reading. |
| 1588 |
*/ |
| 1589 |
|
| 1590 |
extern "C" FIO_File FIO_openForRandom (void * fname, unsigned int flength, bool towrite, bool newfile) |
| 1591 |
{ |
| 1592 |
FIO_File f; |
| 1593 |
|
| 1594 |
f = GetNextFreeDescriptor (); |
| 1595 |
if (f == Error) |
| 1596 |
{ |
| 1597 |
SetState (f, FIO_toomanyfilesopen); |
| 1598 |
} |
| 1599 |
else |
| 1600 |
{ |
| 1601 |
f = InitializeFile (f, fname, flength, FIO_successful, FIO_openedforrandom, towrite, MaxBufferLength); |
| 1602 |
ConnectToUnix (f, towrite, newfile); |
| 1603 |
} |
| 1604 |
return f; |
| 1605 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1606 |
__builtin_unreachable (); |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
/* |
| 1611 |
FlushBuffer - flush contents of file, f. |
| 1612 |
*/ |
| 1613 |
|
| 1614 |
extern "C" void FIO_FlushBuffer (FIO_File f) |
| 1615 |
{ |
| 1616 |
FIO_FileDescriptor fd; |
| 1617 |
|
| 1618 |
if (f != Error) |
| 1619 |
{ |
| 1620 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1621 |
if (fd != NULL) |
| 1622 |
{ |
| 1623 |
if (fd->output && (fd->buffer != NULL)) |
| 1624 |
{ |
| 1625 |
if ((fd->buffer->position == 0) || ((libc_write (fd->unixfd, fd->buffer->address, static_cast<size_t> (fd->buffer->position))) == ((int ) (fd->buffer->position)))) |
| 1626 |
{ |
| 1627 |
fd->abspos += fd->buffer->position; |
| 1628 |
fd->buffer->bufstart = fd->abspos; |
| 1629 |
fd->buffer->position = 0; |
| 1630 |
fd->buffer->filled = 0; |
| 1631 |
fd->buffer->left = fd->buffer->size; |
| 1632 |
} |
| 1633 |
else |
| 1634 |
{ |
| 1635 |
fd->state = FIO_failed; |
| 1636 |
} |
| 1637 |
} |
| 1638 |
} |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
|
| 1643 |
/* |
| 1644 |
ReadNBytes - reads nBytes of a file into memory area, dest, returning |
| 1645 |
the number of bytes actually read. |
| 1646 |
This function will consume from the buffer and then |
| 1647 |
perform direct libc reads. It is ideal for large reads. |
| 1648 |
*/ |
| 1649 |
|
| 1650 |
extern "C" unsigned int FIO_ReadNBytes (FIO_File f, unsigned int nBytes, void * dest) |
| 1651 |
{ |
| 1652 |
typedef char *ReadNBytes__T2; |
| 1653 |
|
| 1654 |
int n; |
| 1655 |
ReadNBytes__T2 p; |
| 1656 |
|
| 1657 |
if (f != Error) |
| 1658 |
{ |
| 1659 |
CheckAccess (f, FIO_openedforread, false); |
| 1660 |
n = ReadFromBuffer (f, dest, nBytes); |
| 1661 |
if (n <= 0) |
| 1662 |
{ |
| 1663 |
return 0; |
| 1664 |
} |
| 1665 |
else |
| 1666 |
{ |
| 1667 |
p = static_cast<ReadNBytes__T2> (dest); |
| 1668 |
p += n-1; |
| 1669 |
SetEndOfLine (f, (*p)); |
| 1670 |
return n; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
else |
| 1674 |
{ |
| 1675 |
return 0; |
| 1676 |
} |
| 1677 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1678 |
__builtin_unreachable (); |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
/* |
| 1683 |
ReadAny - reads HIGH (a) + 1 bytes into, a. All input |
| 1684 |
is fully buffered, unlike ReadNBytes and thus is more |
| 1685 |
suited to small reads. |
| 1686 |
*/ |
| 1687 |
|
| 1688 |
extern "C" void FIO_ReadAny (FIO_File f, unsigned char *a, unsigned int _a_high) |
| 1689 |
{ |
| 1690 |
CheckAccess (f, FIO_openedforread, false); |
| 1691 |
if ((BufferedRead (f, _a_high+1, a)) == ((int ) (_a_high+1))) |
| 1692 |
{ |
| 1693 |
SetEndOfLine (f, static_cast<char> (a[_a_high])); |
| 1694 |
} |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
/* |
| 1699 |
WriteNBytes - writes nBytes from memory area src to a file |
| 1700 |
returning the number of bytes actually written. |
| 1701 |
This function will flush the buffer and then |
| 1702 |
write the nBytes using a direct write from libc. |
| 1703 |
It is ideal for large writes. |
| 1704 |
*/ |
| 1705 |
|
| 1706 |
extern "C" unsigned int FIO_WriteNBytes (FIO_File f, unsigned int nBytes, void * src) |
| 1707 |
{ |
| 1708 |
int total; |
| 1709 |
FIO_FileDescriptor fd; |
| 1710 |
|
| 1711 |
CheckAccess (f, FIO_openedforwrite, true); |
| 1712 |
FIO_FlushBuffer (f); |
| 1713 |
if (f != Error) |
| 1714 |
{ |
| 1715 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1716 |
if (fd != NULL) |
| 1717 |
{ |
| 1718 |
total = static_cast<int> (libc_write (fd->unixfd, src, static_cast<size_t> ((int ) (nBytes)))); |
| 1719 |
if (total < 0) |
| 1720 |
{ |
| 1721 |
fd->state = FIO_failed; |
| 1722 |
return 0; |
| 1723 |
} |
| 1724 |
else |
| 1725 |
{ |
| 1726 |
fd->abspos += (unsigned int ) (total); |
| 1727 |
if (fd->buffer != NULL) |
| 1728 |
{ |
| 1729 |
fd->buffer->bufstart = fd->abspos; |
| 1730 |
} |
| 1731 |
return (unsigned int ) (total); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
} |
| 1735 |
return 0; |
| 1736 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1737 |
__builtin_unreachable (); |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
/* |
| 1742 |
WriteAny - writes HIGH (a) + 1 bytes onto, file, f. All output |
| 1743 |
is fully buffered, unlike WriteNBytes and thus is more |
| 1744 |
suited to small writes. |
| 1745 |
*/ |
| 1746 |
|
| 1747 |
extern "C" void FIO_WriteAny (FIO_File f, unsigned char *a, unsigned int _a_high) |
| 1748 |
{ |
| 1749 |
CheckAccess (f, FIO_openedforwrite, true); |
| 1750 |
if ((BufferedWrite (f, _a_high+1, a)) == ((int ) (_a_high+1))) |
| 1751 |
{} /* empty. */ |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
/* |
| 1756 |
WriteChar - writes a single character to file, f. |
| 1757 |
*/ |
| 1758 |
|
| 1759 |
extern "C" void FIO_WriteChar (FIO_File f, char ch) |
| 1760 |
{ |
| 1761 |
CheckAccess (f, FIO_openedforwrite, true); |
| 1762 |
if ((BufferedWrite (f, sizeof (ch), &ch)) == ((int ) (sizeof (ch)))) |
| 1763 |
{} /* empty. */ |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
/* |
| 1768 |
EOF - tests to see whether a file, f, has reached end of file. |
| 1769 |
*/ |
| 1770 |
|
| 1771 |
extern "C" bool FIO_EOF (FIO_File f) |
| 1772 |
{ |
| 1773 |
FIO_FileDescriptor fd; |
| 1774 |
|
| 1775 |
CheckAccess (f, FIO_openedforread, false); |
| 1776 |
if (f != Error) |
| 1777 |
{ |
| 1778 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1779 |
if (fd != NULL) |
| 1780 |
{ |
| 1781 |
return fd->state == FIO_endoffile; |
| 1782 |
} |
| 1783 |
} |
| 1784 |
return true; |
| 1785 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1786 |
__builtin_unreachable (); |
| 1787 |
} |
| 1788 |
|
| 1789 |
|
| 1790 |
/* |
| 1791 |
EOLN - tests to see whether a file, f, is upon a newline. |
| 1792 |
It does NOT consume the newline. |
| 1793 |
*/ |
| 1794 |
|
| 1795 |
extern "C" bool FIO_EOLN (FIO_File f) |
| 1796 |
{ |
| 1797 |
char ch; |
| 1798 |
FIO_FileDescriptor fd; |
| 1799 |
|
| 1800 |
CheckAccess (f, FIO_openedforread, false); |
| 1801 |
/* |
| 1802 |
we will read a character and then push it back onto the input stream, |
| 1803 |
having noted the file status, we also reset the status. |
| 1804 |
*/ |
| 1805 |
if (f != Error) |
| 1806 |
{ |
| 1807 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1808 |
if (fd != NULL) |
| 1809 |
{ |
| 1810 |
if ((fd->state == FIO_successful) || (fd->state == FIO_endofline)) |
| 1811 |
{ |
| 1812 |
ch = FIO_ReadChar (f); |
| 1813 |
if ((fd->state == FIO_successful) || (fd->state == FIO_endofline)) |
| 1814 |
{ |
| 1815 |
FIO_UnReadChar (f, ch); |
| 1816 |
} |
| 1817 |
return ch == ASCII_nl; |
| 1818 |
} |
| 1819 |
} |
| 1820 |
} |
| 1821 |
return false; |
| 1822 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1823 |
__builtin_unreachable (); |
| 1824 |
} |
| 1825 |
|
| 1826 |
|
| 1827 |
/* |
| 1828 |
WasEOLN - tests to see whether a file, f, has just seen a newline. |
| 1829 |
*/ |
| 1830 |
|
| 1831 |
extern "C" bool FIO_WasEOLN (FIO_File f) |
| 1832 |
{ |
| 1833 |
FIO_FileDescriptor fd; |
| 1834 |
|
| 1835 |
CheckAccess (f, FIO_openedforread, false); |
| 1836 |
if (f == Error) |
| 1837 |
{ |
| 1838 |
return false; |
| 1839 |
} |
| 1840 |
else |
| 1841 |
{ |
| 1842 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1843 |
return (fd != NULL) && (fd->state == FIO_endofline); |
| 1844 |
} |
| 1845 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1846 |
__builtin_unreachable (); |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
/* |
| 1851 |
ReadChar - returns a character read from file f. |
| 1852 |
Sensible to check with IsNoError or EOF after calling |
| 1853 |
this function. |
| 1854 |
*/ |
| 1855 |
|
| 1856 |
extern "C" char FIO_ReadChar (FIO_File f) |
| 1857 |
{ |
| 1858 |
char ch; |
| 1859 |
|
| 1860 |
CheckAccess (f, FIO_openedforread, false); |
| 1861 |
if ((BufferedRead (f, sizeof (ch), &ch)) == ((int ) (sizeof (ch)))) |
| 1862 |
{ |
| 1863 |
SetEndOfLine (f, ch); |
| 1864 |
return ch; |
| 1865 |
} |
| 1866 |
else |
| 1867 |
{ |
| 1868 |
return ASCII_nul; |
| 1869 |
} |
| 1870 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1871 |
__builtin_unreachable (); |
| 1872 |
} |
| 1873 |
|
| 1874 |
|
| 1875 |
/* |
| 1876 |
UnReadChar - replaces a character, ch, back into file f. |
| 1877 |
This character must have been read by ReadChar |
| 1878 |
and it does not allow successive calls. It may |
| 1879 |
only be called if the previous read was successful |
| 1880 |
or end of file was seen. |
| 1881 |
If the state was previously endoffile then it |
| 1882 |
is altered to successful. |
| 1883 |
Otherwise it is left alone. |
| 1884 |
*/ |
| 1885 |
|
| 1886 |
extern "C" void FIO_UnReadChar (FIO_File f, char ch) |
| 1887 |
{ |
| 1888 |
FIO_FileDescriptor fd; |
| 1889 |
unsigned int n; |
| 1890 |
void * a; |
| 1891 |
void * b; |
| 1892 |
|
| 1893 |
CheckAccess (f, FIO_openedforread, false); |
| 1894 |
if (f != Error) |
| 1895 |
{ |
| 1896 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 1897 |
if (((fd->state == FIO_successful) || (fd->state == FIO_endoffile)) || (fd->state == FIO_endofline)) |
| 1898 |
{ |
| 1899 |
/* avoid dangling else. */ |
| 1900 |
if ((fd->buffer != NULL) && fd->buffer->valid) |
| 1901 |
{ |
| 1902 |
/* we assume that a ReadChar has occurred, we will check just in case. */ |
| 1903 |
if (fd->state == FIO_endoffile) |
| 1904 |
{ |
| 1905 |
fd->buffer->position = MaxBufferLength; |
| 1906 |
fd->buffer->left = 0; |
| 1907 |
fd->buffer->filled = 0; |
| 1908 |
fd->state = FIO_successful; |
| 1909 |
} |
| 1910 |
if (fd->buffer->position > 0) |
| 1911 |
{ |
| 1912 |
fd->buffer->position -= 1; |
| 1913 |
fd->buffer->left += 1; |
| 1914 |
(*fd->buffer->contents).array[fd->buffer->position] = ch; |
| 1915 |
} |
| 1916 |
else |
| 1917 |
{ |
| 1918 |
/* if possible make room and store ch */ |
| 1919 |
if (fd->buffer->filled == fd->buffer->size) |
| 1920 |
{ |
| 1921 |
FormatError1 ((const char *) "performing too many UnReadChar calls on file (%d)\\n", 51, (const unsigned char *) &f, (sizeof (f)-1)); |
| 1922 |
} |
| 1923 |
else |
| 1924 |
{ |
| 1925 |
n = fd->buffer->filled-fd->buffer->position; |
| 1926 |
b = &(*fd->buffer->contents).array[fd->buffer->position]; |
| 1927 |
a = &(*fd->buffer->contents).array[fd->buffer->position+1]; |
| 1928 |
a = libc_memcpy (a, b, static_cast<size_t> (n)); |
| 1929 |
fd->buffer->filled += 1; |
| 1930 |
(*fd->buffer->contents).array[fd->buffer->position] = ch; |
| 1931 |
} |
| 1932 |
} |
| 1933 |
} |
| 1934 |
} |
| 1935 |
else |
| 1936 |
{ |
| 1937 |
FormatError1 ((const char *) "UnReadChar can only be called if the previous read was successful or end of file, error on file (%d)\\n", 102, (const unsigned char *) &f, (sizeof (f)-1)); |
| 1938 |
} |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
|
| 1943 |
/* |
| 1944 |
WriteLine - writes out a linefeed to file, f. |
| 1945 |
*/ |
| 1946 |
|
| 1947 |
extern "C" void FIO_WriteLine (FIO_File f) |
| 1948 |
{ |
| 1949 |
FIO_WriteChar (f, ASCII_nl); |
| 1950 |
} |
| 1951 |
|
| 1952 |
|
| 1953 |
/* |
| 1954 |
WriteString - writes a string to file, f. |
| 1955 |
*/ |
| 1956 |
|
| 1957 |
extern "C" void FIO_WriteString (FIO_File f, const char *a_, unsigned int _a_high) |
| 1958 |
{ |
| 1959 |
unsigned int l; |
| 1960 |
char a[_a_high+1]; |
| 1961 |
|
| 1962 |
/* make a local copy of each unbounded array. */ |
| 1963 |
memcpy (a, a_, _a_high+1); |
| 1964 |
|
| 1965 |
l = StrLib_StrLen ((const char *) a, _a_high); |
| 1966 |
if ((FIO_WriteNBytes (f, l, const_cast<void*> (static_cast<const void*>(a)))) != l) |
| 1967 |
{} /* empty. */ |
| 1968 |
} |
| 1969 |
|
| 1970 |
|
| 1971 |
/* |
| 1972 |
ReadString - reads a string from file, f, into string, a. |
| 1973 |
It terminates the string if HIGH is reached or |
| 1974 |
if a newline is seen or an error occurs. |
| 1975 |
*/ |
| 1976 |
|
| 1977 |
extern "C" void FIO_ReadString (FIO_File f, char *a, unsigned int _a_high) |
| 1978 |
{ |
| 1979 |
unsigned int high; |
| 1980 |
unsigned int i; |
| 1981 |
char ch; |
| 1982 |
|
| 1983 |
CheckAccess (f, FIO_openedforread, false); |
| 1984 |
high = _a_high; |
| 1985 |
i = 0; |
| 1986 |
do { |
| 1987 |
ch = FIO_ReadChar (f); |
| 1988 |
if (i <= high) |
| 1989 |
{ |
| 1990 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 1991 |
if (((ch == ASCII_nl) || (! (FIO_IsNoError (f)))) || (FIO_EOF (f))) |
| 1992 |
{ |
| 1993 |
const_cast<char *>(a)[i] = ASCII_nul; |
| 1994 |
i += 1; |
| 1995 |
} |
| 1996 |
else |
| 1997 |
{ |
| 1998 |
const_cast<char *>(a)[i] = ch; |
| 1999 |
i += 1; |
| 2000 |
} |
| 2001 |
} |
| 2002 |
} while (! ((((ch == ASCII_nl) || (i > high)) || (! (FIO_IsNoError (f)))) || (FIO_EOF (f)))); |
| 2003 |
} |
| 2004 |
|
| 2005 |
|
| 2006 |
/* |
| 2007 |
WriteCardinal - writes a CARDINAL to file, f. |
| 2008 |
It writes the binary image of the cardinal |
| 2009 |
to file, f. |
| 2010 |
*/ |
| 2011 |
|
| 2012 |
extern "C" void FIO_WriteCardinal (FIO_File f, unsigned int c) |
| 2013 |
{ |
| 2014 |
FIO_WriteAny (f, (unsigned char *) &c, (sizeof (c)-1)); |
| 2015 |
} |
| 2016 |
|
| 2017 |
|
| 2018 |
/* |
| 2019 |
ReadCardinal - reads a CARDINAL from file, f. |
| 2020 |
It reads a binary image of a CARDINAL |
| 2021 |
from a file, f. |
| 2022 |
*/ |
| 2023 |
|
| 2024 |
extern "C" unsigned int FIO_ReadCardinal (FIO_File f) |
| 2025 |
{ |
| 2026 |
unsigned int c; |
| 2027 |
|
| 2028 |
FIO_ReadAny (f, (unsigned char *) &c, (sizeof (c)-1)); |
| 2029 |
return c; |
| 2030 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 2031 |
__builtin_unreachable (); |
| 2032 |
} |
| 2033 |
|
| 2034 |
|
| 2035 |
/* |
| 2036 |
GetUnixFileDescriptor - returns the UNIX file descriptor of a file. |
| 2037 |
*/ |
| 2038 |
|
| 2039 |
extern "C" int FIO_GetUnixFileDescriptor (FIO_File f) |
| 2040 |
{ |
| 2041 |
FIO_FileDescriptor fd; |
| 2042 |
|
| 2043 |
if (f != Error) |
| 2044 |
{ |
| 2045 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2046 |
if (fd != NULL) |
| 2047 |
{ |
| 2048 |
return fd->unixfd; |
| 2049 |
} |
| 2050 |
} |
| 2051 |
FormatError1 ((const char *) "file %d has not been opened or is out of range\\n", 48, (const unsigned char *) &f, (sizeof (f)-1)); |
| 2052 |
return -1; |
| 2053 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 2054 |
__builtin_unreachable (); |
| 2055 |
} |
| 2056 |
|
| 2057 |
|
| 2058 |
/* |
| 2059 |
SetPositionFromBeginning - sets the position from the beginning of the file. |
| 2060 |
*/ |
| 2061 |
|
| 2062 |
extern "C" void FIO_SetPositionFromBeginning (FIO_File f, long int pos) |
| 2063 |
{ |
| 2064 |
long int offset; |
| 2065 |
FIO_FileDescriptor fd; |
| 2066 |
|
| 2067 |
if (f != Error) |
| 2068 |
{ |
| 2069 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2070 |
if (fd != NULL) |
| 2071 |
{ |
| 2072 |
/* always force the lseek, until we are confident that abspos is always correct, |
| 2073 |
basically it needs some hard testing before we should remove the OR TRUE. */ |
| 2074 |
if ((fd->abspos != pos) || true) |
| 2075 |
{ |
| 2076 |
FIO_FlushBuffer (f); |
| 2077 |
if (fd->buffer != NULL) |
| 2078 |
{ |
| 2079 |
if (fd->output) |
| 2080 |
{ |
| 2081 |
fd->buffer->left = fd->buffer->size; |
| 2082 |
} |
| 2083 |
else |
| 2084 |
{ |
| 2085 |
fd->buffer->left = 0; |
| 2086 |
} |
| 2087 |
fd->buffer->position = 0; |
| 2088 |
fd->buffer->filled = 0; |
| 2089 |
} |
| 2090 |
offset = static_cast<long int> (libc_lseek (fd->unixfd, (off_t ) (pos), wrapc_SeekSet ())); |
| 2091 |
if ((offset >= 0) && (pos == offset)) |
| 2092 |
{ |
| 2093 |
fd->abspos = pos; |
| 2094 |
} |
| 2095 |
else |
| 2096 |
{ |
| 2097 |
fd->state = FIO_failed; |
| 2098 |
fd->abspos = 0; |
| 2099 |
} |
| 2100 |
if (fd->buffer != NULL) |
| 2101 |
{ |
| 2102 |
fd->buffer->valid = false; |
| 2103 |
fd->buffer->bufstart = fd->abspos; |
| 2104 |
} |
| 2105 |
} |
| 2106 |
} |
| 2107 |
} |
| 2108 |
} |
| 2109 |
|
| 2110 |
|
| 2111 |
/* |
| 2112 |
SetPositionFromEnd - sets the position from the end of the file. |
| 2113 |
*/ |
| 2114 |
|
| 2115 |
extern "C" void FIO_SetPositionFromEnd (FIO_File f, long int pos) |
| 2116 |
{ |
| 2117 |
long int offset; |
| 2118 |
FIO_FileDescriptor fd; |
| 2119 |
|
| 2120 |
if (f != Error) |
| 2121 |
{ |
| 2122 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2123 |
if (fd != NULL) |
| 2124 |
{ |
| 2125 |
FIO_FlushBuffer (f); |
| 2126 |
if (fd->buffer != NULL) |
| 2127 |
{ |
| 2128 |
if (fd->output) |
| 2129 |
{ |
| 2130 |
fd->buffer->left = fd->buffer->size; |
| 2131 |
} |
| 2132 |
else |
| 2133 |
{ |
| 2134 |
fd->buffer->left = 0; |
| 2135 |
} |
| 2136 |
fd->buffer->position = 0; |
| 2137 |
fd->buffer->filled = 0; |
| 2138 |
} |
| 2139 |
offset = static_cast<long int> (libc_lseek (fd->unixfd, (off_t ) (pos), wrapc_SeekEnd ())); |
| 2140 |
if (offset >= 0) |
| 2141 |
{ |
| 2142 |
fd->abspos = offset; |
| 2143 |
} |
| 2144 |
else |
| 2145 |
{ |
| 2146 |
fd->state = FIO_failed; |
| 2147 |
fd->abspos = 0; |
| 2148 |
offset = 0; |
| 2149 |
} |
| 2150 |
if (fd->buffer != NULL) |
| 2151 |
{ |
| 2152 |
fd->buffer->valid = false; |
| 2153 |
fd->buffer->bufstart = offset; |
| 2154 |
} |
| 2155 |
} |
| 2156 |
} |
| 2157 |
} |
| 2158 |
|
| 2159 |
|
| 2160 |
/* |
| 2161 |
FindPosition - returns the current absolute position in file, f. |
| 2162 |
*/ |
| 2163 |
|
| 2164 |
extern "C" long int FIO_FindPosition (FIO_File f) |
| 2165 |
{ |
| 2166 |
FIO_FileDescriptor fd; |
| 2167 |
|
| 2168 |
if (f != Error) |
| 2169 |
{ |
| 2170 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2171 |
if (fd != NULL) |
| 2172 |
{ |
| 2173 |
if ((fd->buffer == NULL) || ! fd->buffer->valid) |
| 2174 |
{ |
| 2175 |
return fd->abspos; |
| 2176 |
} |
| 2177 |
else |
| 2178 |
{ |
| 2179 |
return fd->buffer->bufstart+((long int ) (fd->buffer->position)); |
| 2180 |
} |
| 2181 |
} |
| 2182 |
} |
| 2183 |
return 0; |
| 2184 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 2185 |
__builtin_unreachable (); |
| 2186 |
} |
| 2187 |
|
| 2188 |
|
| 2189 |
/* |
| 2190 |
GetFileName - assigns, a, with the filename associated with, f. |
| 2191 |
*/ |
| 2192 |
|
| 2193 |
extern "C" void FIO_GetFileName (FIO_File f, char *a, unsigned int _a_high) |
| 2194 |
{ |
| 2195 |
typedef char *GetFileName__T6; |
| 2196 |
|
| 2197 |
unsigned int i; |
| 2198 |
GetFileName__T6 p; |
| 2199 |
FIO_FileDescriptor fd; |
| 2200 |
|
| 2201 |
if (f != Error) |
| 2202 |
{ |
| 2203 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2204 |
if (fd == NULL) |
| 2205 |
{ |
| 2206 |
FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96); |
| 2207 |
M2RTS_HALT (-1); |
| 2208 |
__builtin_unreachable (); |
| 2209 |
} |
| 2210 |
else |
| 2211 |
{ |
| 2212 |
if (fd->name.address == NULL) |
| 2213 |
{ |
| 2214 |
StrLib_StrCopy ((const char *) "", 0, (char *) a, _a_high); |
| 2215 |
} |
| 2216 |
else |
| 2217 |
{ |
| 2218 |
p = static_cast<GetFileName__T6> (fd->name.address); |
| 2219 |
i = 0; |
| 2220 |
while (((*p) != ASCII_nul) && (i <= _a_high)) |
| 2221 |
{ |
| 2222 |
const_cast<char *>(a)[i] = (*p); |
| 2223 |
p += 1; |
| 2224 |
i += 1; |
| 2225 |
} |
| 2226 |
} |
| 2227 |
} |
| 2228 |
} |
| 2229 |
} |
| 2230 |
|
| 2231 |
|
| 2232 |
/* |
| 2233 |
getFileName - returns the address of the filename associated with, f. |
| 2234 |
*/ |
| 2235 |
|
| 2236 |
extern "C" void * FIO_getFileName (FIO_File f) |
| 2237 |
{ |
| 2238 |
FIO_FileDescriptor fd; |
| 2239 |
|
| 2240 |
if (f != Error) |
| 2241 |
{ |
| 2242 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2243 |
if (fd == NULL) |
| 2244 |
{ |
| 2245 |
FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96); |
| 2246 |
M2RTS_HALT (-1); |
| 2247 |
__builtin_unreachable (); |
| 2248 |
} |
| 2249 |
else |
| 2250 |
{ |
| 2251 |
return fd->name.address; |
| 2252 |
} |
| 2253 |
} |
| 2254 |
return NULL; |
| 2255 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 2256 |
__builtin_unreachable (); |
| 2257 |
} |
| 2258 |
|
| 2259 |
|
| 2260 |
/* |
| 2261 |
getFileNameLength - returns the number of characters associated with filename, f. |
| 2262 |
*/ |
| 2263 |
|
| 2264 |
extern "C" unsigned int FIO_getFileNameLength (FIO_File f) |
| 2265 |
{ |
| 2266 |
FIO_FileDescriptor fd; |
| 2267 |
|
| 2268 |
if (f != Error) |
| 2269 |
{ |
| 2270 |
fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); |
| 2271 |
if (fd == NULL) |
| 2272 |
{ |
| 2273 |
FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96); |
| 2274 |
M2RTS_HALT (-1); |
| 2275 |
__builtin_unreachable (); |
| 2276 |
} |
| 2277 |
else |
| 2278 |
{ |
| 2279 |
return fd->name.size; |
| 2280 |
} |
| 2281 |
} |
| 2282 |
return 0; |
| 2283 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 2284 |
__builtin_unreachable (); |
| 2285 |
} |
| 2286 |
|
| 2287 |
|
| 2288 |
/* |
| 2289 |
FlushOutErr - flushes, StdOut, and, StdErr. |
| 2290 |
It is also called when the application calls M2RTS.Terminate. |
| 2291 |
(which is automatically placed in program modules by the GM2 |
| 2292 |
scaffold). |
| 2293 |
*/ |
| 2294 |
|
| 2295 |
extern "C" void FIO_FlushOutErr (void) |
| 2296 |
{ |
| 2297 |
if (FIO_IsNoError (FIO_StdOut)) |
| 2298 |
{ |
| 2299 |
FIO_FlushBuffer (FIO_StdOut); |
| 2300 |
} |
| 2301 |
if (FIO_IsNoError (FIO_StdErr)) |
| 2302 |
{ |
| 2303 |
FIO_FlushBuffer (FIO_StdErr); |
| 2304 |
} |
| 2305 |
} |
| 2306 |
|
| 2307 |
extern "C" void _M2_FIO_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 2308 |
{ |
| 2309 |
Init (); |
| 2310 |
} |
| 2311 |
|
| 2312 |
extern "C" void _M2_FIO_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 2313 |
{ |
| 2314 |
FIO_FlushOutErr (); |
| 2315 |
} |