Пример #1
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; 
}
Пример #2
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;
}
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;
}
Пример #4
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;
}
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;
    }
  }
}
Пример #6
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;
}
Пример #7
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;
}
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;
    }
  }
}
Пример #9
0
offset_type fortio_ftell( const fortio_type * fortio ) {
  return util_ftell( fortio->stream );
}
Пример #10
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;
}