uses enum
[scilab.git] / scilab / modules / output_stream / src / cpp / getFullFilename.cpp
1 /*--------------------------------------------------------------------------*/ 
2 /*
3 * ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) DIGITEO - 2009 - Allan CORNET
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 "getFullFilename.hxx"
15 /*--------------------------------------------------------------------------*/ 
16 #ifndef _MSC_VER
17 #include <unistd.h>
18 #endif
19 extern "C"
20 {
21 #include "machine.h"
22 #include "PATH_MAX.h"
23 #include "MALLOC.h"
24 #include "charEncoding.h"
25 }
26 /*--------------------------------------------------------------------------*/ 
27 static void wcsplitpath(const wchar_t* path, wchar_t* drv, wchar_t* dir, wchar_t* name, wchar_t* ext);
28 /*--------------------------------------------------------------------------*/ 
29 std::wstring getFullFilename(std::wstring _wfilename)
30 {
31         wchar_t wcdrive[PATH_MAX];
32         wchar_t wcdirectory[PATH_MAX];
33         wchar_t wcname[PATH_MAX];
34         wchar_t wcext [PATH_MAX];
35
36         std::wstring wfullfilename(L"");
37         std::wstring tmpWstr;
38
39         size_t found = _wfilename.rfind(L"\\");
40
41         while (found != std::wstring::npos)
42         {
43                 _wfilename.replace (found, 1, L"/");
44                 found = _wfilename.rfind(L"\\");
45         }
46         wcsplitpath(_wfilename.c_str(), wcdrive, wcdirectory, wcname, wcext);
47         wfullfilename.append(tmpWstr.assign(wcdrive));
48         wfullfilename.append(tmpWstr.assign(wcdirectory));
49         if (wfullfilename.compare(L"") == 0)
50         {
51                 /* to get current directory as wide characters */
52 #if _MSC_VER
53                 wchar_t wcCurrentDir[PATH_MAX];
54                 if ( _wgetcwd(wcCurrentDir, PATH_MAX) != NULL)
55                 {
56                         wfullfilename = tmpWstr.assign(wcCurrentDir);
57 #else
58                 char CurrentDir[PATH_MAX];
59                 if (getcwd(CurrentDir, PATH_MAX) != NULL)
60                 {
61                         wchar_t *wcCurrentDir = to_wide_string(CurrentDir);
62                         wfullfilename = tmpWstr.assign(wcCurrentDir);
63                         FREE(wcCurrentDir);
64 #endif
65                         /* replaces separator */
66                         size_t found = wfullfilename.rfind(L"\\");
67                         while (found != std::wstring::npos)
68                         {
69                                 wfullfilename.replace (found, 1, L"/");
70                                 found = wfullfilename.rfind(L"\\");
71                         }
72                         wfullfilename.append(L"/");
73                 }
74                 else
75                 {
76                         wfullfilename.assign(L"");
77                 }
78         }
79         wfullfilename.append(tmpWstr.assign(wcname));
80         wfullfilename.append(tmpWstr.assign(wcext));
81
82         return wfullfilename;
83 }
84 /*--------------------------------------------------------------------------*/ 
85 static void wcsplitpath(const wchar_t* path, wchar_t* drv, wchar_t* dir, wchar_t* name, wchar_t* ext)
86 {
87         const wchar_t* end; /* end of processed string */
88         const wchar_t* p;   /* search pointer */
89         const wchar_t* s;   /* copy pointer */
90
91         /* extract drive name */
92         if (path[0] && path[1]==':') 
93         {
94                 if (drv) 
95                 {
96                         *drv++ = *path++;
97                         *drv++ = *path++;
98                         *drv = L'\0';
99                 }
100         } else if (drv) *drv = L'\0';
101
102         /* search for end of string or stream separator */
103         for(end=path; *end && *end!=L':'; )     end++;
104
105         /* search for begin of file extension */
106         for(p=end; p>path && *--p!=L'\\' && *p!=L'/'; )
107         {
108                 if (*p == L'.') 
109                 {
110                         end = p;
111                         break;
112                 }
113         }
114
115         if (ext) for(s=end; (*ext=*s++); ) ext++;
116
117         /* search for end of directory name */
118         for(p=end; p>path; )
119         {
120                 if (*--p=='\\' || *p=='/') 
121                 {
122                         p++;
123                         break;
124                 }
125         }
126         if (name) 
127         {
128                 for(s=p; s<end; ) *name++ = *s++;
129                 *name = L'\0';
130         }
131
132         if (dir) 
133         {
134                 for(s=path; s<p; ) *dir++ = *s++;
135                 *dir = L'\0';
136         }
137 }
138 /*--------------------------------------------------------------------------*/