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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include "replacer.h"
#include "Logger.h"
#include <ctype.h>
const char* keywords[] = {
"cosnt", "struct", "static"
};
const char* types[] = {
"void",
"int", "int8", "int16", "int32", "int64",
"float", "float8", "float16", "float32", "float64",
"long",
"double",
"bool",
"Vector2", "Vector3", "Vector4",
"struct", "class"
};
const char* commentType = "//";
bool isType(char* str, int size) {
if (size == 0) {
return false;
}
for (int i = 0; i < sizeof(types) / sizeof(char*); i++) {
if (size == strlen(types[i]) && strncmp(str, types[i], size) == 0) {
return true;
}
}
return false;
}
bool isTokenDelim(char c) {
return isspace(c) || c == '\0' || c == ';' || c == ',';
}
// @TODO: This has brought me to two realizations:
// 1) C++ string manipulaton is difficult for no apparent reason
// 2) My current implementation of String is lacking so many features that I would want
// 3) I have no plans to update my current implementation any time soon, so I will
// begrudginly give my heart and soul over to the standard library.
std::string insertCodeSnippets(char* workingDirectory, char* bodyContent) {
std::string strWorkingDirectory (workingDirectory);
strWorkingDirectory = strWorkingDirectory.substr(0, strWorkingDirectory.find_last_of("/") + 1);
std::string strContent(bodyContent);
size_t found = -1;
do {
found = strContent.find("#SNIPPET", found + 1);
if (found == std::string::npos) {
break;
}
int startFound = found;
found += strlen("#SNIPPET ");
std::string fileName(strWorkingDirectory);
while (!isspace(strContent[found])) {
fileName += strContent[found];
found++;
}
size_t endFound = found + 1;
FILE* snippetFile = fopen(fileName.c_str(), "r+");
if (snippetFile == NULL) {
logger_warning("Could not find snippet: %s", fileName.c_str());
continue;
}
fseek(snippetFile, 0, SEEK_END);
long fsize = ftell(snippetFile);
fseek(snippetFile, 0, SEEK_SET);
char* snippetContent = new char[fsize + 1];
fread(snippetContent, 1, fsize, snippetFile);
snippetContent[fsize] = '\0';
std::string s;
int tokenStart = 0, tokenEnd = 0;
while (snippetContent[tokenEnd] != '\0') {
while (!isTokenDelim(snippetContent[tokenEnd])) {
tokenEnd++;
}
int tokenLength = (tokenEnd - tokenStart);
if (tokenLength == strlen(commentType) && strncmp(&snippetContent[tokenStart], commentType, tokenLength) == 0) {
// @NOTE: Assuming comments are always on a single line
s.append("<span class=\"code_comment\">");
while (snippetContent[tokenEnd] != '\n') {
tokenEnd++;
}
s.append(&snippetContent[tokenStart], tokenEnd - tokenStart);
s.append("</span>");
} else if (isType(&snippetContent[tokenStart], tokenLength)) {
s.append("<span class=\"code_keyword\">");
s.append(&snippetContent[tokenStart], tokenLength);
s.append("</span>");
} else {
s.append(&snippetContent[tokenStart], tokenLength);
}
s += snippetContent[tokenEnd];
tokenStart = tokenEnd + 1;
tokenEnd = tokenStart;
}
while (s[0] == '\n') {
s.erase(0, 1);
}
s.insert(0, "<code>");
s.insert(0, "<pre>");
s.append("</code>");
s.append("</pre>");
delete [] snippetContent;
strContent.replace(startFound, endFound - startFound, s);
} while (true);
return strContent;
}
|