From cef402c8a5344f9c687e1b2a4c873051e6f46b15 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 15 May 2012 13:26:35 +0200 Subject: [PATCH] Added NzInputStream Added NzFile::Read(buffer, size) NzFile::Read(nullptr, size) now acts as a skip function (ex: SetCursorPos(GetCursorPos() + size) --- include/Nazara/Core/Config.hpp | 3 ++ include/Nazara/Core/Error.hpp | 1 + include/Nazara/Core/File.hpp | 4 ++- include/Nazara/Core/InputStream.hpp | 21 +++++++++++++ include/Nazara/Core/String.hpp | 8 ++--- src/Nazara/Core/Directory.cpp | 1 + src/Nazara/Core/File.cpp | 22 +++++++++++--- src/Nazara/Core/Hash/SHA/Internal.cpp | 12 +++++--- src/Nazara/Core/Hash/SHA/Internal.hpp | 4 +-- src/Nazara/Core/HashDigest.cpp | 1 + src/Nazara/Core/InputStream.cpp | 8 +++++ src/Nazara/Core/Log.cpp | 1 + src/Nazara/Core/String.cpp | 40 ++++++++++++------------- src/Nazara/Core/Thread.cpp | 1 + src/Nazara/Core/Unicode.cpp | 1 + src/Nazara/Core/Win32/FileImpl.cpp | 21 +++++-------- src/Nazara/Core/Win32/SemaphoreImpl.cpp | 1 + 17 files changed, 101 insertions(+), 49 deletions(-) create mode 100644 include/Nazara/Core/InputStream.hpp create mode 100644 src/Nazara/Core/InputStream.cpp diff --git a/include/Nazara/Core/Config.hpp b/include/Nazara/Core/Config.hpp index 942e532f8..ebc710456 100644 --- a/include/Nazara/Core/Config.hpp +++ b/include/Nazara/Core/Config.hpp @@ -71,6 +71,9 @@ #define NAZARA_THREADSAFETY_STRINGSTREAM 0 // NzStringStream #endif +// Optimise certaines parties du code avec les avancées venues de Windows Vista (Nécessite Vista ou supérieur et compilateur compatible) +#define NAZARA_CORE_WINDOWS_VISTA 0 + /* // Règle le temps entre le réveil du thread des timers et l'activation d'un timer (En millisecondes) #define NAZARA_CORE_TIMER_WAKEUPTIME 10 diff --git a/include/Nazara/Core/Error.hpp b/include/Nazara/Core/Error.hpp index 30af17177..056fae28a 100644 --- a/include/Nazara/Core/Error.hpp +++ b/include/Nazara/Core/Error.hpp @@ -8,6 +8,7 @@ #define NAZARA_ERROR_HPP #include +#include #include #if NAZARA_CORE_ENABLE_ASSERTS || defined(NAZARA_DEBUG) diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index 2af83c1bc..79b8fc42f 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -20,7 +21,7 @@ class NzFileImpl; -class NAZARA_API NzFile : public NzHashable, NzNonCopyable +class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable { public: enum CursorPosition @@ -74,6 +75,7 @@ class NAZARA_API NzFile : public NzHashable, NzNonCopyable bool Open(unsigned long openMode = Current); + std::size_t Read(void* buffer, std::size_t size); std::size_t Read(void* buffer, std::size_t typeSize, unsigned int count); bool Rename(const NzString& newFilePath); diff --git a/include/Nazara/Core/InputStream.hpp b/include/Nazara/Core/InputStream.hpp new file mode 100644 index 000000000..71766e556 --- /dev/null +++ b/include/Nazara/Core/InputStream.hpp @@ -0,0 +1,21 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_INPUTSTREAM_HPP +#define NAZARA_INPUTSTREAM_HPP + +#include + +class NzInputStream +{ + public: + virtual ~NzInputStream(); + + virtual bool EndOfFile() const = 0; + virtual std::size_t Read(void* buffer, std::size_t size) = 0; +}; + +#endif // NAZARA_INPUTSTREAM_HPP diff --git a/include/Nazara/Core/String.hpp b/include/Nazara/Core/String.hpp index 86eee15be..b120661a7 100644 --- a/include/Nazara/Core/String.hpp +++ b/include/Nazara/Core/String.hpp @@ -283,19 +283,19 @@ class NAZARA_API NzString : public NzHashable { } - SharedString(unsigned int bufferSize, unsigned int stringSize, unsigned short referenceCount, char* str) : + SharedString(unsigned short referenceCount, unsigned int bufferSize, unsigned int stringSize, char* str) : allocatedSize(bufferSize), size(stringSize), - refCount(referenceCount), - string(str) + string(str), + refCount(referenceCount) { } unsigned int allocatedSize; unsigned int size; - unsigned short refCount; char* string; + unsigned short refCount; NazaraMutex(mutex) }; diff --git a/src/Nazara/Core/Directory.cpp b/src/Nazara/Core/Directory.cpp index 5e45db71b..654f3bdec 100644 --- a/src/Nazara/Core/Directory.cpp +++ b/src/Nazara/Core/Directory.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include //#include diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index b2b8f96c2..d50cbae27 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -270,7 +270,7 @@ bool NzFile::IsOpen() const return m_impl != nullptr; } -std::size_t NzFile::Read(void* buffer, std::size_t typeSize, unsigned int count) +std::size_t NzFile::Read(void* buffer, std::size_t size) { NazaraLock(m_mutex) @@ -288,14 +288,28 @@ std::size_t NzFile::Read(void* buffer, std::size_t typeSize, unsigned int count) } #endif - if (!buffer || count == 0 || typeSize == 0) + if (size == 0) return 0; - std::size_t byteRead = m_impl->Read(buffer, typeSize*count); + if (buffer) + return m_impl->Read(buffer, size); + else + { + nzUInt64 currentPos = m_impl->GetCursorPos(); + + m_impl->SetCursorPos(NzFile::AtCurrent, size); + + return m_impl->GetCursorPos()-currentPos; + } +} + +std::size_t NzFile::Read(void* buffer, std::size_t typeSize, unsigned int count) +{ + std::size_t byteRead = Read(buffer, typeSize*count); if (byteRead == 0) return 0; - if (m_endianness != nzEndianness_Unknown && m_endianness != NzGetPlatformEndianness() && typeSize != 1) + if (buffer && m_endianness != nzEndianness_Unknown && m_endianness != NzGetPlatformEndianness() && typeSize != 1) { unsigned int typeCount = byteRead/typeSize; for (unsigned int i = 0; i < typeCount; ++i) diff --git a/src/Nazara/Core/Hash/SHA/Internal.cpp b/src/Nazara/Core/Hash/SHA/Internal.cpp index eb06e7107..0ab51a9ce 100644 --- a/src/Nazara/Core/Hash/SHA/Internal.cpp +++ b/src/Nazara/Core/Hash/SHA/Internal.cpp @@ -761,15 +761,18 @@ void SHA256_End(SHA_CTX* context, nzUInt8* digest) /*** SHA-224: *********************************************************/ -void SHA224_Init(SHA_CTX* context) { +void SHA224_Init(SHA_CTX* context) +{ SHA256_Internal_Init(context, sha224_initial_hash_value); } -void SHA224_Internal_Transform(SHA_CTX* context, const nzUInt32* data) { +void SHA224_Internal_Transform(SHA_CTX* context, const nzUInt32* data) +{ SHA256_Internal_Transform(context, data); } -void SHA224_Update(SHA_CTX* context, const nzUInt8 *data, std::size_t len) { +void SHA224_Update(SHA_CTX* context, const nzUInt8 *data, std::size_t len) +{ SHA256_Update(context, data, len); } @@ -807,7 +810,8 @@ void SHA512_Internal_Init(SHA_CTX* context, const nzUInt64* ihv) context->s512.bitcount[0] = context->s512.bitcount[1] = 0; } -void SHA512_Init(SHA_CTX* context) { +void SHA512_Init(SHA_CTX* context) +{ SHA512_Internal_Init(context, sha512_initial_hash_value); } diff --git a/src/Nazara/Core/Hash/SHA/Internal.hpp b/src/Nazara/Core/Hash/SHA/Internal.hpp index bdf951f5f..8230964b0 100644 --- a/src/Nazara/Core/Hash/SHA/Internal.hpp +++ b/src/Nazara/Core/Hash/SHA/Internal.hpp @@ -42,6 +42,8 @@ #ifndef NAZARA_HASH_SHA2_INTERNAL_HPP #define NAZARA_HASH_SHA2_INTERNAL_HPP +#include + /* Digest lengths for SHA-1/224/256/384/512 */ #define SHA1_DIGEST_LENGTH 20 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1) @@ -54,8 +56,6 @@ #define SHA512_DIGEST_LENGTH 64 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) -#include - union SHA_CTX { /* SHA-1 uses this part of the union: */ diff --git a/src/Nazara/Core/HashDigest.cpp b/src/Nazara/Core/HashDigest.cpp index 453573cf3..e41666b55 100644 --- a/src/Nazara/Core/HashDigest.cpp +++ b/src/Nazara/Core/HashDigest.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include diff --git a/src/Nazara/Core/InputStream.cpp b/src/Nazara/Core/InputStream.cpp new file mode 100644 index 000000000..d4705061d --- /dev/null +++ b/src/Nazara/Core/InputStream.cpp @@ -0,0 +1,8 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +NzInputStream::~NzInputStream() = default; diff --git a/src/Nazara/Core/Log.cpp b/src/Nazara/Core/Log.cpp index 1c60ce7dd..1b338c60f 100644 --- a/src/Nazara/Core/Log.cpp +++ b/src/Nazara/Core/Log.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include diff --git a/src/Nazara/Core/String.cpp b/src/Nazara/Core/String.cpp index 740e323b2..dbed3d288 100644 --- a/src/Nazara/Core/String.cpp +++ b/src/Nazara/Core/String.cpp @@ -3269,7 +3269,7 @@ NzString NzString::Resized(int size, char character) const str[newSize] = '\0'; - return NzString(new SharedString(newSize, newSize, 1, str)); + return NzString(new SharedString(1, newSize, newSize, str)); } NzString& NzString::Reverse() @@ -3302,7 +3302,7 @@ NzString NzString::Reversed() const str[m_sharedString->size] = '\0'; - return NzString(new SharedString(m_sharedString->size, m_sharedString->size, 1, str)); + return NzString(new SharedString(1, m_sharedString->size, m_sharedString->size, str)); } NzString NzString::Simplified(nzUInt32 flags) const @@ -3360,7 +3360,7 @@ NzString NzString::Simplified(nzUInt32 flags) const *p = '\0'; - return NzString(new SharedString(m_sharedString->size, p-str, 1, str)); + return NzString(new SharedString(1, m_sharedString->size, p-str, str)); } NzString& NzString::Simplify(nzUInt32 flags) @@ -3691,7 +3691,7 @@ NzString NzString::Substr(int startPos, int endPos) const std::memcpy(str, &m_sharedString->string[start], size*sizeof(char)); str[size] = '\0'; - return NzString(new SharedString(size, size, 1, str)); + return NzString(new SharedString(1, size, size, str)); } NzString NzString::SubstrFrom(char character, int startPos, bool fromLast, bool include, nzUInt32 flags) const @@ -3890,7 +3890,7 @@ NzString NzString::ToLower(nzUInt32 flags) const *s = '\0'; - return NzString(new SharedString(m_sharedString->size, m_sharedString->size, 1, str)); + return NzString(new SharedString(1, m_sharedString->size, m_sharedString->size, str)); } } @@ -3922,7 +3922,7 @@ NzString NzString::ToUpper(nzUInt32 flags) const *s = '\0'; - return NzString(new SharedString(m_sharedString->size, m_sharedString->size, 1, str)); + return NzString(new SharedString(1, m_sharedString->size, m_sharedString->size, str)); } } @@ -4206,7 +4206,7 @@ NzString NzString::operator+(char character) const str[m_sharedString->size] = character; str[totalSize] = '\0'; - return NzString(new SharedString(totalSize, totalSize, 1, str)); + return NzString(new SharedString(1, totalSize, totalSize, str)); } NzString NzString::operator+(const char* string) const @@ -4226,7 +4226,7 @@ NzString NzString::operator+(const char* string) const std::memcpy(str, m_sharedString->string, m_sharedString->size*sizeof(char)); std::strcpy(&str[m_sharedString->size], string); - return NzString(new SharedString(totalSize, totalSize, 1, str)); + return NzString(new SharedString(1, totalSize, totalSize, str)); } NzString NzString::operator+(const std::string& string) const @@ -4242,7 +4242,7 @@ NzString NzString::operator+(const std::string& string) const std::memcpy(str, m_sharedString->string, m_sharedString->size*sizeof(char)); std::strcpy(&str[m_sharedString->size], string.c_str()); - return NzString(new SharedString(totalSize, totalSize, 1, str)); + return NzString(new SharedString(1, totalSize, totalSize, str)); } NzString NzString::operator+(const NzString& string) const @@ -4258,7 +4258,7 @@ NzString NzString::operator+(const NzString& string) const std::memcpy(str, m_sharedString->string, m_sharedString->size*sizeof(char)); std::strcpy(&str[m_sharedString->size], string.m_sharedString->string); - return NzString(new SharedString(totalSize, totalSize, 1, str)); + return NzString(new SharedString(1, totalSize, totalSize, str)); } NzString& NzString::operator+=(char character) @@ -4610,7 +4610,7 @@ NzString NzString::Boolean(bool boolean) char* str = new char[size+1]; std::strcpy(str, (boolean) ? "true" : "false"); - return NzString(new SharedString(size, size, 1, str)); + return NzString(new SharedString(1, size, size, str)); } int NzString::Compare(const NzString& first, const NzString& second) @@ -4707,7 +4707,7 @@ NzString NzString::Pointer(const void* ptr) char* str = new char[size+1]; std::sprintf(str, "%p", ptr); - return NzString(new SharedString(size, size, 1, str)); + return NzString(new SharedString(1, size, size, str)); } NzString NzString::Unicode(char32_t character) @@ -4729,7 +4729,7 @@ NzString NzString::Unicode(char32_t character) utf8::append(character, str); str[count] = '\0'; - return NzString(new SharedString(count, count, 1, str)); + return NzString(new SharedString(1, count, count, str)); } NzString NzString::Unicode(const char* u8String) @@ -4754,7 +4754,7 @@ NzString NzString::Unicode(const char16_t* u16String) char* r = utf8::utf16to8(u16String, ptr, str); *r = '\0'; - return NzString(new SharedString(count, r-str, 1, str)); + return NzString(new SharedString(1, count, r-str, str)); } NzString NzString::Unicode(const char32_t* u32String) @@ -4782,7 +4782,7 @@ NzString NzString::Unicode(const char32_t* u32String) char* r = utf8::utf32to8(u32String, ptr, str); *r = '\0'; - return NzString(new SharedString(count, count, 1, str)); + return NzString(new SharedString(1, count, count, str)); } NzString NzString::Unicode(const wchar_t* wString) @@ -4810,7 +4810,7 @@ NzString NzString::Unicode(const wchar_t* wString) char* r = utf8::utf32to8(wString, ptr, str); *r = '\0'; - return NzString(new SharedString(count, count, 1, str)); + return NzString(new SharedString(1, count, count, str)); } std::istream& operator>>(std::istream& is, NzString& str) @@ -4859,7 +4859,7 @@ NzString operator+(char character, const NzString& string) str[0] = character; std::strcpy(str, string.m_sharedString->string); - return NzString(new NzString::SharedString(totalSize, totalSize, 1, str)); + return NzString(new NzString::SharedString(1, totalSize, totalSize, str)); } NzString operator+(const char* string, const NzString& nstring) @@ -4876,7 +4876,7 @@ NzString operator+(const char* string, const NzString& nstring) std::memcpy(str, string, size*sizeof(char)); std::strcpy(&str[size], nstring.m_sharedString->string); - return NzString(new NzString::SharedString(totalSize, totalSize, 1, str)); + return NzString(new NzString::SharedString(1, totalSize, totalSize, str)); } NzString operator+(const std::string& string, const NzString& nstring) @@ -4892,7 +4892,7 @@ NzString operator+(const std::string& string, const NzString& nstring) std::memcpy(str, string.c_str(), string.size()*sizeof(char)); std::strcpy(&str[string.size()], nstring.m_sharedString->string); - return NzString(new NzString::SharedString(totalSize, totalSize, 1, str)); + return NzString(new NzString::SharedString(1, totalSize, totalSize, str)); } bool operator==(const NzString& first, const NzString& second) @@ -5040,7 +5040,7 @@ void NzString::EnsureOwnership() char* string = new char[m_sharedString->allocatedSize+1]; std::strcpy(string, m_sharedString->string); - m_sharedString = new SharedString(m_sharedString->allocatedSize, m_sharedString->size, 1, string); + m_sharedString = new SharedString(1, m_sharedString->allocatedSize, m_sharedString->size, string); } } diff --git a/src/Nazara/Core/Thread.cpp b/src/Nazara/Core/Thread.cpp index 81cdb2fa8..5938fe305 100644 --- a/src/Nazara/Core/Thread.cpp +++ b/src/Nazara/Core/Thread.cpp @@ -5,6 +5,7 @@ // Inspiré du code de la SFML par Laurent Gomila #include +#include #include #if defined(NAZARA_PLATFORM_WINDOWS) diff --git a/src/Nazara/Core/Unicode.cpp b/src/Nazara/Core/Unicode.cpp index 575ad65d1..da7d59f5d 100644 --- a/src/Nazara/Core/Unicode.cpp +++ b/src/Nazara/Core/Unicode.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include namespace NzUnicode diff --git a/src/Nazara/Core/Win32/FileImpl.cpp b/src/Nazara/Core/Win32/FileImpl.cpp index ce00babe7..e7546b593 100644 --- a/src/Nazara/Core/Win32/FileImpl.cpp +++ b/src/Nazara/Core/Win32/FileImpl.cpp @@ -151,19 +151,15 @@ bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath) { wchar_t* path = sourcePath.GetWideBuffer(); wchar_t* newPath = targetPath.GetWideBuffer(); + bool success = CopyFileW(path, newPath, false); + delete[] path; + delete[] newPath; - if (CopyFileW(path, newPath, false)) - { - delete[] path; - delete[] newPath; - + if (success) return true; - } else { NazaraError("Unable to copy file: " + NzGetLastSystemError()); - delete[] path; - delete[] newPath; return false; } @@ -172,17 +168,14 @@ bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath) bool NzFileImpl::Delete(const NzString& filePath) { wchar_t* path = filePath.GetWideBuffer(); + bool success = DeleteFileW(path); + delete[] path; - if (DeleteFileW(path)) - { - delete[] path; - + if (success) return true; - } else { NazaraError("Unable to delete file (" + filePath + "): " + NzGetLastSystemError()); - delete[] path; return false; } diff --git a/src/Nazara/Core/Win32/SemaphoreImpl.cpp b/src/Nazara/Core/Win32/SemaphoreImpl.cpp index 3fba23de5..c9df1ad4f 100644 --- a/src/Nazara/Core/Win32/SemaphoreImpl.cpp +++ b/src/Nazara/Core/Win32/SemaphoreImpl.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include