diff --git a/src/zimAccessor/Makefile b/src/zimAccessor/Makefile new file mode 100644 index 0000000..37b14b3 --- /dev/null +++ b/src/zimAccessor/Makefile @@ -0,0 +1,9 @@ + +all: wrapperlib + +wrapperlib: + g++ -shared -o libZimCAccessor.so ZimAccessorWrapper.cpp zimAccessor.cpp ../common/kiwix/reader.cpp -I . -I ../zimAccessor/ -I ../common/ -I ../zimlib/include/ -L ../zimlib/src/.libs/ -lzim ../dependences/xz/src/liblzma/.libs/liblzma.a -I /usr/include/ + +clean: + find . -name 'libZimCAccessor.so' -delete + find . -name '*.o' -delete diff --git a/src/zimAccessor/ZimAccessorWrapper.cpp b/src/zimAccessor/ZimAccessorWrapper.cpp new file mode 100644 index 0000000..afdb434 --- /dev/null +++ b/src/zimAccessor/ZimAccessorWrapper.cpp @@ -0,0 +1,78 @@ +/* + * Copyright 2011 Renaud Gaudin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include +#include "ZimAccessorWrapper.h" +#include "zimAccessor.h" + +/* creates instance of ZimAccessor */ +HZIMCLASS ZimAccessor_Create( void ) { + ZimAccessor * zimAccessor = new ZimAccessor(0); + + // Return its pointer (opaque) + return (HZIMCLASS)zimAccessor; +} + +/* Delete instance of ZimAccessor */ +void ZimAccessor_Destroy( HZIMCLASS h ) { + assert(h != NULL); + + // Convert from handle to ZimAccessor pointer + ZimAccessor * zimAccessor = (ZimAccessor *)h; + + delete zimAccessor; +} + +int ZimAccessor_LoadFile( HZIMCLASS h, char* zimPath) { + assert(h != NULL); + + ZimAccessor * zimAccessor = (ZimAccessor *)h; + + if (zimAccessor->LoadFile( zimPath )) + return true; + else + return false; +} + +/* Return the welcome page URL */ +const char* ZimAccessor_GetMainPageUrl( HZIMCLASS h ) { + assert(h != NULL); + + ZimAccessor * zimAccessor = (ZimAccessor *)h; + + string hp = ""; + zimAccessor->GetMainPageUrl( hp ); + return hp.c_str(); +} + +/* Get a content from a zim file */ +const char* ZimAccessor_GetContent(HZIMCLASS h, char* url) { + assert(h != NULL); + + ZimAccessor * zimAccessor = (ZimAccessor *)h; + + string cpurl = string(url); + string content = ""; + unsigned int contentLength = 0; + string contentType = ""; + zimAccessor->GetContent( cpurl, content, contentLength, contentType ); + return content.c_str(); +} diff --git a/src/zimAccessor/ZimAccessorWrapper.h b/src/zimAccessor/ZimAccessorWrapper.h new file mode 100644 index 0000000..d3cecae --- /dev/null +++ b/src/zimAccessor/ZimAccessorWrapper.h @@ -0,0 +1,22 @@ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef void * HZIMCLASS; + +HZIMCLASS ZimAccessor_Create( void ); + +void ZimAccessor_Destroy( HZIMCLASS h ); + +int ZimAccessor_LoadFile( HZIMCLASS h, char* zimPath ); +const char* ZimAccessor_GetMainPageUrl( HZIMCLASS h ); +const char* ZimAccessor_GetContent( HZIMCLASS h, char* url); + +#ifdef __cplusplus +}; +#endif diff --git a/src/zimAccessor/zimAccessor.cpp b/src/zimAccessor/zimAccessor.cpp new file mode 100644 index 0000000..52c1172 --- /dev/null +++ b/src/zimAccessor/zimAccessor.cpp @@ -0,0 +1,226 @@ +/* + * Copyright 2011 Renaud Gaudin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include "zimAccessor.h" + +ZimAccessor::ZimAccessor(int x) : reader(NULL) {} + +bool ZimAccessor::LoadFile(string path) { + try { + this->reader = new kiwix::Reader(path); + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + return true; +} + +/* Reset the cursor for GetNextArticle() */ +bool ZimAccessor::Reset() { + try { + this->reader->reset(); + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + return true; +} + +/* Get the count of articles which can be indexed/displayed */ +bool ZimAccessor::GetArticleCount(unsigned int &count) { + try { + if (this->reader != NULL) { + count = this->reader->getArticleCount(); + return true; + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + return false; +} + +/* Return the UID of the ZIM file */ +bool ZimAccessor::GetId(string &id) { + try { + if (this->reader != NULL) { + id = this->reader->getId().c_str(); + return true; + } + } catch (exception &e) { + cerr << e.what() << endl; + } + + return false; +} + +/* Return a page url fronm title */ +bool ZimAccessor::GetPageUrlFromTitle(const string &title, string &url) { + string urlstr; + //const char *ctitle; + //NS_CStringGetData(title, &ctitle); + + try { + if (this->reader != NULL) { + if (this->reader->getPageUrlFromTitle(title.c_str(), urlstr)) { + url = urlstr.c_str(); + return true; + } + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + return false; +} + +/* Return the welcome page URL */ +bool ZimAccessor::GetMainPageUrl(string &url) { + try { + if (this->reader != NULL) { + string urlstr = this->reader->getMainPageUrl(); + + if (urlstr.empty()) { + urlstr = this->reader->getFirstPageUrl(); + } + + url = urlstr.c_str(); + return true; + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return false; +} + +/* Get a metatag value */ +bool ZimAccessor::GetMetatag(const string &name, string &value) { + string valueStr; + + try { + if (this->reader != NULL) { + if (this->reader->getMetatag(name.c_str(), valueStr)) { + value = valueStr.data(); + return true; + } + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return false; +} + +/* Get a content from a zim file */ +bool ZimAccessor::GetContent(string url, string &content, unsigned int &contentLength, string &contentType) { + + /* strings */ + string contentStr; + string contentTypeStr; + unsigned int contentLengthInt; + + /* default value */ + content = ""; + contentLength = 0; + + try { + if (this->reader != NULL) { + if (this->reader->getContentByUrl(url, contentStr, contentLength, contentTypeStr)) { + contentType = contentTypeStr.data(); + content = contentStr.data(); + return true; + } + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return true; +} + +/* Search titles by prefix*/ +bool ZimAccessor::SearchSuggestions(const string &prefix, unsigned int suggestionsCount) { + try { + if (this->reader != NULL) { + if (this->reader->searchSuggestions(prefix.c_str(), suggestionsCount)) { + return true; + } + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return false; +} + +/* Get next suggestion */ +bool ZimAccessor::GetNextSuggestion(string &title) { + string titleStr; + + try { + if (this->reader != NULL) { + if (this->reader->getNextSuggestion(titleStr)) { + title = titleStr.c_str(); + return true; + } + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return false; +} + +/* Can I check the integrity - for old ZIM file without checksum */ +bool ZimAccessor::CanCheckIntegrity() { + try { + if (this->reader != NULL) { + return this->reader->canCheckIntegrity(); + } + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return false; +} + +/* Return true if corrupted, false otherwise */ +bool ZimAccessor::IsCorrupted() { + try { + if (this->reader != NULL) { + return this->reader->isCorrupted(); + } + } catch (exception &e) { + cerr << e.what() << endl; + return true; + } + + return true; +} diff --git a/src/zimAccessor/zimAccessor.h b/src/zimAccessor/zimAccessor.h new file mode 100644 index 0000000..aea211a --- /dev/null +++ b/src/zimAccessor/zimAccessor.h @@ -0,0 +1,27 @@ + +#include +#include +#include +#include + +class ZimAccessor +{ + public: + ZimAccessor(int); + bool LoadFile(string); + bool GetArticleCount(unsigned int&); + bool Reset(); + bool GetId(string&); + bool GetPageUrlFromTitle(const string &, string &); + bool GetMainPageUrl(string &); + bool GetMetatag(const string &, string &); + // WARNING: previous API expected an nsIURI instead of string + bool GetContent(string, string &, unsigned int &, string &); + bool SearchSuggestions(const string &, unsigned int); + bool GetNextSuggestion(string &); + bool CanCheckIntegrity(); + bool IsCorrupted(); + + public: + kiwix::Reader *reader; +}; diff --git a/src/zimTester/Makefile b/src/zimTester/Makefile new file mode 100644 index 0000000..cc3b922 --- /dev/null +++ b/src/zimTester/Makefile @@ -0,0 +1,20 @@ + +all: zimCTester zimTester + +zimTester: + g++ zimTester.cpp ../common/kiwix/reader.cpp ../zimAccessor/zimAccessor.cpp -o zimTester -I . -I ../zimAccessor/ -I ../common/ -I ../zimlib/include/ -L ../zimlib/src/.libs/ -lzim ../dependences/xz/src/liblzma/.libs/liblzma.a -I /usr/include/ + +zimCTester: + ln -fs ../zimAccessor/libZimCAccessor.so . + gcc -o zimCTester zimCTester.c -I ../zimAccessor/ -L ../zimAccessor/ -lZimCAccessor + +cleanc: + find . -name 'zimCTester' -delete + +cleancpp: + find . -name 'zimTester' -delete + +clean: cleanc cleancpp + find . -name 'libZimCAccessor.so' -delete + find . -name '*.o' -delete + diff --git a/src/zimTester/zimCTester.c b/src/zimTester/zimCTester.c new file mode 100644 index 0000000..ab8097e --- /dev/null +++ b/src/zimTester/zimCTester.c @@ -0,0 +1,57 @@ +/* + * Copyright 2011 Renaud Gaudin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include + + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + printf("Usage: %s zimPath\n", argv[0]); + printf("No ZIM file path provided.\n"); + exit(0); + } + char* zimPath = argv[1]; + printf("Kiwix C Tester.\nLoading %s...\n", zimPath); + + HZIMCLASS zimA = NULL; + zimA = ZimAccessor_Create(); + if (zimA == NULL) { + printf("Error creating ZimAccessor class\n"); + exit(1); + } + ZimAccessor_LoadFile( zimA, zimPath ); + printf("Done.\n"); + const char* hp; + hp = ZimAccessor_GetMainPageUrl( zimA ); + printf("homepage: %s\n", hp); + + char * url = "A/Lugha.html"; + const char* content; + content = ZimAccessor_GetContent( zimA, url ); + + printf("%s", content); + + ZimAccessor_Destroy( zimA ); + zimA = NULL; + + return 0; +} diff --git a/src/zimTester/zimTester.cpp b/src/zimTester/zimTester.cpp new file mode 100644 index 0000000..7b547ca --- /dev/null +++ b/src/zimTester/zimTester.cpp @@ -0,0 +1,96 @@ +/* + * Copyright 2011 Renaud Gaudin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include + + +int main(int argc, char* argv[]) +{ + ZimAccessor zim_acc(0); + ZimAccessor* zima = new ZimAccessor(0); + + if (argc < 2) { + cout << "Usage: " << argv[0] << " zimPath" << endl << endl; + cout << "No ZIM file path provided." << endl; + exit(0); + } + string zimPath = (string) argv[1]; + cout << "Kiwix Tester." << endl << "Loading " << zimPath << "..." << endl; + + if (zima->LoadFile(zimPath)) + cout << "Successfully loaded ZIM file." << endl; + else + cout << "Unable to load ZIM file." << endl; + + unsigned int count = 0; + if (zima->GetArticleCount(count)) { + printf("ZIM file contains %d articles.\n", count); + } else { + cout << "Unable to count articles in ZIM file." << endl; + } + + zima->Reset(); + + string id; + zima->GetId(id); + cout << "Current ID: " << id << endl; + + string title = "Lugha"; + string url; + zima->GetPageUrlFromTitle(title, url); + + cout << "URL for " << title << ": " << url << endl; + + string homepage; + zima->GetMainPageUrl(homepage); + cout << "Homepage is: " << homepage << endl; + + // Untested as it's not used in the code and don't know behavior + //zima->GetMetatag(const string &name, string &value); + + string content; + unsigned int contentLength; + string contentType; + string req = "A/Lugha.html"; + zima->GetContent(req, content, contentLength, contentType); + cout << content << endl; + cout << contentType << endl; + cout << contentLength << endl; + + // Untested as it's not used in the code and don't know behavior + const string prefix = "Lug"; + unsigned int sugg_count = 1000; + zima->SearchSuggestions(prefix, sugg_count); + cout << "Suggestions for " << prefix << ": " << sugg_count << endl; + + string next_sugg; + zima->GetNextSuggestion(next_sugg); + cout << "Next Suggestion: " << next_sugg << endl; + + cout << "Can check integrity ? " << zima->CanCheckIntegrity() << endl; + + // commented as it takes a while + //cout << "Is corrupted ? " << zima->IsCorrupted() << endl; + + delete zima; + + return 0; +}