Skip to content

Commit 0e64d02

Browse files
committed
ECC-2167 added test + absolute offset
1 parent fbd9656 commit 0e64d02

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

src/eccodes/grib_io.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ static int ecc_read_any(reader* r, int no_alloc, int grib_ok, int bufr_ok, int h
868868
unsigned char c;
869869
int err = 0;
870870
unsigned long magic = 0;
871-
size_t offset = 0;
871+
off_t offset = r->offset;
872872

873873
while (r->read(r->read_data, &c, 1, &err) == 1 && err == 0) {
874874
offset++;
@@ -1192,7 +1192,7 @@ static int ecc_wmo_read_any_from_file(FILE* f, void* buffer, size_t* len, off_t*
11921192
r.seek = &stdio_seek;
11931193
r.seek_from_start = &stdio_seek_from_start;
11941194
r.tell = &stdio_tell;
1195-
r.offset = 0;
1195+
r.offset = *offset;
11961196
r.message_size = 0;
11971197

11981198
err = read_any(&r, no_alloc, grib_ok, bufr_ok, hdf5_ok, wrap_ok);

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ list(APPEND test_c_bins
1313
wmo_read_bufr_from_file
1414
wmo_read_gts_from_file
1515
wmo_read_any_from_file
16+
wmo_read_any_from_file_offset
1617
wmo_read_any_from_stream
1718
grib_bpv_limit
1819
grib_double_cmp
@@ -219,6 +220,7 @@ if( HAVE_BUILD_TOOLS )
219220
wmo_read_bufr_from_file
220221
wmo_read_gts_from_file
221222
wmo_read_any_from_file
223+
wmo_read_any_from_file_offset
222224
wmo_read_any_from_stream
223225
bufr_templates
224226
bufr_rdbSubTypes
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* (C) Copyright 2005- ECMWF.
3+
*
4+
* This software is licensed under the terms of the Apache Licence Version 2.0
5+
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
*
7+
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8+
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9+
*/
10+
11+
#include <stdio.h>
12+
13+
#include "grib_api_internal.h"
14+
15+
#define SIZE 1024 * 1024
16+
char buffer[SIZE];
17+
18+
int test_next(FILE* in, size_t* len, off_t offset_expected, off_t* offset) // , size_t* previous_end_of_message)
19+
{
20+
*len = SIZE;
21+
22+
int err = wmo_read_any_from_file_offset(in, buffer, len, offset);
23+
if (err == GRIB_END_OF_FILE && *len == 0)
24+
printf("end of file\n");
25+
26+
ECCODES_ASSERT(*offset == offset_expected);
27+
28+
// *previous_end_of_message = *offset + *len;
29+
*offset += *len;
30+
31+
return err;
32+
}
33+
34+
int main(int argc, char** argv)
35+
{
36+
int err = 0;
37+
FILE* in = NULL;
38+
size_t len = SIZE;
39+
40+
if (argc != 2) return 1;
41+
42+
in = fopen(argv[1], "r");
43+
if (!in) return 1;
44+
45+
off_t offset = 0;
46+
// size_t previous_end_of_message = 0;
47+
48+
err = test_next(in, &len, 5, &offset); // , &previous_end_of_message);
49+
err = test_next(in, &len, 14161, &offset); // , &previous_end_of_message);
50+
err = test_next(in, &len, 27581, &offset); // , &previous_end_of_message);
51+
err = test_next(in, &len, 27689, &offset); // , &previous_end_of_message);
52+
err = test_next(in, &len, 42133, &offset); // , &previous_end_of_message);
53+
err = test_next(in, &len, 43333, &offset); // , &previous_end_of_message);
54+
55+
return err;
56+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
# (C) Copyright 2005- ECMWF.
3+
#
4+
# This software is licensed under the terms of the Apache Licence Version 2.0
5+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
#
7+
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8+
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9+
#
10+
11+
. ./include.ctest.sh
12+
13+
label="wmo_read_any_from_file_offset_test"
14+
tempText=temp.$label.txt
15+
16+
if [ $ECCODES_ON_WINDOWS -eq 1 ]; then
17+
echo "$0: This test is currently disabled on Windows"
18+
exit 0
19+
fi
20+
21+
${test_dir}/wmo_read_any_from_file_offset $data_dir/pad.grib
22+
23+
# Clean up
24+
rm -f $tempText

0 commit comments

Comments
 (0)