File: bufr_check_descriptors.cc

package info (click to toggle)
eccodes 2.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,404 kB
  • sloc: cpp: 162,953; ansic: 26,308; sh: 21,742; f90: 6,854; perl: 6,361; python: 5,172; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 283; xml: 183; awk: 66
file content (132 lines) | stat: -rw-r--r-- 4,932 bytes parent folder | download | duplicates (2)
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
/*
 * (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 "grib_api_internal.h"

static const size_t MIN_NUM_COLUMNS = 8;
static const size_t NUM_DESCRIPTOR_DIGITS = 6; /* FXY */

#define NUMBER(a) (sizeof(a) / sizeof(a[0]))
static const char* allowed_types[] = { "long", "double", "table", "flag", "string" };

static int check_descriptor_type(const char* atype)
{
    size_t i = 0, numTypes = NUMBER(allowed_types);
    for (i = 0; i < numTypes; ++i) {
        if (strcmp(atype, allowed_types[i]) == 0) {
            return GRIB_SUCCESS;
        }
    }
    return GRIB_INVALID_ARGUMENT;
}

int main(int argc, char** argv)
{
    char* filename  = NULL;
    FILE* fp        = NULL;
    char line[1024] = {0,};
    char** list = NULL;
    size_t i = 0, line_number = 0;
    long lValue     = 0;
    char* str_code  = NULL; /* descriptor */
    char* str_key   = NULL;
    char* str_type  = NULL;
    char* str_scale = NULL;
    char* str_ref   = NULL;
    char* str_width = NULL;
    char* str_units = NULL;
    bufr_descriptor v;
    const size_t maxlen_keyName = sizeof(v.shortName);
    const size_t maxlen_units   = sizeof(v.units);

    ECCODES_ASSERT(argc == 2);

    filename = argv[1];
    fp       = fopen(filename, "r");
    ECCODES_ASSERT(fp);

    while (fgets(line, sizeof(line) - 1, fp)) {
        ++line_number;
        ECCODES_ASSERT(strlen(line) > 0);
        if (line[0] == '#') continue; /* Ignore first line with column titles */
        list = string_split(line, "|");
        if (!list) {
            fprintf(stderr, "Error on line %zu: string_split failed!\n", line_number);
            return 1;
        }
        for (i = 0; list[i] != NULL; ++i) {} /* count how many tokens */
        if (i < MIN_NUM_COLUMNS) {
            fprintf(stderr, "Error on line %zu: Number of columns (=%zu) < required miniumum (=%zu)!\n",
                    line_number, i, MIN_NUM_COLUMNS);
            return 1;
        }
        str_code = list[0];
        if (string_to_long(str_code, &lValue, 1) != GRIB_SUCCESS) {
            fprintf(stderr, "Error on line %zu: descriptor code '%s' (column 1) is not numeric.\n",
                    line_number, str_code);
            return 1;
        }
        if (strlen(str_code) != NUM_DESCRIPTOR_DIGITS) {
            fprintf(stderr, "Error on line %zu: descriptor code '%s' (column 1) is not %zu digits.\n",
                    line_number, str_code, NUM_DESCRIPTOR_DIGITS);
            return 1;
        }
        str_key  = list[1];
        str_type = list[2];
        if (check_descriptor_type(str_type) != GRIB_SUCCESS) {
            fprintf(stderr, "Error on line %zu: descriptor key type '%s' (column 3) is not valid.\n",
                    line_number, str_type);
            fprintf(stderr, "Please choose one of:\n");
            for (i = 0; i < NUMBER(allowed_types); ++i) {
                fprintf(stderr, "\t%s\n", allowed_types[i]);
            }
            return 1;
        }
        if (strlen(str_key) >= maxlen_keyName) {
            fprintf(stderr, "Error on line %zu: descriptor key name '%s' (column 2) exceeds %zu characters.\n",
                    line_number, str_key, maxlen_keyName);
            return 1;
        }
        str_units = list[4];
        if (strlen(str_units) >= maxlen_units) {
            fprintf(stderr, "Error on line %zu: descriptor units '%s' (column 5) exceeds %zu characters.\n",
                    line_number, str_units, maxlen_units);
            return 1;
        }
        str_scale = list[5];
        str_ref   = list[6];
        str_width = list[7];
        if (string_to_long(str_scale, &lValue, 1) != GRIB_SUCCESS) {
            fprintf(stderr, "Error on line %zu: descriptor scale '%s' (column 6) is not numeric.\n",
                    line_number, str_scale);
            return 1;
        }
        if (string_to_long(str_ref, &lValue, 1) != GRIB_SUCCESS) {
            fprintf(stderr, "Error on line %zu: descriptor reference '%s' (column 7) is not numeric.\n",
                    line_number, str_ref);
            return 1;
        }
        // The final width column can have spaces etc at the end. So turn off strict mode
        if (string_to_long(str_width, &lValue, /*strict=*/0) != GRIB_SUCCESS) {
            fprintf(stderr, "Error on line %zu: descriptor width '%s' (column 8) is not numeric.\n",
                    line_number, str_width);
            return 1;
        }

        for (i = 0; list[i] != NULL; ++i)
            free(list[i]);
        free(list);
    }

    fclose(fp);

    grib_context_delete(grib_context_get_default());
    return 0;
}