Difference between revisions of "Libzim"

Jump to navigation Jump to search
1,696 bytes added ,  09:12, 13 November 2016
(18 intermediate revisions by 7 users not shown)
Line 1: Line 1:
ZIMlib is the library which implements the access to ZIM files. Use libzim in your own software - like reader applications - to make them ZIM-capable without the need having to dig too much into the ZIM file format.
The '''zimlib''' is the standard implementation of the [[ZIM file format|ZIM specification]]. It is a library which implements the read and write method for ZIM files. The zimlib is released under the [https://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2 license terms]. Use the zimlib in your own software - like reader applications - to make them ZIM-capable without the need having to dig too much into the ZIM file format.
 
Have a look to the [[Releases]] or [[git]] pages to get more information about how to download it.


== Programming ==
== Programming ==


=== Introduction ===
=== Introduction ===  
zimlib is written in C++. To use the library, you need the include files of zimlib have to link against libzim. Both are installed when zimlib is built with the normal "./configure; make; make install".
zimlib is written in C++. To use the library, you need the include files of zimlib have to link against zimlib. Both are installed when zimlib is built (<tt>./autogen.sh; ./configure; make; make install</tt>).


Errors are handled with exceptions. When something goes wrong, zimlib throws an error, which is always derived from std::exception.
Errors are handled with exceptions. When something goes wrong, zimlib throws an error, which is always derived from std::exception.
Line 21: Line 23:
#include <zim/fileiterator.h>
#include <zim/fileiterator.h>
#include <iostream>
#include <iostream>
int main(int argc, char* argv[])  
int main(int argc, char* argv[])  
{
{
Line 42: Line 43:


You may save that file under the name "zimlist.cpp" and compile using the command:
You may save that file under the name "zimlist.cpp" and compile using the command:
'''g++ -o zimlist -lzim zimlist.cpp'''. You get a program, which lists the urls and titles of the file named ''wikipedia.zim''. Of course it is better to pass that name as a parameter in argc/argv. But this should be an easy task for you, so I do not show that.
'''<tt>g++ -o zimlist -lzim zimlist.cpp</tt>'''. You get a program, which lists the urls and titles of the file named ''wikipedia.zim''. Of course it is better to pass that name as a parameter in argc/argv. But this should be an easy task for you, so I do not show that.


In subsequent examples I show only the code needed to use the library. The main-function with the error catcher should always be in place.
In subsequent examples I show only the code needed to use the library. The main-function with the error catcher should always be in place.
Line 50: Line 51:
Articles are addressed either by index or by namespace and url or title. The index is normally not that useful. So let us look how to find a specific article.
Articles are addressed either by index or by namespace and url or title. The index is normally not that useful. So let us look how to find a specific article.


''zim::File'' has methods ''find'' and ''findByTitle''. Both take 2 parameters. A char for the namespace (which is normally 'A' for articles) and a string, which specifies the url (in find) or the title (in findByTitle). It returns a const_iterator pointing to the lexicographically next article. Be aware, that the returned iterator may point to end(), so you should check that, before dereferencing the iterator.
''zim::File'' has methods ''find'' and ''findByTitle''. Both take 2 parameters. A char for the namespace (which is normally 'A' for articles) and a string, which specifies the url (in find) or the title (in findByTitle). It returns a const_iterator pointing to the lexicographically next article. Be aware, that the returned iterator may point to end(), so you should check that, before dereferencing the iterator. I should add, that both - the url and title - are case sensitive.


;Sample: find a article by title and print the content:
;Sample: find a article by title and print the content:
Line 99: Line 100:


The simpler searches articles by title either by a praefix or by a range of titles. You pass a namespace and either a single ''std::string'' and you get all articles, whose title starts with that string or you pass two strings and you get all articles whose title is between these strings (including the passed strings).
The simpler searches articles by title either by a praefix or by a range of titles. You pass a namespace and either a single ''std::string'' and you get all articles, whose title starts with that string or you pass two strings and you get all articles whose title is between these strings (including the passed strings).
The method zim::Search::search implements a full text search. For that it needs the full text index file. The full text index file is also a zim file, so you pass the index file as second paramter of type zim::File. The search-method is then called with the SearchResults-reference and a string, containing a space delimited list of words. The full text search then returns a list of articles where at least one of the words is found. Each article is weighted and the result is sorted by weight starting with the highest weighted article.
== Methods ==
=== zim::file::const_iterator = zim::file::findx(char ns, const std::string& title) ===
returns an iterator to the article named ''title'' in namespace ''ns''
=== zim::file::const_iterator = zim::file::findx(const std::string& url) ===
returns an iterator to the article with the (non-encoded) URL ''url''
=== std::string = zim::urldecode(const std::string& url) ===
returns an URL-decoded string for use in findx()
== Requirements ==
If you compile the software from source you need the following libraries installed:
* libxz-dev
* autoconf
* automake
* libtool
== See also ==
* [[Releases]]
* [[Bindings]]
* [[Readers]]
* [[zimwriterdb]]
* [[zimdump]]
* [[Git|Get the source code]]
* in [https://tracker.debian.org/pkg/zimlib Debian]

Navigation menu