gui
[lbook_fbreader.git] / fbreader / src / formats / util / MiscUtil.cpp
blobf9ec755bbcc293b863fa09431433a0f369e1e13b
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <stdlib.h>
22 #include <ZLApplication.h>
23 #include <ZLFile.h>
24 #include <ZLStringUtil.h>
26 #include "MiscUtil.h"
28 bool MiscUtil::isReference(const std::string &text) {
29 return
30 ZLStringUtil::stringStartsWith(text, "http://") ||
31 ZLStringUtil::stringStartsWith(text, "https://") ||
32 ZLStringUtil::stringStartsWith(text, "mailto:") ||
33 ZLStringUtil::stringStartsWith(text, "ftp://");
36 std::string MiscUtil::htmlDirectoryPrefix(const std::string &fileName) {
37 ZLFile file(fileName);
38 std::string shortName = file.name(false);
39 std::string path = file.path();
40 int index = -1;
41 if ((path.length() > shortName.length()) &&
42 (path[path.length() - shortName.length() - 1] == ':')) {
43 index = shortName.rfind('/');
45 return path.substr(0, path.length() - shortName.length() + index + 1);
48 std::string MiscUtil::htmlFileName(const std::string &fileName) {
49 ZLFile file(fileName);
50 std::string shortName = file.name(false);
51 std::string path = file.path();
52 int index = -1;
53 if ((path.length() > shortName.length()) &&
54 (path[path.length() - shortName.length() - 1] == ':')) {
55 index = shortName.rfind('/');
57 return path.substr(path.length() - shortName.length() + index + 1);
60 std::string MiscUtil::decodeHtmlURL(const std::string &encoded) {
61 char buffer[3];
62 buffer[2] = '\0';
64 std::string decoded;
65 const int len = encoded.length();
66 decoded.reserve(len);
67 for (int i = 0; i < len; i++) {
68 if ((encoded[i] == '%') && (i < len - 2)) {
69 buffer[0] = *(encoded.data() + i + 1);
70 buffer[1] = *(encoded.data() + i + 2);
71 decoded += (char)strtol(buffer, 0, 16);
72 i += 2;
73 } else {
74 decoded += encoded[i];
77 return decoded;