#include "replacer.h" #include "Logger.h" #include 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(""); while (snippetContent[tokenEnd] != '\n') { tokenEnd++; } s.append(&snippetContent[tokenStart], tokenEnd - tokenStart); s.append(""); } else if (isType(&snippetContent[tokenStart], tokenLength)) { s.append(""); s.append(&snippetContent[tokenStart], tokenLength); s.append(""); } 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, ""); s.insert(0, "
");
        s.append("");
        s.append("
"); delete [] snippetContent; strContent.replace(startFound, endFound - startFound, s); } while (true); return strContent; }