eccodes/tools/codes_export_resource.c

105 lines
2.8 KiB
C
Raw Normal View History

2022-03-12 12:52:00 +00:00
/*
* (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"
2022-03-12 14:23:23 +00:00
typedef enum ResourceType
{
UNKNOWN,
SAMPLE,
DEFINITION
} ResourceType;
2022-03-12 12:52:00 +00:00
static void usage(const char* prog)
{
2022-03-12 14:23:23 +00:00
printf("Usage: %s [-s | -d] resource_path out_file\n", prog);
2022-03-12 12:52:00 +00:00
exit(1);
}
2022-03-12 14:23:23 +00:00
#define SIZE (1024 * 1024)
2022-03-12 12:52:00 +00:00
int main(int argc, char* argv[])
{
2022-03-12 14:23:23 +00:00
char* resource_path = NULL;
const char* resource_name = NULL;
2022-03-12 14:23:23 +00:00
ResourceType resource_type = UNKNOWN;
char* full_path = NULL;
char* out_file = NULL;
char* option = NULL;
grib_context* c = grib_context_get_default();
FILE* fin = NULL;
FILE* fout = NULL;
char buffer[SIZE] = {0,};
size_t bytes = 0;
2022-03-12 12:52:00 +00:00
2022-03-12 14:23:23 +00:00
if (argc != 4) usage(argv[0]);
2022-03-12 12:52:00 +00:00
2022-03-12 14:23:23 +00:00
option = argv[1];
resource_path = argv[2];
out_file = argv[3];
2022-03-12 12:52:00 +00:00
if (strcmp(option, "-s") == 0) {
2022-03-12 14:23:23 +00:00
resource_type = SAMPLE;
resource_name = "sample";
}
else if (strcmp(option, "-d") == 0) {
resource_type = DEFINITION;
resource_name = "definition";
}
else {
2022-03-12 12:52:00 +00:00
fprintf(stderr, "Invalid option: Specify either -s or -d\n");
return 1;
}
2022-03-12 14:23:23 +00:00
if (resource_type == SAMPLE) {
2022-03-14 19:54:22 +00:00
full_path = get_external_template_path(c, resource_path);
2022-03-12 14:23:23 +00:00
}
else if (resource_type == DEFINITION) {
full_path = grib_context_full_defs_path(c, resource_path);
}
if (!full_path) {
2022-03-12 14:45:57 +00:00
fprintf(stderr, "Failed to access %s: '%s'\n", resource_name, resource_path);
2022-03-12 14:23:23 +00:00
return 1;
}
fout = fopen(out_file, "wb");
if (!fout) {
2022-03-12 14:45:57 +00:00
fprintf(stderr, "Failed to open output file '%s'\n", out_file);
2022-03-12 14:23:23 +00:00
return 1;
2022-03-12 12:52:00 +00:00
}
2022-03-12 14:23:23 +00:00
fin = codes_fopen(full_path, "r");
if (!fin) {
2022-03-12 14:45:57 +00:00
fprintf(stderr, "Failed to open resource '%s'\n", full_path);
2022-11-04 16:59:12 +00:00
fclose(fout);
2022-03-12 14:23:23 +00:00
return 1;
2022-03-12 12:52:00 +00:00
}
2022-03-12 14:45:57 +00:00
/* write resource bytes to fout */
while (0 < (bytes = fread(buffer, 1, sizeof(buffer), fin))) {
if (fwrite(buffer, 1, bytes, fout) != bytes) {
fprintf(stderr, "Failed to write out bytes\n");
return 1;
}
}
2022-03-12 14:23:23 +00:00
if (fclose(fin) != 0) {
fprintf(stderr, "Call to fclose failed (input)\n");
2022-03-12 12:52:00 +00:00
return 1;
}
2022-03-12 14:23:23 +00:00
if (fclose(fout) != 0) {
fprintf(stderr, "Call to fclose failed (output)\n");
return 1;
}
grib_context_free(c, full_path);
2022-03-12 12:52:00 +00:00
printf("Resource exported to file '%s'.\n", out_file);
2022-03-12 12:52:00 +00:00
return 0;
}