2d8d4f343b36ffdd2ed4505be017fd44b25aa818
[scilab.git] / scilab / modules / fileio / src / c / getFullFilename.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 /*--------------------------------------------------------------------------*/
16 #include "getFullFilename.h"
17 #include "charEncoding.h"
18 #include "sci_malloc.h"
19 #include "splitpath.h"
20 #include "PATH_MAX.h"
21 #include "scicurdir.h"
22 #include "isdir.h"
23 #include "fullpath.h"
24 /*--------------------------------------------------------------------------*/
25 wchar_t *getFullFilenameW(const wchar_t* FilenameInput)
26 {
27     wchar_t *pStwcFullFilename = NULL;
28
29     pStwcFullFilename = (wchar_t *)MALLOC(sizeof(wchar_t) * (PATH_MAX * 2));
30     if (pStwcFullFilename)
31     {
32         int i = 0;
33         int lenPath = 0;
34
35         wchar_t wcDrv[PATH_MAX * 2];
36         wchar_t wcDir[PATH_MAX * 2];
37         wchar_t wcName[PATH_MAX * 2];
38         wchar_t wcExt[PATH_MAX * 2];
39
40         wchar_t *wcNameExt = (wchar_t *)MALLOC(sizeof(wchar_t) * (PATH_MAX * 2));
41         wchar_t *wcPath = (wchar_t *)MALLOC(sizeof(wchar_t) * (PATH_MAX * 2));
42         wchar_t *wcTmp = NULL;
43
44         if (wcNameExt == NULL || wcPath == NULL)
45         {
46             FREE(pStwcFullFilename);
47             if (wcNameExt)
48             {
49                 FREE(wcNameExt);
50             }
51             if (wcPath)
52             {
53                 FREE(wcPath);
54             }
55             return NULL;
56         }
57         splitpathW(FilenameInput, TRUE, wcDrv, wcDir,  wcName, wcExt);
58
59         wcscpy(wcNameExt, wcName);
60         wcscat(wcNameExt, wcExt);
61
62         wcscpy(wcPath, wcDrv);
63         wcscat(wcPath, wcDir);
64
65         if (wcscmp(wcPath, L"") == 0)
66         {
67             int ierr = 0;
68             wchar_t *wcCurrentDir = scigetcwdW(&ierr);
69             if (ierr == 0)
70             {
71                 wcscpy(wcPath, wcCurrentDir);
72             }
73             if (wcCurrentDir)
74             {
75                 FREE(wcCurrentDir);
76                 wcCurrentDir = NULL;
77             }
78         }
79
80         wcTmp = get_full_pathW((const wchar_t*)wcPath);
81         wcscpy(wcPath, wcTmp);
82         FREE(wcTmp);
83         wcTmp = NULL;
84
85         lenPath = (int)wcslen(wcPath);
86         if (lenPath - 1 >= 0)
87         {
88             if ( (wcPath[lenPath - 1 ] != L'/') && (wcPath[lenPath - 1 ] != L'\\') )
89             {
90                 wcscat(wcPath, L"/");
91                 lenPath = (int)wcslen(wcPath);
92             }
93         }
94
95         for ( i = 0; i < lenPath; i++)
96         {
97 #ifdef _MSC_VER
98             if (wcPath[i] == L'/')
99             {
100                 wcPath[i] = L'\\';
101             }
102 #else
103             if (wcPath[i] == L'\\')
104             {
105                 wcPath[i] = L'/';
106             }
107 #endif
108         }
109
110         wcscpy(pStwcFullFilename, wcPath);
111         wcscat(pStwcFullFilename, wcNameExt);
112
113         FREE(wcNameExt);
114         FREE(wcPath);
115     }
116     return pStwcFullFilename;
117
118 }
119 /*--------------------------------------------------------------------------*/
120 char *getFullFilename(const char* Filename)
121 {
122     char *pStFullFilename = NULL;
123     if (Filename)
124     {
125         wchar_t *wcFilename = to_wide_string(Filename);
126         if (wcFilename)
127         {
128             wchar_t *pStwcFullFilename = getFullFilenameW(wcFilename);
129             FREE(wcFilename);
130             if (pStwcFullFilename)
131             {
132                 pStFullFilename = wide_string_to_UTF8(pStwcFullFilename);
133                 FREE(pStwcFullFilename);
134             }
135         }
136     }
137
138     return pStFullFilename;
139 }
140 /*--------------------------------------------------------------------------*/