2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2019-2011 - Stéphane MOTTELET
6 * This file is hereby licensed under the terms of the GNU GPL v2.0,
7 * pursuant to article 5.3.4 of the CeCILL v.2.1.
8 * This file was originally licensed under the terms of the CeCILL v2.1,
9 * and continues to be available under such terms.
10 * For more information, see the COPYING file which you should have received
11 * along with this program.
14 /*--------------------------------------------------------------------------*/
20 #include "charEncoding.h"
22 #include "sci_malloc.h"
23 #include "os_string.h"
26 char *get_full_path(const char *_Path)
29 char *_FullPath = NULL;
30 wchar_t *wFullPath = NULL;
31 wchar_t *wPath = to_wide_string((char *)_Path);
35 wFullPath = get_full_pathW(wPath);
39 _FullPath = wide_string_to_UTF8(wFullPath);
45 std::filesystem::path relPath = std::filesystem::path(_Path);
46 std::filesystem::path canonPath = std::filesystem::weakly_canonical(relPath);
47 auto relPathIt = relPath.end();
48 auto canonPathIt = canonPath.end();
49 if ((--relPathIt)->string().empty() && !(--canonPathIt)->string().empty())
54 return os_strdup(std::filesystem::absolute(canonPath).string().c_str());
58 /*--------------------------------------------------------------------------*/
59 wchar_t *get_full_pathW(const wchar_t * _wcPath)
62 std::wstring s(_wcPath);
63 size_t pos = s.find_last_of(L"/\\");
67 if (pos != std::wstring::npos && s.substr(pos + 1) != L"..")
73 std::filesystem::path relPath = std::filesystem::path(s);
74 std::filesystem::path canonPath = std::filesystem::weakly_canonical(relPath);
75 auto relPathIt = relPath.end();
76 auto canonPathIt = canonPath.end();
77 if ((--relPathIt)->wstring().empty() && !(--canonPathIt)->wstring().empty())
82 std::filesystem::path p = std::filesystem::absolute(canonPath);
87 p /= (_wcPath + pos + 1);
90 return os_wcsdup(p.wstring().c_str());
92 char *_FullPath = NULL;
93 wchar_t *wFullPath = NULL;
94 char *_Path = wide_string_to_UTF8(_wcPath);
98 _FullPath = get_full_path(_Path);
102 wFullPath = to_wide_string(_FullPath);