Merge remote-tracking branch 'origin/master' into windows
[scilab.git] / scilab / modules / fileio / src / c / pathconvert.c
1 /*
2 * ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) DIGITEO - 2009-2010 - 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 <ctype.h>
17 #include <string.h>
18 #include "pathconvert.h"
19 #include "sci_malloc.h"
20 #include "expandPathVariable.h"
21 #include "splitpath.h"
22 #include "charEncoding.h"
23 #include "BOOL.h"
24 /*--------------------------------------------------------------------------*/
25 #define CYGWINSTART "/cygdrive/"
26 /*--------------------------------------------------------------------------*/
27 static char* cygwintowindowspath(const char* cygwinpath, BOOL *bConverted);
28 static char* windowstocygwinpath(const char* windowspath, BOOL *bConverted);
29 /*--------------------------------------------------------------------------*/
30 char *pathconvert(const char* path, BOOL flagtrail, BOOL flagexpand, PathConvertType PType)
31 {
32     char* convertedPath = NULL;
33     if (path)
34     {
35         BOOL bConvCyg = FALSE;
36         char* expandedPath = NULL;
37         PathConvertType PTypelocal = PType;
38         int i = 0;
39
40         if (PType == AUTO_STYLE)
41         {
42 #ifdef _MSC_VER
43             PTypelocal = WINDOWS_STYLE;
44 #else
45             PTypelocal = UNIX_STYLE;
46 #endif
47         }
48
49         if (flagexpand)
50         {
51             expandedPath = expandPathVariable(path);
52         }
53         else
54         {
55             expandedPath = (char*)MALLOC(sizeof(char) * ((int)strlen(path) + 1));
56             strcpy(expandedPath, path);
57         }
58
59         if (PTypelocal == WINDOWS_STYLE)
60         {
61             convertedPath = cygwintowindowspath(expandedPath, &bConvCyg);
62         }
63         else
64         {
65             convertedPath = windowstocygwinpath(expandedPath, &bConvCyg);
66         }
67
68         FREE(expandedPath);
69
70         if (convertedPath)
71         {
72             if (flagtrail)
73             {
74                 int currentLen = (int)strlen(convertedPath);
75                 if ( (convertedPath[currentLen - 1] != '/') && (convertedPath[currentLen - 1] != '\\') )
76                 {
77                     convertedPath = (char*)REALLOC(convertedPath, (currentLen + 2) * sizeof(char));
78                     if (PTypelocal == WINDOWS_STYLE)
79                     {
80                         strcat(convertedPath, "\\");
81                     }
82                     else
83                     {
84                         strcat(convertedPath, "/");
85                     }
86                 }
87             }
88             else
89             {
90                 int currentLen = (int) strlen(convertedPath);
91                 if ( (convertedPath[currentLen - 1] == '/') || (convertedPath[currentLen - 1] == '\\') )
92                 {
93                     convertedPath[currentLen - 1] = '\0';
94                 }
95             }
96
97             for (i = 0; i < (int)strlen(convertedPath); i++)
98             {
99                 if (PTypelocal == WINDOWS_STYLE)
100                 {
101                     if (convertedPath[i] == '/')
102                     {
103                         convertedPath[i] = '\\';
104                     }
105                 }
106                 else
107                 {
108                     if (convertedPath[i] == '\\')
109                     {
110                         convertedPath[i] = '/';
111                     }
112                 }
113             }
114         }
115     }
116     return convertedPath;
117 }
118 /*--------------------------------------------------------------------------*/
119 static char* cygwintowindowspath(const char* cygwinpath, BOOL *bConverted)
120 {
121     char* windowspath = NULL;
122     *bConverted = FALSE;
123     if (cygwinpath)
124     {
125         int lenBegin = (int)strlen(CYGWINSTART);
126         int lenPath = (int)strlen(cygwinpath);
127
128         windowspath = (char*)MALLOC(sizeof(char) * (lenPath + 1));
129
130         if ( strncmp(cygwinpath, CYGWINSTART, lenBegin) == 0)
131         {
132             if (lenBegin == lenPath)
133             {
134                 if (windowspath)
135                 {
136                     strcpy(windowspath, cygwinpath);
137                     *bConverted = FALSE;
138                 }
139             }
140             else
141             {
142                 if ( (lenPath > lenBegin) && isalpha(cygwinpath[lenBegin]) )
143                 {
144                     if ( (lenPath >= lenBegin + 1) &&
145                             ((cygwinpath[lenBegin + 1] == '/') || (cygwinpath[lenBegin + 1] == '\\')) )
146                     {
147                         windowspath[0] = cygwinpath[lenBegin];
148                         windowspath[1] = ':';
149                         windowspath[2] = '\0';
150
151                         if (lenPath > lenBegin + 1)
152                         {
153                             strcat(windowspath, &cygwinpath[lenBegin + 1]);
154                         }
155                         *bConverted     = TRUE;
156                     }
157                     else
158                     {
159                         if (windowspath)
160                         {
161                             strcpy(windowspath, cygwinpath);
162                         }
163                         *bConverted     = FALSE;
164                     }
165                 }
166                 else
167                 {
168                     if (windowspath)
169                     {
170                         strcpy(windowspath, cygwinpath);
171                     }
172                     *bConverted = FALSE;
173                 }
174             }
175         }
176         else
177         {
178             if (windowspath)
179             {
180                 strcpy(windowspath, cygwinpath);
181             }
182             *bConverted = FALSE;
183         }
184     }
185     return windowspath;
186 }
187 /*--------------------------------------------------------------------------*/
188 static char* windowstocygwinpath(const char* windowspath, BOOL *bConverted)
189 {
190     char* cygwinpath = NULL;
191     *bConverted = FALSE;
192
193     if (windowspath)
194     {
195         char *drv = (char*)MALLOC(sizeof(char) * ((int)strlen(windowspath) + 1));
196         char* dir = (char*)MALLOC(sizeof(char) * ((int)strlen(windowspath) + 1));
197         char* name = (char*)MALLOC(sizeof(char) * ((int)strlen(windowspath) + 1));
198         char* ext = (char*)MALLOC(sizeof(char) * ((int)strlen(windowspath) + 1));
199
200         splitpath(windowspath, FALSE, drv, dir, name, ext);
201         if (strcmp(drv, "") != 0)
202         {
203             int len = (int)strlen(CYGWINSTART) + (int)strlen(drv) +
204                       (int)strlen(dir) + (int)strlen(name) +
205                       (int)strlen(ext) + 3;
206
207             cygwinpath = (char*)MALLOC(sizeof(char) * len);
208             if (cygwinpath)
209             {
210                 strcpy(cygwinpath, CYGWINSTART);
211                 strcat(cygwinpath, drv);
212                 len = (int)strlen(cygwinpath);
213                 if (cygwinpath[len - 1] == ':')
214                 {
215                     cygwinpath[len - 1] = '\0';
216                 }
217                 if (strcmp(dir, "") != 0)
218                 {
219                     strcat(cygwinpath, dir);
220
221                     if (strcmp(name, "") != 0)
222                     {
223                         strcat(cygwinpath, name);
224                         if (strcmp(ext, "") != 0)
225                         {
226                             strcat(cygwinpath, ext);
227                         }
228                     }
229                 }
230             }
231             *bConverted = TRUE;
232         }
233         else
234         {
235             cygwinpath = (char*)MALLOC(sizeof(char) * ((int)strlen(windowspath) + 1));
236             if (cygwinpath)
237             {
238                 strcpy(cygwinpath, windowspath);
239             }
240         }
241
242         FREE(drv);
243         FREE(dir);
244         FREE(name);
245         FREE(ext);
246     }
247     return cygwinpath;
248 }
249 /*--------------------------------------------------------------------------*/