summaryrefslogtreecommitdiff
path: root/src/utility/string.cpp
blob: f82e6be7a5af389930f9c955f18a2039043b06d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***************************************************************************
 *   Copyright (C) 2008-2017 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include <cassert>
#include <cwctype>
#include <algorithm>
#include "utility/string.h"

std::string getBasename(const std::string &path)
{
	size_t slash = path.rfind("/");
	if (slash == std::string::npos)
		return path;
	else
		return path.substr(slash+1);
}

std::string getParentDirectory(std::string path)
{
	size_t slash = path.rfind('/');
	if (slash == std::string::npos)
		path = "";
	else
		path.resize(slash);
	return path;
}

std::string getSharedDirectory(const std::string &dir1, const std::string &dir2)
{
	size_t i = 0;
	size_t min_len = std::min(dir1.length(), dir2.length());
	while (i < min_len && !dir1.compare(i, 1, dir2, i, 1))
		++i;
	i = dir1.rfind("/", i);
	if (i == std::string::npos)
		return "/";
	else
		return dir1.substr(0, i);
}

std::string getEnclosedString(const std::string &s, char a, char b, size_t *pos)
{
	std::string result;
	size_t i = s.find(a, pos ? *pos : 0);
	if (i != std::string::npos)
	{
		++i;
		while (i < s.length() && s[i] != b)
		{
			if (s[i] == '\\' && i+1 < s.length() && (s[i+1] == '\\' || s[i+1] == b))
				result += s[++i];
			else
				result += s[i];
			++i;
		}
		// we want to set pos to char after b if possible
		if (i < s.length())
			++i;
		else // we reached end of string and didn't encounter closing char
			result.clear();
	}
	if (pos != 0)
		*pos = i;
	return result;
}

void removeInvalidCharsFromFilename(std::string &filename, bool win32_compatible)
{
	const char *unallowed_chars = win32_compatible ? "\"*/:<>?\\|" : "/";
	for (const char *c = unallowed_chars; *c; ++c)
	{
		for (size_t i = 0; i < filename.length(); ++i)
		{
			if (filename[i] == *c)
			{
				filename.erase(filename.begin()+i);
				--i;
			}
		}
	}
}

void escapeSingleQuotes(std::string &filename)
{
	for (size_t i = 0; i < filename.length(); ++i)
	{
		if (filename[i] == '\'')
		{
			filename.replace(i, 1, "'\\''");
			i += 3;
		}
	}
}