Revert memcpy'ing input string into local buffer.

This commit is contained in:
Syoyo Fujita
2016-07-15 02:01:50 +09:00
parent 7d6318c3ad
commit 333bb55d84

View File

@@ -391,7 +391,6 @@ static vertex_index parseRawTriple(const char **token) {
static_cast<int>(0x80000000)); // 0x80000000 = -2147483648 = invalid static_cast<int>(0x80000000)); // 0x80000000 = -2147483648 = invalid
vi.v_idx = my_atoi((*token)); vi.v_idx = my_atoi((*token));
//(*token) += strcspn((*token), "/ \t\r");
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -405,7 +404,6 @@ static vertex_index parseRawTriple(const char **token) {
if ((*token)[0] == '/') { if ((*token)[0] == '/') {
(*token)++; (*token)++;
vi.vn_idx = my_atoi((*token)); vi.vn_idx = my_atoi((*token));
//(*token) += strcspn((*token), "/ \t\r");
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -415,7 +413,6 @@ static vertex_index parseRawTriple(const char **token) {
// i/j/k or i/j // i/j/k or i/j
vi.vt_idx = my_atoi((*token)); vi.vt_idx = my_atoi((*token));
//(*token) += strcspn((*token), "/ \t\r");
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -427,7 +424,6 @@ static vertex_index parseRawTriple(const char **token) {
// i/j/k // i/j/k
(*token)++; // skip '/' (*token)++; // skip '/'
vi.vn_idx = my_atoi((*token)); vi.vn_idx = my_atoi((*token));
//(*token) += strcspn((*token), "/ \t\r");
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -436,17 +432,17 @@ static vertex_index parseRawTriple(const char **token) {
} }
static inline bool parseString(ShortString *s, const char **token) { static inline bool parseString(ShortString *s, const char **token) {
skip_space(token); //(*token) += strspn((*token), " \t"); skip_space(token);
size_t e = until_space((*token)); // strcspn((*token), " \t\r"); size_t e = until_space((*token));
(*s)->insert((*s)->end(), (*token), (*token) + e); (*s)->insert((*s)->end(), (*token), (*token) + e);
(*token) += e; (*token) += e;
return true; return true;
} }
static inline int parseInt(const char **token) { static inline int parseInt(const char **token) {
skip_space(token); //(*token) += strspn((*token), " \t"); skip_space(token);
int i = my_atoi((*token)); int i = my_atoi((*token));
(*token) += until_space((*token)); // strcspn((*token), " \t\r"); (*token) += until_space((*token));
return i; return i;
} }
@@ -601,13 +597,13 @@ fail:
} }
static inline float parseFloat(const char **token) { static inline float parseFloat(const char **token) {
skip_space(token); //(*token) += strspn((*token), " \t"); skip_space(token);
#ifdef TINY_OBJ_LOADER_OLD_FLOAT_PARSER #ifdef TINY_OBJ_LOADER_OLD_FLOAT_PARSER
float f = static_cast<float>(atof(*token)); float f = static_cast<float>(atof(*token));
(*token) += strcspn((*token), " \t\r"); (*token) += strcspn((*token), " \t\r");
#else #else
const char *end = const char *end =
(*token) + until_space((*token)); // strcspn((*token), " \t\r"); (*token) + until_space((*token));
double val = 0.0; double val = 0.0;
tryParseDouble((*token), end, &val); tryParseDouble((*token), end, &val);
float f = static_cast<float>(val); float f = static_cast<float>(val);
@@ -951,18 +947,18 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
static bool parseLine(Command *command, const char *p, size_t p_len, static bool parseLine(Command *command, const char *p, size_t p_len,
bool triangulate = true) { bool triangulate = true) {
//char linebuf[4096]; // @todo { operate directly on pointer `p'. to do that, add range check for string operatoion against `p', since `p' is not null-terminated at p[p_len] }
//assert(p_len < 4095); char linebuf[4096];
//memcpy(linebuf, p, p_len); assert(p_len < 4095);
//linebuf[p_len] = '\0'; memcpy(linebuf, p, p_len);
linebuf[p_len] = '\0';
const char *token = p; const char *token = linebuf;
command->type = COMMAND_EMPTY; command->type = COMMAND_EMPTY;
// Skip leading space. // Skip leading space.
// token += strspn(token, " \t"); skip_space(&token);
skip_space(&token); //(*token) += strspn((*token), " \t");
assert(token); assert(token);
if (token[0] == '\0') { // empty line if (token[0] == '\0') { // empty line
@@ -1011,19 +1007,12 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// face // face
if (token[0] == 'f' && IS_SPACE((token[1]))) { if (token[0] == 'f' && IS_SPACE((token[1]))) {
token += 2; token += 2;
// token += strspn(token, " \t");
skip_space(&token); skip_space(&token);
StackVector<vertex_index, 8> f; StackVector<vertex_index, 8> f;
while (!IS_NEW_LINE(token[0])) { while (!IS_NEW_LINE(token[0])) {
vertex_index vi = parseRawTriple(&token); vertex_index vi = parseRawTriple(&token);
// printf("v = %d, %d, %d\n", vi.v_idx, vi.vn_idx, vi.vt_idx);
// if (callback.index_cb) {
// callback.index_cb(user_data, vi.v_idx, vi.vn_idx, vi.vt_idx);
//}
// size_t n = strspn(token, " \t\r");
// token += n;
skip_space_and_cr(&token); skip_space_and_cr(&token);
f->push_back(vi); f->push_back(vi);
@@ -1076,10 +1065,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// namebuf + strlen(namebuf)); // namebuf + strlen(namebuf));
// command->material_name->push_back('\0'); // command->material_name->push_back('\0');
skip_space(&token); skip_space(&token);
command->material_name = token; // p + (token - linebuf); command->material_name = p + (token - linebuf);
command->material_name_len = command->material_name_len =
length_until_newline(token, p_len - (token - p)) + 1; length_until_newline(token, p_len - (token - linebuf)) + 1;
//length_until_newline(token, p_len - (token - linebuf)) + 1;
command->type = COMMAND_USEMTL; command->type = COMMAND_USEMTL;
return true; return true;
@@ -1091,9 +1079,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
token += 7; token += 7;
skip_space(&token); skip_space(&token);
command->mtllib_name = token; //p + (token - linebuf); command->mtllib_name = p + (token - linebuf);
command->mtllib_name_len = command->mtllib_name_len =
length_until_newline(token, p_len - (token - p)) + 1; length_until_newline(token, p_len - (token - linebuf)) + 1;
command->type = COMMAND_MTLLIB; command->type = COMMAND_MTLLIB;
return true; return true;
@@ -1104,9 +1092,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// @todo { multiple group name. } // @todo { multiple group name. }
token += 2; token += 2;
command->group_name = token; command->group_name = p + (token - linebuf);
command->group_name_len = command->group_name_len =
length_until_newline(token, p_len - (token - p)) + 1; length_until_newline(token, p_len - (token - linebuf)) + 1;
command->type = COMMAND_G; command->type = COMMAND_G;
return true; return true;
@@ -1117,9 +1105,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// @todo { multiple object name? } // @todo { multiple object name? }
token += 2; token += 2;
command->object_name = token; command->object_name = p + (token - linebuf);
command->object_name_len = command->object_name_len =
length_until_newline(token, p_len - (token - p)) + 1; length_until_newline(token, p_len - (token - linebuf)) + 1;
command->type = COMMAND_O; command->type = COMMAND_O;
return true; return true;