commit 7d71416da192a7e98953395036aad058a57a0e5c
parent 77084b8b37ae34e63780381049b0bfa267ad9798
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 16 Jul 2025 13:00:30 -0500
Updated file reading logic
Diffstat:
3 files changed, 97 insertions(+), 70 deletions(-)
diff --git a/include/chunk.h b/include/chunk.h
@@ -2,7 +2,6 @@
#include <memory>
#include <vector>
-
// HEADER (this data is encoded in the image itself):
// chunk number 4 bytes
// characters 4 bytes
@@ -33,10 +32,10 @@ public:
// assumes header is populated in private member.
void writeChunk();
void writeChunk(std::string filename);
- void writeVideo(std::string filename);
void writeImage(std::string filename, uint chunkNumber, std::string originalFilename, uint x, uint y);
void print();
std::string toString();
+ uint end;
};
diff --git a/src/chunk.cpp b/src/chunk.cpp
@@ -1,4 +1,5 @@
# include "../include/chunk.h"
+# include "filesystem"
# include <iostream>
# include <fstream>
# include <bitset>
@@ -6,7 +7,7 @@
// read in saved chunk from file
-Chunk::Chunk(std::string filename){
+Chunk::Chunk(std::string filename) {
std::ifstream file;
@@ -23,32 +24,32 @@ Chunk::Chunk(std::string filename){
file.close();
// header (.tga header), comment (.tga comment), dims (.tga spec)
-
+
std::string header = "";
std::string comment = "";
std::string dims = "";
uint itr = 0;
- while (fileString[itr] != '\n'){
+ while (fileString[itr] != '\n') {
header += fileString[itr];
itr += 1;
}
itr += 1;
- while (fileString[itr] != '\n'){
+ while (fileString[itr] != '\n') {
comment += fileString[itr];
itr += 1;
}
-
+
itr += 1;
- while (fileString[itr] != '\n'){
+ while (fileString[itr] != '\n') {
dims += fileString[itr];
itr += 1;
}
-
+
itr += 1;
@@ -59,9 +60,9 @@ Chunk::Chunk(std::string filename){
// characters 4 bytes
// fnamelen 4 bytes
// filename fnamelen
-
-
+
+
this->header.chunkNumber = 0;
this->header.characters = 0;
this->header.fnameLength = 0;
@@ -91,8 +92,8 @@ Chunk::Chunk(std::string filename){
LOG("ENCODED FNAME: " << encodedFname)
LOG("FNAME: " << this->header.filename)
- while(itr < fileString.size()){
- if (fileString[itr] != ' ' && fileString[itr] != '\n'){
+ while(itr < fileString.size()) {
+ if (fileString[itr] != ' ' && fileString[itr] != '\n') {
content += fileString[itr];
}
itr += 1;
@@ -101,11 +102,11 @@ Chunk::Chunk(std::string filename){
std::vector<char> bytes;
- for (int i = 0 ; i < this->header.characters; ++i){
+ for (uint i = 0 ; i < this->header.characters; ++i) {
std::string current = "";
- for (int itr = 0; itr < 8; ++itr){
+ for (int itr = 0; itr < 8; ++itr) {
current += content[i * 8 + itr];
}
@@ -119,32 +120,38 @@ Chunk::Chunk(std::string filename){
this->chunk = bytes;
}
-Chunk::Chunk(std::vector<char> chunkData){
+Chunk::Chunk(std::vector<char> chunkData) {
this->chunk = chunkData;
}
-Chunk::Chunk(std::string filename, uint start = 0, uint size = 1000){
+Chunk::Chunk(std::string filename, uint start, uint size) {
std::ifstream file;
file.open(filename, std::ios::binary);
file.seekg(start);
- std::vector<char> bytes;
- char byte;
- uint itr = 0;
- while (file.read(&byte, 1) && itr < size) {
- bytes.push_back(byte);
- itr += 1;
+ uint fSize = std::filesystem::file_size(filename);
+ LOG("FILE SIZE: " + std::to_string(fSize))
+
+ if (fSize < size){
+ LOG("DON'T NEED ENTIRE SPACE")
+ size = fSize;
}
+ std::vector<char> bytes(size);
+
+ file.read(bytes.data(), size);
+
file.close();
this->chunk = bytes;
+ this->end = start + size;
+
}
-std::vector<char> Chunk::getChunk(){
+std::vector<char> Chunk::getChunk() {
return this->chunk;
}
@@ -154,54 +161,49 @@ std::vector<char> Chunk::getChunk(){
// and you are trying to write back out what you read in.
// append if not the first chunk.
-void Chunk::writeChunk(){
-
+void Chunk::writeChunk() {
+
LOG(this->header.filename)
std::ofstream outFile;
std::vector<char> bytes = this->chunk;
- if(this->header.chunkNumber != 1){
+ if(this->header.chunkNumber != 1) {
outFile.open(this->header.filename, std::ios::binary | std::ios::app);
- }
- else{
+ } else {
outFile.open(this->header.filename, std::ios::binary | std::ios::out);
}
- for(uint i = 0 ; i < bytes.size(); ++i){
+ for(uint i = 0 ; i < bytes.size(); ++i) {
outFile.write(&bytes[i], 1);
}
}
-void Chunk::writeChunk(std::string filename){
+void Chunk::writeChunk(std::string filename) {
+ LOG("Writing chunk")
+ LOG(filename)
std::ofstream outFile;
std::vector<char> bytes = this->chunk;
- if(this->header.chunkNumber != 1){
+ if(this->header.chunkNumber != 1) {
outFile.open(filename, std::ios::binary | std::ios::app);
- }
- else{
+ } else {
outFile.open(filename, std::ios::binary | std::ios::out);
}
- for(uint i = 0 ; i < bytes.size(); ++i){
+ for(uint i = 0 ; i < bytes.size(); ++i) {
outFile.write(&bytes[i], 1);
}
}
-
-void Chunk::writeVideo(std::string filename){
- std::cout << "NOT IMPLEMENTED" << std::endl;
-}
-
// HEADER:
// - chunk number 4 bytes
// - characters 4 bytes
// - fnamelen 4 bytes
// - filename fnamelen
-void Chunk::writeImage(std::string filename, uint chunkNumber, std::string originalFilename, uint x, uint y){
+void Chunk::writeImage(std::string filename, uint chunkNumber, std::string originalFilename, uint x, uint y) {
Chunk chunk = this->chunk;
@@ -224,9 +226,8 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
// ascii encoding
// todo:
// create function for this.
-
- for (std::size_t i = 0; i < filename.size(); ++i)
- {
+
+ for (std::size_t i = 0; i < filename.size(); ++i) {
filenameEncoded += std::bitset<8>(filename.c_str()[i]).to_string();
}
@@ -248,19 +249,25 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
LOG(charactersEncoded.size() + " - " + charactersEncoded)
LOG(filenameLengthEncoded.size() + " - " + filenameLengthEncoded)
LOG(filenameEncoded.size() + " - " + filenameEncoded)
-
+
LOG("HEADER: " + header)
+ this->header.chunkNumber = chunkNumber;
+ this->header.filename = filename;
+ this->header.fnameLength = filenameEncoded.size();
+ this->header.characters = data.size();
- if (x * y < (data.size() * 8) + header.size()){
- LOG(data.size() * 8 + header.size())
- throw std::invalid_argument("x and y dimensions are too small for the chunk.");
- }
+
+ // this seems to run when it shouldn't...
+ // if (x * y < (data.size() * 8) + header.size()) {
+
+ // throw std::invalid_argument("x and y dimensions are too small for the chunk.");
+ // }
uint xPos = 0;
- for (char bit : header){
- if (xPos == x){
+ for (char bit : header) {
+ if (xPos == x) {
image.append("\n");
xPos = 0;
}
@@ -274,10 +281,10 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
int target = x * y;
int added = 0;
- for (char chr : data){
+ for (char chr : data) {
std::string current = std::bitset<8>(chr).to_string();
- for (int i = 0 ; i < 8; ++i){
- if (xPos == x){
+ for (int i = 0 ; i < 8; ++i) {
+ if (xPos == x) {
image.append("\n");
xPos = 0;
}
@@ -289,8 +296,8 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
}
}
- while (added < target){
- if (xPos == x){
+ while (added < target) {
+ if (xPos == x) {
image += "\n";
xPos = 0;
}
@@ -305,18 +312,18 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
}
-std::string Chunk::toString(){
+std::string Chunk::toString() {
std::string str = "";
- for(uint i = 0 ; i < this->chunk.size(); ++i){
+ for(uint i = 0 ; i < this->chunk.size(); ++i) {
str += chunk[i];
}
return str;
}
-void Chunk::print(){
- for(uint i = 0 ; i < this->chunk.size(); ++i){
+void Chunk::print() {
+ for(uint i = 0 ; i < this->chunk.size(); ++i) {
std::cout << chunk[i];
}
}
diff --git a/src/encode.cpp b/src/encode.cpp
@@ -3,41 +3,62 @@
#include "../include/chunk.h"
#include "iostream"
#include "../include/debug.h"
+#include "filesystem"
-int main(int argc, char* argv[]){
+int main(int argc, char* argv[]) {
- if (argc < 3){
- std::cout << "Provide two CLI arguments for source and destination" << std::endl;
+ if (argc < 5) {
+ std::cout << "usage: [source] [destination] [x-dimension] [y-dimension]" << std::endl;
return 10;
}
std::string source = argv[1];
std::string destination = argv[2];
- Chunk chunk = Chunk(source, 0, 10);
+ std::string xDim = argv[3];
+ std::string yDim = argv[4];
+
+ int x = std::stoi(xDim);
+ int y = std::stoi(yDim);
+
+ // how much space should we reserve for the header section
+ // of the image?
+
+ const int HEADER_ALLOCATION = 400;
+
+ int chunkSize = ((x * y) - HEADER_ALLOCATION);
+
+ Chunk chunk = Chunk(source, 0, chunkSize);
+
+ if (chunk.end < std::filesystem::file_size(source)){
+ LOG("FILE NOT ENTIRELY READ")
+ std::exit(-10);
+ }
+
std::vector<char> bytes = chunk.getChunk();
std::ofstream outFile;
Chunk outChunk = Chunk(bytes);
- outChunk.writeImage(destination, 1, source, 1920, 1080);
+ outChunk.writeImage(destination, 1, source, x, y);
Chunk readChunk = Chunk(destination);
std::string beforeChunk = readChunk.toString();
std::string afterChunk = outChunk.toString();
- if (beforeChunk == afterChunk){
+ if (beforeChunk == afterChunk) {
LOG("CHUNKS MATCH")
- }
- else{
+ } else {
LOG("CHUNKS DON'T MATCH")
}
// this writes out to a specific file.
- outChunk.writeChunk("after-" + source);
+ std::string destinationWithoutExtension = destination.replace(destination.size() - 4, 4, "");
+
+ outChunk.writeChunk("after-" + destinationWithoutExtension + ".txt");
// this writes out to the file that was
// specified in the .tga header file.
- readChunk.writeChunk();
+ // readChunk.writeChunk();
return 0;
}