Skip to content

Commit

Permalink
added unsignedfstream.h
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzoDK committed Aug 30, 2021
1 parent 9f3a13a commit 802dd14
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .kdev4/RPCommonLib.kdev4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Buildset]
BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x01\x00\x00\x00\x16\x00R\x00P\x00C\x00o\x00m\x00m\x00o\x00n\x00L\x00i\x00b)
BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x00)

[Launch]
Launch Configurations=Launch Configuration 0,Launch Configuration 1
Expand Down
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pkgname="rpcommon"
provides=('rpcommon')
conflicts=('rpcommon-dev')
pkgver=0.1.6
pkgver=0.1.7
pkgrel=1
arch=('any')
pkgdesc="A compilation of small and common functions to make the dev life easier"
Expand Down
1 change: 1 addition & 0 deletions RPCommon/RPCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
//This header is not useful at the current state and has been disabled
//#include "rpcommonerr.h"
#include "eat.h"
#include "unsignedfstream.h"
11 changes: 7 additions & 4 deletions RPCommon/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
template <typename T>
struct Key
{
Key(){keyName = "";};
Key(){keyName = ""; keyValue = nullptr;};
//Don't use this - This is meant to be used to create empty keys for errors
Key(std::string name){ keyName = name, keyValue = nullptr;};

Key(std::string name, T val){keyName = name; keyValue = val;};
~Key(){}
Key(std::string name, T val){keyName = name; keyValue = *val;};

Key(std::string name, T* val){keyName = name; keyValue = new T(val);}

~Key(){delete keyValue;}
std::string keyName;
T keyValue;
T* keyValue;

};

Expand Down
53 changes: 53 additions & 0 deletions RPCommon/unsignedfstream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include <fstream>
#include <iostream>

inline unsigned char* ReadPartialUnsignedFile(std::string file, size_t& bytes) //reads 'bytes' bytes from file - Bytes will be updated to match the bytes read
{
unsigned char* buffer = new unsigned char[bytes];
std::ifstream in = std::ifstream(file,std::ios::binary | std::ios::ate);
size_t size = in.tellg();
in.seekg(0,std::ios::beg);
if(bytes > size)
{
//Err
std::cout << "Trying to read more bytes than is available in file: '" << file << "', trying to read: '" << bytes << "' but file did only contain: '" << size << "' bytes - reading maxsize instead" << std::endl;
bytes = size;
}
if(in.read(reinterpret_cast<char*>(buffer),size))
{
in.close();
return buffer;
}
else
{
in.close();
std::cout << "File read error" << std::endl;
delete[] buffer;
bytes = 1;
return new unsigned char[1];
}

}
inline unsigned char* ReadUnsignedFile(std::string file, size_t& bytesRead) //Reads the whole file
{
unsigned char* buffer;
std::ifstream in = std::ifstream(file,std::ios::binary | std::ios::ate);
size_t size = in.tellg();
in.seekg(0,std::ios::beg);
bytesRead = size;
buffer = new unsigned char[size];
if(in.read(reinterpret_cast<char*>(buffer),size))
{
in.close();
return buffer;
}
else
{
in.close();
std::cout << "File read error" << std::endl;
delete[] buffer;
bytesRead = 1;
return new unsigned char[1];
}
}
60 changes: 60 additions & 0 deletions test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,60 @@ bool PerformTestEat()
return true;


}
bool PerformTestFstream()
{
unsigned char expected[45] =
{
0x4e, 0x6f, 0x72, 0x6d,
0x61, 0x6c, 0x54, 0x65,
0x78, 0x74, 0x52, 0x69,
0x67, 0x68, 0x74, 0x20,
0x68, 0x65, 0x72, 0x65,
0x0a, 0x00, 0x00, 0x00,
0x44, 0x33, 0x43, 0x44,
0x03, 0x32, 0x23, 0x01,
0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09,
0x03, 0x04, 0x05, 0x06,
0x00
};

std::cout << "Testing File Read with correct parameters..." << std::endl;

size_t bytesRead = 0;
unsigned char* input = ReadUnsignedFile("testRead.txt",bytesRead);
if(bytesRead == 45)
{
if(!Cstrcmp(reinterpret_cast<char*>(input),bytesRead,reinterpret_cast<char*>(expected),45,true))
{
std::cout << TERMINAL_COLOR_RED << GetBytes(input,bytesRead) << " is supposed to be equal to " << GetBytes(expected,45) << TERMINAL_COLOR_RESET << std::endl;
return false;
}
}
else
{
std::cout << TERMINAL_COLOR_RED << GetBytes(input,bytesRead) << " is supposed 45 bytes long.. " << TERMINAL_COLOR_RESET << std::endl;
return false;
}
bytesRead = 0;

std::cout << TERMINAL_COLOR_GREEN << "Passed!" << TERMINAL_COLOR_RESET << std::endl;

std::cout << "Testing File Read with wrong paramenters..." << std::endl;
delete[] input;
bytesRead = 40;
input = ReadPartialUnsignedFile("testRead.txt",bytesRead);
if(Cstrcmp(reinterpret_cast<char*>(input),bytesRead,reinterpret_cast<char*>(expected),45,true))
{
std::cout << TERMINAL_COLOR_RED << GetBytes(expected,bytesRead) << " is not supposed to be equal to " << GetBytes(input,45) << TERMINAL_COLOR_RESET << std::endl;
return false;
}

std::cout << TERMINAL_COLOR_GREEN << "Passed!" << TERMINAL_COLOR_RESET << std::endl;

return true;

}


Expand Down Expand Up @@ -261,6 +315,12 @@ int main()
std::cout << TERMINAL_COLOR_RED << "eat failed!" << TERMINAL_COLOR_RESET << std::endl;
exit(0b00100000);
}
std::cout << TERMINAL_COLOR_YELLOW << "Running tests on unsignedfstream" << TERMINAL_COLOR_RESET << std::endl;
if(!PerformTestFstream())
{
std::cout << TERMINAL_COLOR_RED << "unsignedfstream failed!" << TERMINAL_COLOR_RESET << std::endl;
exit(0b01000000);
}

std::cout << TERMINAL_COLOR_GREEN << "Test Complete - No errors!" << TERMINAL_COLOR_RESET << std::endl;
return 0;
Expand Down
Binary file added testRead.txt
Binary file not shown.

0 comments on commit 802dd14

Please sign in to comment.