Coverity: fileio module memory errors fixed.
[scilab.git] / scilab / modules / fileio / sci_gateway / cpp / sci_get_absolute_file_path.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2011 - Digiteo - Cedric DELAMARRE
4  *
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
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.
14  *
15  */
16 /*--------------------------------------------------------------------------*/
17 #include "funcmanager.hxx"
18 #include "fileio_gw.hxx"
19 #include "function.hxx"
20 #include "string.hxx"
21 #include "filemanager.hxx"
22
23 extern "C"
24 {
25 #include <stdio.h>
26 #include <string.h>
27 #include "sci_malloc.h"
28 #include "Scierror.h"
29 #include "localization.h"
30 #include "freeArrayOfString.h"
31 }
32
33 /*--------------------------------------------------------------------------*/
34 types::Function::ReturnValue sci_get_absolute_file_path(types::typed_list &in, int _iRetCount, types::typed_list &out)
35 {
36     wchar_t* wcsFileName = NULL;
37     wchar_t** wcsFilesOpened = NULL;
38     wchar_t* wcsTemp = NULL;
39     wchar_t* wcsPath = NULL;
40
41     if (in.size() != 1)
42     {
43         Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "get_absolute_file_path", 1);
44         return types::Function::Error;
45     }
46
47     if (in[0]->isString() == false || in[0]->getAs<types::String>()->isScalar() == false)
48     {
49         Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "get_absolute_file_path", 1);
50         return types::Function::Error;
51     }
52
53     wcsFileName = in[0]->getAs<types::String>()->get(0);
54     wcsFilesOpened = FileManager::getFilenames();
55
56     for (int i = FileManager::getOpenedCount() - 1; i >= 0; --i)
57     {
58         wcsTemp = wcsstr(wcsFilesOpened[i], wcsFileName);
59         if (wcsTemp)
60         {
61             int iSize = (int)(wcsTemp - wcsFilesOpened[i]);
62             if (wcslen(wcsFilesOpened[i]) == wcslen(wcsFileName) + iSize)
63             {
64                 wcsPath = (wchar_t*)MALLOC((iSize + 1) * sizeof(wchar_t));
65                 memcpy(wcsPath, wcsFilesOpened[i], iSize * sizeof(wchar_t));
66                 wcsPath[iSize] = L'\0';
67                 types::String* pStringOut = new types::String(wcsPath);
68                 FREE(wcsPath);
69                 out.push_back(pStringOut);
70                 freeArrayOfWideString(wcsFilesOpened, FileManager::getOpenedCount());
71                 return types::Function::OK;
72             }
73             else
74             {
75                 wcsTemp = NULL;
76             }
77         }
78     }
79
80     freeArrayOfWideString(wcsFilesOpened, FileManager::getOpenedCount());
81
82     char* pstFile = wide_string_to_UTF8(wcsFileName);
83     Scierror(999, _("%s: The file %s is not opened in scilab.\n"), "get_absolute_file_path", pstFile);
84     FREE(pstFile);
85     return types::Function::Error;
86 }
87 /*--------------------------------------------------------------------------*/