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
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] == '/')) {
30 const int index
= reference
.rfind("::");
32 return reference
.substr(index
+ 2);
36 while (reference
.substr(counter
* 3, 3) == "../") {
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()) {
54 std::string fullRef
= doConvert
? fullReference(myPrefix
, reference
) : MiscUtil::decodeHtmlURL(reference
);
56 const int index
= fullRef
.find('#');
58 fullRef
= ZLUnicodeUtil::toLower(fullRef
);
60 fullRef
= ZLUnicodeUtil::toLower(fullRef
.substr(0, index
));
62 std::set
<std::string
>::const_iterator it
= myReferences
.find(fullRef
);
63 if (it
!= myReferences
.end()) {
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()) {
80 const std::string front
= myReferenceQueue
.front();
81 myReferenceQueue
.pop();
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 {