commit 77084b8b37ae34e63780381049b0bfa267ad9798
parent b63a1e993c7d3f6346b69cc8f709835e1035c1e5
Author: Andrew Laack <andrew@laack.co>
Date: Fri, 11 Jul 2025 12:53:30 -0500
Updated some logic, need to add more testing and consideration for enforcing header values
Diffstat:
3 files changed, 57 insertions(+), 18 deletions(-)
diff --git a/include/chunk.h b/include/chunk.h
@@ -22,6 +22,7 @@ private:
std::vector<char> chunk;
std::string extractEncodedSection(const std::string& fileString, uint& itr, int count);
std::string binaryToAscii(const std::string& binaryStr);
+ Header header;
public:
Chunk(std::string filename, uint start, uint size);
@@ -29,10 +30,13 @@ public:
// read in the written chunk from tga format.
Chunk(std::string filename);
std::vector<char> getChunk();
+ // 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();
+
};
diff --git a/src/chunk.cpp b/src/chunk.cpp
@@ -62,36 +62,34 @@ Chunk::Chunk(std::string filename){
- Header head;
-
- head.chunkNumber = 0;
- head.characters = 0;
- head.fnameLength = 0;
- head.filename = "";
+ this->header.chunkNumber = 0;
+ this->header.characters = 0;
+ this->header.fnameLength = 0;
+ this->header.filename = "";
std::string encodedCNum = extractEncodedSection(fileString, itr, 4 * 8);
std::string encodedCharacters = extractEncodedSection(fileString, itr, 4 * 8);
std::string encodedFnameLength = extractEncodedSection(fileString, itr, 4 * 8);
- head.characters = std::bitset<4*8>(encodedCharacters).to_ullong();
- head.chunkNumber = std::bitset<4*8>(encodedCNum).to_ullong();
- head.fnameLength = std::bitset<4*8>(encodedFnameLength).to_ullong();
+ this->header.characters = std::bitset<4*8>(encodedCharacters).to_ullong();
+ this->header.chunkNumber = std::bitset<4*8>(encodedCNum).to_ullong();
+ this->header.fnameLength = std::bitset<4*8>(encodedFnameLength).to_ullong();
- std::string encodedFname = extractEncodedSection(fileString, itr, head.fnameLength);
- head.filename = binaryToAscii(encodedFname);
+ std::string encodedFname = extractEncodedSection(fileString, itr, this->header.fnameLength);
+ this->header.filename = binaryToAscii(encodedFname);
LOG("ENCODED CNUM: " << encodedCNum)
- LOG("CNUM: " << head.chunkNumber)
+ LOG("CNUM: " << this->header.chunkNumber)
LOG("ENCODED CHARACTERS: " << encodedCharacters)
- LOG("CHARACTERS: " << head.characters)
+ LOG("CHARACTERS: " << this->header.characters)
LOG("ENCODED FNAMELENGTH: " << encodedFnameLength)
- LOG("FNAMELENGTH: " << head.fnameLength)
+ LOG("FNAMELENGTH: " << this->header.fnameLength)
LOG("ENCODED FNAME: " << encodedFname)
- LOG("FNAME: " << head.filename)
+ LOG("FNAME: " << this->header.filename)
while(itr < fileString.size()){
if (fileString[itr] != ' ' && fileString[itr] != '\n'){
@@ -103,7 +101,7 @@ Chunk::Chunk(std::string filename){
std::vector<char> bytes;
- for (int i = 0 ; i < head.characters; ++i){
+ for (int i = 0 ; i < this->header.characters; ++i){
std::string current = "";
@@ -150,12 +148,42 @@ std::vector<char> Chunk::getChunk(){
return this->chunk;
}
+
+
+// use if header info is populated in the object
+// and you are trying to write back out what you read in.
+// append if not the first chunk.
+
+void Chunk::writeChunk(){
+
+ LOG(this->header.filename)
+ std::ofstream outFile;
+ std::vector<char> bytes = this->chunk;
+
+ if(this->header.chunkNumber != 1){
+ outFile.open(this->header.filename, std::ios::binary | std::ios::app);
+ }
+ else{
+ outFile.open(this->header.filename, std::ios::binary | std::ios::out);
+ }
+
+ for(uint i = 0 ; i < bytes.size(); ++i){
+ outFile.write(&bytes[i], 1);
+ }
+}
+
void Chunk::writeChunk(std::string filename){
std::ofstream outFile;
std::vector<char> bytes = this->chunk;
- outFile.open(filename, std::ios::binary | std::ios::out);
+
+ if(this->header.chunkNumber != 1){
+ outFile.open(filename, std::ios::binary | std::ios::app);
+ }
+ else{
+ outFile.open(filename, std::ios::binary | std::ios::out);
+ }
for(uint i = 0 ; i < bytes.size(); ++i){
outFile.write(&bytes[i], 1);
diff --git a/src/encode.cpp b/src/encode.cpp
@@ -14,7 +14,7 @@ int main(int argc, char* argv[]){
std::string source = argv[1];
std::string destination = argv[2];
- Chunk chunk = Chunk(source, 0, 1000000000);
+ Chunk chunk = Chunk(source, 0, 10);
std::vector<char> bytes = chunk.getChunk();
std::ofstream outFile;
Chunk outChunk = Chunk(bytes);
@@ -32,6 +32,13 @@ int main(int argc, char* argv[]){
LOG("CHUNKS DON'T MATCH")
}
+
+ // this writes out to a specific file.
+ outChunk.writeChunk("after-" + source);
+ // this writes out to the file that was
+ // specified in the .tga header file.
+ readChunk.writeChunk();
+
return 0;
}