2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Allan CORNET
4 * Copyright (C) 2010 - DIGITEO - Antoine ELIAS
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 /*--------------------------------------------------------------------------*/
18 #include "filemanager.hxx"
27 #include "sci_malloc.h"
28 #include "os_string.h"
32 #include "freeArrayOfString.h"
37 /*--------------------------------------------------------------------------*/
38 // we do not want to depend on the OS specific LINE_MAX setting
48 /*--------------------------------------------------------------------------*/
49 static wchar_t *removeEOL(wchar_t *_inString);
50 static char *convertAnsiToUtf(char *_inString);
51 static wchar_t* getLine(wchar_t* _pstLine, int _iLineSize, types::File* _pFile);
52 /*--------------------------------------------------------------------------*/
53 #define UTF_16BE_BOM 0xFEFF // 0xFEFF = to_wide_string(0xEFBBBF)
54 /*--------------------------------------------------------------------------*/
55 wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
57 wchar_t **strLines = NULL;
58 types::File* pFile = NULL;
59 int iLineSizeMult = 1;
63 pFile = FileManager::getFile(fd);
65 if (nbLinesIn < 0 && fd == 5)
72 // int posix_handle = ::_fileno(pFile->getFiledesc());
74 // std::ifstream ifs(::_wfdopen(posix_handle, pFile->getFileMode().c_str()));
75 // std::list<string> lst;
78 // while(ifs.eof() == false && lst.size() < nbLinesIn)
80 // std::getline(ifs, str);
81 // lst.push_back(str);
84 // sciprint("size : %d\n", lst.size());
86 // *nbLinesOut = (int)lst.size();
87 // if(*nbLinesOut == 0)
92 // strLines = (wchar_t**)MALLOC(sizeof(wchar_t*) * *nbLinesOut);
93 // for(int i = 0 ; i < *nbLinesOut ; i++)
95 // strLines[i] = to_wide_string(lst.front().c_str());
104 wchar_t* Line = (wchar_t*)MALLOC(LINE_MAX * iLineSizeMult * sizeof(wchar_t));
109 strLines = (wchar_t **)MALLOC(sizeof(wchar_t *));
110 if (strLines == NULL)
113 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
117 while ( getLine ( Line, LINE_MAX * iLineSizeMult, pFile ) != NULL )
119 if (((int) wcslen(Line)) >= (LINE_MAX * iLineSizeMult) - 1 && iPos >= 0)
123 Line = (wchar_t*)MALLOC(LINE_MAX * iLineSizeMult * sizeof(wchar_t));
124 mseek(fd, iPos, SEEK_SET);
131 if ((nbLines == 0) && (Line[0] == UTF_16BE_BOM))
133 wchar_t* tmpLine = os_wcsdup(Line);
134 memset(Line, 0x00, LINE_MAX * iLineSizeMult);
135 wcscpy(Line, &tmpLine[1]);
140 strLines = (wchar_t **)REALLOC(strLines, nbLines * sizeof(wchar_t *));
141 if (strLines == NULL)
144 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
149 strLines[nbLines - 1] = os_wcsdup(removeEOL(Line));
150 if (strLines[nbLines - 1] == NULL)
153 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
154 freeArrayOfWideString(strLines, nbLines);
158 wcscpy(Line, EMPTYSTRW);
160 *nbLinesOut = nbLines;
161 *ierr = MGETL_NO_ERROR;
172 BOOL bContinue = TRUE;
174 strLines = (wchar_t **)MALLOC(sizeof(wchar_t *) * nbLinesIn);
175 if (strLines == NULL)
178 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
185 if (nbLines < nbLinesIn)
189 if ((ftell(pFile->getFiledesc()) == 0) && (nbLines == 0))
194 if ( getLine ( Line, LINE_MAX * iLineSizeMult, pFile) != NULL)
196 if (((int) wcslen(Line)) >= (LINE_MAX * iLineSizeMult) - 1)
200 Line = (wchar_t*)MALLOC(LINE_MAX * iLineSizeMult * sizeof(wchar_t));
201 mseek(fd, iPos, SEEK_SET);
208 if (header && (Line[0] == UTF_16BE_BOM))
210 wchar_t* tmpLine = os_wcsdup(Line);
211 memset(Line, 0x00, LINE_MAX * iLineSizeMult);
212 wcscpy(Line, &tmpLine[1]);
216 strLines[nbLines - 1] = os_wcsdup(removeEOL(Line));
217 if (strLines[nbLines - 1] == NULL)
220 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
222 freeArrayOfWideString(strLines, nbLines);
225 wcscpy(Line, EMPTYSTRW);
230 if (feof(pFile->getFiledesc()))
244 *nbLinesOut = nbLines;
251 *ierr = MGETL_NO_ERROR;
259 /*--------------------------------------------------------------------------*/
260 wchar_t* getLine(wchar_t* _pstLine, int _iLineSize, types::File* _pFile)
262 char* pstTemp = (char*)MALLOC(sizeof(char) * _iLineSize);
263 if (fgets(pstTemp, _iLineSize, _pFile->getFiledesc()) == NULL)
269 wchar_t* pstTempWide = to_wide_string(pstTemp);
270 wcscpy(_pstLine, pstTempWide);
275 /*--------------------------------------------------------------------------*/
276 wchar_t *removeEOL(wchar_t *_inString)
280 wchar_t *pos = wcschr(_inString, LF);
286 pos = wcschr(_inString, CR);
294 /*--------------------------------------------------------------------------*/
296 * convert ansi to Utf
298 static char *convertAnsiToUtf(char *_inString)
300 char *outString = NULL;
304 if (IsValidUTF8(_inString))
306 outString = os_strdup(_inString);
310 /* conversion ANSI to UTF */
313 BSTR bstrCode = NULL;
315 Len = MultiByteToWideChar(CP_ACP, 0, _inString, lstrlen(_inString), NULL, NULL);
316 bstrCode = SysAllocStringLen(NULL, Len);
319 MultiByteToWideChar(CP_ACP, 0, _inString, lstrlen(_inString), bstrCode, Len);
320 newLen = WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, outString, 0, NULL, NULL);
321 outString = (char*) MALLOC(sizeof(char) * (newLen + 1));
324 WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, outString, newLen, NULL, NULL);
328 outString = os_strdup(_inString);
330 SysFreeString(bstrCode);
335 outString = os_strdup(_inString);
339 if (IsValidUTF8(_inString))
341 outString = os_strdup(_inString);
345 int len = (int)strlen(_inString);
348 outString = (char*)MALLOC(((len * 3) + 1) * sizeof(char));
349 if (outString == NULL)
354 strcpy(outString, EMPTYSTR);
356 for (i = 0; i < len; i++)
358 char *outUtfChar = NULL;
359 unsigned char inAnsiChar = 0;
361 if (_inString[i] < 0)
363 inAnsiChar = 256 + _inString[i];
367 inAnsiChar = _inString[i];
370 if (inAnsiChar < 128)
372 outUtfChar = (char *)CALLOC(2, sizeof(char));
375 outUtfChar[0] = inAnsiChar;
381 outUtfChar = (char *)CALLOC(3, sizeof(char));
384 outUtfChar[0] = (inAnsiChar >> 6) | 0xC0;
385 outUtfChar[1] = (inAnsiChar & 0x3F) | 0x80;
392 strcat(outString, outUtfChar);
402 /*--------------------------------------------------------------------------*/