1/*
2 *  utils.m
3 *
4 *  $Id$
5 *
6 *  The iODBC driver manager.
7 *
8 *  Copyright (C) 1996-2021 OpenLink Software <iodbc@openlinksw.com>
9 *  All Rights Reserved.
10 *
11 *  This software is released under the terms of either of the following
12 *  licenses:
13 *
14 *      - GNU Library General Public License (see LICENSE.LGPL)
15 *      - The BSD License (see LICENSE.BSD).
16 *
17 *  Note that the only valid version of the LGPL license as far as this
18 *  project is concerned is the original GNU Library General Public License
19 *  Version 2, dated June 1991.
20 *
21 *  While not mandated by the BSD license, any patches you make to the
22 *  iODBC source code may be contributed back into the iODBC project
23 *  at your discretion. Contributions will benefit the Open Source and
24 *  Data Access community as a whole. Submissions may be made at:
25 *
26 *      http://www.iodbc.org
27 *
28 *
29 *  GNU Library Generic Public License Version 2
30 *  ============================================
31 *  This library is free software; you can redistribute it and/or
32 *  modify it under the terms of the GNU Library General Public
33 *  License as published by the Free Software Foundation; only
34 *  Version 2 of the License dated June 1991.
35 *
36 *  This library is distributed in the hope that it will be useful,
37 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
38 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39 *  Library General Public License for more details.
40 *
41 *  You should have received a copy of the GNU Library General Public
42 *  License along with this library; if not, write to the Free
43 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
44 *
45 *
46 *  The BSD License
47 *  ===============
48 *  Redistribution and use in source and binary forms, with or without
49 *  modification, are permitted provided that the following conditions
50 *  are met:
51 *
52 *  1. Redistributions of source code must retain the above copyright
53 *     notice, this list of conditions and the following disclaimer.
54 *  2. Redistributions in binary form must reproduce the above copyright
55 *     notice, this list of conditions and the following disclaimer in
56 *     the documentation and/or other materials provided with the
57 *     distribution.
58 *  3. Neither the name of OpenLink Software Inc. nor the names of its
59 *     contributors may be used to endorse or promote products derived
60 *     from this software without specific prior written permission.
61 *
62 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
66 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 */
74
75#import "utils.h"
76
77#define OPL_W2A(XW, XA, SIZE)      wcstombs((char *) XA, (wchar_t *) XW, SIZE)
78#define OPL_A2W(XA, XW, SIZE)      mbstowcs((wchar_t *) XW, (char *) XA, SIZE)
79
80
81static char *str_W2A (const wchar_t *inStr)
82{
83    char *outStr = NULL;
84    size_t len;
85
86    if (inStr == NULL)
87        return NULL;
88
89    len = wcslen (inStr);
90
91    if ((outStr = (char *) malloc (len*4 + 1)) != NULL)  /* for multi-byte encodings */
92    {
93        if (len > 0)
94            OPL_W2A (inStr, outStr, len);
95        outStr[len] = '\0';
96    }
97    return outStr;
98}
99
100
101static wchar_t* str_A2W (const char *inStr)
102{
103    wchar_t *outStr = NULL;
104    size_t len;
105
106    if (inStr == NULL)
107        return NULL;
108
109    len = strlen (inStr);
110
111    if ((outStr = (wchar_t*) calloc (len + 1, sizeof (wchar_t))) != NULL)
112    {
113        if (len > 0)
114            OPL_A2W (inStr, outStr, len);
115        outStr[len] = L'\0';
116    }
117    return outStr;
118}
119
120
121NSString* conv_wchar_to_NSString(const wchar_t* str)
122{
123    if (!str)
124        return nil;
125
126    int num = 1;
127    if(*(char *)&num == 1)
128        return [[[NSString alloc] initWithBytes:str length:wcslen(str)*sizeof(wchar_t) encoding:NSUTF32LittleEndianStringEncoding] autorelease];
129    else
130        return [[[NSString alloc] initWithBytes:str length:wcslen(str)*sizeof(wchar_t) encoding:NSUTF32BigEndianStringEncoding] autorelease];
131}
132
133#if OLD_1
134NSString* conv_wchar_to_NSString(const wchar_t* str)
135{
136    if (!str)
137        return nil;
138
139    CFMutableStringRef prov = CFStringCreateMutable(NULL, 0);
140    CFIndex i;
141    UniChar c;
142
143    if(prov)
144    {
145        for(i = 0 ; str[i] != L'\0' ; i++)
146        {
147            c = (UniChar)str[i];
148            CFStringAppendCharacters(prov, &c, 1);
149        }
150    }
151    return (NSString*)prov;
152}
153#endif
154
155
156
157wchar_t* conv_NSString_to_wchar(NSString* str)
158{
159    if (str == nil)
160        return NULL;
161
162    int len = str.length;
163    wchar_t *prov = malloc(sizeof(wchar_t) * (len+1));
164    CFIndex i;
165
166    if(prov)
167    {
168        for(i = 0 ; i<len ; i++)
169            prov[i] = CFStringGetCharacterAtIndex((CFStringRef)str, i);
170        prov[i] = L'\0';
171    }
172    return prov;
173}
174
175char* conv_NSString_to_char(NSString* str)
176{
177    if (str == nil)
178        return NULL;
179
180    wchar_t *prov = conv_NSString_to_wchar (str);
181    char *buffer = NULL;
182
183    if (prov)
184    {
185        buffer = str_W2A(prov);
186        free(prov);
187    }
188    return buffer;
189}
190
191
192NSString* conv_char_to_NSString(const char* str)
193{
194    if (!str)
195        return nil;
196    NSString *ret = nil;
197    wchar_t *buffer = str_A2W(str);
198    if (buffer)
199    {
200        ret = conv_wchar_to_NSString(buffer);
201        free(buffer);
202    }
203    return ret;
204}
205
206NSString* conv_to_NSString(const void * str, char waMode)
207{
208    if (str) {
209        if (waMode == 'A')
210            return conv_char_to_NSString((const char*)str);
211        else
212            return conv_wchar_to_NSString((const wchar_t*)str);
213    }
214    else
215        return nil;
216}
217
218
219BOOL showConfirm(const void *title, const void *message, char waMode)
220{
221    @autoreleasepool {
222        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
223        [alert addButtonWithTitle:@"Yes"]; /* first button */
224        [alert addButtonWithTitle:@"No"];
225        [alert setMessageText:(title?conv_to_NSString(title, waMode):@"")];
226        [alert setInformativeText:(message?conv_to_NSString(message, waMode):@"")];
227        [alert setAlertStyle:NSInformationalAlertStyle];
228        BOOL rc = ([alert runModal] == NSAlertFirstButtonReturn);
229/**
230        if ([alert runModal] == NSAlertFirstButtonReturn) {
231            // OK clicked, delete the record
232        }
233**/
234        return rc;
235
236    }
237
238}
239
240
241
242