From 802dd144cd8f2e0bf7ec603cfbd332148bf5dd5b Mon Sep 17 00:00:00 2001 From: AnzoDK Date: Mon, 30 Aug 2021 21:01:52 +0200 Subject: [PATCH] added unsignedfstream.h --- .kdev4/RPCommonLib.kdev4 | 2 +- PKGBUILD | 2 +- RPCommon/RPCommon.h | 1 + RPCommon/keymap.h | 11 ++++--- RPCommon/unsignedfstream.h | 53 ++++++++++++++++++++++++++++++++ test1.cpp | 60 +++++++++++++++++++++++++++++++++++++ testRead.txt | Bin 0 -> 45 bytes 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 RPCommon/unsignedfstream.h create mode 100644 testRead.txt diff --git a/.kdev4/RPCommonLib.kdev4 b/.kdev4/RPCommonLib.kdev4 index c8e6587..4fb1e09 100644 --- a/.kdev4/RPCommonLib.kdev4 +++ b/.kdev4/RPCommonLib.kdev4 @@ -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 diff --git a/PKGBUILD b/PKGBUILD index f8c6f98..84f491a 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -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" diff --git a/RPCommon/RPCommon.h b/RPCommon/RPCommon.h index 4165b35..e3f5e3c 100644 --- a/RPCommon/RPCommon.h +++ b/RPCommon/RPCommon.h @@ -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" diff --git a/RPCommon/keymap.h b/RPCommon/keymap.h index 7d6edd6..271ac20 100644 --- a/RPCommon/keymap.h +++ b/RPCommon/keymap.h @@ -4,14 +4,17 @@ template 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; }; diff --git a/RPCommon/unsignedfstream.h b/RPCommon/unsignedfstream.h new file mode 100644 index 0000000..4e0d739 --- /dev/null +++ b/RPCommon/unsignedfstream.h @@ -0,0 +1,53 @@ +#pragma once +#include +#include + +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(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(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]; + } +} diff --git a/test1.cpp b/test1.cpp index 882fcfc..4844eb8 100644 --- a/test1.cpp +++ b/test1.cpp @@ -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(input),bytesRead,reinterpret_cast(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(input),bytesRead,reinterpret_cast(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; + } @@ -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; diff --git a/testRead.txt b/testRead.txt new file mode 100644 index 0000000000000000000000000000000000000000..bc8f6285e659bb95b4ebdb4765ef5d13969f4f77 GIT binary patch literal 45 xcmeavFUn2K2}!Le3Cc{*C{f5rElTBLU|?`Dc6MPlQf6dgW?^Mx=imfU3;;2!32*=a literal 0 HcmV?d00001