MLBookProc 1.0
 
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
60
70class AuxFunc
71{
72public:
76 virtual ~AuxFunc();
77
88 std::string
89 to_utf_8(const std::string &input, const char *conv_name);
90
96 std::string
97 utf8_to_system(const std::string &input);
98
108 std::string
109 utf_8_to(const std::string &input, const char *conv_name);
110
118 const char *
119 get_converter_by_number(const int32_t &num);
120
127 std::string
128 detect_encoding(const std::string &buf);
129
134 std::filesystem::path
136
141 std::filesystem::path
143
148 std::filesystem::path
150
159 std::filesystem::path
161
170 std::vector<GenreGroup>
172
183 std::string
184 libgcrypt_error_handling(const gcry_error_t &err);
185
194 std::string
195 to_hex(const std::string &source);
196
202 std::string
203 stringToLower(const std::string &line);
204
209 std::string
211
218 std::string
219 time_t_to_date(const time_t &tt);
220
229 bool
230 if_supported_type(const std::filesystem::path &ch_p);
231
239 void
240 html_to_utf8(std::string &input);
241
246 void
247 open_book_callback(const std::filesystem::path &path);
248
259 void
260 copy_book_callback(const std::filesystem::path &source,
261 const std::filesystem::path &out);
262
268 std::vector<std::string>
270
276 std::vector<std::string>
278
284 std::vector<std::string>
286
292 std::string
293 get_extension(const std::filesystem::path &p);
294
304 int32_t
306
313 bool
315
316#ifdef USE_OPENMP
330 template <class InputIt,
331 class T = typename std::iterator_traits<InputIt>::value_type>
332 static InputIt
333 parallelFind(InputIt start, InputIt end, const T &val)
334 {
335 InputIt res = end;
336#pragma omp parallel
337 {
338#pragma omp for
339 for(InputIt i = start; i != end; i++)
340 {
341 if(*i == val)
342 {
343#pragma omp critical
344 {
345 if(i < res)
346 {
347 res = i;
348 }
349 }
350#pragma omp cancel for
351 }
352 }
353 }
354
355 return res;
356 }
357
371 template <class InputIt, class UnaryPred>
372 static InputIt
373 parallelFindIf(InputIt start, InputIt end, UnaryPred predicate)
374 {
375 InputIt res = end;
376#pragma omp parallel
377 {
378#pragma omp for
379 for(InputIt i = start; i != end; i++)
380 {
381 if(predicate(*i))
382 {
383#pragma omp critical
384 {
385 if(i < res)
386 {
387 res = i;
388 }
389 }
390#pragma omp cancel for
391 }
392 }
393 }
394
395 return res;
396 }
397
413 template <class InputIt,
414 class T = typename std::iterator_traits<InputIt>::value_type>
415 static InputIt
416 parallelRemove(InputIt start, InputIt end, const T &val)
417 {
418 start = parallelFind(start, end, val);
419 if(start != end)
420 {
421 for(InputIt i = start + 1; i != end; i++)
422 {
423 if(*i != val)
424 {
425 std::swap(*start, *i);
426 start++;
427 }
428 }
429 }
430 return start;
431 }
432
448 template <class InputIt, class UnaryPred>
449 static InputIt
450 parallelRemoveIf(InputIt start, InputIt end, UnaryPred predicate)
451 {
452 start = parallelFindIf(start, end, predicate);
453 if(start != end)
454 {
455 for(InputIt i = start + 1; i != end; i++)
456 {
457 if(!predicate(*i))
458 {
459 std::swap(*start, *i);
460 start++;
461 }
462 }
463 }
464 return start;
465 }
466#endif
467
476 static std::shared_ptr<AuxFunc>
478
485 std::shared_ptr<ddjvu_context_t>
487
488private:
489 AuxFunc();
490
491 std::vector<std::tuple<std::string, Genre>>
492 read_genres(const bool &wrong_loc, const std::string &locname);
493
494 std::vector<GenreGroup>
495 read_genre_groups(const bool &wrong_loc, const std::string &locname);
496
497 bool
498 handleDJVUmsgs(const std::shared_ptr<ddjvu_context_t> &ctx);
499
500 bool activated = true;
501
502 std::mt19937_64 *rng;
503 std::uniform_int_distribution<uint64_t> *dist;
504
505 std::shared_ptr<ddjvu_context_t> djvu_context;
506};
507
508#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.
static InputIt parallelRemove(InputIt start, InputIt end, const T &val)
"Remove" method for C++ containers.
Definition AuxFunc.h:416
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.
static InputIt parallelFind(InputIt start, InputIt end, const T &val)
'Find' method for C++ standard containers.
Definition AuxFunc.h:333
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.
static InputIt parallelFindIf(InputIt start, InputIt end, UnaryPred predicate)
'Find' method for C++ standard containers.
Definition AuxFunc.h:373
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.
static InputIt parallelRemoveIf(InputIt start, InputIt end, UnaryPred predicate)
'Remove' method for C++ containers.
Definition AuxFunc.h:450
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.