/src/netcdf-c/libncpoco/ncpoco.c
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************* |
2 | | * Copyright 1993, UCAR/Unidata |
3 | | * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
4 | | * Derived from poco library from Boost Software: see nccpoco/SourceLicense file. |
5 | | *********************************************************************/ |
6 | | |
7 | | #include "config.h" |
8 | | #include <stdlib.h> |
9 | | #include <string.h> |
10 | | #ifdef HAVE_UNISTD_H |
11 | | #include <unistd.h> |
12 | | #endif |
13 | | |
14 | | #include "netcdf.h" |
15 | | #include "ncpoco.h" |
16 | | |
17 | | /**************************************************/ |
18 | | #ifdef _WIN32 |
19 | | extern struct NCPAPI ncp_win32_api; |
20 | | #else |
21 | | extern struct NCPAPI ncp_unix_api; |
22 | | #endif |
23 | | |
24 | | /**************************************************/ |
25 | | |
26 | | /* Creates a SharedLib object. */ |
27 | | EXTERNL int |
28 | | ncpsharedlibnew(NCPSharedLib** libp) |
29 | 0 | { |
30 | 0 | int ret = NC_NOERR; |
31 | 0 | NCPSharedLib* lib; |
32 | 0 | lib = (NCPSharedLib*)calloc(1,sizeof(NCPSharedLib)); |
33 | 0 | if(lib == 0) |
34 | 0 | {ret = NC_ENOMEM; goto done;} |
35 | | /* fill in the api */ |
36 | | #ifdef _WIN32 |
37 | | lib->api = ncp_win32_api; |
38 | | #else |
39 | 0 | lib->api = ncp_unix_api; |
40 | 0 | #endif |
41 | 0 | ret = lib->api.init(lib); |
42 | 0 | if(ret == NC_NOERR && libp) |
43 | 0 | *libp = lib; |
44 | | |
45 | 0 | done: |
46 | 0 | return ret; |
47 | 0 | } |
48 | | |
49 | | /* free this shared library */ |
50 | | EXTERNL int |
51 | | ncpsharedlibfree(NCPSharedLib* lib) |
52 | 0 | { |
53 | 0 | int ret = NC_NOERR; |
54 | 0 | if(lib == NULL) return NC_NOERR; |
55 | 0 | ret = lib->api.unload(lib); |
56 | 0 | ret = lib->api.reclaim(lib); |
57 | | /* reclaim common stuff */ |
58 | 0 | nullfree(lib->path); |
59 | 0 | free(lib); |
60 | 0 | return ret; |
61 | 0 | } |
62 | | |
63 | | /* |
64 | | Loads a shared library from the given path using specified flags. |
65 | | Returns error if a library has already been loaded or cannot be loaded. |
66 | | */ |
67 | | EXTERNL int |
68 | | ncpload(NCPSharedLib* lib, const char* path, int flags) |
69 | 0 | { |
70 | 0 | if(lib == NULL || path == NULL) return NC_EINVAL; |
71 | 0 | return lib->api.load(lib,path,flags); |
72 | 0 | } |
73 | | |
74 | | EXTERNL int |
75 | | ncpunload(NCPSharedLib* lib) /* Unloads a shared library. */ |
76 | 0 | { |
77 | 0 | if(lib == NULL) return NC_EINVAL; |
78 | 0 | return lib->api.unload(lib); |
79 | 0 | } |
80 | | |
81 | | EXTERNL int |
82 | | ncpisloaded(NCPSharedLib* lib) /* Returns 1 iff a library has been loaded. */ |
83 | 0 | { |
84 | 0 | if(lib == NULL) return 0; |
85 | 0 | return lib->api.isloaded(lib); |
86 | 0 | } |
87 | | |
88 | | /* Returns the address of the symbol with the given name. |
89 | | For functions, this is the entry point of the function. |
90 | | Return error if the symbol does not exist |
91 | | */ |
92 | | EXTERNL void* |
93 | | ncpgetsymbol(NCPSharedLib* lib,const char* name) |
94 | 0 | { |
95 | 0 | if(lib == NULL) return NULL; |
96 | 0 | return lib->api.getsymbol(lib,name); |
97 | 0 | } |
98 | | |
99 | | /* Returns the path of the library, as specified in |
100 | | a call to load() |
101 | | */ |
102 | | EXTERNL const char* |
103 | | ncpgetpath(NCPSharedLib* lib) |
104 | 0 | { |
105 | 0 | if(lib == NULL) return NULL; |
106 | 0 | return lib->api.getpath(lib); |
107 | 0 | } |
108 | | |
109 | | /* Returns the last err msg. */ |
110 | | EXTERNL const char* |
111 | | ncpgeterrmsg(NCPSharedLib* lib) |
112 | 0 | { |
113 | 0 | if(lib == NULL) return NULL; |
114 | 0 | return (lib->err.msg[0] == '\0' ? NULL : lib->err.msg); |
115 | 0 | } |