Browse Source

Minor cleanup

master
Jake Wakeling 1 month ago
parent
commit
12f4508deb
4 changed files with 57 additions and 72 deletions
  1. 2
    2
      CMakeLists.txt
  2. 35
    45
      src/header.cpp
  3. 7
    18
      src/header.h
  4. 13
    7
      src/oar.cpp

+ 2
- 2
CMakeLists.txt View File

@@ -5,8 +5,8 @@ project(OmegaARc) # Project
set(CMAKE_CXX_STANDARD 17) # C++ Standard

# Output Directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

file(GLOB SOURCES # Add Source Files

+ 35
- 45
src/header.cpp View File

@@ -3,69 +3,59 @@
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#include <string>

#include "header.h"

using namespace Oar;

// Construct empty header object
Header::Header(void) {
// Default constructor
}

// Construct header object with file name
Header::Header(std::string name) {
this->name = name;
/*
Construct a header with path
*/
Header::Header(std::string path) {
this->path = path;
}

// Construct header object with file name, size and checksum
Header::Header(std::string name, std::string size, std::string checksum) {
this->name = name;
/*
Construct a header with path, size and checksum
*/
Header::Header(std::string path, std::string size, std::string checksum) {
this->path = path;
this->size = size;
this->checksum = checksum;
}

// Get header elements
std::string Header::getName(void) {
return this->name; // Return value of name element
}
std::string Header::getSize(void) {
return this->size; // Return value of size element
}
std::string Header::getChecksum(void) {
return this->checksum; // Return value of checksum element
}

// Set header elements
int Header::setName(std::string name) {
this->name = name;
return 0x0000;
}
int Header::setSize(std::string size) {
this->size = size;
return 0x0000;
}
int Header::setChecksum(std::string checksum) {
this->checksum = checksum;
return 0x0000;
}

// Return the length of the header including space for null characters
/*
Return the length of the header
Three bytes are added to make room for null terminators.
*/
size_t Header::length(void) {
return this->name.size() + this->size.size() + this->checksum.size() + 3;
return this->path.length() + this->size.length() + this->checksum.length() + 3;
}

// Return the header as byte array ready to write to file
// 0x000A: Failed to allocate memory
/*
Return the header as a byte array ready to be written
Pointer to byte array is returned, delete after call to avoid memory leak.

Errors:
0x000A: Failed to allocate memory
*/
std::byte *Header::toBinary(void) {
std::byte *header = (std::byte *)malloc(this->length());
if (header == nullptr) {
std::byte *header;
try {
header = new std::byte[this->length()];
}
catch (std::bad_alloc) {
fprintf(stderr, "OARx000A: Failed to allocate memory\n");
exit(0x000A);
}

memcpy(header, this->name.c_str(), this->name.size() + 1);
memcpy(header + this->name.size() + 1, this->size.c_str(), this->size.size() + 1);
memcpy(header + this->name.size() + this->size.size() + 2, this->checksum.c_str(), this->checksum.size() + 1);
/*
Copy each header element into the byte array with a following null terminator.
*/
memcpy(header, this->path.c_str(), this->path.size() + 1);
memcpy(header + this->path.size() + 1, this->size.c_str(), this->size.size() + 1);
memcpy(header + this->path.size() + this->size.size() + 2, this->checksum.c_str(), this->checksum.size() + 1);

return header;
}

+ 7
- 18
src/header.h View File

@@ -6,30 +6,19 @@
#ifndef HEADER_H
#define HEADER_H

#include <string>

namespace Oar {
class Header {
public:
std::string getName(void);
std::string getSize(void);
std::string getChecksum(void);
std::string path; // File Path
std::string size; // File Size
std::string checksum; // File Checksum

int setName(std::string name);
int setSize(std::string size);
int setChecksum(std::string checksum);
Header(void);
Header(std::string name);
Header(std::string name, std::string size, std::string checksum);
Header(void) {}; // Default constructor
Header(std::string path);
Header(std::string path, std::string size, std::string checksum);

size_t Header::length(void);
size_t length(void);
std::byte *toBinary(void);

private:
std::string name; // File Name
std::string size; // File Size
std::string checksum; // File Checksum
};
}


+ 13
- 7
src/oar.cpp View File

@@ -54,9 +54,9 @@ int Oar::extract(std::string oarPath, std::string extractPath) {
else {
std::string filePath;
filePath.assign(extractPath); // Form file extraction path
filePath.append(header->getName());
filePath.append(header->path);

fileExtract(&oarFile, filePath, std::stoull(header->getSize()));
fileExtract(&oarFile, filePath, std::stoull(header->size));
}
delete header;
}
@@ -79,10 +79,16 @@ int Oar::fileAdd(std::ofstream *oarFile, std::string filePath) {
}

file.seekg(0, file.end); // Seek end of input file
header->setSize(std::to_string(file.tellg())); // Store input file size as string
header->size = std::to_string(file.tellg()); // Store input file size as string
file.seekg(0, file.beg); // Seek beginning of input file

oarFile->write((char *)header->toBinary(), header->length()); // Write header to OAR archive
/*
Get the header as a byte array and write the array to the OAR archive.
Header byte array is then deleted.
*/
std::byte *headerBinary = header->toBinary();
oarFile->write((char *)headerBinary, header->length());
delete[] headerBinary;

while (!file.eof()) { // Copy input file into OAR archive
std::byte *buffer;
@@ -189,7 +195,7 @@ Oar::Header *Oar::fileDecode(std::ifstream *oarFile) {
}
}

header->setName(tempName);
header->path = tempName;

for (;;) {
if (oarFile->peek() == '\0') {
@@ -203,7 +209,7 @@ Oar::Header *Oar::fileDecode(std::ifstream *oarFile) {
}
}

header->setSize(tempSize);
header->size = tempSize;

for (;;) {
if (oarFile->peek() == '\0') {
@@ -217,7 +223,7 @@ Oar::Header *Oar::fileDecode(std::ifstream *oarFile) {
}
}

header->setChecksum(tempChecksum);
header->checksum = tempChecksum;

return header;
}

Loading…
Cancel
Save