GithubHelp home page GithubHelp logo

end2endzone / bin2cpp Goto Github PK

View Code? Open in Web Editor NEW
74.0 74.0 8.0 2.37 MB

bin2cpp: The easiest way to embed small files into a c++ executable. bin2cpp converts text or binary files to C++ files (*.h, *.cpp) for easy access within the code.

Home Page: http://www.end2endzone.com/bin2cpp-the-easiest-way-to-embed-small-files-into-a-c-executable/

License: MIT License

Batchfile 4.02% C++ 69.44% PowerShell 0.24% CMake 16.23% Shell 8.63% Python 1.44%
cpp embedded-files ressources

bin2cpp's People

Contributors

end2endzone avatar matthewscholefield avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bin2cpp's Issues

Application icon

The repository contains an application icon. Create bin2cpp.rc file to add the bin2cpp icon file into bin2cpp executable as a resource to show an icon when viewed from File Explorer on Windows platform.

Disable steps that requires secrets in GitHub Action workflows

The 3 GitHub Action workflows for this projects builds and executes tests on multiple platforms. The workflows can be summarized as follows:

  1. At the beginning of the workflow, there is a step that uploads a "test incomplete" badge to one of my gist. This is to override the previous existing badge in case the workflow encounters an error.
  2. Build and run unit tests
  3. At the end of the workflow, a step uploads a green "53 passed" badge such as image to the same gist. This badge overrides the one uploaded at step 1.

Since these 2 steps require the use of secrets environment variable to get write access to the gist.

The problem is they fails when executed as a PR checks because (from the project's Actions secrets page) : "Secrets are not passed to workflows that are triggered by a pull request from a fork. Learn more.".

Using `.hpp` for header filename causes `.cpppp` for source file

It makes sense that foo.h leads to foo.cpp but foo.hpp now also leads to foo.cpp.

Command:

$ dd if=/dev/urandom bs=1M count=10 of=data.bin
$ bin2cpp --file=data.bin --headerfile="foo.hpp" --output=. --identifier=Foo
bin2cpp v2.4.0 - Convert binary files into C++ source code.
Copyright (C) 2013-2021 end2endzone.com. All rights reserved.
bin2cpp is open source software, see http://github.com/end2endzone/bin2cpp 
Embedding "data.bin"...
Writing file "./foo.hpp"...
Writing file "./foo.cpppp"...

Add pre-build linux binaries.

A good addition to the project would be to provide pre-build binaries for linux. They could be downloaded as a portable version (in a compressed file).

The INSTALL documentation could also be updated to specify which platforms are supported.

Github don't identify the repository LICENSE as MIT.

Github do not identify the license of the application as MIT. This is observable when looking at the main application page as it says View license next to the "justice" icon. It would normally say "MIT license".

I think this is because the repository contains multiple files called LICENSE:

  • LICENSE (the official license file)
  • LICENSE.h (the c++ template to use in source code files)

Clarify the expected usage of bin2cpp.

The README.md file do not explain how bin2cpp is indented to be used. More documentation and examples should be created.

  1. In the header of the readme, a note should be added to explain something like "When executed, bin2cpp takes a binary file and outputs c++ code (a function) that when called allows a c++ program to retrieve the content of the input binary file".

  2. The project description "bin2cpp converts text or binary files to C++ files (*.h, *.cpp) for easy access within the code." should also appears in the README.md file.

  3. A section called bin2cpp worlflow which explains how one can use bin2cpp to embed a binary file could be created.

Update command line parameters syntax with the --name=value syntax

Rename arguments fields:

  • inputfile -> file
  • output folder -> output
  • header filename -> headerfile
  • function identifier -> identifier
  • chunk size -> chunksize

ie:
--file=/path/to/input/file
--output=/path/to/output/folder
--headerfile=SplashScreen.h
--identifier=SplashScreen
--chunksize=200

Allow generating into subfolder

Suppose I want to generated files in a subfolder like:

example
├── CMakeLists.txt
└── src
    ├── generated
    │   ├── foo-data.cpp
    │   └── foo-data.h
    └── main.cpp

From main.cpp I would want to use #include "generated/foo-data.h", so I would not add src/generated as a compiler include directive. However, this would cause compilation of foo-data.cpp to error with the #include "foo-data.h".

It would be nice to support generating into a subfolder where the #include "{headerFile}" within the cpp file references it.

I tried this but it didn't work:

dd if=/dev/urandom bs=1M count=10 of=foo-data.bin
bin2cpp --file=foo-data.bin --headerfile="generated/foo-data.h" --output=. --identifier=TorchScript

Support getfilePath api and handle same function identifier

We used bin2cpp to process our SPA single page application resources in a folder (html\css\js).

We have two issues.

  1. For the generation method in a folder, we can‘t get the path of the original file. So we created a new API called getFilePath in File class.
    E.g:
virtual const char * getFilename() const { return "app.4581e082.css"; }
virtual const char * getFilepath() const { return "static/css/app.4581e082.css"; }
  1. call GetFilenameWithoutExtension double times in processInputDirectory, which causes the following files to be overwritten (both a.h):
static/js/A.123.js
static/css/A.456.css

We remove one GetFilenameWithoutExtension call in getFunctionIdentifierFromPath.

  1. The following situation may lead to ths same function identifier (e.g. class A123), leading to compilation errors (redeclaration)
static/js/A.123.js
static/css/A.123.css

We use a map to save identifier times, which is solved by appending increase index, such as class A123_1 and class A123_2, although it is not very elegant.

Remove the concept of segment in the interface

Try to get rid of segments. Segment may not be required.

Try to define data a single multi-line strings like the following:

const char * buffer = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempus gravida ipsum, vel venenatis metus pellentesque eu. In cursus et arcu vitae tempor. Aenean eu arcu condimentum, fringilla felis eget, accumsan quam. Mauris ut vehicula nisi. Phasellus vulputate dictum leo sit amet suscipit. In hac habitasse platea dictumst. Donec a fringilla ipsum, eu molestie nisl. Vivamus faucibus, orci vel gravida condimentum, sapien urna venenatis augue, "
                      "non consectetur augue orci eget dolor.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempus gravida ipsum, vel venenatis metus pellentesque eu. In cursus et arcu vitae tempor. Aenean eu arcu condimentum, fringilla felis eget, accumsan quam. Mauris ut vehicula nisi. Phasellus vulputate dictum leo sit amet suscipit. In hac habitasse platea dictumst. Donec a fringilla ipsum, eu molestie nisl. Vivamus faucibus, orci vel gravida cond"
                      "imentum, sapien urna venenatis augue, non consectetur augue orci eget dolor.";

Generate only c code.

It might be a good idea to enable the generation of c-only code.
In other words, one could configure the application to generate code that do not rely on c++ classes or std classes. This feature would provide greater compatibility with other compilers with multiple environment and platforms.

Simplify building process with scripts and INSTALL documentation.

The build process should be simplified:

  • Scripts for AppVeyor and Tracis CI should not be the scripts used for building explained "up front". This is not intuitive.
  • The documentation should be easier to follow for a new developer that want to start working on the project.
  • Users runs on Linux , Windows or macOS and are usually expecting different build instructions specific for their OS. The best option to follow this rule would be to create linux and windows directories under /ci directory. Each directory should contains the required scripts for these operating systems.
  • Try to have "one click build".

Generate only c++ code

The actual generated code is a mixture of c and c++ code. It is perfectly compatible with c++. However, for aesthetic reasons and since the application is called bin2cpp, it might be preferable to only generate c++ code.

Remove duplicated code that is now available in RapidAssit

There are duplicated functions in bin2cpp that are already available in RapidAssist. When migration to a newer version of RapidAssist, the code in bin2cpp was not sanitized.

For example :

  • getFilenameWithoutExtension()
  • getCopyrightYear()

Modify how 'override' argument is implemented

The current implementation defines --override as the following:

  • If *.h or *.cpp file exists,
  • Compute MD5 of input file and compare with MD5 found in *.cpp file.
  • If MD5 compare is the same, skip file generation
  • Else, look for the --override flag for deciding if we should regenerate the output file.

This makes the --override flag useless if the MD5 in the cpp file is not changed while the content of the *.cpp file is changed.

In other words, --override should always override the output files

The implementation should be changed as the following:

  • By default, the application stores the input file 'modified date' into the generated output files header.
  • If the output files exists and that 'modified date' in the generated files matches the intput files, then skip file regeneration (the resulting files will likely be the same which will trigger Visual Studio to rebuild the project)
  • If the --override flag is specified, then the application should not look for the 'modified date' value and regenerated the files anyway.

Visual Studio: warning C4996: 'fopen': This function or variable may be unsafe.

Cpp files generated by bin2cpp uses fopen() function. On Windows, using this function generates a warning message:

warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

The solution is to add the following in cpp files before the first include:

#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif

Function getIdentifier does not remove special characters

The function getIdentifier does not properly sanitize characters from filenames.

For example the file C:\Users\foobar\AppData\Local\Temp\demo-sample.cpp generate the identifier demo-sample which contains the invalid c++ character - (dash).

The function must remove all invalid c++ characters such as +-*/=!"/$%?&()½¾.,;<>^¸¨: .

Replace icons of sample project

The current icons of the second project sample are released under the Creative Commons Attribution 4.0. The icons could be changed to something more meaningful and have a different license attribution to prevent having to credit the author everywhere.

Store binary data as HEX or OCT

Test if it is better to store binary data using hex representation \x0D of the octal representation \015
Note that google Protocol Buffer seems to use octal representation instead of hex

Uniformalize function argument names. Remove i and o prefix from argument names

Function arguments have multiple coding standard. One of them is prefixing all function arguments with i or o. This is a naming convention I learned at work and I do not think it is largely used in the outside world. The goal is to prefix variables with an i when the variable is an input and an o when the variable is an output (like Parse() functions defined in RapidAssist).

I consider removing the i and the o prefixes as I don't think it is intuitive enough for people outside of my work environment. I am also not always consistent across all files.

We might also adopt function arguments naming convention defined in Google's C++ Style Guide.

Cannot compile on linux

I am trying to build this software on linux, but it's giving me loads of issues.
First of all it complained about missing rapidassist cmake files, so i got the latest version of rapidassist from its repo. And then i set its path properly so bin2cpp cmake can include it but then it fails with it trying to load rapidassist-targets.cmake from the build/cmake directory in rapidassist root. That file seems to be nowhere to be found.

Create 'filter' argument to filter files found with 'dir' argument.

The filter could be specified with --filter=<something> and be wildcard based and support characters like * and ?.

For instance:

  • --filter=*.jpg
  • --filter=*.bin
  • --filter=IMG_?????.jpg

This functionality would also provides the ability of generating files in the same directory as the source binary files. Using the filter *.jpg would prevent picking up *.h and *.cpp files from the generators.

Modify IGenerator interface to use setters/getters

The IGenerator interface has main 2 functions which requires a high number of parameters. the number of parameters keep increasing from each releases as new functionnality in introduced into the software.

The interface should be modified to use setters/getters and would provide the interface the following:

  • Simplified usage with optional parameters
  • Only providing parameters once (the same parameters are shared by the 2 functions)
  • Compatibility with future versions.

Change File and FileManager macro guards prefix according to specified namespace

The --namespace argument was designed to allow users to include the generated files within their own custom namespace instead of using hardcoded namespace bin2cpp.

When users want to generate files in two different custom namespaces, the File and FileManager interfaces are guarded by the same macro guards. Specifically by the macro guards BIN2CPP_EMBEDDEDFILE_CLASS and BIN2CPP_FILEMANAGER_CLASS which is a problem. Each namespace should have an independent (but unfortunately identical) File and FileManager interfaces.

The issue is that when a source file includes two headers of each namespace, the second header cannot register the interfaces because the other header has already defined the macro guards.

Update README file

  • Update the USAGE section with a preformatted listing. Copy & Paste the output of the application.
  • Remove section HISTORY OF CHANGES
  • Rewrite the CODE SAMPLE section:
    • Rename to Example with sub items called:
      • Input File
      • Command
      • Output
      • Using
    • Show an example of how to use the generated code: print the original filename, size and maybe content ?
  • Add a section that explains how to build the code.

All the small things

The following task should be completed :

  • When generating the FileManager files (header and cpp files) from a directory, the header reads Source code for file 'New Text Document.txt', last modified 1619101267. which his incorrect. It should read Source code for file 'FileManager.h', last modified 1619101267.
  • In help message, replace "as a C++ source code" by "as C++ source code"
  • Find out why generated files with --managerfile do not require --registerfile flag. They do automatically registers. Maybe update documentation saying that flag '--registerfile' is automatically set if flag '--managerfile' is specified ?
  • Validate updating a generated file if the input file has changed. Is there already a unit test for that ?
  • In help message, explain that 'headerfile' and 'identifier' parameters are automatically calculated when using 'dir'.
  • Update "Command Line Usage" in readme.md. It is out of date.
  • Create sections for each examples in README.md.
  • Rename https://github.com/end2endzone/bin2cpp#example to "Example 1 - Convert a single html file to c++ source code."
  • Rename section "Input File:" (and the first line) to "Input File: helloworld.html" (drop the html5 and skeleton part)
  • Change the command line to: bin2cpp.exe --file=helloworld.html --output=.\outdir --headerfile=generated_helloworld.h --identifier=HelloWorldHtml --chunksize=50
  • Rename "File file_html5skeleton.h" to Output file: generated_helloworld.h"
  • Rename "File file_html5skeleton.cpp" to Output file: generated_helloworld.cpp"
  • Rename "Code sample (using embedded files)" to "At runtime, show file properties and save/export data back to a file".
  • Create another example: "Example 2 - Convert multiple files in a single directory to c++ source code."
  • Add an examples with --dir using FileManager option to extract all files.
  • Create an example with lena image ?
  • The README.md file do not explain how bin2cpp is indented to be used. More documentation and examples should be created.
  • In the header of the readme, a note should be added to explain something like "When executed, bin2cpp takes a binary file and outputs c++ code (a function) that when called allows a c++ program to retrieve the content of the input binary file".
  • The project description "bin2cpp converts text or binary files to C++ files (*.h, *.cpp) for easy access within the code." should also appears in the README.md file.
  • A section called bin2cpp worlflow which explains how one can use bin2cpp to embed a binary file could be created.

Code sample for the --dir example :

#include "../../demo/generated_files.2/FileManager.h"

int main(int argc, char* argv[])
{
  bin2cpp::FileManager & mgr = bin2cpp::FileManager::getInstance();

  //Print information about all files generated with "--managerfile" or --registerfile flags.
  size_t num_files = mgr.getFileCount();
  printf("Found %lu embedded files...\n", num_files);

  //Listing files.
  for(size_t i=0; i<num_files; i++)
  {
    const bin2cpp::File * file = mgr.getFile(i);
    printf("  File '%s', %lu bytes\n", file->getFilename(), file->getSize());
  }

  //Saving content back to files.
  const char * temp_dir = getenv("TEMP");
  printf("Saving embedded files to directory '%s'...\n", temp_dir);
  bool saved = mgr.saveFiles(temp_dir);
  if (saved)
    printf("saved\n");
  else
    printf("failed\n");

  return 0;
}


Found 4 embedded files...
  File 'New Bitmap Image.bmp', 17994 bytes
  File 'New Microsoft Excel Worksheet.xlsx', 6203 bytes
  File 'New Microsoft Word Document.docx', 11340 bytes
  File 'New Text Document.txt', 17 bytes
Saving embedded files to directory 'C:\Users\antoine\AppData\Local\Temp'...
saved
Press any key to continue . . .

bin2cpp on Linux

Make sure that code can also be compiled by a unix compiler (ie gcc).

Support for macOS

It would be nice if the application would be available on mac.

Following the latest release of RapidAssit, macOS support could be implemented easily.

Implement a --namespace argument

Implement a --namespace argument to allow changing the default namespace from bin2cpp to the value specified by the argument.

Relative path in resource files generated by win32 generator.

Path generated by win32 generator should be relative to resource file instead of absolute.

The current win32 generator implementation adds absolute path to the resource file (*.rc) file. This makes all projects that uses this to be unable to be moved and compiled to new locations.
Current path generated

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.