Debian Codegraph @

3rdparty/xspublic/xstypes/xslibraryloader.c

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #include "xslibraryloader.h"
34 #include "xsstring.h"
35
36 #ifdef __GNUC__
37 #include <dlfcn.h>
38 #elif defined(_MSC_VER)
39 #include <Windows.h>
40 #endif
41
42 /*! \brief Dynamically load a library
43
44 \param[in,out] thisp the XsLibraryLoader object handle
45 \param[in] libraryName the name of the library to load. The library
46 should be present in the current search path, or be specified by a path
47 \return non-zero if the library could be loaded, zero otherwise
48 */
49 int XsLibraryLoader_load(XsLibraryLoader* thisp, const XsString* libraryName)
50 {
51 #ifdef __GNUC__
52 if (XsLibraryLoader_isLoaded(thisp))
53 return 0;
54 thisp->m_handle = dlopen(libraryName->m_data, RTLD_LAZY);
55 #elif defined(_MSC_VER)
56 wchar_t *libraryNameW;
57 XsSize required;
58 if (XsLibraryLoader_isLoaded(thisp))
59 return 0;
60 required = XsString_copyToWCharArray(libraryName, NULL, 0);
61 libraryNameW = (wchar_t*)malloc(required * sizeof(wchar_t));
62
63 (void)XsString_copyToWCharArray(libraryName, libraryNameW, required);
64
65 thisp->m_handle = LoadLibrary(libraryNameW);
66
67 free(libraryNameW);
68 #endif
69 return XsLibraryLoader_isLoaded(thisp);
70 }
71
72 /*! \brief Resolve a function from the library
73
74 \param[in] thisp the library handle
75 \param[in] functionName the name of the function
76 \return a pointer to the resolved function, may be NULL if no function could be resolved
77 */
78 void* XsLibraryLoader_resolve(const XsLibraryLoader* thisp, const char* functionName)
79 {
80 #ifdef __GNUC__
81 return dlsym(thisp->m_handle, functionName);
82 #elif defined(_MSC_VER)
83 #pragma warning(push)
84 #pragma warning(disable: 4152)
85 return GetProcAddress(thisp->m_handle, functionName);
86 #pragma warning(pop)
87 #endif
88 }
89
90 /*! \brief Unload the loaded library
91
92 \param[in,out] thisp the library handle
93 \return zero on failure, non-zero otherwise
94 */
95 int XsLibraryLoader_unload(XsLibraryLoader* thisp)
96 {
97 void *handle = thisp->m_handle;
98 thisp->m_handle = NULL;
99 if (handle)
100 {
101 #ifdef __GNUC__
102 return dlclose(handle) == 0;
103 #elif defined(_MSC_VER)
104 return FreeLibrary(handle) != 0;
105 #endif
106 }
107 return 0;
108 }
109
110 /*! \brief Check if a library is loaded
111
112 \param[in] thisp the library handle
113 \return zero if nothing is loaded, non-zero otherwise
114 */
115 int XsLibraryLoader_isLoaded(const XsLibraryLoader* thisp)
116 {
117 return thisp->m_handle != NULL;
118 }
119
120 /*! \brief Get an error string after a failure occurred
121 \param[in,out] error the string to fill with the result error
122 */
123 void XsLibraryLoader_getErrorString(XsString* error)
124 {
125 #ifdef __GNUC__
126 XsString_assignCharArray(error, dlerror());
127 #elif defined(_MSC_VER)
128 LPTSTR errorText = NULL;
129 (void)FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
130 |FORMAT_MESSAGE_ALLOCATE_BUFFER
131 |FORMAT_MESSAGE_IGNORE_INSERTS,
132 NULL,
133 GetLastError(),
134 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
135 (LPTSTR)&errorText,
136 0,
137 NULL);
138
139 XsString_assignWCharArray(error, errorText);
140 LocalFree(errorText);
141 #endif
142 }