+ Book.getHumanReadableIdFromPath()

This commit is contained in:
kelson42 2012-04-16 19:32:24 +00:00
parent 2e8806e4d7
commit 179ed1917d
6 changed files with 31 additions and 9 deletions

View File

@ -59,6 +59,14 @@ namespace kiwix {
return strcmp(a.language.c_str(), b.language.c_str()) < 0;
}
std::string Book::getHumanReadableIdFromPath() {
std::string id = removeAccents(path);
replaceRegex(id, "", "^.*/");
replaceRegex(id, "", "\\.zim[a-z]*$");
replaceRegex(id, "_", " ");
return id;
}
/* Constructor */
Library::Library():
version(KIWIX_LIBRARY_VERSION) {

View File

@ -27,6 +27,9 @@
#include <vector>
#include <stack>
#include <unaccent.h>
#include <regexTools.h>
#define KIWIX_LIBRARY_VERSION "20110515"
using namespace std;
@ -48,6 +51,7 @@ namespace kiwix {
static bool sortByCreator(const Book &a, const Book &b);
static bool sortByPublisher(const Book &a, const Book &b);
static bool sortByLanguage(const Book &a, const Book &b);
string getHumanReadableIdFromPath();
string id;
string path;

View File

@ -311,20 +311,15 @@ namespace kiwix {
return booksLanguages;
}
vector<string> Manager::getBooksCreators() {
std::vector<string> booksCreators;
vector<string> Manager::getBooksIds() {
std::vector<string> booksIds;
std::vector<kiwix::Book>::iterator itr;
std::map<string, bool> booksCreatorsMap;
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByCreator);
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if (booksCreatorsMap.find(itr->creator) == booksCreatorsMap.end()) {
booksCreatorsMap[itr->creator] = true;
booksCreators.push_back(itr->creator);
}
booksIds.push_back(itr->id);
}
return booksCreators;
return booksIds;
}
vector<string> Manager::getBooksPublishers() {

View File

@ -69,6 +69,7 @@ namespace kiwix {
vector<string> getBooksLanguages();
vector<string> getBooksCreators();
vector<string> getBooksPublishers();
vector<string> getBooksIds();
string writableLibraryPath;

View File

@ -53,6 +53,19 @@ bool matchRegex(const std::string &content, const std::string &regex) {
return matcher->find();
}
void replaceRegex(std::string &content, const std::string &replacement, const std::string &regex) {
ucnv_setDefaultName("UTF-8");
UnicodeString ucontent = UnicodeString(content.c_str());
UnicodeString ureplacement = UnicodeString(replacement.c_str());
RegexMatcher *matcher = buildRegex(regex);
matcher->reset(ucontent);
UErrorCode status = U_ZERO_ERROR;
UnicodeString uresult = matcher->replaceAll(ureplacement, status);
std::string tmp;
uresult.toUTF8String(tmp);
content=tmp;
}
void appendToFirstOccurence(std::string &content, const std::string regex, const std::string &replacement) {
ucnv_setDefaultName("UTF-8");
UnicodeString ucontent = UnicodeString(content.c_str());

View File

@ -26,6 +26,7 @@
#include <map>
bool matchRegex(const std::string &content, const std::string &regex);
void replaceRegex(std::string &content, const std::string &replacement, const std::string &regex);
void appendToFirstOccurence(std::string &content, const std::string regex, const std::string &replacement);
#endif