Replaced sscanf usage

This commit is contained in:
Chlumsky 2024-01-05 13:38:47 +01:00
parent 2357140784
commit 49453c96f1
4 changed files with 72 additions and 58 deletions

View File

@ -24,8 +24,8 @@ struct Vector2 {
} }
/// Sets individual elements of the vector. /// Sets individual elements of the vector.
inline void set(double x, double y) { inline void set(double newX, double newY) {
this->x = x, this->y = y; x = newX, y = newY;
} }
/// Returns the vector's squared length. /// Returns the vector's squared length.

View File

@ -2,6 +2,8 @@
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#include "shape-description.h" #include "shape-description.h"
#include <cstdlib>
namespace msdfgen { namespace msdfgen {
int readCharF(FILE *input) { int readCharF(FILE *input) {
@ -25,14 +27,25 @@ int readCharS(const char **input) {
} }
int readCoordF(FILE *input, Point2 &coord) { int readCoordF(FILE *input, Point2 &coord) {
return fscanf(input, "%lf,%lf", &coord.x, &coord.y); return fscanf(input, "%lf , %lf", &coord.x, &coord.y);
} }
int readCoordS(const char **input, Point2 &coord) { int readCoordS(const char **input, Point2 &coord) {
int read = 0; char *end = NULL;
int result = sscanf(*input, "%lf,%lf%n", &coord.x, &coord.y, &read); coord.x = strtod(*input, &end);
*input += read; if (end <= *input)
return result; return 0;
*input = end;
while (**input == ' ' || **input == '\t' || **input == '\n' || **input == '\r')
++*input;
if (**input != ',')
return 1;
++*input;
coord.y = strtod(*input, &end);
if (end <= *input)
return 1;
*input = end;
return 2;
} }
static bool writeCoord(FILE *output, Point2 coord) { static bool writeCoord(FILE *output, Point2 coord) {

View File

@ -52,37 +52,27 @@ static bool readNodeType(char &output, const char *&pathDef) {
return false; return false;
} }
static bool readCoord(Point2 &output, const char *&pathDef) { static bool readDouble(double &output, const char *&pathDef) {
skipExtraChars(pathDef); char *end = NULL;
int shift; output = strtod(pathDef, &end);
double x, y; if (end > pathDef) {
if (sscanf(pathDef, "%lf%lf%n", &x, &y, &shift) == 2 || sscanf(pathDef, "%lf , %lf%n", &x, &y, &shift) == 2) { pathDef = end;
output.x = x;
output.y = y;
pathDef += shift;
return true; return true;
} }
return false; return false;
} }
static bool readDouble(double &output, const char *&pathDef) { static bool readCoord(Point2 &output, const char *&pathDef) {
skipExtraChars(pathDef); skipExtraChars(pathDef);
int shift; return readDouble(output.x, pathDef) && (skipExtraChars(pathDef), readDouble(output.y, pathDef));
double v;
if (sscanf(pathDef, "%lf%n", &v, &shift) == 1) {
pathDef += shift;
output = v;
return true;
}
return false;
} }
static bool readBool(bool &output, const char *&pathDef) { static bool readBool(bool &output, const char *&pathDef) {
skipExtraChars(pathDef); skipExtraChars(pathDef);
int shift; char *end = NULL;
int v; long v = strtol(pathDef, &end, 10);
if (sscanf(pathDef, "%d%n", &v, &shift) == 1) { if (end > pathDef) {
pathDef += shift; pathDef = end;
output = v != 0; output = v != 0;
return true; return true;
} }
@ -339,10 +329,10 @@ bool loadSvgShape(Shape &output, const char *filename, int pathIndex, Vector2 *d
return false; return false;
Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height")); Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
double left, top; if (const char *viewBox = root->Attribute("viewBox")) {
const char *viewBox = root->Attribute("viewBox"); double left = 0, top = 0;
if (viewBox) readDouble(left, viewBox) && readDouble(top, viewBox) && readDouble(dims.x, viewBox) && readDouble(dims.y, viewBox);
sscanf(viewBox, "%lf %lf %lf %lf", &left, &top, &dims.x, &dims.y); }
if (dimensions) if (dimensions)
*dimensions = dims; *dimensions = dims;
output.contours.clear(); output.contours.clear();
@ -372,9 +362,8 @@ int loadSvgShape(Shape &output, Shape::Bounds &viewBox, const char *filename) {
viewBox.l = 0, viewBox.b = 0; viewBox.l = 0, viewBox.b = 0;
Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height")); Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
const char *viewBoxStr = root->Attribute("viewBox"); if (const char *viewBoxStr = root->Attribute("viewBox"))
if (viewBoxStr) readDouble(viewBox.l, viewBoxStr) && readDouble(viewBox.b, viewBoxStr) && readDouble(dims.x, viewBoxStr) && readDouble(dims.y, viewBoxStr);
sscanf(viewBoxStr, "%lf %lf %lf %lf", &viewBox.l, &viewBox.b, &dims.x, &dims.y);
viewBox.r = viewBox.l+dims.x; viewBox.r = viewBox.l+dims.x;
viewBox.t = viewBox.b+dims.y; viewBox.t = viewBox.b+dims.y;
output.contours.clear(); output.contours.clear();
@ -550,9 +539,8 @@ int loadSvgShape(Shape &output, Shape::Bounds &viewBox, const char *filename) {
viewBox.l = 0, viewBox.b = 0; viewBox.l = 0, viewBox.b = 0;
Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height")); Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
const char *viewBoxStr = root->Attribute("viewBox"); if (const char *viewBoxStr = root->Attribute("viewBox"))
if (viewBoxStr) readDouble(viewBox.l, viewBoxStr) && readDouble(viewBox.b, viewBoxStr) && readDouble(dims.x, viewBoxStr) && readDouble(dims.y, viewBoxStr);
sscanf(viewBoxStr, "%lf %lf %lf %lf", &viewBox.l, &viewBox.b, &dims.x, &dims.y);
viewBox.r = viewBox.l+dims.x; viewBox.r = viewBox.l+dims.x;
viewBox.t = viewBox.b+dims.y; viewBox.t = viewBox.b+dims.y;
return flags; return flags;

View File

@ -10,6 +10,7 @@
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <cstdio> #include <cstdio>
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
@ -56,36 +57,48 @@ static char toupper(char c) {
} }
static bool parseUnsigned(unsigned &value, const char *arg) { static bool parseUnsigned(unsigned &value, const char *arg) {
char c; char *end = NULL;
return sscanf(arg, "%u%c", &value, &c) == 1; value = (unsigned) strtoul(arg, &end, 10);
return end > arg && !*end;
} }
static bool parseUnsignedDecOrHex(unsigned &value, const char *arg) { static bool parseUnsignedDecOrHex(unsigned &value, const char *arg) {
char *end = NULL;
if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X')) { if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X')) {
char c; arg += 2;
return sscanf(arg+2, "%x%c", &value, &c) == 1; value = (unsigned) strtoul(arg, &end, 16);
} else
value = (unsigned) strtoul(arg, &end, 10);
return end > arg && !*end;
} }
return parseUnsigned(value, arg);
}
static bool parseUnsignedLL(unsigned long long &value, const char *arg) { static bool parseUnsignedLL(unsigned long long &value, const char *arg) {
char c; if (*arg >= '0' && *arg <= '9') {
return sscanf(arg, "%llu%c", &value, &c) == 1; value = 0;
do {
value = 10*value+(*arg++-'0');
} while (*arg >= '0' && *arg <= '9');
return !*arg;
}
return false;
} }
static bool parseDouble(double &value, const char *arg) { static bool parseDouble(double &value, const char *arg) {
char c; char *end = NULL;
return sscanf(arg, "%lf%c", &value, &c) == 1; value = strtod(arg, &end);
return end > arg && !*end;
} }
static bool parseAngle(double &value, const char *arg) { static bool parseAngle(double &value, const char *arg) {
char c1, c2; char *end = NULL;
int result = sscanf(arg, "%lf%c%c", &value, &c1, &c2); value = strtod(arg, &end);
if (result == 1) if (end > arg) {
return true; arg = end;
if (result == 2 && (c1 == 'd' || c1 == 'D')) { if (*arg == 'd' || *arg == 'D') {
++arg;
value *= M_PI/180; value *= M_PI/180;
return true; }
return !*arg;
} }
return false; return false;
} }
@ -159,11 +172,11 @@ static FontHandle *loadVarFont(FreetypeHandle *library, const char *filename) {
while (*filename && *filename != '=') while (*filename && *filename != '=')
buffer.push_back(*filename++); buffer.push_back(*filename++);
if (*filename == '=') { if (*filename == '=') {
double value = 0; char *end = NULL;
int skip = 0; double value = strtod(++filename, &end);
if (sscanf(++filename, "%lf%n", &value, &skip) == 1) { if (end > filename) {
filename = end;
setFontVariationAxis(library, font, buffer.c_str(), value); setFontVariationAxis(library, font, buffer.c_str(), value);
filename += skip;
} }
} }
} while (*filename++ == '&'); } while (*filename++ == '&');