Browse Source

Merge header.cpp into header.h

master
Jake Wakeling 4 weeks ago
parent
commit
6f3e3b6eca
3 changed files with 61 additions and 75 deletions
  1. 0
    61
      src/header.cpp
  2. 61
    13
      src/header.h
  3. 0
    1
      src/oar.cpp

+ 0
- 61
src/header.cpp View File

@@ -1,61 +0,0 @@
// header.cpp
// Main source file for OmegaARc file headers
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#include <string>

#include "header.h"

using namespace Oar;

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

/*
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;
}

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

/*
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;
try {
header = new std::byte[this->length()];
}
catch (std::bad_alloc) {
fprintf(stderr, "OARx000A: Failed to allocate memory\n");
exit(0x000A);
}

/*
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;
}

+ 61
- 13
src/header.h View File

@@ -1,24 +1,72 @@
// header.h
// Main header file for OmegaARc file headers
// Header file for OmegaARc file headers
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#ifndef HEADER_H
#define HEADER_H

#include <string>

namespace Oar {
class Header {
public:
std::string path; // File Path
std::string size; // File Size
std::string checksum; // File Checksum

Header(void) {}; // Default constructor
Header(std::string path);
Header(std::string path, std::string size, std::string checksum);

size_t length(void);
std::byte *toBinary(void);
struct Header {
std::string path; // File Path
std::string size; // File Size
std::string checksum; // File Checksum

Header(void) {}; // Default Constructor

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

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

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

/*
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 *toBinary(void) {
std::byte *header;
try {
header = new std::byte[this->length()];
}
catch (std::bad_alloc) {
throw;
//fprintf(stderr, "OARx000A: Failed to allocate memory\n");
//exit(0x000A);
}

/*
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;
}
};
}


+ 0
- 1
src/oar.cpp View File

@@ -9,7 +9,6 @@
#include <vector>
#include <oendianness.h>


#include "header.h"
#include "oar.h"
#include "version.h"

Loading…
Cancel
Save