eccodes/tools/codes_export_resource.cc

119 lines
3.5 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);
2023-06-02 11:11:34 +00:00
printf(" -s Extract a resource from the samples directory\n");
printf(" -d Extract a resource from the definitions directory\n");
printf("\n");
printf(" E.g., %s -s GRIB2.tmpl my.grib2\n", prog);
2024-04-24 17:17:02 +00:00
printf(" E.g., %s -d grib2/boot.def grib2_boot.txt\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 {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: Invalid option: Specify either -s or -d\n");
2022-03-12 12:52:00 +00:00
return 1;
}
2022-03-12 14:23:23 +00:00
if (resource_type == SAMPLE) {
2024-03-01 23:34:18 +00:00
full_path = get_external_sample_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) {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: Failed to access %s file: '%s'\n", resource_name, resource_path);
2022-03-12 14:23:23 +00:00
return 1;
}
fout = fopen(out_file, "wb");
if (!fout) {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: Failed to open output file '%s'\n", out_file);
2024-01-10 17:08:29 +00:00
perror(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) {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: 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
}
2024-04-24 17:17:02 +00:00
2022-03-12 14:45:57 +00:00
/* write resource bytes to fout */
2024-04-24 17:17:02 +00:00
int read_count = 0;
2022-03-12 14:45:57 +00:00
while (0 < (bytes = fread(buffer, 1, sizeof(buffer), fin))) {
if (fwrite(buffer, 1, bytes, fout) != bytes) {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: Failed to write out bytes\n");
2022-03-12 14:45:57 +00:00
return 1;
}
2024-04-24 17:17:02 +00:00
++read_count;
2022-03-12 14:45:57 +00:00
}
2022-03-12 14:23:23 +00:00
if (fclose(fin) != 0) {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: 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) {
2024-04-24 17:17:02 +00:00
fprintf(stderr, "Error: Call to fclose failed (output)\n");
2022-03-12 14:23:23 +00:00
return 1;
}
grib_context_free(c, full_path);
2022-03-12 12:52:00 +00:00
2024-04-24 17:17:02 +00:00
if (read_count == 0) {
2024-11-12 21:54:49 +00:00
fprintf(stderr, "Error: Number of bytes read == 0. Please check the path refers to a regular %s file.\n",
(resource_type == SAMPLE) ? "sample" : "definition");
2024-04-24 17:17:02 +00:00
} else {
printf("Resource exported to file '%s'.\n", out_file);
}
2022-03-12 12:52:00 +00:00
return 0;
}