3
0
Fork 0

contentManager lib and C wrapper and tester

small_fixes
reg_ 14 years ago
parent 1424821ff5
commit 2656535d42

@ -0,0 +1,215 @@
/*
* Copyright 2011 Renaud Gaudin <reg@kiwix.org>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "ContentManagerWrapper.h"
#include "contentManager.h"
/* creates instance of ContentManager */
HCONTCLASS ContentManager_Create( void ) {
ContentManager * contentManager = new ContentManager(0);
// Return its pointer (opaque)
return (HCONTCLASS)contentManager;
}
/* Delete instance of ZimAccessor */
void ContentManager_Destroy( HCONTCLASS h ) {
assert(h != NULL);
// Convert from handle to ContentManager pointer
ContentManager * contentManager = (ContentManager *)h;
delete contentManager;
}
int ContentManager_OpenLibraryFromFile( HCONTCLASS h, char* path, int readOnly) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
return contentManager->OpenLibraryFromFile( string(path), readOnly );
}
int ContentManager_OpenLibraryFromText( HCONTCLASS h, char* xml, int readOnly) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string xml_str = string(xml);
return contentManager->OpenLibraryFromText( xml_str, readOnly );
}
int ContentManager_WriteLibrary( HCONTCLASS h) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
return contentManager->WriteLibrary();
}
int ContentManager_WriteLibraryToFile( HCONTCLASS h, char* path) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string path_str = string(path);
return contentManager->WriteLibraryToFile( path_str );
}
int ContentManager_AddBookFromPath( HCONTCLASS h, char* path) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string path_str = string(path);
return contentManager->AddBookFromPath( path_str );
}
int ContentManager_RemoveBookById( HCONTCLASS h, char* id) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string id_str = string(id);
return contentManager->RemoveBookById( id_str );
}
int ContentManager_SetCurrentBookId( HCONTCLASS h, char* id) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string id_str = string(id);
return contentManager->SetCurrentBookId( id_str );
}
const char* ContentManager_GetCurrentBookId( HCONTCLASS h ) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string id_str = contentManager->GetCurrentBookId();
return id_str.c_str();
}
CMBook ContentManager_GetBookById( HCONTCLASS h, char* id ) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
CMBook book;
string path, title, indexPath, indexType, description, articleCount, mediaCount, size, creator, date, language, favicon, url;
string id_str = string(id);
contentManager->GetBookById(id_str, path, title, indexPath, indexType, description, articleCount, mediaCount, size, creator, date, language, favicon, url);
book.path = path.c_str();
book.title = title.c_str();
book.indexPath = indexPath.c_str();
book.indexType = indexType.c_str();
book.description = description.c_str();
book.articleCount = articleCount.c_str();
book.mediaCount = mediaCount.c_str();
book.size = size.c_str();
book.creator = creator.c_str();
book.date = date.c_str();
book.language = language.c_str();
book.favicon = favicon.c_str();
book.url = url.c_str();
return book;
}
int ContentManager_UpdateBookLastOpenDateById( HCONTCLASS h, char* id) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string id_str = string(id);
return contentManager->UpdateBookLastOpenDateById( id_str );
}
unsigned int ContentManager_GetBookCount( HCONTCLASS h, int remote, int local) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
return contentManager->GetBookCount( remote, local );
}
const char* ContentManager_GetListNextBookId( HCONTCLASS h) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
return contentManager->GetListNextBookId();
}
int ContentManager_SetBookIndex( HCONTCLASS h, char* id, char* path, char* type) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string id_str = string(id);
string path_str = string(path);
string type_str = string(type);
return contentManager->SetBookIndex( id_str, path_str, type_str );
}
int ContentManager_SetBookPath( HCONTCLASS h, char* id, char* path) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string id_str = string(id);
string path_str = string(path);
return contentManager->SetBookPath( id_str, path_str );
}
const char* ContentManager_GetBooksLanguages( HCONTCLASS h ) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string languages = contentManager->GetBooksLanguages();
return languages.c_str();
}
const char* ContentManager_GetBooksPublishers( HCONTCLASS h ) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string publishers = contentManager->GetBooksPublishers();
return publishers.c_str();
}
int ContentManager_ListBooks( HCONTCLASS h, char* mode, char* sortBy, unsigned int maxSize, char* language, char* publisher, char* search ) {
assert(h != NULL);
ContentManager * contentManager = (ContentManager *)h;
string mode_str = string(mode);
string sort_str = string(sortBy);
string lang_str = string(language);
string pub_str = string(publisher);
string search_str = string(search);
return contentManager->ListBooks( mode_str, sort_str, maxSize, lang_str, pub_str, search_str );
}

@ -0,0 +1,52 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <ctype.h>
typedef void * HCONTCLASS;
typedef struct {
const char* path;
const char* title;
const char* indexPath;
const char* indexType;
const char* description;
const char* articleCount;
const char* mediaCount;
const char* size;
const char* creator;
const char* date;
const char* language;
const char* favicon;
const char* url;
} CMBook;
HCONTCLASS ContentManager_Create( void );
void ContentManager_Destroy( HCONTCLASS h );
int ContentManager_OpenLibraryFromFile( HCONTCLASS h, char* path, int readOnly );
int ContentManager_OpenLibraryFromText( HCONTCLASS h, char* xml, int readOnly );
int ContentManager_WriteLibrary( HCONTCLASS h );
int ContentManager_WriteLibraryToFile( HCONTCLASS h, char* path );
int ContentManager_AddBookFromPath( HCONTCLASS h, char* path );
int ContentManager_RemoveBookById( HCONTCLASS h, char* id );
int ContentManager_SetCurrentBookId( HCONTCLASS h, char* id );
const char* ContentManager_GetCurrentBookId( HCONTCLASS h );
CMBook ContentManager_GetBookById( HCONTCLASS h, char* id );
int ContentManager_UpdateBookLastOpenDateById( HCONTCLASS h, char* id );
unsigned int ContentManager_GetBookCount(HCONTCLASS h, int remote, int local );
const char* ContentManager_GetListNextBookId( HCONTCLASS h );
int ContentManager_SetBookIndex( HCONTCLASS h, char* id, char* path, char* type );
int ContentManager_SetBookPath( HCONTCLASS h, char* id, char* path );
const char* ContentManager_GetBooksLanguages( HCONTCLASS h );
const char* ContentManager_GetBooksPublishers( HCONTCLASS h );
int ContentManager_ListBooks( HCONTCLASS h, char* mode, char* sortBy, unsigned int maxSize, char* language, char* publisher, char* search );
#ifdef __cplusplus
};
#endif

@ -2,14 +2,13 @@
all: contentTester
wrapperlib:
g++ -shared -o libContentCManager.so ContentManagerWrapper.cpp contentManager.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/
g++ -shared ContentManagerWrapper.cpp -o libContentCManager.so contentManager.cpp ../common/kiwix/library.cpp ../common/kiwix/manager.cpp ../common/kiwix/reader.cpp ../common/base64.cpp ../common/pathTools.cpp ../common/componentTools.cpp ../common/regexTools.cpp ../common/kiwix/library.h ../common/kiwix/manager.h ../common/kiwix/reader.h ../pugixml/pugixml.hpp ../common/base64.h ../common/pathTools.h ../common/componentTools.h ../common/regexTools.h -I . -I ../common/ -I ../pugixml/ -I ../zimlib/include/ -I /usr/include/ -L ../zimlib/src/.libs/ -lzim ../pugixml/.libs/libpugixml.a ../dependences/xz/src/liblzma/.libs/liblzma.a
contentTester:
g++ contentManagerTester.cpp contentManager.cpp ../common/kiwix/library.cpp ../common/kiwix/manager.cpp ../common/kiwix/reader.cpp ../common/base64.cpp ../common/pathTools.cpp ../common/componentTools.cpp ../common/regexTools.cpp ../common/kiwix/library.h ../common/kiwix/manager.h ../common/kiwix/reader.h ../pugixml/pugixml.hpp ../common/base64.h ../common/pathTools.h ../common/componentTools.h ../common/regexTools.h -o contentTester -I ../common/ -I ../pugixml/ -I ../zimlib/include/ -I /usr/include/ -L ../zimlib/src/.libs/ -lzim ../pugixml/.libs/libpugixml.a ../dependences/xz/src/liblzma/.libs/liblzma.a
contentCTester:
ln -fs ../zimAccessor/libZimCAccessor.so .
gcc -o zimCTester zimCTester.c -I ../zimAccessor/ -L ../zimAccessor/ -lZimCAccessor
contentCTester: wrapperlib
gcc -o contentCTester contentCTester.c -I . -L . -lContentCManager
cleanc:
find . -name 'contentCTester' -delete

@ -0,0 +1,137 @@
/*
* Copyright 2011 Renaud Gaudin <reg@kiwix.org>
*
* 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 <stdio.h>
#include <stdlib.h>
#include "ContentManagerWrapper.h"
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("Content Manager C Tester.\nLoading %s...\n", zimPath);
HCONTCLASS contM = NULL;
contM = ContentManager_Create();
if (contM == NULL) {
printf("Error creating ContentManager class\n");
exit(1);
}
int readOnly = 1;
if (ContentManager_OpenLibraryFromFile( contM, zimPath, readOnly ))
printf("Successfully opened library from %s\n", zimPath);
else
printf("Unable to open library from %s\n", zimPath);
char * xmltext = "<library current='dummy'><book id='dummy' path='blabla.zim' indexPath='blabla.idx' indexType='xapian'/></library>";
if (ContentManager_OpenLibraryFromText( contM, xmltext, readOnly ))
printf("Successfully opened library from text.\n");
else
printf("Unable to open library from text.\n");
if (ContentManager_WriteLibrary( contM ))
printf("Successfully wrote library.\n");
else
printf("Unable to write library\n");
char* libPath = "/tmp/totoc";
if (ContentManager_WriteLibraryToFile( contM, libPath ))
printf("Successfully wrote library to %s\n", libPath);
else
printf("Unable to write library to %s\n", libPath);
if (ContentManager_AddBookFromPath( contM, zimPath ))
printf("Successfully added book from %s\n", zimPath);
else
printf("Unable to add book from %s\n", zimPath);
char* bookId = "57b0b7c3-25a5-431e-431e-dce1771ee052963f";
if (ContentManager_SetCurrentBookId( contM, bookId ))
printf("Successfully set book %s\n", bookId);
else
printf("Unable to set book %s\n", bookId);
const char* currBookId = ContentManager_GetCurrentBookId( contM );
printf("Current Book: %s\n", currBookId);
if (ContentManager_UpdateBookLastOpenDateById( contM, bookId ))
printf("Successfully updated last open date for book %s\n", bookId);
else
printf("Unable to update last open date for book %s\n", bookId);
unsigned int bookCount = 0;
bookCount = ContentManager_GetBookCount( contM, 1, 1 );
printf("Book count: %d\n", bookCount);
char* indexPath = "/home/reg/wksw.index";
char* indexType = "xapian";
if (ContentManager_SetBookIndex( contM, bookId, indexPath, indexType ))
printf("Successfully added index for book %s\n", bookId);
else
printf("Unable to add index for book %s\n", bookId);
if (ContentManager_SetBookPath( contM, bookId, zimPath ))
printf("Successfully set path for book %s\n", bookId);
else
printf("Unable to set path for book %s\n", bookId);
const char* langs;
langs = ContentManager_GetBooksLanguages( contM );
printf("Languages: %s\n", langs);
const char* pubs;
langs = ContentManager_GetBooksPublishers( contM );
printf("Publishers: %s\n", langs);
char* mode = "lastOpen";
char* lsortBy = "size";
unsigned int lmaxSize = 0;
char* llanguage = "";
char* lpublisher = "";
char* lsearch = "";
if (ContentManager_ListBooks( contM, mode, lsortBy, lmaxSize, llanguage, lpublisher, lsearch ))
printf("Successfully listed books\n");
else
printf("Unable to list books\n");
const char* nextBookId;
nextBookId = ContentManager_GetListNextBookId( contM );
printf("Next book id: %s\n", nextBookId);
CMBook book = ContentManager_GetBookById( contM, bookId );
printf("Book: %s\n\tarticles: %s\n\tlangs: %s\n", book.title, book.articleCount, book.language);
if (ContentManager_RemoveBookById( contM, bookId ))
printf("Successfully removed book %s\n", bookId);
else
printf("Unable to remove book %s\n", bookId);
ContentManager_WriteLibraryToFile( contM, libPath );
ContentManager_Destroy( contM );
contM = NULL;
return 0;
}
Loading…
Cancel
Save