mirror of
https://github.com/isledecomp/actionheadergen.git
synced 2024-11-23 07:47:58 -05:00
generate special header for forward declarations
This commit is contained in:
parent
ade9bd1d3f
commit
0c9856fbbb
3 changed files with 68 additions and 23 deletions
|
@ -7,7 +7,9 @@
|
|||
|
||||
class HeaderGenerator {
|
||||
public:
|
||||
bool CreateForwardDeclHeader(char *p_outputDir);
|
||||
bool GenerateHeader(char *p_interleafName, std::map<size_t, std::string> *p_actionMap, char *p_outputDir);
|
||||
std::ofstream m_declFout;
|
||||
|
||||
private:
|
||||
bool CreateHeader(char *p_interleafName, char *p_outputDir);
|
||||
|
|
|
@ -16,11 +16,21 @@ const char *g_enumEntryPrefix = "c_";
|
|||
// notice to not edit autogenerated headers
|
||||
const char *g_doNotEditNotice = "// This file was automatically generated by the actionheadergen tool.\n// Please do not manually edit this file.\n";
|
||||
|
||||
bool HeaderGenerator::CreateForwardDeclHeader(char *p_outputDir)
|
||||
{
|
||||
// attempt to create special forward decl header
|
||||
if (!CreateHeader("", p_outputDir)) {
|
||||
printf("Failed to create forward declaration header, check file permissions?\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HeaderGenerator::GenerateHeader(char *p_interleafName, std::map<size_t, std::string> *p_actionMap, char *p_outputDir)
|
||||
{
|
||||
// attempt to create header file
|
||||
if (!CreateHeader(p_interleafName, p_outputDir)) {
|
||||
printf("Failed to create header, check file permissions?\n");
|
||||
printf("Failed to create Interleaf header, check file permissions?\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -38,35 +48,55 @@ bool HeaderGenerator::CreateHeader(char *p_interleafName, char *p_outputDir)
|
|||
{
|
||||
char headerName[1024];
|
||||
char outputPath[2048];
|
||||
bool decl = false;
|
||||
|
||||
strcpy(m_normalizedInlfName, p_interleafName);
|
||||
if (strcmp(p_interleafName, "") != 0) {
|
||||
strcpy(m_normalizedInlfName, p_interleafName);
|
||||
|
||||
// format Interleaf name to mostly lowercase,
|
||||
// to make acceptable by decomp styling guidelines
|
||||
for (int i = 1; i < strlen(p_interleafName); i++) {
|
||||
m_normalizedInlfName[i] = tolower(m_normalizedInlfName[i]);
|
||||
// format Interleaf name to mostly lowercase,
|
||||
// to make acceptable by decomp styling guidelines
|
||||
for (int i = 1; i < strlen(p_interleafName); i++) {
|
||||
m_normalizedInlfName[i] = tolower(m_normalizedInlfName[i]);
|
||||
}
|
||||
|
||||
// set the header name to the Interleaf name
|
||||
strcpy(headerName, m_normalizedInlfName);
|
||||
|
||||
// set first character to lowercase, which
|
||||
// was skipped by the previous operation
|
||||
headerName[0] = tolower(headerName[0]);
|
||||
|
||||
// append the generic extension
|
||||
strcat(headerName, g_headerExt);
|
||||
}
|
||||
else {
|
||||
decl = true;
|
||||
strcpy(headerName, "actionsfwd.h");
|
||||
}
|
||||
|
||||
// set the header name to the Interleaf name
|
||||
strcpy(headerName, m_normalizedInlfName);
|
||||
|
||||
// set first character to lowercase, which
|
||||
// was skipped by the previous operation
|
||||
headerName[0] = tolower(headerName[0]);
|
||||
|
||||
// append the generic extension
|
||||
strcat(headerName, g_headerExt);
|
||||
|
||||
// generate the full path of the output file
|
||||
strcpy(outputPath, p_outputDir);
|
||||
strcat(outputPath, "/");
|
||||
strcat(outputPath, headerName);
|
||||
|
||||
m_fout.open(outputPath);
|
||||
if (!decl) {
|
||||
m_fout.open(outputPath);
|
||||
|
||||
// make sure we can actually create this file
|
||||
if (!m_fout.is_open()) {
|
||||
return false;
|
||||
// make sure we can actually create this file
|
||||
if (!m_fout.is_open()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_declFout.open(outputPath);
|
||||
|
||||
// make sure we can actually create this file
|
||||
if (!m_declFout.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_declFout << g_doNotEditNotice;
|
||||
m_declFout << "#ifndef ACTIONSFWD_H\n#define ACTIONSFWD_H\n\n";
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -96,12 +126,15 @@ bool HeaderGenerator::WriteHeader(char *p_interleafName, std::map<size_t, std::s
|
|||
|
||||
// we need to scope this enum to avoid conflicts
|
||||
m_fout << "namespace " << m_normalizedInlfName << "Script\n{\n";
|
||||
m_declFout << "namespace " << m_normalizedInlfName << "Script\n{\n";
|
||||
|
||||
// add ifdefs for conditional compilation (decomp requirement)
|
||||
m_fout << "#ifdef COMPAT_MODE\nenum Script : int {\n#else\n";
|
||||
m_declFout << "#ifdef COMPAT_MODE\nenum Script : int;\n#else\n";
|
||||
|
||||
// declare enum
|
||||
m_fout << "enum Script {\n#endif\n";
|
||||
m_declFout << "enum Script;\n#endif\n";
|
||||
|
||||
// add generic "none" state to the enum
|
||||
m_fout << "\tc_none" << m_normalizedInlfName << " = -1,\n\n";
|
||||
|
@ -121,7 +154,8 @@ bool HeaderGenerator::WriteHeader(char *p_interleafName, std::map<size_t, std::s
|
|||
if (i == 2000) {
|
||||
// some compilers don't accept extremely large enums,
|
||||
// so we split them up here in case of 2000+ entries
|
||||
m_fout << "};\n\n#ifdef COMPAT_MODE\nenum Script2 : int {\n#else\nenum Script2 {\n#endif\n";
|
||||
m_fout << "};\n\n#ifdef COMPAT_MODE\nenum Script2 : int {\n#else\nenum Script2 {\n#endif\n";
|
||||
m_declFout << "\n#ifdef COMPAT_MODE\nenum Script2 : int;\n#else\nenum Script2;\n#endif\n";
|
||||
}
|
||||
|
||||
// actually write the enum entry
|
||||
|
@ -137,6 +171,7 @@ bool HeaderGenerator::WriteHeader(char *p_interleafName, std::map<size_t, std::s
|
|||
|
||||
// all done with actions, so lets close the enum and namespace
|
||||
m_fout << "};\n} // namespace " << m_normalizedInlfName << "Script\n\n";
|
||||
m_declFout << "} // namespace " << m_normalizedInlfName << "\n\n";
|
||||
|
||||
// finally, close the include guard
|
||||
m_fout << "#endif // " << guardName << "\n";
|
||||
|
|
12
src/main.cpp
12
src/main.cpp
|
@ -80,6 +80,13 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
else interleafFiles.push_back(filePath); // we were provided a single file, but we'll still use the vector
|
||||
|
||||
// generate special forward declaration header
|
||||
HeaderGenerator hgenerator;
|
||||
|
||||
if (!hgenerator.CreateForwardDeclHeader(outputDir)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// iterate through every Interleaf in the vector and perform our operations
|
||||
for (int i = 0; i < interleafFiles.size(); i++) {
|
||||
|
||||
|
@ -116,8 +123,6 @@ int main(int argc, char *argv[])
|
|||
filename = filename + 1;
|
||||
}
|
||||
|
||||
HeaderGenerator hgenerator;
|
||||
|
||||
// generate the actual header
|
||||
// we use the Interleaf name for the filename
|
||||
// and the vector previously generated to make the enum data
|
||||
|
@ -134,6 +139,9 @@ int main(int argc, char *argv[])
|
|||
|
||||
printf("Successfully generated header for %s\n", filename);
|
||||
}
|
||||
|
||||
hgenerator.m_declFout << "#endif // ACTIONSFWD_H";
|
||||
hgenerator.m_declFout.close();
|
||||
|
||||
printf("Finished!\n");
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue