Skip to content

Commit a187195

Browse files
committed
PluginXBM: use sscanf for simplicity
1 parent cf29a26 commit a187195

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

Source/Plugins/PluginXBM.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
#include "FreeImage.h"
2626
#include "Utilities.h"
2727

28-
#include <regex>
29-
3028
// ==========================================================
3129
// Internal functions
3230
// ==========================================================
3331

34-
constexpr int MAX_LINE = 512;
32+
#define MAX_LINE 512
33+
#define LINE_FORMAT_WIDTH_OR_HEIGHT "#define %" FI_QUOTE(MAX_LINE) "s %d"
34+
#define LINE_FORMAT_BITS_V10 "static short %" FI_QUOTE(MAX_LINE) "s = {"
35+
#define LINE_FORMAT_BITS_V11_OPT1 "static char %" FI_QUOTE(MAX_LINE) "s = {"
36+
#define LINE_FORMAT_BITS_V11_OPT2 "static unsigned char %" FI_QUOTE(MAX_LINE) "s = {"
3537

3638
static const char *ERR_XBM_SYNTAX = "Syntax error";
3739
static const char *ERR_XBM_LINE = "Line too long";
@@ -89,9 +91,8 @@ Read an XBM file into a buffer
8991
*/
9092
static const char*
9193
readXBMFile(FreeImageIO *io, fi_handle handle, int *widthP, int *heightP, std::unique_ptr<void, decltype(&free)> &dataP) {
92-
std::string line(MAX_LINE, '\0');
94+
std::string line(MAX_LINE, '\0'), name(MAX_LINE + 1, '\0');
9395
char *ptr{};
94-
char *t{};
9596
int version = 0;
9697
size_t bytes, bytes_per_line, raster_length;
9798
int c1, c2, value1, value2;
@@ -101,35 +102,39 @@ readXBMFile(FreeImageIO *io, fi_handle handle, int *widthP, int *heightP, std::u
101102
line of the C declaration of the array (the "static char ..." or whatever line)
102103
*/
103104

104-
const std::regex width_regex(R"(\s*#define\s+(\w+)_width\s+(\d+)\s*)");
105-
const std::regex height_regex(R"(\s*#define\s+(\w+)_height\s+(\d+)\s*)");
106-
const std::regex decl_v10_regex(R"(\s*static\s+short\s+(\w+)\s*\[\s*\]\s*=\s*\{\s*)");
107-
const std::regex decl_v11_regex(R"(\s*static\s+(unsigned\s+)?char\s+(\w+)\s*\[\s*\]\s*=\s*\{\s*)");
108-
std::cmatch match;
109-
110105
*widthP = *heightP = -1;
111106

112-
for(;;) {
107+
for (;;) {
113108
if (!readLine(line.data(), MAX_LINE, io, handle)) {
114109
break;
115110
}
116-
111+
117112
if (strlen(line.c_str()) >= MAX_LINE - 1) {
118113
return ERR_XBM_LINE;
119114
}
120115

121-
if (std::regex_match(line.c_str(), match, width_regex)) {
122-
*widthP = std::stoi(match.str(2));
123-
}
124-
else if (std::regex_match(line.c_str(), match, height_regex)) {
125-
*heightP = std::stoi(match.str(2));
116+
int val{};
117+
if (2 == std::sscanf(line.c_str(), LINE_FORMAT_WIDTH_OR_HEIGHT, name.data(), &val)) {
118+
if (const auto suffix = std::strrchr(name.c_str(), '_')) {
119+
if (0 == std::strcmp("_width", suffix)) {
120+
*widthP = val;
121+
continue;
122+
}
123+
if (0 == std::strcmp("_height", suffix)) {
124+
*heightP = val;
125+
continue;
126+
}
127+
}
126128
}
127-
else if (std::regex_match(line.c_str(), match, decl_v10_regex)) {
129+
130+
if (1 == std::sscanf(line.c_str(), LINE_FORMAT_BITS_V10, name.data())) {
128131
version = 10;
129132
found_declaration = true;
130133
break;
131134
}
132-
else if (std::regex_match(line.c_str(), match, decl_v11_regex)) {
135+
136+
if (1 == std::sscanf(line.c_str(), LINE_FORMAT_BITS_V11_OPT1, name.data()) ||
137+
1 == std::sscanf(line.c_str(), LINE_FORMAT_BITS_V11_OPT2, name.data())) {
133138
version = 11;
134139
found_declaration = true;
135140
break;

0 commit comments

Comments
 (0)