[Add] Added a conversion method. From strings to integers.
-- Apparantely I forgot a few files in my last commit too.
This commit is contained in:
parent
c26d5f2cf1
commit
273fdaaf10
@ -37,6 +37,7 @@ HEADERS += ../src/Actor/Player.h \
|
|||||||
../src/Sprite/Sprite.h \
|
../src/Sprite/Sprite.h \
|
||||||
../src/System/Debug.h \
|
../src/System/Debug.h \
|
||||||
../src/System/ResourceManager.h \
|
../src/System/ResourceManager.h \
|
||||||
|
../src/System/Convert/str2int.h \
|
||||||
../src/System/Convert/ConvertType.h \
|
../src/System/Convert/ConvertType.h \
|
||||||
../src/System/Filesystem/InputStream.h \
|
../src/System/Filesystem/InputStream.h \
|
||||||
../src/System/Filesystem/OutputStream.h \
|
../src/System/Filesystem/OutputStream.h \
|
||||||
@ -100,6 +101,7 @@ SOURCES += ../src/Actor/Player.cpp \
|
|||||||
../src/Math/FPS.cpp \
|
../src/Math/FPS.cpp \
|
||||||
../src/Sprite/Sprite.cpp \
|
../src/Sprite/Sprite.cpp \
|
||||||
../src/System/Debug.cpp \
|
../src/System/Debug.cpp \
|
||||||
|
../src/System/Convert/str2int.cpp \
|
||||||
../src/System/Filesystem/InputStream.cpp \
|
../src/System/Filesystem/InputStream.cpp \
|
||||||
../src/System/Filesystem/OutputStream.cpp \
|
../src/System/Filesystem/OutputStream.cpp \
|
||||||
../src/System/Filesystem/FileList.cpp \
|
../src/System/Filesystem/FileList.cpp \
|
||||||
|
55
src/System/Convert/ConvertType.h
Normal file
55
src/System/Convert/ConvertType.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef INCLUDED_CASSERT
|
||||||
|
#define INCLUSED_CASSERT
|
||||||
|
#include <cassert>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace saracraft {
|
||||||
|
namespace filesystem {
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
struct ConvertBase {
|
||||||
|
enum { charCount = sizeof(T) };
|
||||||
|
union Values {
|
||||||
|
unsigned char c[charCount];
|
||||||
|
T t;
|
||||||
|
};
|
||||||
|
|
||||||
|
Values value;
|
||||||
|
|
||||||
|
int GetSize(void) const { return charCount; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
struct ConvertFrom: private ConvertBase<T> {
|
||||||
|
explicit ConvertFrom(const T& t) {
|
||||||
|
this->value.t = t;
|
||||||
|
}
|
||||||
|
using ConvertBase<T>::GetSize;
|
||||||
|
|
||||||
|
unsigned char GetByte(int index) const {
|
||||||
|
assert((index >= 0) && (index < GetSize()));
|
||||||
|
return this->value.c[index];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
struct ConvertTo: private ConvertBase<T> {
|
||||||
|
using ConvertBase<T>::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....
|
||||||
|
}
|
||||||
|
|
113
src/System/Convert/str2int.cpp
Normal file
113
src/System/Convert/str2int.cpp
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
7
src/System/Convert/str2int.h
Normal file
7
src/System/Convert/str2int.h
Normal file
@ -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.
|
||||||
|
|
68
src/System/Filesystem/Checksum.cpp
Normal file
68
src/System/Filesystem/Checksum.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include "Checksum.h"
|
||||||
|
#include "InputStreamWrapper.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
12
src/System/Filesystem/Checksum.h
Normal file
12
src/System/Filesystem/Checksum.h
Normal file
@ -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);
|
||||||
|
};
|
||||||
|
|
23
src/System/Filesystem/SCCopyFile.cpp
Normal file
23
src/System/Filesystem/SCCopyFile.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#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.
|
||||||
|
|
16
src/System/Filesystem/SCCopyFile.h
Normal file
16
src/System/Filesystem/SCCopyFile.h
Normal file
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user