MLBookProc 1.3
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 <atomic>
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#else
35#include <mutex>
36#endif
37
66
76class AuxFunc
77{
78public:
82 virtual ~AuxFunc();
83
97 __attribute__((deprecated)) std::string
98 to_utf_8(const std::string &input, const char *conv_name);
99
109 __attribute__((deprecated)) std::string
110 utf8_to_system(const std::string &input);
111
125 __attribute__((deprecated)) std::string
126 utf_8_to(const std::string &input, const char *conv_name);
127
135 const char *
136 get_converter_by_number(const int32_t &num);
137
147 __attribute__((deprecated)) std::string
148 detect_encoding(const std::string &buf);
149
154 std::filesystem::path
156
165 __attribute__((deprecated)) std::filesystem::path
167
172 std::filesystem::path
174
183 __attribute__((deprecated)) std::filesystem::path
185
190 std::filesystem::path
192
204 __attribute__((deprecated)) std::filesystem::path
206
215 std::filesystem::path
217
226 std::vector<GenreGroup>
228
239 std::string
240 libgcrypt_error_handling(const gcry_error_t &err);
241
250 std::string
251 to_hex(const std::string &source);
252
258 std::string
259 stringToLower(const std::string &line);
260
265 std::string
267
274 std::string
275 time_t_to_date(const time_t &tt);
276
285 bool
286 if_supported_type(const std::filesystem::path &ch_p);
287
297 bool
298 ifSupportedArchiveUnpackaingType(const std::filesystem::path &ch_p);
299
309 bool
310 ifSupportedArchivePackingType(const std::filesystem::path &ch_p);
311
322 __attribute__((deprecated)) void
323 html_to_utf8(std::string &input);
324
332 void
333 htmlToUtf8(std::string &result);
334
339 void
340 open_book_callback(const std::filesystem::path &path);
341
352 void
353 copy_book_callback(const std::filesystem::path &source,
354 const std::filesystem::path &out);
355
361 std::vector<std::string>
363
369 std::vector<std::string>
371
377 std::vector<std::string>
379
385 std::string
386 get_extension(const std::filesystem::path &p);
387
393 std::string
394 getExtension(const std::string &fnm);
395
405 int32_t
407
414 bool
416
417#ifdef USE_OPENMP
431 template <class InputIt,
432 class T = typename std::iterator_traits<InputIt>::value_type>
433 static InputIt
434 parallelFind(InputIt start, InputIt end, const T &val)
435 {
436 InputIt res = end;
437 const T *val_ptr = &val;
438#pragma omp parallel
439 {
440#pragma omp for
441 for(InputIt i = start; i != end; i++)
442 {
443 if(*i == *val_ptr)
444 {
445#pragma omp critical
446 {
447 if(i < res)
448 {
449 res = i;
450 }
451 }
452#pragma omp cancel for
453 }
454 }
455 }
456
457 return res;
458 }
459
473 template <class InputIt, class UnaryPred>
474 static InputIt
475 parallelFindIf(InputIt start, InputIt end, UnaryPred predicate)
476 {
477 InputIt res = end;
478#pragma omp parallel
479 {
480#pragma omp for
481 for(InputIt i = start; i != end; i++)
482 {
483 if(predicate(*i))
484 {
485#pragma omp critical
486 {
487 if(i < res)
488 {
489 res = i;
490 }
491 }
492#pragma omp cancel for
493 }
494 }
495 }
496
497 return res;
498 }
499
515 template <class InputIt,
516 class T = typename std::iterator_traits<InputIt>::value_type>
517 static InputIt
518 parallelRemove(InputIt start, InputIt end, const T &val)
519 {
520 start = parallelFind(start, end, val);
521 if(start != end)
522 {
523 T *s = start.base();
524 T *e = end.base();
525#pragma omp parallel
526#pragma omp for ordered
527 for(T *i = s + 1; i != e; i++)
528 {
529 if((*i) != val)
530 {
531#pragma omp ordered
532 {
533 *s = std::move((*i));
534 s++;
535 }
536 }
537 }
538 start = InputIt(s);
539 }
540 return start;
541 }
542
558 template <class InputIt, class UnaryPred,
559 class T = typename std::iterator_traits<InputIt>::value_type>
560 static InputIt
561 parallelRemoveIf(InputIt start, InputIt end, UnaryPred predicate)
562 {
563 start = parallelFindIf(start, end, predicate);
564 if(start != end)
565 {
566 T *s = start.base();
567 T *e = end.base();
568#pragma omp parallel
569#pragma omp for ordered
570 for(T *i = s + 1; i != e; i++)
571 {
572 if(!predicate(*i))
573 {
574#pragma omp ordered
575 {
576 *s = std::move(*i);
577 s++;
578 }
579 }
580 }
581 start = InputIt(s);
582 }
583 return start;
584 }
585#endif
586
595 static std::shared_ptr<AuxFunc>
597
632 std::tuple<std::shared_ptr<ddjvu_context_t>, std::shared_ptr<int>>
634
635private:
636 AuxFunc();
637
638 std::vector<std::tuple<std::string, Genre>>
639 read_genres(const bool &wrong_loc, const std::string &locname);
640
641 std::vector<GenreGroup>
642 read_genre_groups(const bool &wrong_loc, const std::string &locname);
643
644 static void
645 djvuMessageCallback(ddjvu_context_t *context, void *closure);
646
647 bool activated = true;
648
649 std::mt19937_64 *rng;
650 std::uniform_int_distribution<uint64_t> *dist;
651
652 std::weak_ptr<ddjvu_context_t> djvu_context;
653#ifdef USE_OPENMP
654 omp_lock_t djvu_context_mtx;
655#else
656 std::mutex djvu_context_mtx;
657#endif
658 std::vector<std::weak_ptr<int>> djvu_pipes;
659};
660
661#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::string getExtension(const std::string &fnm)
Returns file extesion.
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.
std::filesystem::path tempPath()
Returns absolute path to system temporary directory.
bool ifSupportedArchivePackingType(const std::filesystem::path &ch_p)
Checks if given archive is supported by MLBookProc for packing.
std::filesystem::path sharePath()
Returns absolute path to share directory, used by MLBookProc.
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.
std::tuple< std::shared_ptr< ddjvu_context_t >, std::shared_ptr< int > > getDJVUContext()
Returns tuple with smart pointer to djvu context object and smart pointer to djvu context signal pipe...
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 htmlToUtf8(std::string &result)
Converst 'html' symbols to UTF-8 characters.
std::filesystem::path getSelfpath()
Returns absolute path to program executable file.
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::string stringToLower(const std::string &line)
Converts all letters of the string to lowercase letters.
bool ifSupportedArchiveUnpackaingType(const std::filesystem::path &ch_p)
Checks if given archive is supported by MLBookProc.