Last active
October 25, 2018 18:09
-
-
Save fallahn/48fef3017d76e7fb6f9435f4dcb6724e to your computer and use it in GitHub Desktop.
Build number generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Simple program which generates a header file containing a build number. | |
If the header file is found to exist then the number is incremented and file updated. | |
If the file is unparsable in some way then it is discarded and a new file starting | |
with the build number 0 is written. | |
Include the generated binary as part of your build chain - ie execute it in the | |
pre-compile stage - and include the resulting output in your source code. | |
*/ | |
#include <fstream> | |
#include <string> | |
#include <iostream> | |
const std::string LineMatch("BUILDNUMBER"); | |
int main() | |
{ | |
std::ifstream file; | |
file.open("buildnumber.hpp"); | |
int buildNumber = 0; | |
if (file.good() && file.is_open()) | |
{ | |
std::string line; | |
while (std::getline(file, line)) | |
{ | |
auto pos = line.find(LineMatch); | |
if (pos != std::string::npos) | |
{ | |
try | |
{ | |
buildNumber = std::stoi(line.substr(pos + LineMatch.size() + 1)); | |
buildNumber++; | |
std::cout << "Build number set to: " << buildNumber << "\n"; | |
break; | |
} | |
catch(...) | |
{ | |
//don't do anything we'll just overwrite the file starting at 0 again | |
} | |
} | |
} | |
} | |
file.close(); | |
//just overwrite the old file | |
std::ofstream oFile; | |
oFile.open("buildnumber.hpp"); | |
if (oFile.good() && oFile.is_open()) | |
{ | |
oFile << "#pragma once\n"; | |
oFile << "#define BUILDNUMBER " << buildNumber << "\n"; | |
oFile << "#define BUILDNUMBER_STR \"" << buildNumber << "\"\n"; | |
} | |
oFile.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment