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
21 #include <ZLOptions.h>
22 #include <ZLStringUtil.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
) {
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();
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
;
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();
78 ZLIntegerOption(FBCategoryKey::BOOKS
, zipFile
.path(), ENTRIES_NUMBER
, -1).setValue(counter
);