Use fopen in binary mode (Windows support)

This commit is contained in:
Shahram Najm 2019-02-28 12:19:28 +00:00
parent 5bac99bc73
commit 974999976e
8 changed files with 76 additions and 73 deletions

View File

@ -90,7 +90,7 @@ int main(int argc, char** argv)
/* Encode the keys back in the data section */ /* Encode the keys back in the data section */
CODES_CHECK(codes_set_long(h, "pack", 1), 0); CODES_CHECK(codes_set_long(h, "pack", 1), 0);
fout = fopen(outfilename, "w"); fout = fopen(outfilename, "wb");
if (!fout) { if (!fout) {
fprintf(stderr, "Failed to open (create) output file.\n"); fprintf(stderr, "Failed to open (create) output file.\n");
return 1; return 1;

View File

@ -29,7 +29,7 @@ int main(int argc,char* argv[])
const char* key_name = "#5#brightnessTemperature"; const char* key_name = "#5#brightnessTemperature";
const double tolerance = 1e-6; const double tolerance = 1e-6;
in=fopen(infile,"r"); in=fopen(infile,"rb");
assert(in); assert(in);
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS)

View File

@ -46,7 +46,7 @@ int main(int argc,char* argv[])
} }
assert(input_filename); assert(input_filename);
f = fopen(input_filename, "r"); f = fopen(input_filename, "rb");
assert(f); assert(f);
h = codes_handle_new_from_file(NULL, f, PRODUCT_BUFR, &err); h = codes_handle_new_from_file(NULL, f, PRODUCT_BUFR, &err);
assert(h); assert(h);

View File

@ -38,13 +38,13 @@ int main(int argc, char* argv[]) {
infile1=argv[1]; infile1=argv[1];
infile2=argv[2]; infile2=argv[2];
f1=fopen(infile1,"r"); f1=fopen(infile1,"rb");
if (!f1) { if (!f1) {
perror(infile1); perror(infile1);
exit(1); exit(1);
} }
f2=fopen(infile2,"r"); f2=fopen(infile2,"rb");
if (!f2) { if (!f2) {
perror(infile2); perror(infile2);
exit(1); exit(1);

View File

@ -36,7 +36,7 @@ int main(int argc, char** argv)
if (argc<2) usage(argv[0]); if (argc<2) usage(argv[0]);
filename=argv[1]; filename=argv[1];
in = fopen(filename,"r"); in = fopen(filename,"rb");
if(!in) { if(!in) {
printf("ERROR: unable to open file %s\n",filename); printf("ERROR: unable to open file %s\n",filename);
return 1; return 1;

View File

@ -36,13 +36,13 @@ int main(int argc, char** argv)
if (argc<2) usage(argv[0]); if (argc<2) usage(argv[0]);
in = fopen(argv[1],"r"); in = fopen(argv[1],"rb");
if(!in) { if(!in) {
printf("ERROR: unable to open file %s\n",argv[1]); printf("ERROR: unable to open file %s\n",argv[1]);
return 1; return 1;
} }
/* create new handle from a message in a file*/ /* create new handle */
err = 0; err = 0;
h = grib_handle_new_from_file(0,in,&err); h = grib_handle_new_from_file(0,in,&err);
if (h == NULL) { if (h == NULL) {

View File

@ -51,7 +51,7 @@ static void test_reduced_gg(int remove_local_def, int edition, const char* packi
grib_util_packing_spec packing_spec={0,}; grib_util_packing_spec packing_spec={0,};
assert(input_filename); assert(input_filename);
in = fopen(input_filename,"r"); assert(in); in = fopen(input_filename,"rb"); assert(in);
handle = grib_handle_new_from_file(0,in,&err); assert(handle); handle = grib_handle_new_from_file(0,in,&err); assert(handle);
CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen),0); CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen),0);
@ -60,7 +60,7 @@ static void test_reduced_gg(int remove_local_def, int edition, const char* packi
return; return;
} }
assert(output_filename); assert(output_filename);
out = fopen(output_filename,"w"); assert(out); out = fopen(output_filename,"wb"); assert(out);
CODES_CHECK(grib_get_size(handle,"values",&inlen), 0); CODES_CHECK(grib_get_size(handle,"values",&inlen), 0);
values = (double*)malloc(sizeof(double)*inlen); values = (double*)malloc(sizeof(double)*inlen);
@ -155,7 +155,7 @@ static void test_regular_ll(int remove_local_def, int edition, const char* packi
grib_util_packing_spec packing_spec={0,}; grib_util_packing_spec packing_spec={0,};
assert(input_filename); assert(input_filename);
in = fopen(input_filename,"r"); assert(in); in = fopen(input_filename,"rb"); assert(in);
handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle); handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle);
CODES_CHECK(codes_get_long(handle, "edition", &input_edition), 0); CODES_CHECK(codes_get_long(handle, "edition", &input_edition), 0);
@ -166,7 +166,7 @@ static void test_regular_ll(int remove_local_def, int edition, const char* packi
return; return;
} }
assert(output_filename); assert(output_filename);
out = fopen(output_filename,"w"); assert(out); out = fopen(output_filename,"wb"); assert(out);
CODES_CHECK(codes_get_size(handle,"values",&inlen), 0); CODES_CHECK(codes_get_size(handle,"values",&inlen), 0);
values = (double*)malloc(sizeof(double)*inlen); values = (double*)malloc(sizeof(double)*inlen);
@ -255,7 +255,7 @@ static void test_grid_complex_spatial_differencing(int remove_local_def, int edi
grib_util_grid_spec spec={0,}; grib_util_grid_spec spec={0,};
grib_util_packing_spec packing_spec={0,}; grib_util_packing_spec packing_spec={0,};
in = fopen(input_filename,"r"); assert(in); in = fopen(input_filename,"rb"); assert(in);
handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle); handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle);
CODES_CHECK(grib_get_string(handle, "packingType", gridType, &slen),0); CODES_CHECK(grib_get_string(handle, "packingType", gridType, &slen),0);
@ -263,7 +263,7 @@ static void test_grid_complex_spatial_differencing(int remove_local_def, int edi
grib_handle_delete(handle); grib_handle_delete(handle);
return; return;
} }
out = fopen(output_filename,"w"); assert(out); out = fopen(output_filename,"wb"); assert(out);
CODES_CHECK(codes_get_size(handle,"values",&inlen), 0); CODES_CHECK(codes_get_size(handle,"values",&inlen), 0);
values = (double*)malloc(sizeof(double)*inlen); values = (double*)malloc(sizeof(double)*inlen);

View File

@ -9,71 +9,74 @@
*/ */
#include "grib_api_internal.h" #include "grib_api_internal.h"
static void usage(const char* prog) { static void usage (const char *prog)
printf("usage: %s infile\n",prog); {
exit(1); printf ("usage: %s infile\n", prog);
exit (1);
} }
static unsigned char buffer[50000000]; static unsigned char buffer[50000000];
int main(int argc,char* argv[]) { int main (int argc, char *argv[])
{
char *filename;
FILE *f;
grib_handle *h = NULL;
grib_context *c;
size_t size = 0;
int ret = 0;
size_t bufsize = sizeof (buffer);
long count, step, edition, totalLength;
char gridType[50], levelType[50], level[50], shortName[50];
size_t gridTypelen = sizeof (gridType);
size_t levelTypelen = sizeof (levelType);
size_t levellen = sizeof (level);
size_t shortNamelen = sizeof (shortName);
size_t len;
char* filename; if (argc != 2)
FILE* f; usage (argv[0]);
grib_handle* h=NULL; filename = argv[1];
grib_context* c;
size_t size=0;
int ret=0;
size_t bufsize=sizeof(buffer);
long count,step,edition,totalLength;
char gridType[50],levelType[50],level[50],shortName[50];
size_t gridTypelen=sizeof(gridType);
size_t levelTypelen=sizeof(levelType);
size_t levellen=sizeof(level);
size_t shortNamelen=sizeof(shortName);
size_t len;
if (argc!=2) usage(argv[0]); f = fopen (filename, "rb");
filename=argv[1]; if (!f) {
perror (filename);
exit (1);
}
c = grib_context_get_default ();
f=fopen(filename,"r"); size = bufsize;
if (!f) { count = 1;
perror(filename); while ((ret = grib_read_any_from_file (c, f, buffer, &size)) == GRIB_SUCCESS) {
exit(1); if ((1)) {
} h = grib_handle_new_from_message_copy (c, buffer, size);
c=grib_context_get_default(); if (!h) {
printf ("unable to new from message\n");
size=bufsize; exit (1);
count=1; }
while ((ret=grib_read_any_from_file(c,f,buffer,&size))==GRIB_SUCCESS) { else {
if ((1)) { grib_get_long (h, "edition", &edition);
h=grib_handle_new_from_message_copy(c,buffer,size); grib_get_long (h, "step", &step);
if (!h) { grib_get_long (h, "totalLength", &totalLength);
printf("unable to new from message\n"); len = gridTypelen;
exit(1); grib_get_string (h, "gridType", gridType, &len);
} else { len = levelTypelen;
grib_get_long(h,"edition",&edition); GRIB_CHECK (grib_get_string (h, "levelType", levelType, &len), 0);
grib_get_long(h,"step",&step); len = levellen;
grib_get_long(h,"totalLength",&totalLength); grib_get_string (h, "level", level, &len);
len=gridTypelen; len = shortNamelen;
grib_get_string(h,"gridType",gridType,&len); grib_get_string (h, "shortName", shortName, &len);
len=levelTypelen; printf ("- %3ld -\t ed=%ld\t size=%8ld totalLength=%8ld \t %s\t %s\t %s\t level=%s\t step=%ld\n",
GRIB_CHECK(grib_get_string(h,"levelType",levelType,&len),0); count, edition, (long) size, totalLength, shortName, gridType, levelType, level, step);
len=levellen; grib_handle_delete (h);
grib_get_string(h,"level",level,&len); }
len=shortNamelen; }
grib_get_string(h,"shortName",shortName,&len); else {
printf("- %3ld -\t ed=%ld\t size=%8ld totalLength=%8ld \t %s\t %s\t %s\t level=%s\t step=%ld\n", printf ("MESSAGE #%ld\n", count);
count, edition, (long)size, totalLength, shortName, gridType, levelType, level, step); }
grib_handle_delete(h); size = bufsize;
} count++;
} else { }
printf("MESSAGE #%ld\n",count);
}
size=bufsize;
count++;
}
return 0;
return 0;
} }