示例#1
0
/*
#include <ctime>
#include <iostream>

struct FunctionTimer
{
	FunctionTimer(const char *f) : funcName(f), start(clock()) {
		std::cerr << "Entered " << funcName << std::endl;
		std::cerr.flush();
	}
	~FunctionTimer() {
		std::cerr << funcName << " finished at "
		          << (double(clock() - start) * 1000. / CLOCKS_PER_SEC) << " ms"
		          << std::endl;
		std::cerr.flush();
	}

	const char *funcName;
	clock_t start;
};
*/
void Library::collectBookFileNames(std::set<std::string> &bookFileNames, std::vector<shared_ptr<ZLInputStream> > &inputStreamCache) const {
	AppLog("Library::collectBookFileNames()");
//	FunctionTimer timer(__PRETTY_FUNCTION__);
	std::set<std::string> dirs;
	AppLog("collectDirNames");
	collectDirNames(dirs);

	while (!dirs.empty()) {
		std::string dirname = *dirs.begin();
		dirs.erase(dirs.begin());
		AppLog("dirname %s",dirname.c_str());
		ZLFile dirfile(dirname);
		std::vector<std::string> files;
		bool inZip = false;
		AppLog("dirfile.directory");
		shared_ptr<ZLDir> dir = dirfile.directory();
		if (dir.isNull()) {
			AppLog("dir.isNull");
			continue;
		}
		
		if (!dirfile.isDirectory())
			inputStreamCache.push_back(dirfile.inputStream());

		if (dirfile.extension() == "zip") {
			ZLFile phys(dirfile.physicalFilePath());
			if (!BooksDBUtil::checkInfo(phys)) {
				BooksDBUtil::resetZipInfo(phys);
				BooksDBUtil::saveInfo(phys);
			}
			BooksDBUtil::listZipEntries(dirfile, files);
			inZip = true;
		} else {
			dir->collectFiles(files, true);
		}
		//std::cerr.precision(5);
		if (!files.empty()) {
			const bool collectBookWithoutMetaInfo = CollectAllBooksOption.value();
			for (std::vector<std::string>::const_iterator jt = files.begin(); jt != files.end(); ++jt) {
				const std::string fileName = (inZip) ? (*jt) : (dir->itemPath(*jt));
				AppLog("fileName %s",fileName.c_str());
				ZLFile file(fileName);
//				std::cerr << "Check file \"" << fileName << "\" ... ";
//				std::cerr.flush();
//				clock_t start = clock();
				if (PluginCollection::Instance().plugin(file, !collectBookWithoutMetaInfo) != 0) {
					bookFileNames.insert(fileName);
				// TODO: zip -> any archive
				} else if (file.extension() == "zip") {
					if (myScanSubdirs || !inZip) {
						AppLog("myScanSubdirs fileName %s",fileName.c_str());
						dirs.insert(fileName);
					}
				}
//				std::cerr << (double(clock() - start) * 1000. / CLOCKS_PER_SEC) << " ms" << std::endl;
//				std::cerr.flush();
			}
		}
	}
}
示例#2
0
void Library::collectBookFileNames(std::set<std::string> &bookFileNames) const {
	std::set<std::string> dirs;
	collectDirNames(dirs);

	while (!dirs.empty()) {
		std::string dirname = *dirs.begin();
		dirs.erase(dirs.begin());
		
		ZLFile dirfile(dirname);
		std::vector<std::string> files;
		bool inZip = false;

		shared_ptr<ZLDir> dir = dirfile.directory();
		if (dir.isNull()) {
			continue;
		}

		if (dirfile.extension() == "zip") {
			ZLFile phys(dirfile.physicalFilePath());
			if (!BooksDBUtil::checkInfo(phys)) {
				BooksDBUtil::resetZipInfo(phys);
				BooksDBUtil::saveInfo(phys);
			}
			BooksDBUtil::listZipEntries(dirfile, files);
			inZip = true;
		} else {
			dir->collectFiles(files, true);
		}
		if (!files.empty()) {
			const bool collectBookWithoutMetaInfo = CollectAllBooksOption.value();
			for (std::vector<std::string>::const_iterator jt = files.begin(); jt != files.end(); ++jt) {
				const std::string fileName = (inZip) ? (*jt) : (dir->itemPath(*jt));
				ZLFile file(fileName);
				if (PluginCollection::Instance().plugin(file, !collectBookWithoutMetaInfo) != 0) {
					bookFileNames.insert(fileName);
				// TODO: zip -> any archive
				} else if (file.extension() == "zip") {
					if (myScanSubdirs || !inZip) {
						dirs.insert(fileName);
					}
				}
			}
		}
	}
}
void BookCollection::rebuild() {
  myPath = PathOption.value();
  myScanSubdirs = ScanSubdirsOption.value();

  myAuthors.clear();
  myCollection.clear();
  myExternalBooks.clear();

  std::set<std::string> fileNamesSet;

  std::set<std::string> dirs;
  collectDirNames(dirs);

  for (std::set<std::string>::iterator it = dirs.begin(); it != dirs.end(); ++it) {
    std::vector<std::string> files;
    shared_ptr<ZLDir> dir = ZLFile(*it).directory();
    if (dir.isNull()) {
      continue;
    }
    dir->collectFiles(files, false);
    if (!files.empty()) {
      const std::string dirName = dir->name() + '/';
      for (std::vector<std::string>::const_iterator jt = files.begin(); jt != files.end(); ++jt) {
        const std::string fileName = dirName + *jt;
        ZLFile file(fileName);
        if (PluginCollection::instance().plugin(file, true) != 0) {
          fileNamesSet.insert(fileName);
        } else if (file.extension() == "zip") {
          if (!BookDescriptionUtil::checkInfo(file)) {
            BookDescriptionUtil::resetZipInfo(file);
            BookDescriptionUtil::saveInfo(file);
          }
          std::vector<std::string> zipEntries;
          BookDescriptionUtil::listZipEntries(file, zipEntries);
          for (std::vector<std::string>::const_iterator zit = zipEntries.begin(); zit != zipEntries.end(); ++zit) {
            fileNamesSet.insert(*zit);
          }
        }
      }
    }
  }

  for (std::set<std::string>::iterator it = fileNamesSet.begin(); it != fileNamesSet.end(); ++it) {
    addDescription(BookDescription::create(*it));
  }

  BookList bookList;
  const std::set<std::string> &bookListSet = bookList.fileNames();
  for (std::set<std::string>::const_iterator it = bookListSet.begin(); it != bookListSet.end(); ++it) {
    if (fileNamesSet.find(*it) == fileNamesSet.end()) {
      BookDescriptionPtr description = BookDescription::create(*it);
      addDescription(description);
      myExternalBooks.insert(description);
    }
  }

  std::sort(myAuthors.begin(), myAuthors.end(), AuthorComparator());
  DescriptionComparator descriptionComparator;
  for (std::map<AuthorPtr,Books>::iterator it = myCollection.begin(); it != myCollection.end(); ++it) {
    std::sort((*it).second.begin(), (*it).second.end(), descriptionComparator);
  }
}