| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1986-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.NAME file.c |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR Alan Richmond [ST-ECF], Francois Ochsenbein [ESO-IPG] |
| 33 |
.CATEGORY Interface to disk file access |
| 34 |
.ENVIRONMENT |
| 35 |
.COMMENTS To improve file handling independance. |
| 36 |
The UNIX i/o functions are used; for output, |
| 37 |
creat generates standard C (i.e. byte stream) files. |
| 38 |
\begin{TeX} |
| 39 |
The basic operations are performed by means of |
| 40 |
\begin{itemize} |
| 41 |
\item fi\_open(filename,mode) to open a file, with mode values |
| 42 |
defined in <filedef.h> |
| 43 |
\item fi\_close to close a file |
| 44 |
\item fi\_name to retrieve the name of a file |
| 45 |
\item fi\_load to load efficiently ascii text from a file |
| 46 |
\item fi\_seek to position to a specified byte position (from the file |
| 47 |
beginning, end, or current position) |
| 48 |
\item fi\_tell to return the current byte location within a file. |
| 49 |
\item to read records, use |
| 50 |
\begin{itemize} |
| 51 |
\item fi\_read reads a specified amount of bytes |
| 52 |
\item fi\_gets reads a record, terminating the record with an EOS; |
| 53 |
a returned status indicates if a full record was read |
| 54 |
\end{itemize} |
| 55 |
\item to write records, use |
| 56 |
\begin{itemize} |
| 57 |
\item fi\_write writes a specified amount of bytes |
| 58 |
\item fi\_put writes an EOS-terminated ASCII string |
| 59 |
\item fi\_puts writes a record (the EOS terminator is replaced |
| 60 |
by an EOL character) |
| 61 |
\end{itemize} |
| 62 |
\end{itemize} |
| 63 |
\end{TeX} |
| 64 |
.VERSION 1.0 Creation Alan Richmond |
| 65 |
.VERSION 2.0 01-Aug-1986: VMS dependancies are grouped within #if VMS blocks. |
| 66 |
Buffering is used for byte stream files. |
| 67 |
.VERSION 2.1 30-Sep-1986: Be sure that the last character is not lost, even |
| 68 |
on VMS-files. |
| 69 |
.VERSION 2.2 16-Dec-1986: Defined internal functions as static |
| 70 |
.VERSION 2.3 17-Feb-1987: Added fi_wopen |
| 71 |
.VERSION 3.0 13-Oct-1988: Use os interfaces |
| 72 |
.VERSION 3.1 07-Dec-1988: Re-added fi_wopen... |
| 73 |
.VERSION 3.2 20-Jun-1989: Translate file name before Open |
| 74 |
.VERSION 3.3 20-Nov-1989: Be sure that fi_load ends with EOS. |
| 75 |
.VERSION 3.4 14-Dec-1989: Removed buf in fi_close when file is 0, 1 , 2 |
| 76 |
|
| 77 |
090831 last modif |
| 78 |
-------------------------------------------------------------*/ |
| 79 |
|
| 80 |
#define PM_LEVEL LEVEL_FI /* The Monitoring level for this file */ |
| 81 |
|
| 82 |
#define DEBUG 0 /* Debugging option */ |
| 83 |
|
| 84 |
#include <midas_def.h> |
| 85 |
#include <osfile.h> |
| 86 |
#include <macrogen.h> |
| 87 |
#include <trace.h> |
| 88 |
#include <levels.h> |
| 89 |
#include <error.h> |
| 90 |
#include <ok.h> |
| 91 |
#include <string.h> |
| 92 |
|
| 93 |
|
| 94 |
extern int eh_ed_i(), eh_ed_as(), eh_put1(); |
| 95 |
extern int pm_enter(), pm_ed_trace(), pm_iexit(), pm_trace(); |
| 96 |
extern int mm_free(); |
| 97 |
|
| 98 |
int fi_open(), fi_read(), fi_close(); |
| 99 |
|
| 100 |
|
| 101 |
#define _NFILES_ 32 /* Maximal number of Files */ |
| 102 |
|
| 103 |
static char opened_mode[_NFILES_] = {0,0,0,0}; |
| 104 |
static char *filenames [_NFILES_] = {"stdin", "stdout", "stderr"}; |
| 105 |
static int mode; |
| 106 |
|
| 107 |
|
| 108 |
#define set_mode(f) mode = (f>=0 && f < _NFILES_ ? opened_mode[f] : 0); |
| 109 |
#define FINISH goto FIN |
| 110 |
|
| 111 |
/*======================================================================*/ |
| 112 |
static char *save (name) |
| 113 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 114 |
.PURPOSE Copy the filename to a new piece of memory |
| 115 |
.RETURNS Address of name |
| 116 |
-------------------------------------------------------------*/ |
| 117 |
char *name; /* IN: File name */ |
| 118 |
{ |
| 119 |
char *p, *mm_alloc(); |
| 120 |
int l; |
| 121 |
|
| 122 |
l = strlen(name) + 1; |
| 123 |
if ((p = mm_alloc(l))) oscopy(p, name, l); |
| 124 |
|
| 125 |
return(p); |
| 126 |
} |
| 127 |
|
| 128 |
/*======================================================================*/ |
| 129 |
static int fi_error (msg, fid) |
| 130 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 131 |
.PURPOSE Write error message. |
| 132 |
.RETURNS 0 |
| 133 |
-------------------------------------------------------------*/ |
| 134 |
char *msg; /* IN: Message associated */ |
| 135 |
int fid; /* IN: File Identification */ |
| 136 |
{ |
| 137 |
if ( (fid < 0) || (fid > _NFILES_) || (!filenames[fid])) |
| 138 |
ERR_ED_I(msg, fid); |
| 139 |
else ERR_ED_STRING(msg, filenames[fid]); |
| 140 |
|
| 141 |
return(0); |
| 142 |
} |
| 143 |
|
| 144 |
/*======================================================================*/ |
| 145 |
int fi_wopen (name) |
| 146 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 147 |
.PURPOSE Open file for special output mode (Variable) |
| 148 |
.RETURNS File number, or NOK if error |
| 149 |
.REMARKS Mode is variable-length, max = 2048 bytes. |
| 150 |
Useless on Unix. |
| 151 |
-----------*/ |
| 152 |
char *name; /* IN: File name */ |
| 153 |
{ |
| 154 |
osfop('V', 2048); |
| 155 |
return(fi_open(name,WRITE|RECORD_MODE)); |
| 156 |
} |
| 157 |
|
| 158 |
/*======================================================================*/ |
| 159 |
int fi_open (name, mode) |
| 160 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 161 |
.PURPOSE Open file for specified access mode. |
| 162 |
.RETURNS File number, or NOK if error |
| 163 |
.REMARKS Mode may include the optional parameter RECORD_MODE. |
| 164 |
generating variable-length files on VMS. |
| 165 |
-----------*/ |
| 166 |
char *name; /* IN: File name */ |
| 167 |
int mode; /* IN: Access mode 0, 1 or 2 */ |
| 168 |
{ |
| 169 |
static char trace[] = "Open file[I ] "; |
| 170 |
static char mop[4] = {'I', 'O', 'M', 'A'}; |
| 171 |
int status, rec_mode; |
| 172 |
char *phname; |
| 173 |
|
| 174 |
ENTER("fi_open"); |
| 175 |
|
| 176 |
status = NOK; |
| 177 |
|
| 178 |
if (name) |
| 179 |
{ trace[10] = mop[mode&3]; |
| 180 |
rec_mode = mode & RECORD_MODE; |
| 181 |
trace[11] = (rec_mode ? 'R' : ' '); |
| 182 |
TRACE_ED_STRING(trace,name); |
| 183 |
status = mode & ~RECORD_MODE; |
| 184 |
phname = osftr(name); /* Physical Name */ |
| 185 |
status = (rec_mode ? osaopen(phname, status) : |
| 186 |
osdopen(phname, status) ); |
| 187 |
if (status < 0) ERR_ED_STRING(osmsg(), name), status = NOK; |
| 188 |
else if (status < _NFILES_) |
| 189 |
opened_mode[status] = (mode & RECORD_MODE ? 1 : 0), |
| 190 |
filenames[status] = save(name); |
| 191 |
} |
| 192 |
else ERROR("Invalid File Name (NULL)"); |
| 193 |
|
| 194 |
EXIT(status); |
| 195 |
} |
| 196 |
|
| 197 |
/*======================================================================*/ |
| 198 |
long fi_seek (file, offset, direction) |
| 199 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 200 |
.PURPOSE Set file position. |
| 201 |
.RETURNS New byte position, or EOF (impossible) |
| 202 |
.REMARKS See osdseek. |
| 203 |
-------------------------------------------------------------*/ |
| 204 |
int file; /* IN: File number */ |
| 205 |
long offset; /* IN: byte offset */ |
| 206 |
int direction; /* IN: FILE_START, FILE_CURRENT, FILE_END */ |
| 207 |
{ |
| 208 |
register long pos; |
| 209 |
|
| 210 |
ENTER(".fi_seek"); |
| 211 |
|
| 212 |
set_mode(file); |
| 213 |
|
| 214 |
pos = ( mode ? osaseek(file,offset,direction) : |
| 215 |
osdseek(file,offset,direction) ); |
| 216 |
|
| 217 |
if (pos == -1) fi_error(osmsg(), file); |
| 218 |
|
| 219 |
EXITl ( pos ); |
| 220 |
} |
| 221 |
|
| 222 |
/*======================================================================*/ |
| 223 |
int fi_load (name, offset, buf, nbytes) |
| 224 |
/*++++++++++ |
| 225 |
.PURPOSE Load the ascii file `name', starting at position `offset', |
| 226 |
up to nbytes. |
| 227 |
.RETURNS Actual number of bytes read (0 for error) |
| 228 |
.REMARKS The most efficient way to load the file is used. |
| 229 |
-----------*/ |
| 230 |
char *name; /* IN: File name */ |
| 231 |
long offset; /* IN: Where to start the reading */ |
| 232 |
char *buf; /* OUT: Buffer to get read bytes */ |
| 233 |
unsigned int nbytes; /* IN: Size of buffer */ |
| 234 |
{ |
| 235 |
int status, fid; |
| 236 |
char *p, *pe; |
| 237 |
|
| 238 |
ENTER("+fi_load"); |
| 239 |
|
| 240 |
p = buf, pe = buf + nbytes; |
| 241 |
*p = EOS; |
| 242 |
status = osfunix(); |
| 243 |
|
| 244 |
if_not(fid = fi_open(name, (status ? READ : READ|RECORD_MODE))) |
| 245 |
FINISH; |
| 246 |
fi_seek(fid, offset, FILE_START); |
| 247 |
|
| 248 |
if (status) /* Unix file */ |
| 249 |
{ status = fi_read(fid, p, pe-p); |
| 250 |
if (status > 0) p += status; |
| 251 |
} |
| 252 |
else /* Use Record Mode */ |
| 253 |
{ while (p < pe) |
| 254 |
{ status = osaread(fid, p, pe-p); |
| 255 |
if (status < 0) break; |
| 256 |
p += status; |
| 257 |
if (p < pe) *(p++) = '\n'; |
| 258 |
} |
| 259 |
if (p > pe) p = pe; |
| 260 |
} |
| 261 |
status = p - buf; |
| 262 |
if (status < nbytes) *p = EOS; |
| 263 |
fi_close(fid); |
| 264 |
|
| 265 |
FIN: |
| 266 |
EXIT(status); |
| 267 |
} |
| 268 |
|
| 269 |
/*======================================================================*/ |
| 270 |
int fi_close (file) |
| 271 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 272 |
.PURPOSE Close file. |
| 273 |
.RETURNS NOK if failure, OK otherwise |
| 274 |
.REMARKS See close. |
| 275 |
-------------------------------------------------------------*/ |
| 276 |
int file; /* IN: File number */ |
| 277 |
{ |
| 278 |
int status; |
| 279 |
|
| 280 |
ENTER("fi_close"); |
| 281 |
|
| 282 |
set_mode(file); |
| 283 |
status = ( mode ? osaclose(file) : osdclose(file)); |
| 284 |
|
| 285 |
if (status<0) fi_error(osmsg(), file), status = NOK; |
| 286 |
else if (file > 2) mm_free(filenames[file]), status = OK, |
| 287 |
filenames[file] = NULL_PTR(char); |
| 288 |
|
| 289 |
EXIT(status); |
| 290 |
} |
| 291 |
|
| 292 |
/*======================================================================*/ |
| 293 |
char *fi_name (file) |
| 294 |
/*++++++++++++++++ |
| 295 |
.PURPOSE Retrieve name of an opened file |
| 296 |
.RETURNS Address of name (NULL if not opened) |
| 297 |
.REMARKS Not traced. |
| 298 |
------------------*/ |
| 299 |
int file; /* IN: File number */ |
| 300 |
{ |
| 301 |
char *p; |
| 302 |
|
| 303 |
if ((file >= 0) && (file < _NFILES_)) |
| 304 |
p = filenames[file]; |
| 305 |
else p = NULL_PTR(char); |
| 306 |
|
| 307 |
return(p); |
| 308 |
} |
| 309 |
|
| 310 |
/*======================================================================*/ |
| 311 |
int fi_read (file, record, nbytes) |
| 312 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 313 |
.PURPOSE Read up to nbytes from a file. |
| 314 |
.RETURNS Actual number of bytes read, or EOF / NOK (error) |
| 315 |
.REMARKS File must have been opened without RECORD_MODE option. |
| 316 |
-------------------------------------------------------------*/ |
| 317 |
int file; /* IN: File number */ |
| 318 |
char *record; /* OUT: record buffer */ |
| 319 |
unsigned int nbytes; /* IN: size of buffer */ |
| 320 |
{ |
| 321 |
int lrec; |
| 322 |
|
| 323 |
ENTER("+fi_read"); |
| 324 |
|
| 325 |
set_mode(file); |
| 326 |
|
| 327 |
lrec = NOK; |
| 328 |
|
| 329 |
if(mode) /* Was opened with RECORD_MODE */ |
| 330 |
lrec = osaread(file, record, nbytes); |
| 331 |
else |
| 332 |
lrec = osdread(file, record, nbytes); |
| 333 |
if ((lrec == -1) && (*osmsg())) |
| 334 |
fi_error(osmsg(), file); |
| 335 |
|
| 336 |
EXIT (lrec); |
| 337 |
} |
| 338 |
|
| 339 |
/*======================================================================*/ |
| 340 |
int fi_write (file, record, nbytes) |
| 341 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 342 |
.PURPOSE Write nbytes to file. |
| 343 |
.RETURNS Number of bytes actually written, or NOK for errors |
| 344 |
.REMARKS |
| 345 |
-------------------------------------------------------------*/ |
| 346 |
int file; /* IN: File number */ |
| 347 |
char *record; /* IN: record buffer */ |
| 348 |
unsigned int nbytes; /* IN: size of buffer */ |
| 349 |
{ |
| 350 |
int status; |
| 351 |
|
| 352 |
ENTER("+fi_write"); |
| 353 |
|
| 354 |
set_mode(file); |
| 355 |
|
| 356 |
status = (mode ? osawrite(file,record,nbytes) : |
| 357 |
osdwrite(file,record,nbytes) ); |
| 358 |
|
| 359 |
if (status<0) fi_error(osmsg(), file), status = NOK; |
| 360 |
else status = OK; |
| 361 |
|
| 362 |
EXIT (status); |
| 363 |
} |
| 364 |
|
| 365 |
/*======================================================================*/ |
| 366 |
long fi_tell (file) |
| 367 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 368 |
.PURPOSE Tells the current file position |
| 369 |
.RETURNS Byte position, or EOF (impossible) |
| 370 |
.REMARKS |
| 371 |
-------------------------------------------------------------*/ |
| 372 |
int file; /* IN: File number */ |
| 373 |
{ |
| 374 |
return(fi_seek(file, 0L, FILE_CURRENT)); |
| 375 |
} |
| 376 |
|
| 377 |
/*======================================================================*/ |
| 378 |
int fi_gets (file, record, nbytes) |
| 379 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 380 |
.PURPOSE Read a record from a file, with insertion of EOS |
| 381 |
.RETURNS OK if buffer large enough, EOF (end of file), |
| 382 |
NOK if record larger than nbytes or an error occured. |
| 383 |
.REMARKS Record is always terminated by EOS; if record is larger |
| 384 |
than nbytes, the remainder is ignored. |
| 385 |
The file muast have been opened with RECORD_MODE option. |
| 386 |
-------------------------------------------------------------*/ |
| 387 |
int file; /* IN: File number */ |
| 388 |
char *record; /* OUT: record buffer */ |
| 389 |
unsigned int nbytes; /* IN: size of buffer */ |
| 390 |
{ |
| 391 |
register int lrec; |
| 392 |
|
| 393 |
ENTER("fi_gets"); |
| 394 |
|
| 395 |
set_mode(file); |
| 396 |
lrec = NOK; |
| 397 |
if ((!mode) && file) /* Not an error for stdin */ |
| 398 |
fi_error("File was not opened with RECORD_MODE: ", file); |
| 399 |
else |
| 400 |
{ lrec = osaread(file, record, nbytes); |
| 401 |
if (lrec >= 0) |
| 402 |
{ lrec = (lrec >= nbytes ? NOK : OK); |
| 403 |
TRACE(record); |
| 404 |
} |
| 405 |
else if (*osmsg()) |
| 406 |
lrec = NOK, |
| 407 |
fi_error(osmsg(), file); |
| 408 |
} |
| 409 |
|
| 410 |
EXIT(lrec); |
| 411 |
} |
| 412 |
|
| 413 |
/*======================================================================*/ |
| 414 |
int fi_put (file, record) |
| 415 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 416 |
.PURPOSE Write an EOS-terminated string (without adding EOL indicator) |
| 417 |
.RETURNS OK if all bytes written, |
| 418 |
NOK if only a part of record was written or an error occured. |
| 419 |
.REMARKS The string must be terminated with EOL followed by EOS for |
| 420 |
writing a complete record (or use fi_puts function) |
| 421 |
-------------------------------------------------------------*/ |
| 422 |
int file; /* IN: File number */ |
| 423 |
char *record; /* IN: EOS-terminated record buffer */ |
| 424 |
{ |
| 425 |
int length, status; |
| 426 |
|
| 427 |
ENTER("fi_put"); |
| 428 |
TRACE(record); |
| 429 |
|
| 430 |
length = strlen(record); |
| 431 |
status = ( fi_write(file,record,length) == length ? OK : NOK); |
| 432 |
|
| 433 |
EXIT (status); |
| 434 |
} |
| 435 |
|
| 436 |
/*======================================================================*/ |
| 437 |
int fi_puts (file, record) |
| 438 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 439 |
.PURPOSE Write up an EOS-terminated string as a new record |
| 440 |
.RETURNS OK if all bytes written, NOK if part of record is written |
| 441 |
or an error occured. |
| 442 |
.REMARKS |
| 443 |
-------------------------------------------------------------*/ |
| 444 |
int file; /* IN: File number */ |
| 445 |
char *record; /* IN: EOS-terminated record buffer */ |
| 446 |
{ |
| 447 |
int status; |
| 448 |
|
| 449 |
ENTER("fi_puts"); |
| 450 |
TRACE(record); |
| 451 |
|
| 452 |
set_mode(file); |
| 453 |
|
| 454 |
status = ( mode ? osaput(file,record) : osdputs(file,record)); |
| 455 |
if (status < 0) fi_error(osmsg(), file), status = NOK; |
| 456 |
else status = OK; |
| 457 |
|
| 458 |
EXIT (status); |
| 459 |
} |
| 460 |
|
| 461 |
/*======================================================================*/ |
| 462 |
int fi_flush (file) |
| 463 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 464 |
.PURPOSE Flush (ensure that buffers are copied to disk) |
| 465 |
.RETURNS OK / NOK |
| 466 |
.REMARKS |
| 467 |
-------------------------------------------------------------*/ |
| 468 |
int file; /* IN: File number */ |
| 469 |
{ |
| 470 |
int status; |
| 471 |
|
| 472 |
ENTER("fi_flush"); |
| 473 |
|
| 474 |
set_mode(file); |
| 475 |
status = ( mode ? osaflush(file) : osdwait()); |
| 476 |
if (status < 0) fi_error(osmsg(), file), status = NOK; |
| 477 |
else status = OK; |
| 478 |
|
| 479 |
EXIT (status); |
| 480 |
} |
| 481 |
|