Skip to content

Instantly share code, notes, and snippets.

@fallahn
Last active October 25, 2018 18:09
Show Gist options
  • Save fallahn/48fef3017d76e7fb6f9435f4dcb6724e to your computer and use it in GitHub Desktop.
Save fallahn/48fef3017d76e7fb6f9435f4dcb6724e to your computer and use it in GitHub Desktop.
Build number generator
/*
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