7b50aa0040ed73048d9295a3baac434147581cbc
[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  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 /*--------------------------------------------------------------------------*/
14 #include <string.h>
15 #include "getshortpathname.h"
16 #include "MALLOC.h"
17 #include "charEncoding.h"
18 #include "os_strdup.h"
19 /*--------------------------------------------------------------------------*/
20 #ifdef _MSC_VER
21         #ifndef MAX_PATH_SHORT
22                 #define MAX_PATH_SHORT 260
23         #endif
24 #endif
25 /*--------------------------------------------------------------------------*/
26 int C2F(getshortpathname)(char *pathname,int *len)
27 {
28         if (pathname)
29         {
30                 BOOL bConvert = FALSE;
31                 char *result = NULL;
32                 pathname[*len] = 0;
33                 result = getshortpathname(pathname,&bConvert);
34                 if (result)
35                 {
36                         strcpy(pathname, result);
37                         *len = (int)strlen(result);
38                         FREE(result);
39                         result = NULL;
40                         return 1;
41                 }
42         }
43         return 0;
44 }
45 /*--------------------------------------------------------------------------*/
46 char *getshortpathname(char *longpathname,BOOL *convertok)
47 {
48         char *ShortName = NULL;
49
50         if (longpathname)
51         {
52                 #ifdef _MSC_VER
53                 /* first we try to call to know path length */
54                 wchar_t *ptwlongpathname = to_wide_string(longpathname);
55                 wchar_t *ptwShortName = NULL;
56                 int length = GetShortPathNameW(ptwlongpathname, NULL, 0);
57
58                 if (length <= 0 ) length = MAX_PATH_SHORT;
59
60                 ptwShortName = (wchar_t*)MALLOC((length + 1)*sizeof(wchar_t));
61
62                 if (ptwShortName) 
63                 {
64                         /* second converts path */
65                         if ( GetShortPathNameW(ptwlongpathname, ptwShortName, length) )
66                         {
67                                 ShortName = wide_string_to_UTF8(ptwShortName);
68                                 *convertok = TRUE;
69                         }
70                         else
71                         {
72                                 /* FAILED */
73                                 ShortName = os_strdup(longpathname);
74                                 *convertok = FALSE;
75                         }
76                         if (ptwShortName) {FREE(ptwShortName);ptwShortName = NULL;}
77                 }
78                 else
79                 {
80                         /* FAILED */
81                         ShortName = os_strdup(longpathname);
82                         *convertok = FALSE;
83                 }
84                 if (ptwlongpathname) { FREE(ptwlongpathname); ptwlongpathname = NULL;}
85                 #else
86                 /* Linux */
87                 int length = (int)strlen(longpathname) + 1;
88                 ShortName = (char*)MALLOC((length) * sizeof(char));
89                 if (ShortName) strcpy(ShortName, longpathname);
90                 *convertok = FALSE;
91                 #endif
92         }
93         else
94         {
95                 /* FAILED */
96                 *convertok = FALSE;
97         }
98         return ShortName;
99 }
100 /*--------------------------------------------------------------------------*/