1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
|
/**
* Copyright 1981-2007 ECMWF
*
* Licensed under the GNU Lesser General Public License which
* incorporates the terms and conditions of version 3 of the GNU
* General Public License.
* See LICENSE and gpl-3.0.txt for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static FILE** fptable = NULL;
static int fptableSize = 0;
#define BUFFLEN 4096
#include "fortint.h"
#include "fileRead.h"
/*
// pbio_alpha.c
*/
#include "fort2c.h"
#include "bufrgrib.h"
#define NAMEBUFFLEN 256
#define MODEBUFFLEN 10
/*************************************************************************
// FUNCTION: pbopen - Open file (from FORTRAN)
**************************************************************************
*/
void PBOPEN(fortint* unit, _fcd name, _fcd mode, fortint* iret, int l1, int l2){
/*
// Purpose:
// Opens file, returns the index of a UNIX FILE pointer held in
// an internal table (fptable).
//
// Function accepts:
// name = filename
// mode = r, w
//
// Note: l1 and l2 are the lengths of the FORTRAN character strings
// in name and mode.
//
// Function returns:
// INTEGER iret:
// -1 = Could not open file.
// -2 = Invalid file name.
// -3 = Invalid open mode specified
// 0 = OK.
*/
char *fname;
char *modes;
char *p;
char flags[4];
char namebuff[NAMEBUFFLEN], modebuff[MODEBUFFLEN];
int n;
/*
// Put the character strings into buffers and ensure that there is a
// null terminator (for case when FORTRAN CHARACTER variable is full
// right to end with characters.
*/
strncpy( namebuff, name, NAMEBUFFLEN - 1);
strncpy( modebuff, mode, MODEBUFFLEN - 1);
namebuff[l1] = '\0';
modebuff[l2] = '\0';
strcpy(flags,"");
*unit = NULL;
*iret = 0;
/*
// Convert Fortran to C string: file name.
*/
if(!(fname = fcd2char(name))) {
*iret = -2;
return;
}
/*
// Strip trailing blanks.
*/
p = fname;
while(*p) {
if(*p == ' ') *p = 0;
p++;
}
/*
// Convert Fortran to C string: open modes.
*/
if(!(modes = fcd2char(mode))) {
free(fname);
*iret = -2;
return;
}
/*
// Build open flags from "modes".
*/
p = modes;
switch(*p) {
case 'a':
case 'A': strcat(flags, "a");
break;
case 'c':
case 'C':
case 'w':
case 'W': strcat(flags, "w");
break;
case 'r':
case 'R': strcat(flags, "r");
break;
default: *iret = -3;
return;
}
/*
// If read/write, change flags.
*/
if( !strcmp(flags,"wr") || !strcmp(flags, "rw") ) strcpy(flags, "r+w" );
/*
// Look for a free slot in fptable.
// (Create the table the first time through).
*/
n = 0;
if( fptableSize == 0 ) {
int i;
fptableSize = 2;
fptable = (FILE **) malloc(fptableSize*sizeof(FILE *));
if( fptable == NULL ) {
perror("Unable to allocate space for table of FILE pointers");
exit(1);
}
for( i = 0; i < fptableSize; i++ )
fptable[i] = 0;
}
else {
while( n < fptableSize ) {
if (fptable[n]==0) {
*unit = n;
break;
}
n++;
}
}
/*
// If the table overflows, double its size.
*/
if( n == fptableSize) {
int i;
fptableSize = 2*fptableSize;
fptable = (FILE **) realloc(fptable, fptableSize*sizeof(FILE *));
if( fptable == NULL ) {
perror("Unable to reallocate space for table of FILE pointers");
exit(1);
}
n = fptableSize/2;
for( i = n; i < fptableSize; i++ )
fptable[i] = 0;
*unit = n;
}
fptable[n] = fopen(fname, flags );
if(fptable[n] == NULL) {
perror(fname);
*iret = -1;
}
free(fname);
free(modes);
return;
}
/*************************************************************************
// FUNCTION: pbseek_ - Seek (from FORTRAN)
**************************************************************************
*/
void PBSEEK(fortint* unit, fortint* offset, fortint* whence, fortint* iret) {
/*
//
// Purpose:
// Seeks to a specified location in file.
//
// Function accepts:
// unit = the index of a UNIX FILE pointer held in
// an internal table (fptable).
//
// offset = byte count
//
// whence = 0, from start of file
// = 1, from current position
// = 2, from end of file.
//
// Returns:
// iret:
// -2 = error in handling file,
// -1 = end-of-file
// otherwise, = byte offset from start of file.
*/
fortint my_offset;
fortint my_whence;
my_offset = *offset;
my_whence = *whence;
/*
// Must use negative offset if working from end-of-file
*/
if( my_whence == 2) my_offset = - abs(my_offset);
*iret = fileTell(fptable[*unit]);
if( *iret == my_offset && my_whence == 0)
*iret = 0;
else
*iret = fileSeek(fptable[*unit], my_offset, my_whence);
if( *iret != 0 ) {
if ( ! feof(fptable[*unit]) ) {
*iret = -2; /* error in file-handling */
perror("pbseek");
}
else
*iret = -1; /* end-of-file */
clearerr(fptable[*unit]);
return;
}
/*
// Return the byte offset from start of file
*/
*iret = fileTell(fptable[*unit]);
return;
}
/*************************************************************************
* FUNCTION: pbread_ - Read (from FORTRAN)
**************************************************************************
*/
void PBREAD(fortint* unit, char * buffer, fortint* nbytes, fortint* iret) {
/*
// Purpose:
// Reads a block of bytes from a file..
//
// Function accepts:
// unit = the index of a UNIX FILE pointer held in
// an internal table (fptable).
//
// nbytes = number of bytes to read.
//
// Returns:
// iret:
// -2 = error in reading file,
// -1 = end-of-file,
// otherwise, = number of bytes read.
*/
if( (*iret = fread(buffer, 1, *nbytes, fptable[*unit]) ) != *nbytes) {
if( ! feof(fptable[*unit]) ) {
*iret = -2; /* error in file-handling */
perror("pbread");
clearerr(fptable[*unit]);
return;
}
else {
*iret = -1; /* end-of-file */
clearerr(fptable[*unit]);
}
}
return;
}
/*************************************************************************
* FUNCTION: pbread2_ - Read (from FORTRAN)
**************************************************************************
*/
void PBREAD2(fortint* unit, char * buffer, fortint* nbytes, fortint* iret) {
/*
// Purpose:
// Reads a block of bytes from a file..
//
// Function accepts:
// unit = the index of a UNIX FILE pointer held in
// an internal table (fptable).
//
// nbytes = number of bytes to read.
//
// Returns:
// iret:
// -2 = error in reading file,
// -1 = end-of-file,
// otherwise, = number of bytes read.
*/
if( (*iret = fread(buffer, 1, *nbytes, fptable[*unit]) ) != *nbytes) {
if( ! feof(fptable[*unit]) ) {
*iret = -2; /* error in file-handling */
perror("pbread");
clearerr(fptable[*unit]);
return;
}
else {
*iret = -1; /* end-of-file */
clearerr(fptable[*unit]);
}
}
return;
}
/*************************************************************************
* FUNCTION: pbwrite_ - Write (from FORTRAN)
**************************************************************************
*/
void PBWRITE(fortint* unit, char * buffer, fortint* nbytes, fortint* iret) {
/*
// Purpose:
// Writes a block of bytes to a file.
//
// Function accepts:
// unit = the index of a UNIX FILE pointer held in
// an internal table (fptable).
//
// nbytes = number of bytes to write.
//
// Returns:
// iret:
// -1 = Could not write to file.
// >=0 = Number of bytes written.
*/
if ( (*iret = fwrite(buffer, 1, *nbytes, fptable[*unit]) ) != *nbytes) {
perror("pbwrite");
*iret = -1;
}
return;
}
/*************************************************************************
* FUNCTION: pbclos_ - clos (from FORTRAN)
**************************************************************************
*/
fortint PBCLOSE(fortint* unit, fortint* iret)
/*
// Purpose:
// Closes file.
//
// Function accepts:
// unit = the index of a UNIX FILE pointer held in
// an internal table (fptable).
//// Returns:
// iret:
// 0 = OK.
// otherwise = error in handling file.
*/
{
*iret = fclose(fptable[*unit]);
fptable[*unit] = 0;
if(*iret != 0) {
perror("pbclose");
return (1);
}
return (0);
}
void GRIBREAD(char * buffer, fortint* buffsize, fortint* readsize,
fortint* status, fortint* stream) {
/*
// Called as a FORTRAN subroutine:
//
// CALL GRIBREAD( KARRAY, KINLEN, KOUTLEN, IRET, KUNIT )
//
*/
fortint holdsize = *buffsize;
/*
// Read GRIB product
*/
*status = readprod("GRIB",buffer,&holdsize,fileRead,fileSeek,fileTell,
fptable[*stream]);
*readsize = abs(holdsize );
return;
}
void BUFRREAD(char * buffer, fortint* buffsize, fortint* readsize,
fortint* status, fortint* stream) {
/*
// Called as a FORTRAN subroutine:
//
// CALL BUFRREAD( KARRAY, KINLEN, KOUTLEN, IRET, KUNIT )
//
*/
fortint holdsize = *buffsize;
/*
// Read BUFR product
*/
*status = readprod("BUFR",buffer,&holdsize,fileRead,fileSeek,fileTell,
fptable[*stream]);
*readsize = abs(holdsize );
return;
}
void PSEUREAD(char * buffer, fortint* buffsize, fortint* readsize,
fortint* status, fortint* stream) {
/*
// Called as a FORTRAN subroutine:
// CALL PSEUREAD( KARRAY, KINLEN, KOUTLEN, IRET, KUNIT )
*/
fortint holdsize = *buffsize;
/*
// Read GRIB product
*/
*status = readprod(NULL,buffer,&holdsize,fileRead,fileSeek,fileTell,
fptable[*stream]);
*readsize = abs(holdsize );
return;
}
/*************************************************************************
// FUNCTION: pbopen - Open file (from FORTRAN)
**************************************************************************
*/
void PBSIZE(fortint* unit, fortint* plen) {
/*
// Purpose:
// Returns the size in bytes of the next GRIB, BUFR, TIDE, BUDG, DIAG
// product.
//
// Called from FORTRAN:
// CALL PBSIZE( KUNIT, LENGTH)
//
// Function accepts:
// unit = the index of a UNIX FILE pointer held in
// an internal table (fptable).
//
// Returns:
// plen = size in bytes of the next product.
// = -2 if error allocating memory for internal buffer.
//
// The input file is left positioned where it started.
*/
fortint iret;
char statbuff[BUFFLEN];
char * buff;
fortint offset, loop = 1;
FILE *in = fptable[*unit];
/*
// Use a smallish buffer for processing; this should suffice for all cases
// except versions -1 and 0 of GRIB and large BUFR products
*/
offset = (fortint) fileTell( in);
*plen = BUFFLEN;
iret = readprod(NULL,statbuff,plen,fileRead,fileSeek,fileTell,in);
if( iret == -2 ) {
printf("readprod error %d\n", iret);
*plen = -2;
return;
}
/*
// If the smallish buffer is too small, progressively increase it until
// big enough */
while ( iret == -4 ) {
loop++;
buff = (char *) malloc( BUFFLEN*loop);
if( buff == NULL) {
perror("malloc failed in PBSIZE");
*plen = -2;
return;
}
*plen = BUFFLEN*loop;
offset = (fortint) fileSeek( in, offset, SEEK_SET);
offset = (fortint) fileTell( in);
iret = readprod(NULL,buff,plen,fileRead,fileSeek,fileTell,in);
free(buff);
}
if( iret == -2 ) {
printf("readprod error %d\n", iret);
*plen = -2;
}
/*
// Put the file pointer back where it started
*/
offset = (fortint) fileSeek( in, offset, SEEK_SET);
return;
}
#define PBOPEN3 pbopen3_
#define PBREAD3 pbread3_
#define PBWRITE3 pbwrite3_
#define PBSEEK3 pbseek3_
#define PBCLOSE3 pbclose3_
void PBREAD3(fortint *unit, char * buffer, fortint * nbytes, fortint * iret)
/*
*
* Purpose: Reads a block of bytes from a file.
*
* Function returns: status : -2 = error in reading file,
* -1 = end-of-file,
* otherwise, = number of bytes read.
*/
{
*iret = read(*unit, buffer, *nbytes);
/* Read problem */
if (*iret == -1)
{
*iret = -2; /* error in file-handling */
perror("pbread3");
return;
}
else if (*iret != *nbytes)
{
*iret = -1;
printf("EOF; pbread3; bytes requested %d; read in: %d\n",
*nbytes,*iret);
return;
}
}
static oct_bin3(int onum)
{
char tmp[20];
int rc;
sprintf(tmp,"%d",onum);
sscanf(tmp,"%o",&rc);
return rc;
}
void PBOPEN3(fortint *unit, _fcd name, _fcd mode, fortint * iret,
fortint l1, fortint l2)
/*
* Purpose: Opens file, return UNIX FILE pointer.
*
* Function returns: iret: -1 = Could not open file.
* -2 = Invalid file name.
* -3 = Invalid open mode specified
* 0 = OK.
*
* Note: l1 and l2 are the lengths of the character strings in
* name and mode on SGI.
*/
{
char *fname;
char *modes;
char *p;
int oflag;
int dmas;
int filemode;
#if (!defined CRAY) && (!defined VAX)
char namebuff[NAMEBUFFLEN], modebuff[MODEBUFFLEN];
/* Put the character strings into buffers and ensure that there is a
null terminator (for SGI case when FORTRAN CHARACTER variable is full
right to end with characters */
strncpy( namebuff, name, NAMEBUFFLEN - 1);
strncpy( modebuff, mode, MODEBUFFLEN - 1);
namebuff[l1] = '\0';
modebuff[l2] = '\0';
#endif
*unit = 0;
*iret = 0;
/* convert fortran to c string : file name */
#if (!defined CRAY) && (!defined VAX)
if(!(fname = fcd2char(namebuff)))
#else
if(!(fname = fcd2char(name)))
#endif
{
*iret = -2;
return;
}
/* strip trailing blanks */
p = fname + strlen(fname) - 1 ;
while(*p == ' ')
{
*p = 0;
p--;
}
/* convert fortran to c string : open modes */
#if (!defined CRAY) && (!defined VAX)
if(!(modes = fcd2char(modebuff)))
#else
if(!(modes = fcd2char(mode)))
#endif
{
free(fname);
*iret = -3;
return;
}
/* build open flags from "modes" */
p = modes;
while(*p)
{
switch(*p)
{
case '+': break;
case 'a':
case 'A': oflag = 0x100 | 2 | 0x08;
filemode = 766;
break;
case 'c':
case 'C':
case 'w':
case 'W': oflag = 0x100 | 1;
filemode = 766;
break;
case 'r':
case 'R': oflag = 0;
filemode = 444;
break;
default: *iret = -3;
return;
}
p++;
}
dmas = umask(000);
*unit = open(fname, oflag, oct_bin3(filemode));
umask(dmas);
if(*unit == -1)
{
perror(fname);
perror("pbopen3");
*iret = -2;
}
free(fname);
free(modes);
}
void PBCLOSE3( fortint * unit, fortint * iret)
/*
*
* Purpose: Closes file.
*
* Function returns: status : non-0 = error in handling file.
* 0 = OK.
*/
{
*iret = close(*unit);
if(*iret != 0) perror("pbclose3");
}
void PBSEEK3(fortint * unit, fortint * offset, fortint * whence, fortint * iret)
/*
*
* Purpose: Seeks to specified location in file.
*
* Function returns: status : -2 = error in handling file,
* -1 = end-of-file
* otherwise, = byte offset from start of file.
*
* whence = 0, from start of file
* = 1, from current position
* = 2, from end of file.
*/
{
fortint my_offset = *offset;
int my_whence;
if ( *whence == 2)
{
my_offset = - abs(my_offset);
my_whence = 2;
}
else if (*whence == 0)
{
my_whence = 0;
}
else
{
my_whence = 1;
}
/* must use negative offset if working
from end-of-file */
if ((*iret=lseek(*unit, my_offset, my_whence)) < 0)
{
perror("pbseek3;");
*iret = -1; /* end-of-file */
}
}
void PBWRITE3( fortint * unit, char * buffer, fortint * nbytes, fortint *iret)
/*
* Purpose: Writes a block of bytes to a file.
*
* Function returns: status : -1 = Could not write to file.
* >=0 = Number of bytes written.
*/
{
if ((*iret = write(*unit, buffer, *nbytes)) != *nbytes)
{
perror("pbwrite3: ");
*iret = -1;
}
}
|