MLBookProc 1.2.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 <atomic>
24#include <filesystem>
25#include <gcrypt.h>
26#include <libdjvu/ddjvuapi.h>
27#include <memory>
28#include <random>
29#include <string>
30#include <tuple>
31#include <vector>
32
33#ifdef USE_OPENMP
34#include <omp.h>
35#else
36#include <mutex>
37#endif
38
65
75class AuxFunc
76{
77public:
81 virtual ~AuxFunc();
82
93 std::string
94 to_utf_8(const std::string &input, const char *conv_name);
95
101 std::string
102 utf8_to_system(const std::string &input);
103
113 std::string
114 utf_8_to(const std::string &input, const char *conv_name);
115
123 const char *
124 get_converter_by_number(const int32_t &num);
125
132 std::string
133 detect_encoding(const std::string &buf);
134
139 std::filesystem::path
141
146 std::filesystem::path
148
153 std::filesystem::path
155
164 std::filesystem::path
166
175 std::vector<GenreGroup>
177
188 std::string
189 libgcrypt_error_handling(const gcry_error_t &err);
190
199 std::string
200 to_hex(const std::string &source);
201
207 std::string
208 stringToLower(const std::string &line);
209
214 std::string
216
223 std::string
224 time_t_to_date(const time_t &tt);
225
234 bool
235 if_supported_type(const std::filesystem::path &ch_p);
236
246 bool
247 ifSupportedArchiveUnpackaingType(const std::filesystem::path &ch_p);
248
258 bool
259 ifSupportedArchivePackingType(const std::filesystem::path &ch_p);
260
268 void
269 html_to_utf8(std::string &input);
270
275 void
276 open_book_callback(const std::filesystem::path &path);
277
288 void
289 copy_book_callback(const std::filesystem::path &source,
290 const std::filesystem::path &out);
291
297 std::vector<std::string>
299
305 std::vector<std::string>
307
313 std::vector<std::string>
315
321 std::string
322 get_extension(const std::filesystem::path &p);
323
333 int32_t
335
342 bool
344
345#ifdef USE_OPENMP
359 template <class InputIt,
360 class T = typename std::iterator_traits<InputIt>::value_type>
361 static InputIt
362 parallelFind(InputIt start, InputIt end, const T &val)
363 {
364 InputIt res = end;
365 const T *val_ptr = &val;
366#pragma omp parallel
367 {
368#pragma omp for
369 for(InputIt i = start; i != end; i++)
370 {
371 if(*i == *val_ptr)
372 {
373#pragma omp critical
374 {
375 if(i < res)
376 {
377 res = i;
378 }
379 }
380#pragma omp cancel for
381 }
382 }
383 }
384
385 return res;
386 }
387
401 template <class InputIt, class UnaryPred>
402 static InputIt
403 parallelFindIf(InputIt start, InputIt end, UnaryPred predicate)
404 {
405 InputIt res = end;
406#pragma omp parallel
407 {
408#pragma omp for
409 for(InputIt i = start; i != end; i++)
410 {
411 if(predicate(*i))
412 {
413#pragma omp critical
414 {
415 if(i < res)
416 {
417 res = i;
418 }
419 }
420#pragma omp cancel for
421 }
422 }
423 }
424
425 return res;
426 }
427
443 template <class InputIt,
444 class T = typename std::iterator_traits<InputIt>::value_type>
445 static InputIt
446 parallelRemove(InputIt start, InputIt end, const T &val)
447 {
448 start = parallelFind(start, end, val);
449 if(start != end)
450 {
451 T *s = start.base();
452 T *e = end.base();
453#pragma omp parallel
454#pragma omp for ordered
455 for(T *i = s + 1; i != e; i++)
456 {
457 if((*i) != val)
458 {
459#pragma omp ordered
460 {
461 *s = std::move((*i));
462 s++;
463 }
464 }
465 }
466 start = InputIt(s);
467 }
468 return start;
469 }
470
486 template <class InputIt, class UnaryPred,
487 class T = typename std::iterator_traits<InputIt>::value_type>
488 static InputIt
489 parallelRemoveIf(InputIt start, InputIt end, UnaryPred predicate)
490 {
491 start = parallelFindIf(start, end, predicate);
492 if(start != end)
493 {
494 T *s = start.base();
495 T *e = end.base();
496#pragma omp parallel
497#pragma omp for ordered
498 for(T *i = s + 1; i != e; i++)
499 {
500 if(!predicate(*i))
501 {
502#pragma omp ordered
503 {
504 *s = std::move(*i);
505 s++;
506 }
507 }
508 }
509 start = InputIt(s);
510 }
511 return start;
512 }
513#endif
514
523 static std::shared_ptr<AuxFunc>
525
560 std::tuple<std::shared_ptr<ddjvu_context_t>, std::shared_ptr<int>>
562
563private:
564 AuxFunc();
565
566 std::vector<std::tuple<std::string, Genre>>
567 read_genres(const bool &wrong_loc, const std::string &locname);
568
569 std::vector<GenreGroup>
570 read_genre_groups(const bool &wrong_loc, const std::string &locname);
571
572 static void
573 djvuMessageCallback(ddjvu_context_t *context, void *closure);
574
575 bool activated = true;
576
577 std::mt19937_64 *rng;
578 std::uniform_int_distribution<uint64_t> *dist;
579
580 std::weak_ptr<ddjvu_context_t> djvu_context;
581#ifdef USE_OPENMP
582 omp_lock_t djvu_context_mtx;
583#else
584 std::mutex djvu_context_mtx;
585#endif
586 std::vector<std::weak_ptr<int>> djvu_pipes;
587};
588
589#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.
bool ifSupportedArchivePackingType(const std::filesystem::path &ch_p)
Checks if given archive is supported by MLBookProc for packing.
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 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.