Difference between revisions of "Libzim"

Jump to navigation Jump to search
Line 79: Line 79:
std::string stringdata = std::string(blob.data(), blob.size());
std::string stringdata = std::string(blob.data(), blob.size());
const char* zptr = stringdata.c_str();  // c_str() guarantees, that the pointer points to zero terminated data
const char* zptr = stringdata.c_str();  // c_str() guarantees, that the pointer points to zero terminated data
</source>
''find'' and ''findByTitle'' return the lexicographically next article. If you whan to know, if you really got exactly the article you requested, you may either compare the url (or title) to the parameter or use ''findx'' or ''findxByTitle''. Both return a std::pair, with the first element a bool, which is true, if a exact match was found and false otherwise. The second element is the actual iterator, as returned by ''find'' or ''findByTitle''. The latter are actually only wrappers around the ''findx''-methods throwing away the flag. If the flag is true, the iterator does point to a article. It is not necessary to check against end() any more.
;Sample: find the exact article by url
<source lang=c>
std::pair<bool, zim::File::const_iterator> it = file.findxByTitle('A', "Wikipedia");
if (!it.first)
  throw std::runtime_error("article not found");
if (it->isRedirect())
  std::cout << "see: " << it->getRedirectArticle().getTitle() << std::endl;
else
  std::cout << it->getData();
</source>
</source>