add getshortpathname function
[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 #include "os_wcsdup.h"
20 /*--------------------------------------------------------------------------*/
21 #ifdef _MSC_VER
22         #ifndef MAX_PATH_SHORT
23                 #define MAX_PATH_SHORT 260
24         #endif
25 #endif
26 /*--------------------------------------------------------------------------*/
27 int C2F(getshortpathname)(char *pathname,int *len)
28 {
29         if (pathname)
30         {
31                 BOOL bConvert = FALSE;
32                 char *result = NULL;
33                 pathname[*len] = 0;
34                 result = getshortpathname(pathname,&bConvert);
35                 if (result)
36                 {
37                         strcpy(pathname, result);
38                         *len = (int)strlen(result);
39                         FREE(result);
40                         result = NULL;
41                         return 1;
42                 }
43         }
44         return 0;
45 }
46 /*--------------------------------------------------------------------------*/
47 char *getshortpathname(char *longpathname,BOOL *convertok)
48 {
49         char *ShortName = NULL;
50
51         if (longpathname)
52         {
53                 #ifdef _MSC_VER
54                 /* first we try to call to know path length */
55                 wchar_t *ptwlongpathname = to_wide_string(longpathname);
56                 wchar_t *ptwShortName = NULL;
57                 int length = GetShortPathNameW(ptwlongpathname, NULL, 0);
58
59                 if (length <= 0 ) length = MAX_PATH_SHORT;
60
61                 ptwShortName = (wchar_t*)MALLOC((length + 1)*sizeof(wchar_t));
62
63                 if (ptwShortName) 
64                 {
65                         /* second converts path */
66                         if ( GetShortPathNameW(ptwlongpathname, ptwShortName, length) )
67                         {
68                                 ShortName = wide_string_to_UTF8(ptwShortName);
69                                 *convertok = TRUE;
70                         }
71                         else
72                         {
73                                 /* FAILED */
74                                 ShortName = os_strdup(longpathname);
75                                 *convertok = FALSE;
76                         }
77                         if (ptwShortName) {FREE(ptwShortName);ptwShortName = NULL;}
78                 }
79                 else
80                 {
81                         /* FAILED */
82                         ShortName = os_strdup(longpathname);
83                         *convertok = FALSE;
84                 }
85                 if (ptwlongpathname) { FREE(ptwlongpathname); ptwlongpathname = NULL;}
86                 #else
87                 /* Linux */
88                 int length = (int)strlen(longpathname) + 1;
89                 ShortName = (char*)MALLOC((length) * sizeof(char));
90                 if (ShortName) strcpy(ShortName, longpathname);
91                 *convertok = FALSE;
92                 #endif
93         }
94         else
95         {
96                 /* FAILED */
97                 *convertok = FALSE;
98         }
99         return ShortName;
100 }
101 /*--------------------------------------------------------------------------*/
102 wchar_t* getshortpathnameW(wchar_t* _pwstLongPathName, BOOL* _pbOK)
103 {
104     wchar_t* pwstOutput = NULL;
105     if(_pwstLongPathName)
106     {
107 #ifdef _MSC_VER
108         int iLen = GetShortPathNameW(_pwstLongPathName, NULL, 0);
109
110         if(iLen <= 0)
111         {
112             iLen = MAX_PATH_SHORT;
113         }
114
115         pwstOutput = (wchar_t*)MALLOC((iLen + 1) * sizeof(wchar_t));
116
117         if(pwstOutput) 
118         {
119             /* second converts path */
120             if(GetShortPathNameW(_pwstLongPathName, pwstOutput, iLen))
121             {
122                 *_pbOK = TRUE;
123             }
124             else
125             {
126                 FREE(_pwstLongPathName);
127                 pwstOutput = os_wcsdup(_pwstLongPathName);
128                 *_pbOK = FALSE;
129             }
130         }
131         else
132         {
133             /* FAILED */
134             pwstOutput = os_wcsdup(_pwstLongPathName);
135             *_pbOK = FALSE;
136         }
137 #else
138                 /* Linux and MacOS*/
139         pwstOutput = os_wcsdup(_pwstLongPathName);
140                 *_pbOK = FALSE;
141 #endif
142     }
143     else
144     {
145         *_pbOK = FALSE;
146     }
147     return pwstOutput;
148 }
149 /*--------------------------------------------------------------------------*/