(String) Remade GetXBuffer, now called GetXString

And returns a standard string


Former-commit-id: 516735324a62cb6296e19d3be1960322073e5f3a
This commit is contained in:
Lynix 2015-02-03 21:16:17 +01:00
parent 1c180ca9f2
commit 25dc252666
8 changed files with 60 additions and 134 deletions

View File

@ -87,10 +87,10 @@ class NAZARA_API NzString : public NzHashable
const char* GetConstBuffer() const; const char* GetConstBuffer() const;
unsigned int GetLength() const; unsigned int GetLength() const;
unsigned int GetSize() const; unsigned int GetSize() const;
char* GetUtf8Buffer(unsigned int* size = nullptr) const; std::string GetUtf8String() const;
char16_t* GetUtf16Buffer(unsigned int* size = nullptr) const; std::u16string GetUtf16String() const;
char32_t* GetUtf32Buffer(unsigned int* size = nullptr) const; std::u32string GetUtf32String() const;
wchar_t* GetWideBuffer(unsigned int* size = nullptr) const; std::wstring GetWideString() const;
NzString GetWord(unsigned int index, nzUInt32 flags = None) const; NzString GetWord(unsigned int index, nzUInt32 flags = None) const;
unsigned int GetWordPosition(unsigned int index, nzUInt32 flags = None) const; unsigned int GetWordPosition(unsigned int index, nzUInt32 flags = None) const;

View File

@ -1776,96 +1776,63 @@ unsigned int NzString::GetSize() const
return m_sharedString->size; return m_sharedString->size;
} }
char* NzString::GetUtf8Buffer(unsigned int* size) const std::string NzString::GetUtf8String() const
{ {
if (m_sharedString->size == 0) return std::string(m_sharedString->string, m_sharedString->size);
return nullptr;
char* buffer = new char[m_sharedString->size+1];
std::memcpy(buffer, m_sharedString->string, m_sharedString->size+1);
if (size)
*size = m_sharedString->size;
return buffer;
} }
char16_t* NzString::GetUtf16Buffer(unsigned int* size) const std::u16string NzString::GetUtf16String() const
{ {
if (m_sharedString->size == 0) if (m_sharedString->size == 0)
return nullptr; return std::u16string();
std::vector<char16_t> utf16; std::u16string str;
utf16.reserve(m_sharedString->size); str.reserve(m_sharedString->size);
utf8::utf8to16(m_sharedString->string, &m_sharedString->string[m_sharedString->size], std::back_inserter(utf16)); utf8::utf8to16(begin(), end(), std::back_inserter(str));
unsigned int bufferSize = utf16.size(); return str;
if (bufferSize == 0)
return nullptr;
char16_t* buffer = new char16_t[bufferSize+1];
std::memcpy(buffer, &utf16[0], bufferSize*sizeof(char16_t));
buffer[bufferSize] ='\0';
if (size)
*size = bufferSize;
return buffer;
} }
char32_t* NzString::GetUtf32Buffer(unsigned int* size) const std::u32string NzString::GetUtf32String() const
{ {
if (m_sharedString->size == 0) if (m_sharedString->size == 0)
return nullptr; return std::u32string();
unsigned int bufferSize = utf8::distance(m_sharedString->string, &m_sharedString->string[m_sharedString->size]); std::u32string str;
if (bufferSize == 0) str.reserve(m_sharedString->size);
return nullptr;
char32_t* buffer = new char32_t[bufferSize+1]; utf8::utf8to32(begin(), end(), std::back_inserter(str));
utf8::utf8to32(m_sharedString->string, &m_sharedString->string[m_sharedString->size], buffer);
buffer[bufferSize] ='\0';
if (size) return str;
*size = bufferSize;
return buffer;
} }
wchar_t* NzString::GetWideBuffer(unsigned int* size) const std::wstring NzString::GetWideString() const
{ {
static_assert(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4, "wchar_t size is not supported"); static_assert(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4, "wchar_t size is not supported");
if (m_sharedString->size == 0) if (m_sharedString->size == 0)
return nullptr; return std::wstring();
unsigned int bufferSize = utf8::distance(m_sharedString->string, &m_sharedString->string[m_sharedString->size]); std::wstring str;
if (bufferSize == 0) str.reserve(m_sharedString->size);
return nullptr;
wchar_t* buffer = new wchar_t[bufferSize+1];
if (sizeof(wchar_t) == 4) // Je veux du static_if :( if (sizeof(wchar_t) == 4) // Je veux du static_if :(
utf8::utf8to32(m_sharedString->string, &m_sharedString->string[m_sharedString->size], buffer); utf8::utf8to32(begin(), end(), std::back_inserter(str));
else else
{ {
wchar_t* ptr = buffer;
utf8::unchecked::iterator<const char*> it(m_sharedString->string); utf8::unchecked::iterator<const char*> it(m_sharedString->string);
do do
{ {
char32_t cp = *it; char32_t cp = *it;
if (cp <= 0xFFFF && (cp < 0xD800 || cp > 0xDFFF)) // @Laurent Gomila if (cp <= 0xFFFF && (cp < 0xD800 || cp > 0xDFFF)) // @Laurent Gomila
*ptr++ = static_cast<wchar_t>(cp); str.push_back(static_cast<wchar_t>(cp));
else else
*ptr++ = L'?'; str.push_back(L'?');
} }
while (*it++); while (*it++);
} }
if (size) return str;
*size = bufferSize;
return buffer;
} }
NzString NzString::GetWord(unsigned int index, nzUInt32 flags) const NzString NzString::GetWord(unsigned int index, nzUInt32 flags) const

View File

@ -62,9 +62,7 @@ bool NzDirectoryImpl::Open(const NzString& dirPath)
{ {
NzString searchPath = dirPath + "\\*"; NzString searchPath = dirPath + "\\*";
std::unique_ptr<wchar_t[]> wPath(searchPath.GetWideBuffer()); m_handle = FindFirstFileW(searchPath.GetWideString().data(), &m_result);
m_handle = FindFirstFileW(wPath.get(), &m_result);
if (m_handle == INVALID_HANDLE_VALUE) if (m_handle == INVALID_HANDLE_VALUE)
{ {
NazaraError("Unable to open directory: " + NzError::GetLastSystemError()); NazaraError("Unable to open directory: " + NzError::GetLastSystemError());
@ -78,16 +76,12 @@ bool NzDirectoryImpl::Open(const NzString& dirPath)
bool NzDirectoryImpl::Create(const NzString& dirPath) bool NzDirectoryImpl::Create(const NzString& dirPath)
{ {
std::unique_ptr<wchar_t[]> wPath(dirPath.GetWideBuffer()); return (CreateDirectoryW(dirPath.GetWideString().data(), nullptr) != 0) || GetLastError() == ERROR_ALREADY_EXISTS;
return (CreateDirectoryW(wPath.get(), nullptr) != 0) || GetLastError() == ERROR_ALREADY_EXISTS;
} }
bool NzDirectoryImpl::Exists(const NzString& dirPath) bool NzDirectoryImpl::Exists(const NzString& dirPath)
{ {
std::unique_ptr<wchar_t[]> wPath(dirPath.GetWideBuffer()); DWORD attributes = GetFileAttributesW(dirPath.GetWideString().data());
DWORD attributes = GetFileAttributesW(wPath.get());
if (attributes != INVALID_FILE_ATTRIBUTES) if (attributes != INVALID_FILE_ATTRIBUTES)
return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
else else
@ -118,8 +112,7 @@ NzString NzDirectoryImpl::GetCurrent()
bool NzDirectoryImpl::Remove(const NzString& dirPath) bool NzDirectoryImpl::Remove(const NzString& dirPath)
{ {
std::unique_ptr<wchar_t[]> path(dirPath.GetWideBuffer()); bool success = RemoveDirectoryW(dirPath.GetWideString().data()) != 0;
bool success = RemoveDirectoryW(path.get()) != 0;
DWORD error = GetLastError(); DWORD error = GetLastError();
return success || error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND; return success || error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND;

View File

@ -30,9 +30,7 @@ bool NzDynLibImpl::Load(const NzString& libraryPath, NzString* errorMessage)
if (!path.EndsWith(".dll")) if (!path.EndsWith(".dll"))
path += ".dll"; path += ".dll";
std::unique_ptr<wchar_t[]> wPath(path.GetWideBuffer()); m_handle = LoadLibraryExW(path.GetWideString().data(), nullptr, (NzFile::IsAbsolute(path)) ? LOAD_WITH_ALTERED_SEARCH_PATH : 0);
m_handle = LoadLibraryExW(wPath.get(), nullptr, (NzFile::IsAbsolute(path)) ? LOAD_WITH_ALTERED_SEARCH_PATH : 0);
if (m_handle) if (m_handle)
return true; return true;
else else

View File

@ -92,9 +92,7 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
if ((mode & NzFile::Lock) == 0) if ((mode & NzFile::Lock) == 0)
shareMode |= FILE_SHARE_WRITE; shareMode |= FILE_SHARE_WRITE;
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); m_handle = CreateFileW(filePath.GetWideString().data(), access, shareMode, nullptr, openMode, 0, nullptr);
m_handle = CreateFileW(path.get(), access, shareMode, nullptr, openMode, 0, nullptr);
return m_handle != INVALID_HANDLE_VALUE; return m_handle != INVALID_HANDLE_VALUE;
} }
@ -184,117 +182,96 @@ std::size_t NzFileImpl::Write(const void* buffer, std::size_t size)
bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath) bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath)
{ {
std::unique_ptr<wchar_t[]> srcPath(sourcePath.GetWideBuffer()); if (CopyFileW(sourcePath.GetWideString().data(), targetPath.GetWideString().data(), false))
std::unique_ptr<wchar_t[]> dstPath(targetPath.GetWideBuffer());
if (CopyFileW(srcPath.get(), dstPath.get(), false))
return true; return true;
else else
{ {
NazaraError("Failed to copy file: " + NzError::GetLastSystemError()); NazaraError("Failed to copy file: " + NzError::GetLastSystemError());
return false; return false;
} }
} }
bool NzFileImpl::Delete(const NzString& filePath) bool NzFileImpl::Delete(const NzString& filePath)
{ {
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); if (DeleteFileW(filePath.GetWideString().data()))
if (DeleteFileW(path.get()))
return true; return true;
else else
{ {
NazaraError("Failed to delete file (" + filePath + "): " + NzError::GetLastSystemError()); NazaraError("Failed to delete file (" + filePath + "): " + NzError::GetLastSystemError());
return false; return false;
} }
} }
bool NzFileImpl::Exists(const NzString& filePath) bool NzFileImpl::Exists(const NzString& filePath)
{ {
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); HANDLE handle = CreateFileW(filePath.GetWideString().data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return false; return false;
CloseHandle(handle); CloseHandle(handle);
return true; return true;
} }
time_t NzFileImpl::GetCreationTime(const NzString& filePath) time_t NzFileImpl::GetCreationTime(const NzString& filePath)
{ {
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); HANDLE handle = CreateFileW(filePath.GetWideString().data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
FILETIME creationTime; FILETIME creationTime;
if (!GetFileTime(handle, &creationTime, nullptr, nullptr)) if (!GetFileTime(handle, &creationTime, nullptr, nullptr))
{ {
NazaraError("Unable to get creation time: " + NzError::GetLastSystemError());
CloseHandle(handle); CloseHandle(handle);
NazaraError("Unable to get creation time: " + NzError::GetLastSystemError());
return 0; return 0;
} }
CloseHandle(handle); CloseHandle(handle);
return NzFileTimeToTime(&creationTime); return NzFileTimeToTime(&creationTime);
} }
time_t NzFileImpl::GetLastAccessTime(const NzString& filePath) time_t NzFileImpl::GetLastAccessTime(const NzString& filePath)
{ {
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); HANDLE handle = CreateFileW(filePath.GetWideString().data(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
FILETIME accessTime; FILETIME accessTime;
if (!GetFileTime(handle, nullptr, &accessTime, nullptr)) if (!GetFileTime(handle, nullptr, &accessTime, nullptr))
{ {
NazaraError("Unable to get last access time: " + NzError::GetLastSystemError());
CloseHandle(handle); CloseHandle(handle);
NazaraError("Unable to get last access time: " + NzError::GetLastSystemError());
return 0; return 0;
} }
CloseHandle(handle); CloseHandle(handle);
return NzFileTimeToTime(&accessTime); return NzFileTimeToTime(&accessTime);
} }
time_t NzFileImpl::GetLastWriteTime(const NzString& filePath) time_t NzFileImpl::GetLastWriteTime(const NzString& filePath)
{ {
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); HANDLE handle = CreateFileW(filePath.GetWideString().data(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
FILETIME writeTime; FILETIME writeTime;
if (!GetFileTime(handle, nullptr, nullptr, &writeTime)) if (!GetFileTime(handle, nullptr, nullptr, &writeTime))
{ {
NazaraError("Unable to get last write time: " + NzError::GetLastSystemError());
CloseHandle(handle); CloseHandle(handle);
NazaraError("Unable to get last write time: " + NzError::GetLastSystemError());
return 0; return 0;
} }
CloseHandle(handle); CloseHandle(handle);
return NzFileTimeToTime(&writeTime); return NzFileTimeToTime(&writeTime);
} }
nzUInt64 NzFileImpl::GetSize(const NzString& filePath) nzUInt64 NzFileImpl::GetSize(const NzString& filePath)
{ {
std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer()); HANDLE handle = CreateFileW(filePath.GetWideString().data(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
@ -303,16 +280,12 @@ nzUInt64 NzFileImpl::GetSize(const NzString& filePath)
fileSize.QuadPart = 0; fileSize.QuadPart = 0;
CloseHandle(handle); CloseHandle(handle);
return fileSize.QuadPart; return fileSize.QuadPart;
} }
bool NzFileImpl::Rename(const NzString& sourcePath, const NzString& targetPath) bool NzFileImpl::Rename(const NzString& sourcePath, const NzString& targetPath)
{ {
std::unique_ptr<wchar_t[]> srcPath(sourcePath.GetWideBuffer()); if (MoveFileExW(sourcePath.GetWideString().data(), targetPath.GetWideString().data(), MOVEFILE_COPY_ALLOWED))
std::unique_ptr<wchar_t[]> dstPath(targetPath.GetWideBuffer());
if (MoveFileExW(srcPath.get(), dstPath.get(), MOVEFILE_COPY_ALLOWED))
return true; return true;
else else
{ {

View File

@ -253,9 +253,9 @@ bool NzFont::Precache(unsigned int characterSize, nzUInt32 style, char32_t chara
bool NzFont::Precache(unsigned int characterSize, nzUInt32 style, const NzString& characterSet) const bool NzFont::Precache(unsigned int characterSize, nzUInt32 style, const NzString& characterSet) const
{ {
unsigned int size; ///TODO: Itération UTF-8 => UTF-32 sans allocation de buffer (Exposer utf8cpp ?)
std::unique_ptr<char32_t[]> characters(characterSet.GetUtf32Buffer(&size)); std::u32string set = characterSet.GetUtf32String();
if (!characters) if (set.empty())
{ {
NazaraError("Invalid character set"); NazaraError("Invalid character set");
return false; return false;
@ -263,8 +263,8 @@ bool NzFont::Precache(unsigned int characterSize, nzUInt32 style, const NzString
nzUInt64 key = ComputeKey(characterSize, style); nzUInt64 key = ComputeKey(characterSize, style);
auto& glyphMap = m_glyphes[key]; auto& glyphMap = m_glyphes[key];
for (unsigned int i = 0; i < size; ++i) for (char32_t character : set)
PrecacheGlyph(glyphMap, characterSize, style, characters[i]); PrecacheGlyph(glyphMap, characterSize, style, character);
return true; return true;
} }

View File

@ -192,9 +192,8 @@ void NzSimpleTextDrawer::UpdateGlyphs() const
return; return;
///TODO: Itération UTF-8 => UTF-32 sans allocation de buffer (Exposer utf8cpp ?) ///TODO: Itération UTF-8 => UTF-32 sans allocation de buffer (Exposer utf8cpp ?)
unsigned int size; std::u32string characters = m_text.GetUtf32String();
std::unique_ptr<char32_t[]> characters(m_text.GetUtf32Buffer(&size)); if (characters.empty())
if (!characters)
{ {
NazaraError("Invalid character set"); NazaraError("Invalid character set");
return; return;
@ -208,12 +207,11 @@ void NzSimpleTextDrawer::UpdateGlyphs() const
// On calcule les bornes en flottants pour accélérer les calculs (il est coûteux de changer de type trop souvent) // On calcule les bornes en flottants pour accélérer les calculs (il est coûteux de changer de type trop souvent)
bool firstGlyph = true; bool firstGlyph = true;
NzRectf textBounds = NzRectf::Zero(); NzRectf textBounds = NzRectf::Zero();
m_glyphs.reserve(size);
nzUInt32 previousCharacter = 0; nzUInt32 previousCharacter = 0;
for (unsigned int i = 0; i < size; ++i)
{
char32_t character = characters[i];
m_glyphs.reserve(characters.size());
for (char32_t character : characters)
{
if (previousCharacter != 0) if (previousCharacter != 0)
drawPos.x += m_font->GetKerning(m_characterSize, previousCharacter, character); drawPos.x += m_font->GetKerning(m_characterSize, previousCharacter, character);
@ -291,8 +289,8 @@ void NzSimpleTextDrawer::UpdateGlyphs() const
firstGlyph = false; firstGlyph = false;
} }
for (unsigned int j = 0; j < 4; ++j) for (unsigned int i = 0; i < 4; ++i)
textBounds.ExtendTo(glyph.corners[j]); textBounds.ExtendTo(glyph.corners[i]);
drawPos.x += advance; drawPos.x += advance;
m_glyphs.push_back(glyph); m_glyphs.push_back(glyph);

View File

@ -145,8 +145,6 @@ bool NzWindowImpl::Create(const NzVideoMode& mode, const NzString& title, nzUInt
m_callback = 0; m_callback = 0;
std::unique_ptr<wchar_t[]> wtitle(title.GetWideBuffer());
#if NAZARA_UTILITY_THREADED_WINDOW #if NAZARA_UTILITY_THREADED_WINDOW
NzMutex mutex; NzMutex mutex;
NzConditionVariable condition; NzConditionVariable condition;
@ -154,11 +152,11 @@ bool NzWindowImpl::Create(const NzVideoMode& mode, const NzString& title, nzUInt
// On attend que la fenêtre soit créée // On attend que la fenêtre soit créée
mutex.Lock(); mutex.Lock();
m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle.get(), win32Style, x, y, width, height, this, &mutex, &condition); m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, title.GetWideString().data(), win32Style, x, y, width, height, this, &mutex, &condition);
condition.Wait(&mutex); condition.Wait(&mutex);
mutex.Unlock(); mutex.Unlock();
#else #else
m_handle = CreateWindowExW(win32StyleEx, className, wtitle.get(), win32Style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), this); m_handle = CreateWindowExW(win32StyleEx, className, title.GetWideString().data(), win32Style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), this);
#endif #endif
if (!m_handle) if (!m_handle)
@ -445,8 +443,7 @@ void NzWindowImpl::SetStayOnTop(bool stayOnTop)
void NzWindowImpl::SetTitle(const NzString& title) void NzWindowImpl::SetTitle(const NzString& title)
{ {
std::unique_ptr<wchar_t[]> wTitle(title.GetWideBuffer()); SetWindowTextW(m_handle, title.GetWideString().data());
SetWindowTextW(m_handle, wTitle.get());
} }
void NzWindowImpl::SetVisible(bool visible) void NzWindowImpl::SetVisible(bool visible)