2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA - Allan CORNET
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 "getshortpathname.h"
19 #include "sci_malloc.h"
20 #include "charEncoding.h"
21 #include "os_string.h"
22 /*--------------------------------------------------------------------------*/
24 #include <Windows.h> /* GetShortPathNameW */
25 #ifndef MAX_PATH_SHORT
26 #define MAX_PATH_SHORT 260
29 /*--------------------------------------------------------------------------*/
30 int C2F(getshortpathname)(char *pathname, int *len)
34 BOOL bConvert = FALSE;
37 result = getshortpathname(pathname, &bConvert);
40 strcpy(pathname, result);
41 *len = (int)strlen(result);
49 /*--------------------------------------------------------------------------*/
50 char *getshortpathname(const char *longpathname, BOOL *convertok)
52 char *ShortName = NULL;
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);
64 length = MAX_PATH_SHORT;
67 ptwShortName = (wchar_t*)MALLOC((length + 1) * sizeof(wchar_t));
71 /* second converts path */
72 if ( GetShortPathNameW(ptwlongpathname, ptwShortName, length) )
74 ShortName = wide_string_to_UTF8(ptwShortName);
80 ShortName = os_strdup(longpathname);
92 ShortName = os_strdup(longpathname);
97 FREE(ptwlongpathname);
98 ptwlongpathname = NULL;
102 int length = (int)strlen(longpathname) + 1;
103 ShortName = (char*)MALLOC((length) * sizeof(char));
106 strcpy(ShortName, longpathname);
118 /*--------------------------------------------------------------------------*/
119 wchar_t* getshortpathnameW(const wchar_t* _pwstLongPathName, BOOL* _pbOK)
121 wchar_t* pwstOutput = NULL;
122 if (_pwstLongPathName)
125 int iLen = GetShortPathNameW(_pwstLongPathName, NULL, 0);
129 iLen = MAX_PATH_SHORT;
132 pwstOutput = (wchar_t*)MALLOC((iLen + 1) * sizeof(wchar_t));
136 /* second converts path */
137 if (GetShortPathNameW(_pwstLongPathName, pwstOutput, iLen))
144 pwstOutput = os_wcsdup(_pwstLongPathName);
151 pwstOutput = os_wcsdup(_pwstLongPathName);
156 pwstOutput = os_wcsdup(_pwstLongPathName);
166 /*--------------------------------------------------------------------------*/