Merge remote-tracking branch 'origin/master' into windows
[scilab.git] / scilab / modules / fileio / src / c / getFullFilename.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - 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 <string.h>
17 #include "getFullFilename.h"
18 #include "charEncoding.h"
19 #include "sci_malloc.h"
20 #include "splitpath.h"
21 #include "PATH_MAX.h"
22 #include "scicurdir.h"
23 #include "isdir.h"
24 #include "fullpath.h"
25 /*--------------------------------------------------------------------------*/
26 char *getFullFilename(const char* Filename)
27 {
28     char *pStFullFilename = NULL;
29
30     pStFullFilename = (char *)MALLOC(sizeof(char) * (PATH_MAX * 2));
31     if (pStFullFilename)
32     {
33         int i = 0;
34         int lenPath = 0;
35
36         char Drv[PATH_MAX * 2];
37         char Dir[PATH_MAX * 2];
38         char Name[PATH_MAX * 2];
39         char Ext[PATH_MAX * 2];
40
41         char *NameExt = (char *)MALLOC(sizeof(char) * (PATH_MAX * 2));
42         char *Path = (char *)MALLOC(sizeof(char) * (PATH_MAX * 2));
43         char *Tmp = NULL;
44
45         if (NameExt == NULL || Path == NULL)
46         {
47             FREE(pStFullFilename);
48             if (NameExt)
49             {
50                 FREE(NameExt);
51             }
52             if (Path)
53             {
54                 FREE(Path);
55             }
56             return NULL;
57         }
58         splitpath(Filename, TRUE, Drv, Dir, Name, Ext);
59
60         strcpy(NameExt, Name);
61         strcat(NameExt, Ext);
62
63         strcpy(Path, Drv);
64         strcat(Path, Dir);
65
66         if (strcmp(Path, "") == 0)
67         {
68             int ierr = 0;
69             char *CurrentDir = scigetcwd(&ierr);
70             if (ierr == 0)
71             {
72                 strcpy(Path, CurrentDir);
73             }
74             if (CurrentDir)
75             {
76                 FREE(CurrentDir);
77                 CurrentDir = NULL;
78             }
79         }
80
81         Tmp = (char*)MALLOC(sizeof(char) * (PATH_MAX * 2));
82         if (Tmp)
83         {
84             get_full_path(Tmp, (const char*)Path, PATH_MAX * 2);
85             strcpy(Path, Tmp);
86             FREE(Tmp);
87             Tmp = NULL;
88         }
89
90         lenPath = (int)strlen(Path);
91         if (lenPath - 1 >= 0)
92         {
93             if ( (Path[lenPath - 1 ] != '/') && (Path[lenPath - 1 ] != '\\') )
94             {
95                 strcat(Path, "/");
96                 lenPath = (int)strlen(Path);
97             }
98         }
99
100         for ( i = 0; i < lenPath; i++)
101         {
102 #ifdef _MSC_VER
103             if (Path[i] == '/')
104             {
105                 Path[i] = '\\';
106             }
107 #else
108             if (Path[i] == '\\')
109             {
110                 Path[i] = '/';
111             }
112 #endif
113         }
114
115         strcpy(pStFullFilename, Path);
116         strcat(pStFullFilename, NameExt);
117
118         FREE(NameExt);
119         FREE(Path);
120     }
121     return pStFullFilename;
122
123 }
124 /*--------------------------------------------------------------------------*/