diff --git a/Bin/LibD.pro b/Bin/LibD.pro index bc198c7..e1f46f3 100644 --- a/Bin/LibD.pro +++ b/Bin/LibD.pro @@ -37,6 +37,7 @@ HEADERS += ../src/Actor/Player.h \ ../src/Sprite/Sprite.h \ ../src/System/Debug.h \ ../src/System/ResourceManager.h \ + ../src/System/Convert/str2int.h \ ../src/System/Convert/ConvertType.h \ ../src/System/Filesystem/InputStream.h \ ../src/System/Filesystem/OutputStream.h \ @@ -100,6 +101,7 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/Math/FPS.cpp \ ../src/Sprite/Sprite.cpp \ ../src/System/Debug.cpp \ + ../src/System/Convert/str2int.cpp \ ../src/System/Filesystem/InputStream.cpp \ ../src/System/Filesystem/OutputStream.cpp \ ../src/System/Filesystem/FileList.cpp \ diff --git a/src/System/Convert/ConvertType.h b/src/System/Convert/ConvertType.h new file mode 100644 index 0000000..ca93ad9 --- /dev/null +++ b/src/System/Convert/ConvertType.h @@ -0,0 +1,55 @@ +#pragma once + +#ifndef INCLUDED_CASSERT +#define INCLUSED_CASSERT +#include +#endif + +namespace saracraft { +namespace filesystem { + +template +struct ConvertBase { + enum { charCount = sizeof(T) }; + union Values { + unsigned char c[charCount]; + T t; + }; + + Values value; + + int GetSize(void) const { return charCount; } +}; + +template +struct ConvertFrom: private ConvertBase { + explicit ConvertFrom(const T& t) { + this->value.t = t; + } + using ConvertBase::GetSize; + + unsigned char GetByte(int index) const { + assert((index >= 0) && (index < GetSize())); + return this->value.c[index]; + } +}; + +template +struct ConvertTo: private ConvertBase { + using ConvertBase::GetSize; + + void SetByte(int index, unsigned char c) { + assert((index >= 0) && (index < GetSize())); + this->value.c[index] = c; + } + + const T& GetValue(void) const { + return this->value.t; + } +}; + +typedef unsigned short uint16_t; + +} // Namespaces.... +} + diff --git a/src/System/Convert/str2int.cpp b/src/System/Convert/str2int.cpp new file mode 100644 index 0000000..d512d2b --- /dev/null +++ b/src/System/Convert/str2int.cpp @@ -0,0 +1,113 @@ +#include +#include "str2int.h" + +char convert_strbuf[16]; + +// I'm presuming 32 bit int.. + +int _last_str2int_errno = 0; + +char* int2str(int value) { + int tmp; + int expv; + int strpos = 0; + + if(value < 0) { + convert_strbuf[0] = '-'; + strpos++; + value = -value; + } + + for(expv = 1000000000; expv > 1; expv /= 10) { + if((value / expv) != 0) break; + } + + for( ; expv > 0; expv /= 10) { + tmp = (value/expv); + value -= tmp * expv; + convert_strbuf[strpos] = (char)('0' + tmp); + strpos++; + } + convert_strbuf[strpos] = '\0'; + return convert_strbuf; +} + +// Does not check for possible overflow.. +int str2int(const char* string) { + int i; + int len = strlen(string); + int value = 0; + int exp = 1; + + if(len > 11) { + _last_str2int_errno = 1; + return 0; + } + + for(i = len - 1; i >= 0; i--) { + if(string[i] >= '0' && string[i] <= '9') { + value+=(string[i] - '0') * exp; + exp *= 10; + } else { + if(i == 0) { + if(string[0] == '-') { + _last_str2int_errno = 0; + return -value; + } + } + _last_str2int_errno = 1; + return 0; + } + } + _last_str2int_errno = 0; + return value; +} + +int str2int_errno(void) { + return _last_str2int_errno; +} + +char* time2str(int secs) { + if(secs < 0) { + return "00:00:00"; + } + int max_secs = 99 + 99*60 + 99*60*60; + if(secs > max_secs) { + secs = max_secs; + } + + // Fill. + int hours = secs / 3600; + int mins = (secs - hours * 3600) / 60; + int sex = (secs - hours * 3600 - mins * 60); + int timeVal[3] = {hours, mins, sex}; + + int tmp; + int strpos = 0; + + for(int i = 0; i < 3; i++) { + int value = timeVal[i]; + if(value > 100) value = 99; + + int expv = 10; + if((value / expv) == 0) { + expv = 1; + // Zero padding. + convert_strbuf[strpos] = '0'; + strpos++; + } + for( ; expv > 0; expv /= 10) { + tmp = (value / expv); + value -= tmp * expv; + convert_strbuf[strpos] = (char)('0' + tmp); + strpos++; + } + if(i < 2) { + convert_strbuf[strpos] = ':'; + strpos++; + } + } + convert_strbuf[strpos] = '\0'; + return convert_strbuf; +} + diff --git a/src/System/Convert/str2int.h b/src/System/Convert/str2int.h new file mode 100644 index 0000000..1f6cece --- /dev/null +++ b/src/System/Convert/str2int.h @@ -0,0 +1,7 @@ +#pragma once + +extern char* int2str(int value); +extern int str2int(const char* string); +extern int str2int_errno(void); +extern char* time2str(int secs); // hh:mm::ss. + diff --git a/src/System/Filesystem/Checksum.cpp b/src/System/Filesystem/Checksum.cpp new file mode 100644 index 0000000..bae9d07 --- /dev/null +++ b/src/System/Filesystem/Checksum.cpp @@ -0,0 +1,68 @@ +#include "Checksum.h" +#include "InputStreamWrapper.h" +#include +#include + +using namespace saracraft; + +unsigned int Checksum::CountChecksumForFile(const char* filename) { + unsigned int chksum = 0; + int filesize = 0; + bool success = CountChecksumForFileImpl(&chksum, &filesize, filename); + if(success) { + return chksum; + } else { + assert(0); + return 0; + } +} + +bool Checksum::DoesChecksumAndSizeMatchFile(unsigned int checksum, int filesize, const char* filename) { + unsigned int chksum = 0; + int size = 0; + bool success = CountChecksumForFileImpl(&chksum, &size, filename); + if(success) { + if(chksum == checksum && filesize == size) + return true; + else + return false; + } else { + return false; + } +} + +bool Checksum::CountChecksumForFileImpl(unsigned int* checksum, int* filesize, const char* filename) { + assert(filename != NULL); + + filesystem::FB_FILE* f = filesystem::fb_fopen(filename, "rb"); + if(f == NULL) { + return false; + } + + int size = filesystem::fb_fsize(f); + + char* buf = new char[size]; + + bool success = true; + int got = filesystem::fb_fread(buf, size, 1, f); + if(got != 1) { + success = false; + } else { + success = true; + *filesize = size; + unsigned int hashCode = 1327341033; + int hashmult = 0; + for(int i = 0; i < size; i++) { + if((i % 73) == 0) + hashCode += (buf[i] << hashmult); + else + hashCode ^= (buf[i] << hashmult); + hashmult+=4; + if(hashmult > 23) hashmult -= 23; + } + delete [] buf; + filesystem::fb_fclose(f); + return success; + } +} + diff --git a/src/System/Filesystem/Checksum.h b/src/System/Filesystem/Checksum.h new file mode 100644 index 0000000..7fce821 --- /dev/null +++ b/src/System/Filesystem/Checksum.h @@ -0,0 +1,12 @@ +#pragma once + +class Checksum { +public: + static unsigned int CountChecksumForFile(const char* filename); + static bool DoesChecksumAndSizeMatchFile(unsigned int checksum, + int filesize, const char* filename); +private: + static bool CountChecksumForFileImpl(unsigned int* checksum, + int* filesize, const char* filename); +}; + diff --git a/src/System/Filesystem/SCCopyFile.cpp b/src/System/Filesystem/SCCopyFile.cpp new file mode 100644 index 0000000..c29eda4 --- /dev/null +++ b/src/System/Filesystem/SCCopyFile.cpp @@ -0,0 +1,23 @@ +#include +#include +#include "InputStream.h" +#include "FilePackageManager.h" + +#include "SCCopyFile.h" + +namespace util { + +void SCCopyFile::CopyFile(const std::string& from, const std::string& to) { + std::fstream out(to.c_str(), std::ios::out); + saracraft::filesystem::InputStream in = saracraft::filesystem::FilePackageManager::GetInstance().GetFile(from); + + std::string temp; + temp.resize(in.GetSize()); + in.Read((unsigned char*)&temp[0], in.GetSize()); + out << temp << std::endl; + + out.close(); +} + +} // Namespace util. + diff --git a/src/System/Filesystem/SCCopyFile.h b/src/System/Filesystem/SCCopyFile.h new file mode 100644 index 0000000..4c404a1 --- /dev/null +++ b/src/System/Filesystem/SCCopyFile.h @@ -0,0 +1,16 @@ +#pragma once + +namespace util { + +class SCCopyFile { +public: + + // TODO: This will currently only handle smallish text files + // propperly. + + /*** DON'T USE FOR BINARY FILES!!!! ***/ + static void CopyFile(const std::string& from, const std::string& to); +}; + +} // Namespace util. +