-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFaceElement.cpp
More file actions
68 lines (60 loc) · 2.28 KB
/
FaceElement.cpp
File metadata and controls
68 lines (60 loc) · 2.28 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
//
// Created by rthier on 2016.04.11..
//
#include "FaceElement.h"
#include <cstring> /* strtok_r */
#include "ObjCommon.h"
#include "wincompat.h" // msvc hax
namespace ObjMaster {
bool FaceElement::isParsable(const char *fields) {
// Not an empty string, the first character is an 'f' the second is a ' '
return (fields != nullptr) && (fields[0] != 0) && (fields[0] == 'f') && (fields[1] == ' ');
}
FaceElement::FaceElement(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 it is faster as it do this without the copy
FaceElement::FaceElement(char *fields) {
constructionHelper(fields);
}
// Common parts of constructors
void FaceElement::constructionHelper(char* fields) {
if (isParsable(fields)) {
// Count delimiters
// The first delimiter delimits the 'f' and the first FacePoint
// so to get the number of facePoints all we need to do is to count the delimiters!
int delimCount = 0;
for (int i = 0; (fields[i] != 0); ++i) {
if (fields[i] == OBJ_DELIMITER[0]) {
++delimCount;
}
}
// Tokenization
char *savePtr;
// The 'f' is the first token...
char *key = strtok_r(fields, OBJ_DELIMITER, &savePtr);
// This both sets the facePointCount and fills in the face-points
// The facePointCount can be less than the delimCount when we handle only 3 points and
// there are more delimiters than that!
for (facePointCount = 0;
facePointCount < delimCount && facePointCount < MAX_FACEPOINT_COUNT;
++facePointCount) {
// Extract the currently latest FacePoint
char *facePointStr = strtok_r(nullptr, OBJ_DELIMITER, &savePtr);
// Parse the given facePoint and save it - with added extra check
// in case of nasty errors that should never be there!
if (facePointStr != nullptr) {
facePoints[facePointCount] = FacePoint(facePointStr, true);
}
}
}
}
}