Initial commit. FBReader 0.8.12
[lbook_fbreader.git] / fbreader / src / formats / chm / CHMReferenceCollection.cpp
blob1352c7456ea9ded746c0a9131eb88ef13fe6fcc4
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 <ZLUnicodeUtil.h>
22 #include "CHMReferenceCollection.h"
23 #include "../util/MiscUtil.h"
25 std::string CHMReferenceCollection::fullReference(const std::string &prefix, std::string reference) {
26 reference = MiscUtil::decodeHtmlURL(reference);
27 if ((reference.length() > 0) && (reference[0] == '/')) {
28 return reference;
30 const int index = reference.rfind("::");
31 if (index != -1) {
32 return reference.substr(index + 2);
35 int counter = 0;
36 while (reference.substr(counter * 3, 3) == "../") {
37 ++counter;
40 int slashIndex = prefix.length() - 1;
41 for (int i = 0; (i < counter) && (slashIndex > 0); ++i) {
42 slashIndex = prefix.rfind('/', slashIndex - 1);
44 return prefix.substr(0, slashIndex + 1) + reference.substr(counter * 3);
47 CHMReferenceCollection::CHMReferenceCollection() : myPrefix("/") {
50 const std::string &CHMReferenceCollection::addReference(const std::string &reference, bool doConvert) {
51 if (reference.empty()) {
52 return reference;
54 std::string fullRef = doConvert ? fullReference(myPrefix, reference) : MiscUtil::decodeHtmlURL(reference);
56 const int index = fullRef.find('#');
57 if (index == -1) {
58 fullRef = ZLUnicodeUtil::toLower(fullRef);
59 } else {
60 fullRef = ZLUnicodeUtil::toLower(fullRef.substr(0, index));
62 std::set<std::string>::const_iterator it = myReferences.find(fullRef);
63 if (it != myReferences.end()) {
64 return *it;
67 myReferences.insert(fullRef);
68 myReferenceQueue.push(fullRef);
69 return myReferenceQueue.back();
72 bool CHMReferenceCollection::containsNonProcessedReferences() const {
73 return !myReferenceQueue.empty();
76 const std::string CHMReferenceCollection::nextReference() {
77 if (myReferenceQueue.empty()) {
78 return "";
80 const std::string front = myReferenceQueue.front();
81 myReferenceQueue.pop();
82 return front;
85 void CHMReferenceCollection::setPrefix(const std::string &fileName) {
86 myPrefix = MiscUtil::decodeHtmlURL(fileName.substr(0, fileName.rfind('/') + 1));
89 const std::string &CHMReferenceCollection::prefix() const {
90 return myPrefix;