Compare commits

...

3 Commits

6 changed files with 134 additions and 34 deletions

View File

@ -60,6 +60,7 @@ create_directory bin/tools
# add compile commands to this array
COMPILE_COMMANDS=(
"$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -I./include -Lbin/lib -l:nbt.so -l:javacompat.so -l:cli.so -o bin/tools/dumpnbt"
"$CXX_WITH_FLAGS src/tools/arraydump.cpp -I./include -Lbin/lib -l:file.so -l:cli.so -o bin/tools/arraydump"
#"$CXX_WITH_FLAGS src/tools/hexnet.cpp -I./include -Lbin/lib -l:cli.so -l:libsockpp.so -o bin/tools/hexnet"
)
for command in ${!COMPILE_COMMANDS[@]}; do

View File

View File

@ -1,25 +0,0 @@
#!/usr/bin/env bash
# Copyright 2022, FOSS-VG Developers and Contributers
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# version 3 along with this program.
# If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
if [ -z "$1" ]; then
echo -e "Usage: $0 FILE\n\nRead FILE and print its contents to standard output formatted as array of hexadecimal numbers."
exit 1
fi
echo -n "{"
xxd -p $1 | tr -d '\n\r' | sed -e 's/../0x&, /g;s/, $//'
echo "}"

View File

@ -28,9 +28,13 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa
std::filesystem::path filePath = path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
this->open();
this->isOpen = true;
}
File::~File() {
if (this->isOpen) {
this->fileStream.close();
}
}
void File::open(){
switch(this->mode){
@ -49,6 +53,7 @@ void File::open(){
break;
}
this->isOpen = this->fileStream.is_open();
}
void File::close(){
@ -61,10 +66,6 @@ bool File::eof() {
}
ErrorOr<uint8_t> File::readByte(){
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
if (!this->isOpen) {
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_NOT_OPEN);
}
@ -72,6 +73,10 @@ ErrorOr<uint8_t> File::readByte(){
return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN);
}
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
try {
this->fileStream.seekg(this->cursorPosition);
this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1);
@ -87,10 +92,6 @@ ErrorOr<uint8_t> File::readByte(){
}
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
uint8_t* buffer = new uint8_t[bytes];
std::vector<uint8_t> data;
bool failure = false;
if (!this->isOpen) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN);
}
@ -98,6 +99,10 @@ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN);
}
uint8_t* buffer = new uint8_t[bytes];
std::vector<uint8_t> data;
bool failure = false;
try {
this->fileStream.seekg(this->cursorPosition);
this->fileStream.read(reinterpret_cast<char*>(buffer), bytes);

View File

@ -45,6 +45,8 @@ class File {
ErrorOr<uint64_t> size;
File() {};
~File();
void open();
void close();

117
src/tools/arraydump.cpp Normal file
View File

@ -0,0 +1,117 @@
// Copyright 2022, FOSS-VG Developers and Contributers
//
// Author(s):
// BodgeMaster
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#include <bitset>
#include <iomanip>
#include <iostream>
#include <vector>
#include "../lib/cli.hpp"
#include "../lib/file.hpp"
#define EXIT_SUCCESS 0
#define EXIT_RUNTIME 1
#define EXIT_USAGE 2
int main(int argc, char* argv[]) {
std::vector<CLI::Flag> flags;
flags.push_back(CLI::Flag('x', "hexadecimal", "write output as hexadecimal numbers"));
flags.push_back(CLI::Flag('o', "octal", "write output as octal numbers"));
flags.push_back(CLI::Flag('b', "binary", "write output as binary numbers"));
flags.push_back(CLI::Flag('h', "help", "print help and exit"));
flags.push_back(CLI::Flag('l', "license", "print license information and exit"));
std::vector<CLI::Option> options;
std::vector<CLI::Argument> arguments;
arguments.push_back(CLI::Argument("FILE", "path of the file to dump"));
CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, options, arguments, "Dump files as C++ array literals");
if (cliParser.getFlag("help").value) {
std::cout << "I was here" << std::endl;
std::cout << cliParser.getUsage() << std::endl;
return EXIT_SUCCESS;
}
if (cliParser.getFlag("license").value){
std::cout
<< "Copyright 2022, FOSS-VG Developers and Contributers\n"
<< "\n"
<< "ArrayDump is part of the FOSS-VG development tool suite.\n"
<< "\n"
<< "This program is free software: you can redistribute it and/or modify it\n"
<< "under the terms of the GNU Affero General Public License as published\n"
<< "by the Free Software Foundation, version 3.\n"
<< "\n"
<< "This program is distributed in the hope that it will be useful,\n"
<< "but WITHOUT ANY WARRANTY; without even the implied\n"
<< "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
<< "See the GNU Affero General Public License for more details.\n"
<< "\n"
<< "You should have received a copy of the GNU Affero General Public License\n"
<< "version 3 along with this program.\n"
<< "If not, see https://www.gnu.org/licenses/agpl-3.0.en.html"
<< std::endl;
return EXIT_SUCCESS;
}
if (cliParser.wrongUsage) {
std::cout << cliParser.getUsage() << std::endl;
return EXIT_USAGE;
}
ErrorOr<File*> filePointer = File::open(cliParser.getArgument(0).value, 'r');
if (filePointer.isError) {
std::cout << "Failed to open file: " << cliParser.getArgument(0).value << std::endl;
return EXIT_RUNTIME;
}
File* file = filePointer.value;
std::cout << "{";
if (cliParser.getFlag("hexadecimal").value) {
if (!file->eof()) {
std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << (int) file->readByte().value;
}
while (!file->eof()) {
std::cout << ", 0x" << std::setw(2) << std::setfill('0') << std::hex << (int) file->readByte().value;
}
} else if (cliParser.getFlag("octal").value) {
if (!file->eof()) {
std::cout << "0" << std::setw(3) << std::setfill('0') << std::oct << (int) file->readByte().value;
}
while (!file->eof()) {
std::cout << ", 0" << std::setw(3) << std::setfill('0') << std::oct << (int) file->readByte().value;
}
} else if (cliParser.getFlag("binary").value) {
if (!file->eof()) {
std::cout << "0b" << std::bitset<8>((int) file->readByte().value);
}
while (!file->eof()) {
std::cout << ", 0b" << std::bitset<8>((int) file->readByte().value);
}
} else {
if (!file->eof()) {
std::cout << (int) file->readByte().value;
}
while (!file->eof()) {
std::cout << ", " << (int) file->readByte().value;
}
}
std::cout << "}" << std::endl;
delete file;
return EXIT_SUCCESS;
}