forked from dattaz/libzim_wasm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibzim_bindings.cpp
131 lines (114 loc) · 3.44 KB
/
libzim_bindings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <zim/archive.h>
#include <zim/item.h>
#include <zim/error.h>
#include <zim/search.h>
#include <iostream>
#include <chrono>
#include <emscripten/bind.h>
#include <emscripten/emscripten.h>
#include <emscripten/val.h>
using namespace emscripten;
std::shared_ptr<zim::Archive> g_archive;
int main(int argc, char* argv[])
{
std::cout << "assembler initialized" << std::endl;
return 0;
}
void loadArchive(std::string filename) {
g_archive.reset(new zim::Archive(filename));
std::cout << "archive loaded" << std::endl;
}
// Get article count of a ZIM file
unsigned int getArticleCount() {
return g_archive->getArticleCount();
}
class BlobWrapper{
public:
BlobWrapper(zim::Blob blob):
m_blob(blob)
{ }
val getContent() const {
return val(typed_memory_view(m_blob.size(), m_blob.data()));
}
private:
zim::Blob m_blob;
};
class ItemWrapper{
public:
ItemWrapper(zim::Item item):
m_item(item)
{ }
BlobWrapper getData() const {
return BlobWrapper(m_item.getData());
}
std::string getMimetype() const { return m_item.getMimetype(); }
private:
zim::Item m_item;
};
class EntryWrapper{
public:
EntryWrapper(zim::Entry entry):
m_entry(entry)
{ }
ItemWrapper getItem(bool follow) {
return ItemWrapper(m_entry.getItem(follow));
}
std::string getPath() {
return m_entry.getPath();
}
bool isRedirect() {
return m_entry.isRedirect();
}
EntryWrapper getRedirectEntry() {
return EntryWrapper(m_entry.getRedirectEntry());
}
private:
zim::Entry m_entry;
};
// Get an entry by its path
std::unique_ptr<EntryWrapper> getEntryByPath(std::string url) {
try {
zim::Entry entry = g_archive->getEntryByPath(url);
return std::unique_ptr<EntryWrapper>(new EntryWrapper(entry));
} catch(zim::EntryNotFound& e) {
std::cout << "entry " << url << " not found" << std::endl;
return nullptr;
} catch(std::exception& e) {
std::cout << "other exception : " << e.what() << std::endl;
return nullptr;
}
}
// Search for a text, and returns the path of the first result
std::vector<EntryWrapper> search(std::string text, int numResults) {
auto searcher = zim::Searcher(*g_archive);
auto query = zim::Query(text);
auto search = searcher.search(query);
auto searchResultSet = search.getResults(0, numResults);
std::vector<EntryWrapper> ret;
for(auto entry:searchResultSet) {
ret.push_back(EntryWrapper(entry));
}
return ret;
}
// Binding code
EMSCRIPTEN_BINDINGS(libzim_module) {
emscripten::function("loadArchive", &loadArchive);
emscripten::function("getEntryByPath", &getEntryByPath);
emscripten::function("getArticleCount", &getArticleCount);
emscripten::function("search", &search);
emscripten::register_vector<char>("vector<char>");
emscripten::register_vector<EntryWrapper>("vector(EntryWrapper)");
class_<EntryWrapper>("EntryWrapper")
.function("getItem", &EntryWrapper::getItem)
.function("getPath", &EntryWrapper::getPath)
.function("isRedirect", &EntryWrapper::isRedirect)
.function("getRedirectEntry", &EntryWrapper::getRedirectEntry)
;
class_<ItemWrapper>("ItemWrapper")
.function("getData", &ItemWrapper::getData)
.function("getMimetype", &ItemWrapper::getMimetype)
;
class_<BlobWrapper>("BlobWrapper")
.function("getContent", &BlobWrapper::getContent)
;
}