FBReader 0.8.16
[lbook_fbreader.git] / fbreader / src / description / BookDescriptionUtil.cpp
blobb73e8827d97154f717c64eba4360168e32c4b99f
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 <ZLFile.h>
21 #include <ZLOptions.h>
22 #include <ZLStringUtil.h>
23 #include <ZLDir.h>
25 #include "BookDescriptionUtil.h"
26 #include "BookDescription.h"
27 #include "../formats/FormatPlugin.h"
28 #include "../options/FBOptions.h"
30 static const std::string SIZE = "Size";
31 static const std::string ENTRY = "Entry";
32 static const std::string ENTRIES_NUMBER = "EntriesNumber";
34 bool BookDescriptionUtil::checkInfo(const ZLFile &file) {
35 return
36 (ZLIntegerOption(FBCategoryKey::BOOKS, file.path(), SIZE, -1).value() == (int)file.size());
39 void BookDescriptionUtil::saveInfo(const ZLFile &file) {
40 ZLIntegerOption(FBCategoryKey::BOOKS, file.path(), SIZE, -1).setValue(file.size());
43 void BookDescriptionUtil::listZipEntries(const ZLFile &zipFile, std::vector<std::string> &entries) {
44 int entriesNumber = ZLIntegerOption(FBCategoryKey::BOOKS, zipFile.path(), ENTRIES_NUMBER, -1).value();
45 if (entriesNumber == -1) {
46 resetZipInfo(zipFile.path());
47 entriesNumber = ZLIntegerOption(FBCategoryKey::BOOKS, zipFile.path(), ENTRIES_NUMBER, -1).value();
49 for (int i = 0; i < entriesNumber; ++i) {
50 std::string optionName = ENTRY;
51 ZLStringUtil::appendNumber(optionName, i);
52 std::string entry = ZLStringOption(FBCategoryKey::BOOKS, zipFile.path(), optionName, "").value();
53 if (!entry.empty()) {
54 entries.push_back(entry);
59 void BookDescriptionUtil::resetZipInfo(const ZLFile &zipFile) {
60 ZLOption::clearGroup(zipFile.path());
62 shared_ptr<ZLDir> zipDir = zipFile.directory();
63 if (!zipDir.isNull()) {
64 std::string zipPrefix = zipFile.path() + ':';
65 std::vector<std::string> entries;
66 int counter = 0;
67 zipDir->collectFiles(entries, false);
68 for (std::vector<std::string>::iterator zit = entries.begin(); zit != entries.end(); ++zit) {
69 if (PluginCollection::instance().plugin(ZLFile(*zit), true) != 0) {
70 std::string optionName = ENTRY;
71 ZLStringUtil::appendNumber(optionName, counter);
72 std::string fullName = zipPrefix + *zit;
73 ZLStringOption(FBCategoryKey::BOOKS, zipFile.path(), optionName, "").setValue(fullName);
74 BookInfo(fullName).reset();
75 ++counter;
78 ZLIntegerOption(FBCategoryKey::BOOKS, zipFile.path(), ENTRIES_NUMBER, -1).setValue(counter);
82 void BookDescriptionUtil::removeWhiteSpacesFromTag(std::string &tag) {
83 int index = tag.find('/');
84 if (index == -1) {
85 ZLStringUtil::stripWhiteSpaces(tag);
86 } else {
87 std::string result;
88 int index0 = 0;
89 while (true) {
90 std::string subtag = tag.substr(index0, index - index0);
91 ZLStringUtil::stripWhiteSpaces(subtag);
92 if (!subtag.empty()) {
93 if (!result.empty()) {
94 result += '/';
96 result += subtag;
98 if (index == -1) {
99 break;
101 index0 = index + 1;
102 index = tag.find('/', index0);
104 tag = result;