bug 14909: getshortpathname and getlongpathname return values with '\' instead of '/'
[scilab.git] / scilab / modules / fileio / src / c / getshortpathname.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET
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 <string.h>
18 #include "getshortpathname.h"
19 #include "sci_malloc.h"
20 #include "charEncoding.h"
21 #include "os_string.h"
22 /*--------------------------------------------------------------------------*/
23 #ifdef _MSC_VER
24 #include <Windows.h> /* GetShortPathNameW */
25 #ifndef MAX_PATH_SHORT
26 #define MAX_PATH_SHORT 260
27 #endif
28 #endif
29 /*--------------------------------------------------------------------------*/
30 int C2F(getshortpathname)(char *pathname, int *len)
31 {
32     if (pathname)
33     {
34         BOOL bConvert = FALSE;
35         char *result = NULL;
36         pathname[*len] = 0;
37         result = getshortpathname(pathname, &bConvert);
38         if (result)
39         {
40             strcpy(pathname, result);
41             *len = (int)strlen(result);
42             FREE(result);
43             result = NULL;
44             return 1;
45         }
46     }
47     return 0;
48 }
49 /*--------------------------------------------------------------------------*/
50 char *getshortpathname(const char *longpathname, BOOL *convertok)
51 {
52     char *ShortName = NULL;
53
54     if (longpathname)
55     {
56 #ifdef _MSC_VER
57         /* first we try to call to know path length */
58         wchar_t *ptwlongpathname = to_wide_string(longpathname);
59         wchar_t *ptwShortName = NULL;
60         int length = GetShortPathNameW(ptwlongpathname, NULL, 0);
61
62         if (length <= 0 )
63         {
64             length = MAX_PATH_SHORT;
65         }
66
67         ptwShortName = (wchar_t*)MALLOC((length + 1) * sizeof(wchar_t));
68
69         if (ptwShortName)
70         {
71             /* second converts path */
72             if ( GetShortPathNameW(ptwlongpathname, ptwShortName, length) )
73             {
74                 ShortName = wide_string_to_UTF8(ptwShortName);
75                 *convertok = TRUE;
76             }
77             else
78             {
79                 /* FAILED */
80                 ShortName = os_strdup(longpathname);
81                 *convertok = FALSE;
82             }
83             if (ptwShortName)
84             {
85                 FREE(ptwShortName);
86                 ptwShortName = NULL;
87             }
88         }
89         else
90         {
91             /* FAILED */
92             ShortName = os_strdup(longpathname);
93             *convertok = FALSE;
94         }
95         if (ptwlongpathname)
96         {
97             FREE(ptwlongpathname);
98             ptwlongpathname = NULL;
99         }
100 #else
101         /* Linux */
102         int length = (int)strlen(longpathname) + 1;
103         ShortName = (char*)MALLOC((length) * sizeof(char));
104         if (ShortName)
105         {
106             strcpy(ShortName, longpathname);
107         }
108         *convertok = FALSE;
109 #endif
110     }
111     else
112     {
113         /* FAILED */
114         *convertok = FALSE;
115     }
116     return ShortName;
117 }
118 /*--------------------------------------------------------------------------*/
119 wchar_t* getshortpathnameW(const wchar_t* _pwstLongPathName, BOOL* _pbOK)
120 {
121     wchar_t* pwstOutput = NULL;
122     if (_pwstLongPathName)
123     {
124 #ifdef _MSC_VER
125         int iLen = GetShortPathNameW(_pwstLongPathName, NULL, 0);
126
127         if (iLen <= 0)
128         {
129             iLen = MAX_PATH_SHORT;
130         }
131
132         pwstOutput = (wchar_t*)MALLOC((iLen + 1) * sizeof(wchar_t));
133
134         if (pwstOutput)
135         {
136             /* second converts path */
137             if (GetShortPathNameW(_pwstLongPathName, pwstOutput, iLen))
138             {
139                 *_pbOK = TRUE;
140             }
141             else
142             {
143                 FREE(pwstOutput);
144                 pwstOutput = os_wcsdup(_pwstLongPathName);
145                 *_pbOK = FALSE;
146             }
147         }
148         else
149         {
150             /* FAILED */
151             pwstOutput = os_wcsdup(_pwstLongPathName);
152             *_pbOK = FALSE;
153         }
154 #else
155         /* Linux and MacOS*/
156         pwstOutput = os_wcsdup(_pwstLongPathName);
157         *_pbOK = FALSE;
158 #endif
159     }
160     else
161     {
162         *_pbOK = FALSE;
163     }
164     return pwstOutput;
165 }
166 /*--------------------------------------------------------------------------*/