MLBookProc 1.1
 
Loading...
Searching...
No Matches
AuxFunc.h
1/*
2 * Copyright (C) 2024-2025 Yury Bobylev <bobilev_yury@mail.ru>
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation, version 3.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17#ifndef AUXFUNC_H
18#define AUXFUNC_H
19
20#include <Genre.h>
21#include <GenreGroup.h>
22#include <MLException.h>
23#include <filesystem>
24#include <gcrypt.h>
25#include <libdjvu/ddjvuapi.h>
26#include <memory>
27#include <random>
28#include <string>
29#include <tuple>
30#include <vector>
31
32#ifdef USE_OPENMP
33#include <omp.h>
34#endif
35
61
71class AuxFunc
72{
73public:
77 virtual ~AuxFunc();
78
89 std::string
90 to_utf_8(const std::string &input, const char *conv_name);
91
97 std::string
98 utf8_to_system(const std::string &input);
99
109 std::string
110 utf_8_to(const std::string &input, const char *conv_name);
111
119 const char *
120 get_converter_by_number(const int32_t &num);
121
128 std::string
129 detect_encoding(const std::string &buf);
130
135 std::filesystem::path
137
142 std::filesystem::path
144
149 std::filesystem::path
151
160 std::filesystem::path
162
171 std::vector<GenreGroup>
173
184 std::string
185 libgcrypt_error_handling(const gcry_error_t &err);
186
195 std::string
196 to_hex(const std::string &source);
197
203 std::string
204 stringToLower(const std::string &line);
205
210 std::string
212
219 std::string
220 time_t_to_date(const time_t &tt);
221
230 bool
231 if_supported_type(const std::filesystem::path &ch_p);
232
240 void
241 html_to_utf8(std::string &input);
242
247 void
248 open_book_callback(const std::filesystem::path &path);
249
260 void
261 copy_book_callback(const std::filesystem::path &source,
262 const std::filesystem::path &out);
263
269 std::vector<std::string>
271
277 std::vector<std::string>
279
285 std::vector<std::string>
287
293 std::string
294 get_extension(const std::filesystem::path &p);
295
305 int32_t
307
314 bool
316
317#ifdef USE_OPENMP
331 template <class InputIt,
332 class T = typename std::iterator_traits<InputIt>::value_type>
333 static InputIt
334 parallelFind(InputIt start, InputIt end, const T &val)
335 {
336 InputIt res = end;
337#pragma omp parallel
338 {
339#pragma omp for
340 for(InputIt i = start; i != end; i++)
341 {
342 if(*i == val)
343 {
344#pragma omp critical
345 {
346 if(i < res)
347 {
348 res = i;
349 }
350 }
351#pragma omp cancel for
352 }
353 }
354 }
355
356 return res;
357 }
358
372 template <class InputIt, class UnaryPred>
373 static InputIt
374 parallelFindIf(InputIt start, InputIt end, UnaryPred predicate)
375 {
376 InputIt res = end;
377#pragma omp parallel
378 {
379#pragma omp for
380 for(InputIt i = start; i != end; i++)
381 {
382 if(predicate(*i))
383 {
384#pragma omp critical
385 {
386 if(i < res)
387 {
388 res = i;
389 }
390 }
391#pragma omp cancel for
392 }
393 }
394 }
395
396 return res;
397 }
398
414 template <class InputIt,
415 class T = typename std::iterator_traits<InputIt>::value_type>
416 static InputIt
417 parallelRemove(InputIt start, InputIt end, const T &val)
418 {
419 start = parallelFind(start, end, val);
420 if(start != end)
421 {
422 for(InputIt i = start + 1; i != end; i++)
423 {
424 if(*i != val)
425 {
426 std::swap(*start, *i);
427 start++;
428 }
429 }
430 }
431 return start;
432 }
433
449 template <class InputIt, class UnaryPred>
450 static InputIt
451 parallelRemoveIf(InputIt start, InputIt end, UnaryPred predicate)
452 {
453 start = parallelFindIf(start, end, predicate);
454 if(start != end)
455 {
456 for(InputIt i = start + 1; i != end; i++)
457 {
458 if(!predicate(*i))
459 {
460 std::swap(*start, *i);
461 start++;
462 }
463 }
464 }
465 return start;
466 }
467#endif
468
477 static std::shared_ptr<AuxFunc>
479
486 std::shared_ptr<ddjvu_context_t>
488
489private:
490 AuxFunc();
491
492 std::vector<std::tuple<std::string, Genre>>
493 read_genres(const bool &wrong_loc, const std::string &locname);
494
495 std::vector<GenreGroup>
496 read_genre_groups(const bool &wrong_loc, const std::string &locname);
497
498 bool
499 handleDJVUmsgs(const std::shared_ptr<ddjvu_context_t> &ctx);
500
501 bool activated = true;
502
503 std::mt19937_64 *rng;
504 std::uniform_int_distribution<uint64_t> *dist;
505
506 std::shared_ptr<ddjvu_context_t> djvu_context;
507};
508
509#endif // AUXFUNC_H
std::vector< std::string > get_supported_archive_types_unpacking()
Same as get_supported_types(), but returns only archives types, available for unpacking.
std::filesystem::path get_selfpath()
Returns absolute path to program executable file.
std::string time_t_to_date(const time_t &tt)
Converts time_t value to calendar date.
std::filesystem::path homePath()
Returns user home directory path.
std::vector< std::string > get_supported_archive_types_packing()
Same as get_supported_types(), but returns only archives types, available for packing.
std::string detect_encoding(const std::string &buf)
Tries to detect string encoding.
std::string utf8_to_system(const std::string &input)
Converts UTF-8 string to string in system default encoding.
bool get_activated()
Checks if depencies have been successfully activated.
std::string to_utf_8(const std::string &input, const char *conv_name)
Converts string to UTF-8 string.
std::string randomFileName()
Returns random string.
std::vector< std::string > get_supported_types()
Returns supported file types.
void open_book_callback(const std::filesystem::path &path)
Opens given file in default system application.
static std::shared_ptr< AuxFunc > create()
Creats AuxFunc object.
void html_to_utf8(std::string &input)
Converst 'html' symbols to UTF-8 characters.
std::string utf_8_to(const std::string &input, const char *conv_name)
Converts UTF-8 string to string in chosen encoding.
virtual ~AuxFunc()
AuxFunc destructor.
const char * get_converter_by_number(const int32_t &num)
Returns converter name.
std::string get_extension(const std::filesystem::path &p)
Returns file extesion.
void copy_book_callback(const std::filesystem::path &source, const std::filesystem::path &out)
Replaces out file by source file.
std::string to_hex(const std::string &source)
Converts given string to hex format.
bool if_supported_type(const std::filesystem::path &ch_p)
Checks if given file is supported by MLBookProc.
std::filesystem::path temp_path()
Returns absolute path to system temporary directory.
std::vector< GenreGroup > get_genre_list()
Returns translated genre groups and genres names.
std::filesystem::path share_path()
Returns absolute path to share directory, used by MLBookProc.
std::string libgcrypt_error_handling(const gcry_error_t &err)
Auxiliary method to reinterpret libgcrypt errors as strings.
int32_t get_charset_conv_quantity()
Returns number of available converters.
std::shared_ptr< ddjvu_context_t > getDJVUContext()
Returns smart pointer to djvu context object.
std::string stringToLower(const std::string &line)
Converts all letters of the string to lowercase letters.