-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkurger_cli.cpp
More file actions
109 lines (95 loc) · 4.12 KB
/
kurger_cli.cpp
File metadata and controls
109 lines (95 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 11/09/2025 https://github.com/su8/kurger
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <iostream>
#include <string>
#include <algorithm>
#include <filesystem>
#define VLA 4999
static size_t indexLastSep(const char *str);
static void pdf2img(const char *str, unsigned spin1, unsigned spin2, unsigned resolution);
int main(int argc, char *argv[]) {
if (argc < 9) { std::cout << "kurger from 1 to 10 resolution 100 file \"the book.pdf\" ### the quotes are mandatory for the given file when dealing with spaces in its name" << std::endl; return EXIT_FAILURE; }
pdf2img(argv[8], std::strtoul(argv[2], static_cast<char **>(nullptr), 10), std::strtoul(argv[4], static_cast<char **>(nullptr), 10), std::strtoul(argv[6], static_cast<char **>(nullptr), 10));
return EXIT_SUCCESS;
}
static size_t indexLastSep(const char *str) {
const char *ptr = str;
size_t sepIndex = 0;
size_t x = 0;
for (; *ptr; x++, ptr++) {
#ifdef _WIN32
if ('\\' == *ptr) { sepIndex = x; }
#else
if ('/' == *ptr) { sepIndex = x; /* keep in mind that we use loop */ }
#endif /* _WIN32 */
}
return sepIndex;
}
static void pdf2img(const char *str, unsigned spin1, unsigned spin2, unsigned resolution) {
char imageCombo[] = "png";
char sdeviceCombo[] = "png16m";
char pdfName[VLA+1] = {'\0'};
char BaseName[VLA+1] = {'\0'};
char params[VLA+1] = {'\0'};
char ren1[VLA+1] = {'\0'};
char ren2[VLA+1] = {'\0'};
char createdDir[VLA+1] = {'\0'};
int smallRange = (spin2 - spin1) + 2;
int bigRange = spin1;
int y = 0;
size_t dirnameLen = indexLastSep(str);
size_t x = 0;
size_t z = 0;
size_t fit = strlen(str) - 4; /* exclude the .pdf file extension */
if (1850U <= fit) { std::cout << "The given filename is too long!\n" << std::endl; return; }
if (spin1 > spin2) { std::cout << "From page can't be greater than To page." << std::endl; return; }
snprintf(pdfName, VLA, "%s", str);
for (z = 0, x = dirnameLen + 1; x < fit; x++, z++) { BaseName[z] = pdfName[x]; /* /path/to/some.pdf -> some */ }
BaseName[z] = '\0';
if (250U < z) { std::cout << "The given filename is too long!" << std::endl; return; }
snprintf(createdDir, VLA, "%s_converted", pdfName);
if (!std::filesystem::exists(pdfName)) { std::cout << pdfName << " doesn't exist.\nExiting!" << std::endl; return; }
if (!std::filesystem::is_directory(createdDir)) { std::filesystem::create_directory(createdDir); }
#ifdef _WIN32
#define GS "C:\\gs\\bin\\gswin64c.exe"
#else
#define GS "gs"
#endif /* _WIN32 */
snprintf(params, VLA, GS " -dBATCH -dNOPAUSE -dQUIET -dFirstPage=%d -dLastPage=%d "
"-sOutputFile=\"%s\"_pAge_%%01d.%s -sDEVICE=%s -r%d "
"-dGraphicsAlphaBits=4 -sBandListStorage=memory "
"-dBufferSpace=99000 -dNumRenderingThreads=8 \"%s\"",
spin1, spin2, pdfName, imageCombo, sdeviceCombo, resolution, str);
std::cout << "Please wait until we convert the requested images." << std::endl;
std::system(params);
for (y = 1; y < smallRange; y++, bigRange++) {
snprintf(ren1, VLA, "%s_pAge_%d.%s", pdfName, y, imageCombo);
snprintf(ren2, VLA, "%s/%s_page_%d.%s", createdDir, BaseName, bigRange, imageCombo);
std::remove(ren2);
std::rename(ren1, ren2);
}
std::cout << "Done" << std::endl;
#ifdef _WIN32
snprintf(pdfName, sizeof(pdfName), "explorer %s", createdDir);
#else
snprintf(pdfName, sizeof(pdfName), "xdg-open %s", createdDir);
#endif /* _WIN32 */
std::system(pdfName);
}