Skip to content

Commit

Permalink
Add -H option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtheall committed Oct 10, 2017
1 parent b32544b commit 0176621
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Usage: ./tex3ds [OPTIONS...] <input>
Options:
-f, --format <format> See "Format Options"
-H, --header <file> Output C header to file
-h, --help Show this help message
-i, --include <file> Include options from file
-m, --mipmap <filter> Generate mipmaps. See "Mipmap Filter Options"
Expand Down
9 changes: 7 additions & 2 deletions include/subimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "magick_compat.h"
#include <string>
#include <vector>
#include <libgen.h>

struct SubImage
{
Expand All @@ -35,8 +36,12 @@ struct SubImage
float bottom; ///< Bottom v-coordinate

SubImage(size_t index, const std::string &name, float left, float top, float right, float bottom)
: index(index), name(name), left(left), top(top), right(right), bottom(bottom)
{ }
: index(index), left(left), top(top), right(right), bottom(bottom)
{
std::vector<char> path(name.begin(), name.end());
path.push_back(0);
this->name = ::basename(path.data());
}

bool operator<(const SubImage &rhs) const
{
Expand Down
77 changes: 76 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ enum ProcessingMode
PROCESS_SKYBOX, ///< Skybox
};

/** @brief Header path option */
std::string header_path;

/** @brief Output path option */
std::string output_path;

Expand Down Expand Up @@ -1179,6 +1182,70 @@ void write_output_data()
std::fclose(fp);
}

/** @brief Sanitize identifier
*/
void sanitize_identifier(std::string &id)
{
if(!std::isalnum(id[0]) && id[0] != '_')
id.insert(0, 1, '_');

for(size_t i = 0; i < id.size(); ++i)
{
if(!std::isalnum(id[i]) && id[i] != '_')
id[i] = '_';
}
}

/** @brief Write header
*/
void write_header()
{
// check if we need to output the header
if(header_path.empty())
return;

FILE *fp = std::fopen(header_path.c_str(), "w");
if(!fp)
throw std::runtime_error("Failed to open output header");

std::fprintf(fp, "/* Generated by tex3ds */\n");
std::fprintf(fp, "#pragma once\n\n");

{
std::vector<char> path(header_path.begin(), header_path.end());
path.push_back(0);
header_path = ::basename(path.data());
}

std::string::size_type pos = header_path.rfind('.');
if(pos != std::string::npos)
header_path.resize(pos);

sanitize_identifier(header_path);

for(size_t i = 0; i < subimage_data.size(); ++i)
{
std::string label = subimage_data[i].name;

pos = label.rfind('.');
if(pos != std::string::npos)
label.resize(pos);

sanitize_identifier(label);

label += "_idx";

if(label[0] != '_')
label.insert(0, 1, '_');

std::fprintf(fp, "#define %s%s %zu\n",
header_path.c_str(), label.c_str(), i);
}

// close output header
std::fclose(fp);
}

/** @brief Print version information */
void print_version()
{
Expand Down Expand Up @@ -1210,6 +1277,7 @@ void print_usage(const char *prog)
std::printf(
" Options:\n"
" -f, --format <format> See \"Format Options\"\n"
" -H, --header <file> Output C header to file\n"
" -h, --help Show this help message\n"
" -i, --include <file> Include options from file\n"
" -m, --mipmap <filter> Generate mipmaps. See \"Mipmap Filter Options\"\n"
Expand Down Expand Up @@ -1422,7 +1490,7 @@ ParseStatus parseOptions(const std::string &cwd, std::vector<char*> &args)

// parse options
while((c = ::getopt_long(args.size(), args.data(),
"f:hi:m:o:p:q:rs:vz:", long_options, nullptr)) != -1)
"f:H:hi:m:o:p:q:rs:vz:", long_options, nullptr)) != -1)
{
switch(c)
{
Expand Down Expand Up @@ -1456,6 +1524,10 @@ ParseStatus parseOptions(const std::string &cwd, std::vector<char*> &args)
break;
}

case 'H':
header_path = getPath(cwd, optarg);
break;

case 'h':
// show help
print_usage(prog);
Expand Down Expand Up @@ -1680,6 +1752,9 @@ int main(int argc, char *argv[])

// write output data
write_output_data();

// write header
write_header();
}
catch(const std::exception &e)
{
Expand Down

0 comments on commit 0176621

Please sign in to comment.