void test_truncated() { test_work_area_type * work_area = test_work_area_alloc("ecl_file_truncated" ); { ecl_grid_type * grid = ecl_grid_alloc_rectangular(20,20,20,1,1,1,NULL); ecl_grid_fwrite_EGRID2( grid , "TEST.EGRID", ECL_METRIC_UNITS ); ecl_grid_free( grid ); } { ecl_file_type * ecl_file = ecl_file_open("TEST.EGRID" , 0 ); test_assert_true( ecl_file_is_instance( ecl_file ) ); ecl_file_close( ecl_file ); } { offset_type file_size = util_file_size( "TEST.EGRID"); FILE * stream = util_fopen("TEST.EGRID" , "r+"); util_ftruncate( stream , file_size / 2 ); fclose( stream ); } { ecl_file_type * ecl_file = ecl_file_open("TEST.EGRID" , 0 ); test_assert_NULL( ecl_file ); } test_work_area_free( work_area ); }
void test_fread_alloc() { test_work_area_type * work_area = test_work_area_alloc("ecl_kw_fread" ); { ecl_kw_type * kw1 = ecl_kw_alloc( "INT" , 100 , ECL_INT ); int i; for (i=0; i < 100; i++) ecl_kw_iset_int( kw1 , i , i ); { fortio_type * fortio = fortio_open_writer("INT" , false , true ); ecl_kw_fwrite( kw1 , fortio ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("INT" , false , true ); ecl_kw_type * kw2 = ecl_kw_fread_alloc( fortio ); test_assert_true( ecl_kw_is_instance( kw2 )); test_assert_true( ecl_kw_equal( kw1 , kw2 )); ecl_kw_free( kw2 ); fortio_fclose( fortio ); } { offset_type file_size = util_file_size("INT"); test_truncated("INT" , file_size - 4 ); test_truncated("INT" , file_size - 25 ); test_truncated("INT" , 5 ); test_truncated("INT" , 0 ); } ecl_kw_free( kw1 ); } test_work_area_free( work_area ); }
void log_reopen(log_type *logh , const char *filename) { if (logh->stream != NULL) { /* Close the existing file descriptor. */ size_t file_size; fclose( logh->stream ); file_size = util_file_size( logh->filename ); if (file_size == 0) remove( logh->filename ); /* Unlink the old log file if it had zero size. */ } logh->filename = util_realloc_string_copy( logh->filename , filename ); #ifdef HAVE_PTHREAD pthread_mutex_lock( &logh->mutex ); #endif if (filename != NULL) { logh->stream = util_mkdir_fopen( filename , "a+"); logh->fd = fileno( logh->stream ); } else { /* It is ~OK to open a log with NULL filename, but then log_reopen() with a VALID filename must be called before it is actually used. */ logh->stream = NULL; logh->fd = -1; } #ifdef HAVE_PTHREAD pthread_mutex_unlock( &logh->mutex ); #endif }
void test_fread_truncated_tail() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated2" ); { const size_t buffer_size = 1000; void * buffer = util_malloc( buffer_size ); { fortio_type * fortio = fortio_open_writer( "PRESSURE" , false , true ); fortio_fwrite_record( fortio , buffer , buffer_size ); fortio_fseek( fortio , 0 , SEEK_SET); util_ftruncate( fortio_get_FILE(fortio) , buffer_size + 4); fortio_fclose( fortio ); } test_assert_long_equal( util_file_size( "PRESSURE") , buffer_size + 4); { fortio_type * fortio = fortio_open_reader( "PRESSURE" , false , true ); test_assert_false( fortio_fread_buffer( fortio , buffer , buffer_size )); fortio_fclose( fortio ); } free( buffer ); } test_work_area_free( work_area ); }
bool ecl_util_fmt_file(const char *filename , bool * __fmt_file) { /*const int min_size = 32768;*/ const int min_size = 256; /* Veeeery small */ int report_nr; ecl_file_enum file_type; bool status = true; bool fmt_file; if (util_file_exists(filename)) { file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr); if (file_type == ECL_OTHER_FILE) { if (util_file_size(filename) > min_size) fmt_file = util_fmt_bit8(filename); else status = false; // Do not know ?? } } else { file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr); if (file_type == ECL_OTHER_FILE) status = false; // Do not know ?? } *__fmt_file = fmt_file; return status; }
void buffer_fread_realloc(buffer_type * buffer , const char * filename) { size_t file_size = util_file_size( filename ); FILE * stream = util_fopen( filename , "r"); buffer_clear( buffer ); /* Setting: content_size = 0; pos = 0; */ buffer_stream_fread( buffer , file_size , stream ); buffer_rewind( buffer ); /* Setting: pos = 0; */ fclose( stream ); }
static int osd_load_cfg(char* path, int i, int j) { if (access(path, F_OK)) { return 0; } int ret = -1; int k = 0; int size = util_file_size(path); if (size > 0) { char* buffer = malloc(size); memset(buffer, 0, size); ret = util_file_read(path, (unsigned char*)buffer, size); CHECK(ret == size, -1, "Error with %#x.\n", ret); const char* cmd[] = {"type:", "location:", "content:"}; char* find = strstr(buffer, cmd[k]); if (find) { g_osd_args->item[i][j].type = (OSD_TITLE_TYPE_E)strtold(find+strlen(cmd[k++]), NULL); } find = strstr(buffer, cmd[k]); if (find) { g_osd_args->item[i][j].location = (OSD_LOCATION_E)strtold(find+strlen(cmd[k++]), NULL); } find = strstr(buffer, cmd[k]); if (find) { memset(g_osd_args->item[i][j].buffer, 0, sizeof(g_osd_args->item[i][j].buffer)); char* begin = find+strlen(cmd[k]); memcpy(g_osd_args->item[i][j].buffer, begin, strlen(begin)); k++; } free(buffer); } return 0; }
fortio_status_type fortio_check_file( const char * filename , bool endian_flip) { if (util_file_exists( filename )) { size_t file_size = util_file_size( filename ); if (file_size == 0) return FORTIO_EOF; else { fortio_status_type record_status; { FILE * stream = util_fopen( filename , "r"); do { int record_size; record_status = fortio_check_record( stream , endian_flip , &record_size); } while ( record_status == FORTIO_OK ); fclose( stream ); } if (record_status == FORTIO_EOF) /* Normal exit from loop at EOF */ return FORTIO_OK; else return record_status; } } else return FORTIO_NOENTRY; /* This is an application error. */ }
int main( int argc , char ** argv) { int num_kw = 1000; // Total file size should roughly exceed 2GB int kw_size = 600000; ecl_kw_type * kw = ecl_kw_alloc("KW" , kw_size , ECL_INT_TYPE ); rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); int i; offset_type file_size; for (i=0; i < kw_size; i++) ecl_kw_iset_int( kw , i , rng_get_int( rng , 912732 )); { fortio_type * fortio = fortio_open_writer( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw; i++) { printf("Writing keyword %d/%d to file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fwrite( kw , fortio ); } fortio_fclose( fortio ); } /*{ fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw - 1; i++) { printf("SKipping keyword %d/%d from file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fskip( fortio ); } { ecl_kw_type * file_kw = ecl_kw_fread_alloc( fortio ); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_kw_free( file_kw ); } fortio_fclose( fortio ); } */ file_size = util_file_size( "LARGE_FILE.UNRST" ); printf("File size: %lld \n",file_size); { fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); printf("Seeking to file end: "); fortio_fseek( fortio , file_size , SEEK_SET); fortio_fclose( fortio ); printf("Seek OK \n"); } printf("Doing ecl_file_open(..)\n"); { ecl_file_type * file = ecl_file_open( "LARGE_FILE.UNRST" , 0); ecl_kw_type * file_kw = ecl_file_iget_named_kw( file , "KW" , num_kw - 1); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_file_close( file ); } remove( "LARGE_FILE.UNRST" ); exit(0); }