Merge pull request #1727 from akruphi/master
[far2l.git] / colorer / src / pcolorer2 / tools.cpp
blobc4a88f5a2a95ef5ba520a9f0f2d5e8f6043e0d15
1 #include "tools.h"
2 #include <string>
3 #include <utils.h>
5 wchar_t *rtrim(wchar_t* str)
7 wchar_t *ptr = str;
8 str += wcslen(str);
10 while (iswspace(*(--str))) *str = 0;
12 return ptr;
15 wchar_t *ltrim(wchar_t* str)
17 while (iswspace(*(str++)));
19 return str - 1;
22 wchar_t *trim(wchar_t* str)
24 return ltrim(rtrim(str));
27 /**
28 Function converts a path in the UNC path.
29 Source path can be framed by quotes, be a relative, or contain environment variables
31 wchar_t *PathToFull(const wchar_t *path, bool unc)
33 std::wstring new_path(path);
34 if (new_path.empty()) {
35 return nullptr;
37 // we remove quotes, if they are present, focusing on the first character
38 // if he quote it away and the first and last character.
39 // If the first character quote, but the latter does not - well, it's not our
40 // problem, and so and so error
41 if (new_path.size() > 1 && new_path.front() == L'"' && new_path.back() == '"') {
42 new_path = new_path.substr(1, new_path.size() - 2);
45 // replace the environment variables to their values
46 std::string new_path_mb;
47 StrWide2MB(new_path, new_path_mb);
48 Environment::ExpandString(new_path_mb, false);
49 StrMB2Wide(new_path_mb, new_path);
51 // take the full path to the file, converting all kinds of ../ ./ etc
52 size_t unconverted_len = new_path.size();
53 size_t p = FSF.ConvertPath(CPM_FULL, &new_path[0], nullptr, 0);
54 if (p > unconverted_len) {
55 new_path.resize(p);
56 std::fill(new_path.begin() + unconverted_len, new_path.end(), 0);
58 p = FSF.ConvertPath(CPM_FULL, &new_path[0], &new_path[0], static_cast<int>(new_path.size()));
59 ASSERT(p <= new_path.size());
60 //new_path.resize(p);
62 wchar_t *out = new wchar_t[p];
63 wmemcpy(out, &new_path[0], p);
65 if (unc) {
66 fprintf(stderr, "Wanna UNC for '%ls'\n", out);
69 return out;
72 SString *PathToFullS(const wchar_t *path, bool unc)
74 SString *spath=nullptr;
75 wchar_t *t=PathToFull(path,unc);
76 if (t){
77 spath=new StringBuffer(t);
79 delete[] t;
80 return spath;
83 /* ***** BEGIN LICENSE BLOCK *****
84 * Copyright (C) 1999-2009 Cail Lomecb <irusskih at gmail dot com>.
85 * This program is free software; you can redistribute it and/or
86 * modify it under the terms of the GNU General Public License
87 * as published by the Free Software Foundation; either version 2
88 * of the License, or (at your option) any later version.
89 * This program is distributed in the hope that it will be useful,
90 * but WITHOUT ANY WARRANTY; without even the implied warranty of
91 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
92 * GNU General Public License for more details.
93 * You should have received a copy of the GNU General Public License
94 * along with this program; If not, see <http://www.gnu.org/licenses/>.
95 * ***** END LICENSE BLOCK ***** */