File: grib_ecc-2188.cc

package info (click to toggle)
eccodes 2.46.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 154,956 kB
  • sloc: cpp: 163,970; ansic: 26,310; sh: 22,006; f90: 6,854; perl: 6,361; python: 5,352; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 279; xml: 183; awk: 66
file content (176 lines) | stat: -rw-r--r-- 4,912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * (C) Copyright 2005- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
 */

#include <stdio.h>
#include <iostream>

#include "grib_api_internal.h"
#include "eccodes.h"

#define SIZE 1024 * 1024 * 20


static void deleter(void* data)
{
    delete[] static_cast<unsigned char*>(data);
}

static int test_message_ownership(const char* filename) // , size_t* previous_end_of_message)
{
    FILE* in = fopen(filename, "r");
    if (!in) {
        printf("Error opening file %s\n", filename);
        return 1;
    }

    while (true) {
        size_t buf_len = SIZE;
        void* buf = new unsigned char[SIZE];

        int err = wmo_read_any_from_file(in, buf, &buf_len);

        if (err == GRIB_END_OF_FILE && buf_len == 0) {
            break;
        }

        if (err != GRIB_SUCCESS) {
            printf("Error reading message at offset: %s\n", codes_get_error_message(err));
            return err;
        }

        codes_handle* h = codes_handle_new_from_message(NULL, buf, buf_len);
        if (!h) {
            printf("Error creating handle at offset\n");
            return 1;
        }

        err = codes_handle_change_buffer_ownership(h, deleter);

        if (err != GRIB_SUCCESS) {
            printf("Error taking message ownership at offset: %s\n", codes_get_error_message(err));
            codes_handle_delete(h);
            return err;
        }

        codes_handle_delete(h);
        // codes_context_free(NULL, buf); // Don't free the buffer here. Freed by codes_handle_delete
    }

    fclose(in);
    return 0;
}

static long fread_wrapper(void* stream_data, void* buffer, long len) {
    return fread(buffer, 1, len, (FILE*) stream_data);
}

static int test_codes_handle_from_stream(const char* filename)
{
    FILE* in = fopen(filename, "r");
    if (!in) {
        printf("Error opening file %s\n", filename);
        return 1;
    }

    codes_handle* h = NULL;
    int err = 0;

    while ((h = codes_handle_new_from_stream(NULL, reinterpret_cast<void*>(in), fread_wrapper, &err)) != NULL) {
        if (err != GRIB_SUCCESS) {
            printf("Error reading message from stream: %s\n", codes_get_error_message(err));
            return err;
        }

        long paramId = 0;
        err = codes_get_long(h, "paramId", &paramId); // Just a dummy operation
        if (err != GRIB_SUCCESS) {
            printf("Error getting paramId: %s\n", codes_get_error_message(err));
            codes_handle_delete(h);
            return err;
        }

        codes_handle_delete(h);
    }

    fclose(in);
    return 0;
}

size_t posInStream = 0;

static long fread_wrapper_with_offset(void* stream_data, void* buffer, long len) {
    size_t nBytesRead = fread(buffer, 1, len, (FILE*) stream_data);
    posInStream += nBytesRead;
    return nBytesRead;
}

static int test_codes_handle_from_stream_with_offset(const char* filename)
{
    std::cout << "Testing codes_handle_new_from_stream with offset..." << std::endl;
    FILE* in = fopen(filename, "r");
    if (!in) {
        printf("Error opening file %s\n", filename);
        return 1;
    }

    codes_handle* h = NULL;
    int err = 0;

    while ((h = codes_handle_new_from_stream(NULL, reinterpret_cast<void*>(in), fread_wrapper_with_offset, &err)) != NULL) {
        if (err != GRIB_SUCCESS) {
            printf("Error reading message from stream: %s\n", codes_get_error_message(err));
            return err;
        }

        size_t messageSize = 0;
        err = codes_get_message_size(h, &messageSize);
        if (err != GRIB_SUCCESS) {
            printf("Error getting message length: %s\n", codes_get_error_message(err));
            codes_handle_delete(h);
            return err;
        }

        std::cout << "Message offset: " << posInStream - messageSize << " size: " << messageSize << std::endl;

        long paramId = 0;
        err = codes_get_long(h, "paramId", &paramId); // Just a dummy operation
        if (err != GRIB_SUCCESS) {
            printf("Error getting paramId: %s\n", codes_get_error_message(err));
            codes_handle_delete(h);
            return err;
        }

        codes_handle_delete(h);
    }

    fclose(in);
    return 0;
}

int main(int argc, char** argv)
{
    int err = 0;

    if (argc != 2) return 1;

    err = test_message_ownership(argv[1]);
    if (err) return err;

    err = test_codes_handle_from_stream(argv[1]);
    if (err) return err;

    // TODO(maee): Check why this fails on Windows?
//#ifndef ECCODES_ON_WINDOWS
    err = test_codes_handle_from_stream_with_offset(argv[1]);
    if (err) return err;
//#endif

    return 0;
}