forked from schnaader/precomp-cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprecomp_utils.cpp
More file actions
128 lines (115 loc) · 3.36 KB
/
precomp_utils.cpp
File metadata and controls
128 lines (115 loc) · 3.36 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "precomp_utils.h"
#include <random>
#include <sstream>
#ifndef __unix
#include <conio.h>
#include <windows.h>
#else
#include <time.h>
#include <sys/time.h>
#endif
#ifndef _WIN32
#include <fcntl.h>
#include <unistd.h>
#endif
#ifndef STDTHREAD_IMPORTED
#define STDTHREAD_IMPORTED
#include <thread>
#endif
#ifdef MINGW
#ifndef _GLIBCXX_HAS_GTHREADS
#include "contrib\mingw_std_threads\mingw.thread.h"
#endif // _GLIBCXX_HAS_GTHREADS
#endif
std::string temp_files_tag() {
// Generate a random 8digit tag for the temp files of a recursion level, so they don't overwrite each other
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 15);
std::stringstream ss;
ss << std::hex;
for (int i = 0; i < 8; i++) {
ss << dis(gen);
}
return ss.str();
}
unsigned int auto_detected_thread_count() {
auto threads = std::thread::hardware_concurrency();
if (threads == 0) threads = 2;
return threads;
}
#ifndef _WIN32
int ttyfd = -1;
#endif
void print_to_console(const std::string& format) {
#ifdef _WIN32
for (char chr : format) {
putch(chr);
}
#else
if (ttyfd < 0)
ttyfd = open("/dev/tty", O_RDWR);
write(ttyfd, format.c_str(), format.length());
#endif
}
char get_char_with_echo() {
#ifndef __unix
return getche();
#else
return fgetc(stdin);
#endif
}
std::string precomp_error_msg(int error_nr, const char* extra_info) {
switch (error_nr) {
case ERR_IGNORE_POS_TOO_BIG:
return "Ignore position too big";
case ERR_IDENTICAL_BYTE_SIZE_TOO_BIG:
return "Identical bytes size bigger than 4 GB";
case ERR_ONLY_SET_MIN_SIZE_ONCE:
return "Minimal identical size can only be set once";
case ERR_MORE_THAN_ONE_OUTPUT_FILE:
return "More than one output file given";
case ERR_MORE_THAN_ONE_INPUT_FILE:
return "More than one input file given";
case ERR_DONT_USE_SPACE:
return "Please don't use a space between the -o switch and the output filename";
case ERR_TEMP_FILE_DISAPPEARED:
return "Temporary file has disappeared";
case ERR_DISK_FULL:
return "There is not enough space on disk";
case ERR_RECURSION_DEPTH_TOO_BIG:
return "Recursion depth too big";
case ERR_ONLY_SET_RECURSION_DEPTH_ONCE:
return "Recursion depth can only be set once";
case ERR_CTRL_C:
return "CTRL-C detected";
case ERR_INTENSE_MODE_LIMIT_TOO_BIG:
return "Intense mode level limit too big";
case ERR_BRUTE_MODE_LIMIT_TOO_BIG:
return "Brute mode level limit too big";
case ERR_DURING_RECOMPRESSION:
return "Error recompressing data!";
case ERR_NO_PCF_HEADER:
return "Input stream has no valid PCF header";
case ERR_PCF_HEADER_INCOMPATIBLE_VERSION: {
std::string txt = "Input stream was made with an incompatible Precomp version";
if (extra_info != nullptr) txt += "\n" + std::string(extra_info);
return txt;
}
case ERR_BROTLI_NO_LONGER_SUPPORTED:
return "Precompressed stream has a precompressed JPG using Brunsli with Brotli metadata compression, Brotli is no longer supported by precomp";
default:
return "Unknown error";
}
}
PrecompError::PrecompError(int error_code, std::string extra_info) : error_code(error_code), extra_info(std::move(extra_info)) {}
// get current time in ms
long long get_time_ms() {
#ifndef __unix
return GetTickCount();
#else
timeval t;
gettimeofday(&t, NULL);
return (t.tv_sec * 1000) + (t.tv_usec / 1000);
#endif
}