1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 #include "grib_api_internal.h"
12 
13 #ifdef HAVE_MEMFS
14 /* These two functions are implemented in the generated C file memfs.c in the build area */
15 /* See the memfs.py Python generator */
16 int codes_memfs_exists(const char* path);
17 FILE* codes_memfs_open(const char* path);
18 
codes_fopen(const char * name,const char * mode)19 FILE* codes_fopen(const char* name, const char* mode)
20 {
21     FILE* f;
22 
23     if (strcmp(mode, "r") != 0) {
24         return fopen(name, mode);
25     }
26 
27     f = codes_memfs_open(name); /* Load from memory */
28     if (f) {
29         return f;
30     }
31 
32     return fopen(name, mode);
33 }
34 
codes_access(const char * name,int mode)35 int codes_access(const char* name, int mode)
36 {
37     if (mode != F_OK) {
38         return access(name, mode);
39     }
40 
41     if (codes_memfs_exists(name)) { /* Check memory */
42         return 0;
43     }
44 
45     return access(name, mode);
46 }
47 
48 #else
49 
codes_fopen(const char * name,const char * mode)50 FILE* codes_fopen(const char* name, const char* mode)
51 {
52     return fopen(name, mode);
53 }
54 
codes_access(const char * name,int mode)55 int codes_access(const char* name, int mode)
56 {
57     return access(name, mode);
58 }
59 
60 #endif
61