* Bug #12383 fixed - csvRead memory leak
[scilab.git] / scilab / modules / spreadsheet / src / c / latintoutf.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010-2011 - DIGITEO - Allan CORNET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 #include <string.h>
13 #ifdef _MSC_VER
14 #include <windows.h>
15 #include "strdup_windows.h"
16 #endif
17 #include "latintoutf.h"
18 #include "charEncoding.h"
19 #include "MALLOC.h"
20 // =============================================================================
21 #ifdef _MSC_VER
22 #pragma comment(lib,"oleaut32.lib")
23 #endif
24 // =============================================================================
25 #define LINE_MAX 4096
26 #define EMPTYSTR ""
27 // =============================================================================
28 char *latintoutf(char *_inString)
29 {
30     char *outString = NULL;
31     if (_inString)
32     {
33 #ifdef _MSC_VER
34         if (IsValidUTF8(_inString))
35         {
36             outString = strdup(_inString);
37         }
38         else
39         {
40             /* conversion ANSI to UTF */
41             int Len = 0;
42             int newLen = 0;
43             BSTR bstrCode = NULL;
44
45             Len = MultiByteToWideChar(CP_ACP, 0, _inString, lstrlen(_inString), NULL, NULL);
46             bstrCode = SysAllocStringLen(NULL, Len);
47             if (bstrCode)
48             {
49                 if (MultiByteToWideChar(CP_ACP, 0, _inString, lstrlen(_inString), bstrCode, Len) == 0)
50                 {
51                     return strdup(_inString);
52                 }
53
54                 newLen = WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, outString, 0, NULL, NULL);
55                 outString = (char*) MALLOC(newLen + 1);
56                 if (outString)
57                 {
58                     if (WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, outString, newLen, NULL, NULL) == 0)
59                     {
60                         SysFreeString(bstrCode);
61                         FREE(outString);
62                         return strdup(_inString);
63                     }
64                 }
65                 else
66                 {
67                     outString = strdup(_inString);
68                 }
69                 SysFreeString(bstrCode);
70                 bstrCode = NULL;
71             }
72             else
73             {
74                 outString = strdup(_inString);
75             }
76         }
77 #else
78         if (IsValidUTF8(_inString))
79         {
80             outString = strdup(_inString);
81         }
82         else
83         {
84             int len = (int)strlen(_inString);
85             int i = 0;
86
87             outString = (char*)MALLOC(((len * 3) + 1) * sizeof(char));
88             if (outString == NULL)
89             {
90                 return NULL;
91             }
92             strcpy(outString, EMPTYSTR);
93
94             for (i = 0; i < len; i++)
95             {
96                 unsigned char *outUtfChar = NULL;
97                 unsigned char inAnsiChar = 0;
98
99                 if (_inString[i] < 0)
100                 {
101                     inAnsiChar = 256 + _inString[i];
102                 }
103                 else
104                 {
105                     inAnsiChar = _inString[i];
106                 }
107
108                 if (inAnsiChar < 128)
109                 {
110                     outUtfChar = (unsigned char *)CALLOC(2, sizeof(unsigned char));
111                     if (outUtfChar)
112                     {
113                         outUtfChar[0] = inAnsiChar;
114                         outUtfChar[1] = 0;
115                     }
116                 }
117                 else
118                 {
119                     outUtfChar = (unsigned char *)CALLOC(3, sizeof(unsigned char));
120                     if (outUtfChar)
121                     {
122                         outUtfChar[0] = (inAnsiChar >> 6) | 0xC0;
123                         outUtfChar[1] = (inAnsiChar & 0x3F) | 0x80;
124                         outUtfChar[2] = 0;
125                     }
126                 }
127
128                 if (outUtfChar)
129                 {
130                     strcat(outString, (char*)outUtfChar);
131                     FREE(outUtfChar);
132                     outUtfChar = NULL;
133                 }
134             }
135         }
136 #endif
137     }
138     return outString;
139 }
140 /*--------------------------------------------------------------------------*/