Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Win32 memfs support. #1

Closed
wants to merge 14 commits into from
Closed
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ endif()
ecbuild_add_option( FEATURE MEMFS
DESCRIPTION "Memory based access to definitions/samples"
DEFAULT OFF
CONDITION ECCODES_HAVE_FMEMOPEN OR ECCODES_HAVE_FUNOPEN
CONDITION ECCODES_HAVE_FMEMOPEN OR ECCODES_HAVE_FUNOPEN OR WIN32
REQUIRED_PACKAGES PythonInterp
)

Expand Down
4 changes: 2 additions & 2 deletions cmake/eccodes_test_endiness.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if( NOT DEFINED IEEE_BE )
endif()

if( ECCODES_BIG_ENDIAN AND NOT IEEE_BE )
ecbuild_critical("Failed to sanity check on endiness: OS should be Big-Endian but compiled code runs differently -- to ignore this pass -DIEEE_BE=0 to CMake/ecBuild")
ecbuild_critical("Failed to sanity check on endiness: OS should be Big-Endian but compiled code runs differently -- to ignore this pass -DIEEE_BE=1 to CMake/ecBuild")
endif()

if( NOT DEFINED IEEE_LE )
Expand Down Expand Up @@ -89,5 +89,5 @@ if( NOT DEFINED IEEE_LE )
endif()

if( ECCODES_LITTLE_ENDIAN AND NOT IEEE_LE )
ecbuild_critical("Failed to sanity check on endiness: OS should be Little-Endian but compiled code runs differently -- to ignore this pass -DIEEE_LE=0 to CMake/ecBuild")
ecbuild_critical("Failed to sanity check on endiness: OS should be Little-Endian but compiled code runs differently -- to ignore this pass -DIEEE_LE=1 to CMake/ecBuild")
endif()
2 changes: 1 addition & 1 deletion examples/C/bufr_keys_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int main(int argc,char* argv[])
{
/* not array */
vlen=MAX_VAL_LEN;
bzero(value,vlen);
memset(value, 0, vlen);
codes_get_string(h,name,value,&vlen);
printf("%s\n",value);
}
Expand Down
7 changes: 5 additions & 2 deletions examples/C/grib_keys_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#if !(defined(_WIN32) && defined(_MSC_VER))
#include <unistd.h>
#endif

#include "eccodes.h"

#define MAX_VAL_LEN 1024
Expand Down Expand Up @@ -82,7 +85,7 @@ int main(int argc, char *argv[])
{
const char* name = codes_keys_iterator_get_name(kiter);
vlen=MAX_VAL_LEN;
bzero(value,vlen);
memset(value, 0, vlen);
CODES_CHECK(codes_get_string(h,name,value,&vlen),name);
printf("%s = %s\n",name,value);

Expand Down
4 changes: 2 additions & 2 deletions examples/C/large_grib1.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ int main()
}

for (i=0; i<ni*nj; i++) {
double rand = random();
values[i] = 10*rand;
double r = rand();
values[i] = 10*r;
}

h = codes_grib_handle_new_from_samples(0, "GRIB1");
Expand Down
44 changes: 44 additions & 0 deletions memfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
if ext not in ['.def', '.table', '.tmpl']:
continue

full = full.replace("\\","/")
fname = full[full.find("/%s/" % (dname,)):]
#print("MEMFS add", fname)
name = re.sub(r'\W', '_', fname)
Expand Down Expand Up @@ -171,6 +172,49 @@
return funopen(f, &read_mem, &write_mem, &seek_mem, &close_mem);
}

#elif defined(_WIN32)

#include <io.h>
#include <fcntl.h>
#include <windows.h>

static FILE *fmemopen(void* buffer, size_t size, const char* mode) {
char path[MAX_PATH - 13];
if (!GetTempPath(sizeof(path), path))
return NULL;

char filename[MAX_PATH + 1];
if (!GetTempFileName(path, "ecc", 0, filename))
return NULL;

HANDLE h = CreateFile(filename,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
NULL);

if (h == INVALID_HANDLE_VALUE)
return NULL;

int fd = _open_osfhandle((intptr_t)h, _O_RDWR);
if (fd < 0) {
CloseHandle(h);
return NULL;
}

FILE* f = _fdopen(fd, "w+");
if (!f) {
_close(fd);
return NULL;
}

fwrite(buffer, size, 1, f);
rewind(f);
return f;
}

#endif

static size_t entries_count = sizeof(entries)/sizeof(entries[0]);
Expand Down
1 change: 1 addition & 0 deletions src/eccodes_windef.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/* Microsoft Windows Visual Studio support */
#if defined(_WIN32) && defined(_MSC_VER)
# define ECCODES_ON_WINDOWS
# define YY_NO_UNISTD_H
#endif

#endif
1 change: 0 additions & 1 deletion src/grib_accessor_class_codetable.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ static int pack_string(grib_accessor* a, const char* buffer, size_t *len)
#ifndef ECCODES_ON_WINDOWS
cmpproc cmp = (a->flags & GRIB_ACCESSOR_FLAG_LOWERCASE) ? strcmp_nocase : strcmp;
#else
/* Microsoft Windows Visual Studio support */
cmpproc cmp = (a->flags & GRIB_ACCESSOR_FLAG_LOWERCASE) ? stricmp : strcmp;
#endif

Expand Down
1 change: 0 additions & 1 deletion src/grib_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <string.h>
#include <sys/types.h>

/* Microsoft Windows Visual Studio support */
#include "eccodes_windef.h"

#ifndef ECCODES_ON_WINDOWS
Expand Down
1 change: 0 additions & 1 deletion src/grib_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#include <sys/types.h>
#include <sys/stat.h>

/* Microsoft Windows Visual Studio support */
#include "eccodes_windef.h"

#ifndef ECCODES_ON_WINDOWS
Expand Down
1 change: 0 additions & 1 deletion src/grib_jasper_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ int grib_jasper_encode(grib_context *c, j2k_encode_helper *helper)
#ifndef ECCODES_ON_WINDOWS
snprintf (opts, MAXOPTSSIZE, "mode=real\nrate=%f", 1.0/helper->compression);
#else
/* Microsoft Windows Visual Studio support */
_snprintf(opts, MAXOPTSSIZE, "mode=real\nrate=%f", 1.0/helper->compression);
#endif
}
Expand Down
Loading