-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIVUtil.cpp
More file actions
126 lines (117 loc) · 3.51 KB
/
IVUtil.cpp
File metadata and controls
126 lines (117 loc) · 3.51 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
/*
IVUTIL.CPP
NICK WILSON
2020
*/
#include "IVUtil.hpp"
/**
* formatSupport - Compare file extension provided to known list to confirm support
* extension > File extension as String
* return - int < FILE_TYPE of file type or -1 if no match
*/
int IVUTIL::formatSupport(std::string extension) {
// convert character to upper case for comparison
auto upper = [](char a) -> char {
if (a > 0x60 && a < 0x7B) return (char) (a - 0x20);
else return a;
};
// convert extension string, ignoring leading dot
std::string extensionUppercase = "";
for (unsigned i = 1; i < extension.length(); i++) {
extensionUppercase += upper(extension[i]);
}
// sort by first character of extension
switch (extensionUppercase[0]) {
case 'B':
/* BMP */
if (!extensionUppercase.compare("BMP")) return BMP;
break;
case 'G':
/* GIF */
if (!extensionUppercase.compare("GIF")) return GIF;
break;
case 'H':
/* HEIF */
if (!extensionUppercase.compare("HEIF")) return HEIF;
if (!extensionUppercase.compare("HEIC")) return HEIF;
break;
case 'J':
/* JP(E)G */
if (!extensionUppercase.compare("JPG")) return JPG;
if (!extensionUppercase.compare("JPEG")) return JPG;
break;
case 'P':
/* PNG */
if (!extensionUppercase.compare("PNG")) return PNG;
break;
case 'T':
/* TIF(F) */
if (!extensionUppercase.compare("TIF")) return TIF;
if (!extensionUppercase.compare("TIFF")) return TIF;
/* TGA */
if (!extensionUppercase.compare("TGA")) return TGA;
break;
default:
return -1;
}
return -1;
}
/**
* formatSupport - Return library supporting file type
* extension > File extension as String
* return - int < LIB_TYPE_SUPPORT or -1 if unsupported
*/
int IVUTIL::libSupport(std::string extension) {
switch (formatSupport(extension)) {
case BMP:
case JPG:
case PNG:
case TIF:
case TGA:
return TYPE_SDL;
case GIF:
return TYPE_GIFLIB;
case HEIF:
return TYPE_LIBHEIF;
default:
return -1;
}
}
/**
* readSettings - Load settings file if possible and recover window position and size.
*/
void IVUTIL::readSettings(std::filesystem::path target, IVSETTINGS* settings) {
//open filestream to read file at the end
std::ifstream inputStream(target, std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
if (inputStream.is_open()) {
//current position is end, this will present filesize
int fileLength = inputStream.tellg();
if (fileLength == -1) { //read failed somewhere in iostream
std::cerr << LOG_NOTICE << "Failed to read settings file!" << std::endl;
}
else if ((unsigned) fileLength < sizeof(IVSETTINGS)) { //settings file is too short
std::cerr << LOG_WARNING << "Incorrect settings data size! [Expected " << sizeof(IVSETTINGS) << ", got " << fileLength << "]" << std::endl;
}
else { //no problems encountered
inputStream.seekg(std::ios_base::beg);
inputStream.read(reinterpret_cast<char *>(settings), sizeof(IVSETTINGS));
}
inputStream.close();
}
else { //load defaults
std::cerr << LOG_NOTICE << "No settings file located!" << std::endl;
}
}
/**
* writeSettings - Write to settings file, if it can be written in a valid location
*/
void IVUTIL::writeSettings(std::filesystem::path target, IVSETTINGS* settings) {
std::ofstream outputStream(target, std::ifstream::out | std::ifstream::binary | std::ifstream::trunc);
if (outputStream.is_open()) {
outputStream.write(reinterpret_cast<char *>(settings), sizeof(IVSETTINGS));
outputStream.close();
}
else {
std::cerr << LOG_ERROR << "Could not write to settings file!" << std::endl;
}
}