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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <optional>
#include <vector>
//==============================================================================
struct FileHelpers
{
static std::string getCurrentWorkingDirectory()
{
std::vector<char> buffer (1024);
while (getcwd (buffer.data(), buffer.size() - 1) == nullptr && errno == ERANGE)
buffer.resize (buffer.size() * 2 / 3);
return { buffer.data() };
}
static bool endsWith (const std::string& s, char c)
{
if (s.length() == 0)
return false;
return *s.rbegin() == c;
}
static std::string appendedPaths (const std::string& first, const std::string& second)
{
return endsWith (first, '/') ? first + second : first + "/" + second;
}
static bool exists (const std::string& path)
{
return ! path.empty() && access (path.c_str(), F_OK) == 0;
}
static bool deleteFile (const std::string& path)
{
if (! exists (path))
return true;
return remove (path.c_str()) == 0;
}
static std::string getFilename (const std::string& path)
{
return { std::find_if (path.rbegin(), path.rend(), [] (auto c) { return c == '/'; }).base(),
path.end() };
}
static bool isDirectory (const std::string& path)
{
#if defined (__FreeBSD__) || defined (__OpenBSD__)
#define JUCE_STAT stat
#else
#define JUCE_STAT stat64
#endif
struct JUCE_STAT info;
return ! path.empty()
&& JUCE_STAT (path.c_str(), &info) == 0
&& ((info.st_mode & S_IFDIR) != 0);
}
static std::string getParentDirectory (const std::string& path)
{
std::string p { path.begin(),
std::find_if (path.rbegin(),
path.rend(),
[] (auto c) { return c == '/'; }).base() };
// Trim the ending slash, but only if not root
if (endsWith (p, '/') && p.length() > 1)
return { p.begin(), p.end() - 1 };
return p;
}
static bool createDirectory (const std::string& path)
{
if (isDirectory (path))
return true;
const auto parentDir = getParentDirectory (path);
if (path == parentDir)
return false;
if (createDirectory (parentDir))
return mkdir (path.c_str(), 0777) != -1;
return false;
}
};
//==============================================================================
struct StringHelpers
{
static bool isQuoteCharacter (char c)
{
return c == '"' || c == '\'';
}
static std::string unquoted (const std::string& str)
{
if (str.length() == 0 || (! isQuoteCharacter (str[0])))
return str;
return str.substr (1, str.length() - (isQuoteCharacter (str[str.length() - 1]) ? 1 : 0));
}
static void ltrim (std::string& s)
{
s.erase (s.begin(), std::find_if (s.begin(), s.end(), [] (int c) { return ! std::isspace (c); }));
}
static void rtrim (std::string& s)
{
s.erase (std::find_if (s.rbegin(), s.rend(), [] (int c) { return ! std::isspace (c); }).base(), s.end());
}
static std::string trimmed (const std::string& str)
{
auto result = str;
ltrim (result);
rtrim (result);
return result;
}
static std::string replaced (const std::string& str, char charToReplace, char replaceWith)
{
auto result = str;
std::replace (result.begin(), result.end(), charToReplace, replaceWith);
return result;
}
};
//==============================================================================
static bool addFile (const std::string& filePath,
const std::string& binaryNamespace,
std::ofstream& headerStream,
std::ofstream& cppStream,
bool verbose)
{
std::ifstream fileStream (filePath, std::ios::in | std::ios::binary | std::ios::ate);
if (! fileStream.is_open())
{
std::cerr << "Failed to open input file " << filePath << std::endl;
return false;
}
std::vector<char> buffer ((size_t) fileStream.tellg());
fileStream.seekg (0);
fileStream.read (buffer.data(), static_cast<std::streamsize> (buffer.size()));
const auto variableName = StringHelpers::replaced (StringHelpers::replaced (FileHelpers::getFilename (filePath),
' ',
'_'),
'.',
'_');
if (verbose)
{
std::cout << "Adding " << variableName << ": "
<< buffer.size() << " bytes" << std::endl;
}
headerStream << " extern const char* " << variableName << ";" << std::endl
<< " const int " << variableName << "Size = "
<< buffer.size() << ";" << std::endl;
cppStream << "static const unsigned char temp0[] = {";
auto* data = (const uint8_t*) buffer.data();
for (size_t i = 0; i < buffer.size() - 1; ++i)
{
cppStream << (int) data[i] << ",";
if ((i % 40) == 39)
cppStream << std::endl << " ";
}
cppStream << (int) data[buffer.size() - 1] << ",0,0};" << std::endl;
cppStream << "const char* " << binaryNamespace << "::" << variableName
<< " = (const char*) temp0" << ";" << std::endl << std::endl;
return true;
}
//==============================================================================
class Arguments
{
public:
enum class PositionalArguments
{
sourceFile = 0,
targetDirectory,
targetFilename,
binaryNamespace
};
static std::optional<Arguments> create (int argc, char* argv[])
{
std::vector<std::string> arguments;
bool verbose = false;
for (int i = 1; i < argc; ++i)
{
std::string arg { argv[i] };
if (arg == "-v" || arg == "--verbose")
verbose = true;
else
arguments.emplace_back (std::move (arg));
}
if (arguments.size() != static_cast<size_t> (PositionalArguments::binaryNamespace) + 1)
return std::nullopt;
return Arguments { std::move (arguments), verbose };
}
std::string get (PositionalArguments argument) const
{
return arguments[static_cast<size_t> (argument)];
}
bool isVerbose() const
{
return verbose;
}
private:
Arguments (std::vector<std::string> args, bool verboseIn)
: arguments (std::move (args)), verbose (verboseIn)
{
}
std::vector<std::string> arguments;
bool verbose = false;
};
//==============================================================================
int main (int argc, char* argv[])
{
const auto arguments = Arguments::create (argc, argv);
if (! arguments.has_value())
{
std::cout << " Usage: SimpleBinaryBuilder [-v | --verbose] sourcefile targetdirectory targetfilename namespace"
<< std::endl << std::endl
<< " SimpleBinaryBuilder will encode the provided source file into" << std::endl
<< " two files called (targetfilename).cpp and (targetfilename).h," << std::endl
<< " which it will write into the specified target directory." << std::endl
<< " The target directory will be automatically created if necessary. The binary" << std::endl
<< " resource will be available in the given namespace." << std::endl << std::endl;
return 0;
}
const auto currentWorkingDirectory = FileHelpers::getCurrentWorkingDirectory();
using ArgType = Arguments::PositionalArguments;
const auto sourceFile = FileHelpers::appendedPaths (currentWorkingDirectory,
StringHelpers::unquoted (arguments->get (ArgType::sourceFile)));
if (! FileHelpers::exists (sourceFile))
{
std::cerr << "Source file doesn't exist: "
<< sourceFile
<< std::endl << std::endl;
return 1;
}
const auto targetDirectory = FileHelpers::appendedPaths (currentWorkingDirectory,
StringHelpers::unquoted (arguments->get (ArgType::targetDirectory)));
if (! FileHelpers::exists (targetDirectory))
{
if (! FileHelpers::createDirectory (targetDirectory))
{
std::cerr << "Failed to create target directory: " << targetDirectory << std::endl;
return 1;
}
}
const auto className = StringHelpers::trimmed (arguments->get (ArgType::targetFilename));
const auto binaryNamespace = StringHelpers::trimmed (arguments->get (ArgType::binaryNamespace));
const auto headerFilePath = FileHelpers::appendedPaths (targetDirectory, className + ".h");
const auto cppFilePath = FileHelpers::appendedPaths (targetDirectory, className + ".cpp");
if (arguments->isVerbose())
{
std::cout << "Creating " << headerFilePath
<< " and " << cppFilePath
<< " from file " << sourceFile
<< "..." << std::endl << std::endl;
}
if (! FileHelpers::deleteFile (headerFilePath))
{
std::cerr << "Failed to remove old header file: " << headerFilePath << std::endl;
return 1;
}
if (! FileHelpers::deleteFile (cppFilePath))
{
std::cerr << "Failed to remove old source file: " << cppFilePath << std::endl;
return 1;
}
std::ofstream header (headerFilePath);
if (! header.is_open())
{
std::cerr << "Failed to open " << headerFilePath << std::endl;
return 1;
}
std::ofstream cpp (cppFilePath);
if (! cpp.is_open())
{
std::cerr << "Failed to open " << headerFilePath << std::endl;
return 1;
}
header << "/* (Auto-generated binary data file). */" << std::endl << std::endl
<< "#pragma once" << std::endl << std::endl
<< "namespace " << binaryNamespace << std::endl
<< "{" << std::endl;
cpp << "/* (Auto-generated binary data file). */" << std::endl << std::endl
<< "#include " << std::quoted (className + ".h") << std::endl << std::endl;
if (! addFile (sourceFile, binaryNamespace, header, cpp, arguments->isVerbose()))
return 1;
header << "}" << std::endl << std::endl;
return 0;
}
|