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 177 178 179 180 181 182 183 184 185 186 187
|
/*
* (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 <string.h>
#include <unistd.h>
#include "grib_api_internal.h"
#include "loady.h"
#include "load.h"
FILE* fout;
extern FILE* load_in;
extern int load_parse();
extern int yylineno;
static grib_handle* h = 0;
static double* double_value = 0;
static size_t double_value_size = 0;
static size_t double_value_count = 0;
static long* long_value = 0;
static size_t long_value_size = 0;
static size_t long_value_count = 0;
static int err;
const char* out_file;
static void check(int code, const char* name)
{
if (code) {
printf("%s: cannot set %s: %s\n", prog, name, grib_get_error_message(code));
err++;
}
}
static grib_handle* handle()
{
if (h == 0)
h = grib_handle_new_from_samples(NULL, "GRIB1");
/* grib_dump_content(h,stdout,"debug",~0,NULL); */
return h;
}
void load_finish()
{
const void* buffer;
size_t size;
assert(h);
GRIB_CHECK(grib_get_message(h, &buffer, &size), "");
printf("%s: writing %ld bytes message\n", prog, (long)size);
if (fwrite(buffer, 1, size, fout) != size) {
perror(out_file);
exit(1);
}
grib_handle_delete(h);
h = NULL;
}
void load_missing(const char* name)
{
printf("grib_set_missing(%s)\n", name);
check(grib_set_missing(handle(), name), name);
}
void load_long(const char* name, long value)
{
printf("grib_set_long(%s,%ld)\n", name, value);
check(grib_set_long(handle(), name, value), name);
}
void load_string(const char* name, const char* value)
{
size_t s = strlen(value);
printf("grib_set_string(%s,%s)\n", name, value);
check(grib_set_string(handle(), name, value, &s), name);
}
void load_double(const char* name, double value)
{
printf("grib_set_double(%s,%g)\n", name, value);
check(grib_set_double(handle(), name, value), name);
}
void load_start_array()
{
if (double_value == NULL) {
double_value_size = 100000;
double_value = malloc(double_value_size * sizeof(double));
}
if (long_value == NULL) {
long_value_size = 100000;
long_value = malloc(long_value_size * sizeof(long));
}
double_value_count = 0;
long_value_count = 0;
}
void load_end_array(const char* name)
{
if (double_value_count == long_value_count) {
printf("grib_set_long_array(%s,%ld)\n", name, (long)long_value_count);
check(grib_set_long_array(handle(), name, long_value, long_value_count), name);
}
else {
printf("grib_set_double_array(%s,%ld)\n", name, (long)double_value_count);
check(grib_set_double_array(handle(), name, double_value, double_value_count), name);
}
}
void load_double_value(double value)
{
if (double_value_count == double_value_size) {
double_value_size += 100000;
double_value = realloc(double_value, double_value_size * sizeof(double));
}
double_value[double_value_count++] = value;
}
void load_long_value(long value)
{
if (long_value_count == long_value_size) {
long_value_size += 100000;
long_value = realloc(long_value, long_value_size * sizeof(long));
}
long_value[long_value_count++] = value;
load_double_value(value);
}
int load_wrap()
{
return 1;
}
int load_error(const char* msg)
{
printf("%s line %d\n", msg, yylineno);
err = 1;
return 1;
}
int load_file(const char* in, const char* out)
{
err = 0;
fout = fopen(out, "w");
if (!fout) {
perror(out);
return 1;
}
load_in = fopen(in, "r");
if (!load_in) {
perror(in);
return 1;
}
load_parse();
fclose(load_in);
if (fclose(fout)) {
perror(out);
return 1;
}
return err;
}
|