-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVertexNormalElement.cpp
More file actions
49 lines (43 loc) · 1.77 KB
/
VertexNormalElement.cpp
File metadata and controls
49 lines (43 loc) · 1.77 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
//
// Created by rthier on 2016.04.11..
//
//#include "objmasterlog.h"
#include "VertexNormalElement.h"
#include <cstring> /* strtok_r */
#include <cstdlib> /* atof */
#include "ObjCommon.h"
#include "wincompat.h" // msvc hax
namespace ObjMaster {
bool VertexNormalElement::isParsable(const char *fields) {
// Not an empty string, the first character is a 'v' the second is an 'n' and then a space
return (fields != nullptr) && (fields[0] != 0) && (fields[0] == 'v') && (fields[1] == 'n')
&& fields[2] == ' ';
}
VertexNormalElement::VertexNormalElement(const char *fields) {
// The strtok_r changes the string so we need to duplicate it
char copy[256]; // The line never should be longer than this anyways...
copy[255] = 0; // ensure terminator
strncpy(copy, fields, 254);
constructionHelper(copy);
// Remark: The original code below makes very small malloc and free pairs and fragment memory!
// char *copy = strdup(fields);
// constructionHelper(copy);
// free(copy);
}
// This variant modifies the provided 'string' but is it faster as it do this without the copy
VertexNormalElement::VertexNormalElement(char *fields) {
constructionHelper(fields);
}
void VertexNormalElement::constructionHelper(char *fields) {
if (isParsable(fields)) {
char *savePtr;
char *key = strtok_r(fields, OBJ_DELIMITER, &savePtr);
char *xStr = strtok_r(nullptr, OBJ_DELIMITER, &savePtr);
char *yStr = strtok_r(nullptr, OBJ_DELIMITER, &savePtr);
char *zStr = strtok_r(nullptr, OBJ_DELIMITER, &savePtr);
x = (float)atof(xStr);
y = (float)atof(yStr);
z = (float)atof(zStr);
}
}
}