2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 Universal iconv implementation for OS/2.
25 Andrey Vasilkin, 2016.
28 #define INCL_DOSMODULEMGR /* Module Manager values */
29 #define INCL_DOSERRORS /* Error values */
38 # define debug(s,...) printf(__func__"(): "##s"\n" ,##__VA_ARGS__)
40 # define debug(s,...) do {} while (0)
43 /* Exports from os2iconv.c */
44 extern iconv_t _System os2_iconv_open(const char* tocode, const char* fromcode);
45 extern size_t _System os2_iconv(iconv_t cd, char* * inbuf,
46 size_t *inbytesleft, char* * outbuf,
47 size_t *outbytesleft);
48 extern int _System os2_iconv_close(iconv_t cd);
50 /* Functions pointers types */
51 typedef iconv_t _System (*FNICONV_OPEN)(const char* tocode, const char* fromcode);
52 typedef size_t _System (*FNICONV)(iconv_t cd, char* * inbuf,
53 size_t *inbytesleft, char* * outbuf,
54 size_t *outbytesleft);
55 typedef int _System (*FNICONV_CLOSE)(iconv_t cd);
57 /* Used DLL module handle */
58 static HMODULE hmIconv = NULLHANDLE;
59 /* Functions pointers */
60 static FNICONV_OPEN fn_iconv_open = NULL;
61 static FNICONV fn_iconv = NULL;
62 static FNICONV_CLOSE fn_iconv_close = NULL;
65 static BOOL _loadDLL(PSZ pszName, PSZ pszIconvOpen, PSZ pszIconv,
71 ulRC = DosLoadModule(acError, sizeof(acError), pszName, &hmIconv);
72 if (ulRC != NO_ERROR) {
73 debug("DLL not loaded: %s", &acError);
78 ulRC = DosQueryProcAddr(hmIconv, 0, pszIconvOpen, (PFN *)&fn_iconv_open);
79 if (ulRC != NO_ERROR) {
80 debug("Error: cannot find entry %s in %s", pszIconvOpen, pszName);
84 ulRC = DosQueryProcAddr(hmIconv, 0, pszIconv, (PFN *)&fn_iconv);
85 if (ulRC != NO_ERROR) {
86 debug("Error: cannot find entry %s in %s", pszIconv, pszName);
90 ulRC = DosQueryProcAddr(hmIconv, 0, pszIconvClose, (PFN *)&fn_iconv_close);
91 if (ulRC != NO_ERROR) {
92 debug("Error: cannot find entry %s in %s", pszIconvClose, pszName);
96 debug("DLL %s used", pszName);
100 DosFreeModule(hmIconv);
101 hmIconv = NULLHANDLE;
105 static void _init(void)
107 if (fn_iconv_open != NULL) /* Already was initialized */
110 /* Try to load kiconv.dll, iconv2.dll or iconv.dll */
111 if (!_loadDLL("KICONV", "_libiconv_open", "_libiconv", "_libiconv_close") &&
112 !_loadDLL("ICONV2", "_libiconv_open", "_libiconv", "_libiconv_close") &&
113 !_loadDLL("ICONV", "_iconv_open", "_iconv", "_iconv_close") ) {
114 /* No DLL was loaded - use OS/2 conversion objects API */
115 debug("Uni*() API used");
116 fn_iconv_open = os2_iconv_open;
117 fn_iconv = os2_iconv;
118 fn_iconv_close = os2_iconv_close;
127 /* Non-standard function for iconv to unload the used dynamic library */
128 void libiconv_clean(void)
130 if (hmIconv != NULLHANDLE) {
131 DosFreeModule(hmIconv);
132 hmIconv = NULLHANDLE;
134 fn_iconv_open = NULL;
136 fn_iconv_close = NULL;
140 iconv_t libiconv_open(const char* tocode, const char* fromcode)
143 return fn_iconv_open(tocode, fromcode);
146 size_t libiconv(iconv_t cd, char* * inbuf, size_t *inbytesleft,
147 char* * outbuf, size_t *outbytesleft)
149 return fn_iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
152 int libiconv_close(iconv_t cd)
154 return fn_iconv_close(cd);
157 /* vi: set ts=4 sw=4 expandtab: */