Skip to content

Commit dd578d4

Browse files
committed
Translated C code into C++
1 parent 40fd74a commit dd578d4

4 files changed

Lines changed: 39 additions & 50 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.11)
2-
project(FileToByte CXX)
2+
project(FileToByte)
33

44
set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
@@ -15,7 +15,6 @@ include_directories(imgui/ imgui/backends imgui/misc/cpp)
1515
file(GLOB sources
1616
source/*.cpp
1717
source/*.hpp
18-
source/fonts/*.cpp
1918
source/fonts/*.hpp
2019
imgui/imgui.cpp
2120
imgui/imgui.h

source/FTB.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,48 @@
66
#include "FTB.hpp"
77

88
#include <assert.h>
9-
#include <sstream>
109
#include <stdio.h>
1110
#include <stdlib.h>
12-
#include <string.h>
1311

12+
#include <fstream>
1413
#include <iomanip>
14+
#include <ios>
15+
#include <sstream>
16+
#include <string>
17+
#include <vector>
1518

1619
typedef unsigned int stb_uint;
1720
typedef unsigned char stb_uchar;
1821
stb_uint stb_compress(stb_uchar *out, stb_uchar *in, stb_uint len);
1922

20-
std::string Convert(const char *filename, const char *symbol) {
21-
FILE *f;
22-
errno_t err;
23-
err = fopen_s(&f, filename, "rb");
23+
std::string Convert(std::string file_path, std::string array_name) {
24+
std::ifstream file(file_path, std::ios::in | std::ios::binary);
2425

25-
if (!f)
26-
return "";
26+
if (!file.is_open())
27+
return "File failed to open";
2728

28-
if (err != 0)
29-
return "";
29+
file.seekg(0, std::ios::end);
30+
std::streamsize data_sz = file.tellg();
31+
file.seekg(0, std::ios::beg);
3032

31-
int data_sz;
32-
33-
if (fseek(f, 0, SEEK_END) || (data_sz = (int)ftell(f)) == -1 ||
34-
fseek(f, 0, SEEK_SET)) {
35-
fclose(f);
33+
if (data_sz == -1) {
3634
return "";
3735
}
3836

39-
char *data = new char[data_sz + 4];
40-
41-
if (fread(data, 1, data_sz, f) != (size_t)data_sz) {
42-
fclose(f);
43-
delete[] data;
37+
std::vector<char> data(data_sz + 4);
38+
if (!file.read(data.data(), data_sz)) {
4439
return "";
4540
}
4641

47-
memset((void *)(((char *)data) + data_sz), 0, 4);
48-
fclose(f);
42+
memset(data.data() + data_sz, 0, 4);
4943

5044
int maxlen = data_sz + 512 + (data_sz >> 2) + sizeof(int);
51-
char *compressed = new char[maxlen];
52-
int compressed_sz =
53-
stb_compress((stb_uchar *)compressed, (stb_uchar *)data, data_sz);
45+
std::vector<char> compressed(maxlen);
46+
int compressed_sz = stb_compress(
47+
reinterpret_cast<stb_uchar *>(compressed.data()),
48+
reinterpret_cast<stb_uchar *>(data.data()), static_cast<int>(data_sz));
5449

55-
memset(compressed + compressed_sz, 0, maxlen - compressed_sz);
50+
memset(compressed.data() + compressed_sz, 0, maxlen - compressed_sz);
5651

5752
std::ostringstream stream;
5853

@@ -61,15 +56,15 @@ std::string Convert(const char *filename, const char *symbol) {
6156
stream << " *\tGenerated by File To Byte\n";
6257
stream << " *\thttps://github.com/JerimiahOfficial/FileToByte\n";
6358
stream << " */\n\n";
64-
stream << "static const unsigned int " << symbol
59+
stream << "static const unsigned int " << array_name
6560
<< "_compressed_size = " << (int)compressed_sz << ";\n";
66-
stream << "static const unsigned int " << symbol << "_compressed_data["
61+
stream << "static const unsigned int " << array_name << "_compressed_data["
6762
<< (int)((compressed_sz + 3) / 4) * 4 << " / 4] = {";
6863

6964
int column = 0;
7065
for (int i = 0; i < compressed_sz; i += 4) {
71-
unsigned int d = *(unsigned int *)(compressed + i);
72-
if ((column++ % 12) == 0)
66+
unsigned int d = *reinterpret_cast<unsigned int *>(compressed.data() + i);
67+
if ((column++ % 8) == 0)
7368
stream << "\n\t0x" << std::hex << std::setw(8) << std::setfill('0') << d
7469
<< ", ";
7570
else
@@ -78,9 +73,6 @@ std::string Convert(const char *filename, const char *symbol) {
7873
}
7974
stream << "\n};\n";
8075

81-
delete[] data;
82-
delete[] compressed;
83-
8476
return stream.str();
8577
}
8678

source/FTB.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#pragma once
22
#include <string>
33

4-
extern std::string Convert(const char *filename, const char *symbol);
4+
extern std::string Convert(std::string filename, std::string symbol);

source/application.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
#include "fonts/Consolas.hpp"
1313
#include "fonts/Roboto.hpp"
1414

15+
ImFont *Roboto = nullptr;
16+
ImFont *Consolas = nullptr;
17+
18+
bool open = true;
19+
20+
std::string VarName;
21+
std::string FilePath;
22+
std::string Result;
23+
1524
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && \
1625
!defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
1726
#pragma comment(lib, "legacy_stdio_definitions")
@@ -465,10 +474,10 @@ void Application::Initialize() {
465474
ImFontConfig fontConfig;
466475
fontConfig.FontDataOwnedByAtlas = false;
467476

468-
ImFont *Roboto = io.Fonts->AddFontFromMemoryCompressedTTF(
477+
Roboto = io.Fonts->AddFontFromMemoryCompressedTTF(
469478
Roboto_compressed_data, Roboto_compressed_size, 14.f, &fontConfig);
470479

471-
ImFont *Consolas = io.Fonts->AddFontFromMemoryCompressedTTF(
480+
Consolas = io.Fonts->AddFontFromMemoryCompressedTTF(
472481
Consolas_compressed_data, Consolas_compressed_size, 14.f, &fontConfig);
473482

474483
io.FontDefault = Roboto;
@@ -535,15 +544,6 @@ void Application::Run() {
535544
}
536545
}
537546

538-
ImFont *Roboto = nullptr;
539-
ImFont *Consolas = nullptr;
540-
541-
bool open = true;
542-
543-
std::string VarName;
544-
std::string FilePath;
545-
std::string Result;
546-
547547
void Application::ApplyTheme() {
548548
ImColor Dim = ImColor(32, 32, 32);
549549
ImColor Faint = ImColor(40, 40, 40);
@@ -587,7 +587,6 @@ void Application::Render() {
587587
ImGui::Begin("File To Byte", &open, window_flags);
588588

589589
ImGui::Columns(2, "locations", false);
590-
ImGui::PushFont(Roboto);
591590

592591
ImGui::Text("Array Name");
593592

@@ -596,7 +595,7 @@ void Application::Render() {
596595
ImGui::PopItemWidth();
597596

598597
if (ImGui::Button("Convert", ImVec2(-1, 25.f)))
599-
Result = Convert(FilePath.c_str(), VarName.c_str());
598+
Result = Convert(FilePath, VarName);
600599

601600
ImGui::NextColumn();
602601

@@ -612,7 +611,6 @@ void Application::Render() {
612611
ImGui::LogFinish();
613612
}
614613

615-
ImGui::PopFont();
616614
ImGui::Columns();
617615

618616
ImGui::PushFont(Consolas);

0 commit comments

Comments
 (0)