void test_fseek() { test_work_area_type * work_area = test_work_area_alloc("fortio_fseek" ); { fortio_type * fortio = fortio_open_writer("PRESSURE" , false , true); void * buffer = util_malloc( 100 ); fortio_fwrite_record( fortio , buffer , 100); free( buffer ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("PRESSURE" , false , true); printf("Starting fssek test \n"); test_assert_true( fortio_fseek( fortio , 0 , SEEK_SET )); test_assert_true( fortio_fseek( fortio , 0 , SEEK_END )); test_assert_false( fortio_fseek( fortio , 100000 , SEEK_END)); test_assert_false( fortio_fseek( fortio , 100000 , SEEK_SET)); fortio_fclose( fortio ); } test_work_area_free( work_area ); }
bool fortio_is_fortio_file(fortio_type * fortio) { offset_type init_pos = fortio_ftell(fortio); int elm_read; bool is_fortio_file = false; elm_read = fread(&fortio->active_header , sizeof(fortio->active_header) , 1 , fortio->stream); if (elm_read == 1) { int trailer; if (fortio->endian_flip_header) util_endian_flip_vector(&fortio->active_header , sizeof fortio->active_header , 1); if (fortio_fseek(fortio , (offset_type) fortio->active_header , SEEK_CUR) == 0) { if (fread(&trailer , sizeof(fortio->active_header) , 1 , fortio->stream) == 1) { if (fortio->endian_flip_header) util_endian_flip_vector(&trailer , sizeof trailer , 1); if (trailer == fortio->active_header) is_fortio_file = true; } } } fortio_fseek(fortio , init_pos , SEEK_SET); return is_fortio_file; }
void test_at_eof() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated2" ); { fortio_type * fortio = fortio_open_writer("PRESSURE" , false , true); void * buffer = util_malloc( 100 ); fortio_fwrite_record( fortio , buffer , 100); free( buffer ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("PRESSURE" , false , true); test_assert_false( fortio_read_at_eof( fortio )); fortio_fseek( fortio , 50 , SEEK_SET ); test_assert_false( fortio_read_at_eof( fortio )); fortio_fseek( fortio , 0 , SEEK_END ); test_assert_true( fortio_read_at_eof( fortio )); fortio_fclose( fortio ); } test_work_area_free( work_area ); }
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 ); }
void fortio_data_fskip(fortio_type* fortio, const int element_size, const int element_count, const int block_count) { int headers = block_count * 4; int trailers = block_count * 4; int bytes_to_skip = headers + trailers + (element_size * element_count); fortio_fseek(fortio, bytes_to_skip, SEEK_CUR); }
void ecl_file_kw_inplace_fwrite( ecl_file_kw_type * file_kw , fortio_type * fortio) { ecl_file_kw_assert_kw( file_kw ); fortio_fseek( fortio , file_kw->file_offset , SEEK_SET ); ecl_kw_fskip_header( fortio ); fortio_fclean(fortio); ecl_kw_fwrite_data( file_kw->kw , fortio ); }
void fortio_data_fseek(fortio_type* fortio, offset_type data_offset, size_t data_element, const int element_size, const int element_count, const int block_size) { if(data_element < 0 || data_element >= element_count) { util_abort("%s: Element index is out of range: 0 <= %d < %d \n", __func__, data_element, element_count); } { int block_index = data_element / block_size; int headers = (block_index + 1) * 4; int trailers = block_index * 4; offset_type bytes_to_skip = data_offset + headers + trailers + (data_element * element_size); fortio_fseek(fortio, bytes_to_skip, SEEK_SET); } }
static void ecl_file_kw_load_kw( ecl_file_kw_type * file_kw , fortio_type * fortio , inv_map_type * inv_map) { if (fortio == NULL) util_abort("%s: trying to load a keyword after the backing file has been detached.\n",__func__); if (file_kw->kw != NULL) ecl_file_kw_drop_kw( file_kw , inv_map ); { fortio_fseek( fortio , file_kw->file_offset , SEEK_SET ); file_kw->kw = ecl_kw_fread_alloc( fortio ); ecl_file_kw_assert_kw( file_kw ); inv_map_add_kw( inv_map , file_kw , file_kw->kw ); } }
void ecl_file_kw_replace_kw( ecl_file_kw_type * file_kw , fortio_type * target , ecl_kw_type * new_kw ) { if ((file_kw->ecl_type == ecl_kw_get_type( new_kw )) && (file_kw->kw_size == ecl_kw_get_size( new_kw ))) { if (file_kw->kw != NULL) ecl_kw_free( file_kw->kw ); file_kw->kw = new_kw; fortio_fseek( target , file_kw->file_offset , SEEK_SET ); ecl_kw_fwrite( file_kw->kw , target ); } else util_abort("%s: sorry size/type mismatch between in-file keyword and new keyword \n",__func__); }
void ecl_file_kw_replace_kw( ecl_file_kw_type * file_kw , fortio_type * target , ecl_kw_type * new_kw ) { if (!ecl_type_is_equal( ecl_file_kw_get_data_type(file_kw), ecl_kw_get_data_type(new_kw) )) util_abort("%s: sorry type mismatch between in-file keyword and new keyword \n",__func__); if(file_kw->kw_size != ecl_kw_get_size(new_kw)) util_abort("%s: sorry size mismatch between in-file keyword and new keyword \n",__func__); if (file_kw->kw != NULL) ecl_kw_free( file_kw->kw ); file_kw->kw = new_kw; fortio_fseek( target , file_kw->file_offset , SEEK_SET ); ecl_kw_fwrite( file_kw->kw , target ); }
int fortio_fskip_record(fortio_type *fortio) { int record_size = fortio_init_read(fortio); fortio_fseek(fortio , (offset_type) record_size , SEEK_CUR); fortio_complete_read(fortio); return record_size; }
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); }