Пример #1
0
int fortio_fseek( fortio_type * fortio , offset_type offset , int whence) {
  int fseek_return = util_fseek( fortio->stream , offset , whence );
  /*
    if fseek_return != 0 -> util_abort().
  */
  return fseek_return;
}
bool ecl_kw_grdecl_fseek_kw(const char * kw , bool rewind , FILE * stream) {
  if (ecl_kw_grdecl_fseek_kw__(kw , stream))
    return true;       /* OK - we found the kw between current file pos and EOF. */
  else if (rewind) {
    long int init_pos = util_ftell(stream);
    
    util_fseek(stream , 0L , SEEK_SET);
    if (ecl_kw_grdecl_fseek_kw__( kw , stream )) /* Try again from the beginning of the file. */
      return true;                              
    else
      util_fseek(stream , init_pos , SEEK_SET);       /* Could not find it - reposition to initial position. */
  }

  /* OK: If we are here - that means that we failed to find the kw. */
  return false;
}
Пример #3
0
static int ecl_util_get_num_parallel_cpu__(parser_type* parser, FILE* stream, const char * data_file) {
  int num_cpu = 1;
  char * buffer;  
  long int start_pos = util_ftell( stream );
  int buffer_size;

  /* Look for terminating '/' */
  if (!parser_fseek_string( parser , stream , "/" , false , true))
    util_abort("%s: sorry - could not find \"/\" termination of PARALLEL keyword in data_file: \n",__func__ , data_file);

  buffer_size = (util_ftell(stream) - start_pos)  ;
  buffer = util_calloc( buffer_size + 1  , sizeof * buffer );
  util_fseek( stream , start_pos , SEEK_SET);
  util_fread( buffer , sizeof * buffer , buffer_size ,stream ,  __func__);
  buffer[buffer_size] = '\0';

  {
    stringlist_type * tokens = parser_tokenize_buffer( parser , buffer , true );
    
    if (stringlist_get_size( tokens ) > 0) {
      const char * num_cpu_string = stringlist_iget( tokens , 0 );
      if (!util_sscanf_int( num_cpu_string , &num_cpu))
        fprintf(stderr,"** Warning: failed to interpret:%s as integer - assuming one CPU\n",num_cpu_string);
    } else
      fprintf(stderr,"** Warning: failed to load data for PARALLEL keyword - assuming one CPU\n");

    stringlist_free( tokens );
  }  
  free( buffer );
  return num_cpu; 
}
static bool ecl_kw_grdecl_fseek_kw__(const char * kw , FILE * stream) {
  long init_pos = util_ftell( stream );
  while (true) {
    if (ecl_kw_grdecl_fseek_next_kw( stream )) {
      char next_kw[256];
      fscanf( stream , "%s" , next_kw);
      if (strcmp( kw , next_kw ) == 0) {
        offset_type offset = (offset_type) strlen(next_kw);
        util_fseek( stream , -offset , SEEK_CUR);
        return true;
      }
    } else {
      util_fseek( stream , init_pos , SEEK_SET);
      return false;
    }
  }
}
Пример #5
0
static bool fortio_is_fortran_stream__(FILE * stream , bool endian_flip) {
  const bool strict_checking = true;          /* True: requires that *ALL* records in the file are fortran formatted */
  offset_type init_pos              = util_ftell(stream);
  bool is_fortran_stream     = false;
  int header , tail;
  bool cont;

  do {
    cont = false;
    if (__read_int(stream , &header , endian_flip)) {
      if (header >= 0) {
        if (util_fseek(stream , (offset_type) header , SEEK_CUR) == 0) {
          if (__read_int(stream , &tail , endian_flip)) {
            cont = true;
            /* 
               OK - now we have read a header and a tail - it might be
               a fortran file.
            */
            if (header == tail) {
              if (header != 0) {
                /* This is (most probably) a fortran file */
                is_fortran_stream = true;
                if (strict_checking)
                  cont = true;  
                else
                  cont = false;
              }
              /* Header == tail == 0 - we don't make any inference on this. */
            } else {
              /* Header != tail => this is *not* a fortran file */
              cont = false;
              is_fortran_stream = false;
            }
          }
        }
      } 
    }
  } while (cont);
  util_fseek(stream , init_pos , SEEK_SET);
  return is_fortran_stream;
}
Пример #6
0
static bool rms_tag_at_endtag(FILE *stream) {
    const int init_pos = util_ftell(stream);
    bool at_endtag;
    char tag[7];
    if (rms_util_fread_string(tag , 7 , stream)) {
        if (strcmp(tag , rms_endtag_string) == 0)
            at_endtag = true;
        else
            at_endtag = false;
    } else
        at_endtag = false;

    if (!at_endtag)
        util_fseek(stream , init_pos , SEEK_SET);
    return at_endtag;
}
Пример #7
0
static bool fgetc_while_equal( FILE * stream , const char * string , bool case_sensitive) {
  bool     equal        = true;
  long int current_pos  = util_ftell(stream);
  int string_index;
  for ( string_index = 0; string_index < strlen(string); string_index++) {
    int c = fgetc( stream );
    if (!case_sensitive)
      c = toupper( c );
    
    if (c != string[string_index]) {
      equal = false;
      break;
    }
  }
  
  if (!equal) /* OK - not equal - go back. */
    util_fseek( stream , current_pos , SEEK_SET);
  return equal;
}
Пример #8
0
time_t ecl_util_get_start_date(const char * data_file) { 
  parser_type * parser = parser_alloc(" \t\r\n" , "\"\'" , NULL , NULL , "--" , "\n");
  time_t start_date  = -1;
  FILE * stream      = util_fopen(data_file , "r");
  char * buffer;
  
  if (!parser_fseek_string( parser , stream , "START" , true , true))   /* Seeks case insensitive. */
    util_abort("%s: sorry - could not find START in DATA file %s \n",__func__ , data_file);
  
  {
    long int start_pos = util_ftell( stream );
    int buffer_size;

    /* Look for terminating '/' */
    if (!parser_fseek_string( parser , stream , "/" , false , true))
      util_abort("%s: sorry - could not find \"/\" termination of START keyword in data_file: \n",__func__ , data_file);
    
    buffer_size = (util_ftell(stream) - start_pos)  ;
    buffer = util_calloc( buffer_size + 1 , sizeof * buffer  );
    util_fseek( stream , start_pos , SEEK_SET);
    util_fread( buffer , sizeof * buffer , buffer_size ,stream ,  __func__);
    buffer[buffer_size] = '\0';
  }
  
  
  {
    stringlist_type * tokens = parser_tokenize_buffer( parser , buffer , true );
    int day, year, month_nr;
    if ( util_sscanf_int( stringlist_iget( tokens , 0 ) , &day)   &&   util_sscanf_int( stringlist_iget(tokens , 2) , &year)) {
      month_nr   = ecl_util_get_month_nr(stringlist_iget( tokens , 1));
      start_date = ecl_util_make_date(day , month_nr , year );
    } else
      util_abort("%s: failed to parse DAY MONTH YEAR from : \"%s\" \n",__func__ , buffer);
    stringlist_free( tokens );
  }
  
  free( buffer );
  parser_free( parser );
  fclose(stream);
  
  return start_date;
}
Пример #9
0
void test_readwrite() {
  test_work_area_type * test_area = test_work_area_alloc("matrix-test");
  {
    rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); 
    matrix_type * m1 = matrix_alloc(3  , 3);
    matrix_type * m2 = matrix_alloc(3  , 3);
    matrix_random_init( m1 , rng );
    matrix_assign(m2 , m1);

    test_assert_true( matrix_equal( m1 , m2 ) );
    {
      FILE * stream = util_fopen("m1" , "w");
      matrix_fwrite( m1 , stream );
      fclose( stream );
    }
    matrix_random_init( m1 , rng );
    test_assert_false( matrix_equal( m1 , m2 ) );
    {
      FILE * stream = util_fopen("m1" , "r");
      matrix_free( m1 );
      m1 = matrix_alloc(1,1);
      printf("-----------------------------------------------------------------\n");
      matrix_fread( m1 , stream );
      test_assert_int_equal( matrix_get_rows(m1) , matrix_get_rows( m2));
      test_assert_int_equal( matrix_get_columns(m1) , matrix_get_columns( m2));
      util_fseek( stream , 0 , SEEK_SET);
      {
        matrix_type * m3 = matrix_fread_alloc( stream );
        test_assert_true( matrix_equal( m2 , m3 ));
        matrix_free( m3 );
      }
      fclose( stream );
    }
    test_assert_true( matrix_equal( m1 , m2 ) );

    matrix_free( m2 );
    matrix_free( m1 );
    rng_free( rng );
  }
  test_work_area_free( test_area );
}
Пример #10
0
static fortio_status_type fortio_check_record( FILE * stream , bool endian_flip , int * record_size) {
  int read_count;
  int header, tail;
  fortio_status_type status;

  read_count = fread( &header , sizeof header , 1 , stream );
  if (read_count == 0)
    status = FORTIO_EOF;
  else {
    if (endian_flip)
      util_endian_flip_vector(&header , sizeof header , 1);
    
    if (util_fseek(  stream , (offset_type) header , SEEK_CUR ) != 0) 
      /* The fseek() failed - i.e. the data section was not sufficiently long. */
      status = FORTIO_MISSING_DATA;
    else {
      read_count = fread( &tail , sizeof tail , 1 , stream );
      if (read_count == 1) {
        if (endian_flip)
          util_endian_flip_vector(&tail , sizeof tail , 1);
      
        if (tail == header)
          /* All OK */
          status = FORTIO_OK;
        else if ( tail != header ) 
          /* The numerical value of the tail did not agree with the header. */
          status = FORTIO_HEADER_MISMATCH;
      } else 
        /* The file ended before we could read the tail mark. */
        status = FORTIO_MISSING_TAIL;
    }
  }

  *record_size = header;
  return status;
}
Пример #11
0
bool ecl_kw_grdecl_fseek_next_kw( FILE * stream ) {
  long start_pos = util_ftell( stream );
  long current_pos;
  char next_kw[256];
  
  /*
    Determine if the current position of the file pointer is at the
    beginning of the line; if not skip the rest of the line; this is
    applies even though the tokens leading up this are not comments.
  */
  {
    while (true) {
      char c;
      if (util_ftell(stream) == 0) 
        /*
          We are at the very beginning of the file. Can just jump out of
          the loop.
        */
        break;
      
      util_fseek( stream , -1 , SEEK_CUR );
      c = fgetc( stream );
      if (c == '\n') {
        /* 
           We have walked backwards reaching the start of the line. We
           have not reached any !isspace() characters on the way and
           can go back to start_pos and read from there.
        */
        util_fseek( stream , start_pos , SEEK_SET );
        break;
      }
      
      if (!isspace( c )) {
        /* 
           We hit a non-whitespace character; this means that start_pos
           was not at the start of the line. We skip the rest of this
           line, and then start reading on the next line.
        */
        util_fskip_lines( stream , 1 );
        break;
      }
      util_fseek( stream , -2 , SEEK_CUR );
    }
  }
  
  
  while (true) {
    current_pos = util_ftell( stream );
    if (fscanf(stream , "%s" , next_kw) == 1) {
      if ((next_kw[0] == next_kw[1]) && (next_kw[0] == ECL_COMMENT_CHAR)) 
        // This is a comment line - skip it.
        util_fskip_lines( stream , 1 );
      else {
        // This is a valid keyword i.e. a non-commented out string; return true.
        util_fseek( stream , current_pos , SEEK_SET );
        return true;
      }
    } else {
      // EOF reached - return False.
      util_fseek( stream , start_pos , SEEK_SET );
      return false;
    }
  }
}
Пример #12
0
bool gen_kw_fload(gen_kw_type * gen_kw , const char * filename) {
  FILE * stream  = util_fopen__( filename , "r");
  if (stream) {
    const int size = gen_kw_config_get_data_size(gen_kw->config );
    bool   readOK  = true;
    
    /* First try reading all the data as one long vector. */
    {
      int index = 0;
      while ((index < size) && readOK) {
        double value;
        if (fscanf(stream,"%lg" , &value) == 1) 
          gen_kw->data[index] = value;
        else
          readOK = false;
        index++;
      }
    }
    
    /* 
       OK - rewind and try again with interlaced key + value
       pairs. Observe that we still require that ALL the elements in the
       gen_kw instance are set, i.e. it is not allowed to read only some
       of the keywords; but the ordering is not relevant.
       
       The code will be fooled (and give undefined erronous results) if
       the same key appears several times. Be polite!
    */
    
    if (!readOK) {
      int counter = 0;
      readOK = true;
      util_fseek( stream , 0 , SEEK_SET );
      
      while ((counter < size) && readOK) {
        char key[128];
        double value;
        int    fscanf_return = fscanf(stream , "%s %lg" , key , &value);
        
        if (fscanf_return == 2) {
          int index = gen_kw_config_get_index(gen_kw->config , key);
          if (index >= 0) 
            gen_kw->data[index] = value;
          else
            util_abort("%s: key:%s not recognized as part of GEN_KW instance - error when reading file:%s \n",__func__ , key , filename);
          counter++;
        } else {
          util_abort("%s: failed to read (key,value) pair at line:%d in file:%s \n",__func__ , util_get_current_linenr( stream ) , filename);
          readOK = false;
        }
      }
    }
    
    if (!readOK)
      util_abort("%s: failed loading from file:%s \n",__func__ , filename);

    fclose(stream);
    return true;
  } else
    return false;
}
Пример #13
0
Файл: log.c Проект: jokva/ert
void log_sync(log_type * logh) {
#ifdef HAVE_FSYNC
    fsync( logh->fd );
#endif
    util_fseek( logh->stream , 0 , SEEK_END );
}
Пример #14
0
bool parser_fseek_string(const parser_type * parser , FILE * stream , const char * __string , bool skip_string, bool case_sensitive) {
  bool string_found        = false;
  char * string            = util_alloc_string_copy( __string );
  if (!case_sensitive)
    util_strupr( string );
  {
    long int initial_pos     = util_ftell( stream );   /* Store the inital position. */
    bool cont                = true;
    
    if (strstr( string , parser->comment_start ) != NULL)
      util_abort("%s: sorry the string contains a comment start - will never find it ... \n"); /* A bit harsh ?? */
    
    do {
      int c = fgetc( stream );
      if (!case_sensitive) c = toupper( c );
      
      /* Special treatment of quoters - does not properly handle escaping of the quoters. */
      if (is_in_quoters( c , parser )) {
        long int quote_start_pos = util_ftell(stream);
        if (!fseek_quote_end( c , stream )) {
          util_fseek( stream ,  quote_start_pos , SEEK_SET);
          fprintf(stderr,"Warning: unterminated quotation starting at line: %d \n",util_get_current_linenr( stream ));
          util_fseek(stream , 0 , SEEK_END);
        }
        /* 
           Now we are either at the first character following a
           terminated quotation, or at EOF. 
        */
        continue;
      }
      
      /* Special treatment of comments: */
      if (c == parser->comment_start[0]) {
        /* OK - this might be the start of a comment - let us check further. */
        bool comment_start = fgetc_while_equal( stream , &parser->comment_start[1] , false);
        if (comment_start) {
          long int comment_start_pos = util_ftell(stream) - strlen( parser->comment_start );
          /* Start seeking for comment_end */
          if (!util_fseek_string(stream , parser->comment_end , true , true)) { 
            /* 
               No end comment end was found - what to do about that??
               The file is just positioned at the end - and the routine
               will exit at the next step - with a Warning. 
            */
            util_fseek( stream , comment_start_pos , SEEK_SET);
            fprintf(stderr,"Warning: unterminated comment starting at line: %d \n",util_get_current_linenr( stream ));
            util_fseek(stream , 0 , SEEK_END);
          } continue;
          /* Now we are at the character following a comment end - or at EOF. */
        } 
      }
      
      /*****************************************************************/
      
      /* Now c is a regular character - and we can start looking for our string. */
      if (c == string[0]) {  /* OK - we got the first character right - lets try in more detail: */
        bool equal = fgetc_while_equal( stream , &string[1] , case_sensitive);
        if (equal) {
          string_found = true;
          cont = false;
        } 
      }
      
      if (c == EOF) 
        cont = false;
      
    } while (cont);
    
    if (string_found) {
      if (!skip_string) {
        offset_type offset = (offset_type) strlen( string );
        util_fseek(stream , -offset , SEEK_CUR); /* Reposition to the beginning of 'string' */
      }
    } else
      util_fseek(stream , initial_pos , SEEK_SET);       /* Could not find the string reposition at initial position. */
  }
  free( string );
  return string_found;
}