Changeset View
Changeset View
Standalone View
Standalone View
source/ps/CStr.cpp
/* Copyright (C) 2014 Wildfire Games. | /* Copyright (C) 2017 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 13 Lines | |||||
**/ | **/ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#ifndef CStr_CPP_FIRST | #ifndef CStr_CPP_FIRST | ||||
#define CStr_CPP_FIRST | #define CStr_CPP_FIRST | ||||
#include "lib/fnv_hash.h" | #include "lib/fnv_hash.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "lib/byte_order.h" | |||||
leper: is this one still needed? | |||||
#include "network/Serialization.h" | |||||
#include <iomanip> | #include <iomanip> | ||||
#include <sstream> | #include <sstream> | ||||
#define UNIDOUBLER_HEADER "CStr.cpp" | #define UNIDOUBLER_HEADER "CStr.cpp" | ||||
#include "UniDoubler.h" | #include "UniDoubler.h" | ||||
// Only include these function definitions in the first instance of CStr.cpp: | // Only include these function definitions in the first instance of CStr.cpp: | ||||
/** | /** | ||||
* Convert CStr to UTF-8 | * Convert CStr to UTF-8 | ||||
* | * | ||||
* @return CStr8 converted string | * @return CStr8 converted string | ||||
**/ | **/ | ||||
CStr8 CStrW::ToUTF8() const | CStr8 CStrW::ToUTF8() const | ||||
▲ Show 20 Lines • Show All 380 Lines • ▼ Show 20 Lines | |||||
size_t CStr::GetHashCode() const | size_t CStr::GetHashCode() const | ||||
{ | { | ||||
return (size_t)fnv_hash(data(), length()*sizeof(value_type)); | return (size_t)fnv_hash(data(), length()*sizeof(value_type)); | ||||
// janwas 2005-03-18: now use 32-bit version; 64 is slower and | // janwas 2005-03-18: now use 32-bit version; 64 is slower and | ||||
// the result was truncated down to 32 anyway. | // the result was truncated down to 32 anyway. | ||||
} | } | ||||
#ifdef _UNICODE | |||||
/* | |||||
CStrW is always serialized to/from UTF-16 | |||||
*/ | |||||
u8* CStrW::Serialize(u8* buffer) const | |||||
{ | |||||
size_t len = length(); | |||||
size_t i = 0; | |||||
for (i = 0; i < len; i++) | |||||
{ | |||||
const u16 bigEndian = to_be16((*this)[i]); | |||||
*(u16 *)(buffer + i*2) = bigEndian; | |||||
} | |||||
*(u16 *)(buffer + i*2) = 0; | |||||
return buffer + len*2 + 2; | |||||
} | |||||
const u8* CStrW::Deserialize(const u8* buffer, const u8* bufferend) | |||||
{ | |||||
const u16 *strend = (const u16 *)buffer; | |||||
while ((const u8 *)strend < bufferend && *strend) strend++; | |||||
if ((const u8 *)strend >= bufferend) return NULL; | |||||
resize(strend - (const u16 *)buffer); | |||||
const u16 *ptr = (const u16 *)buffer; | |||||
std::wstring::iterator str = begin(); | |||||
while (ptr < strend) | |||||
{ | |||||
const u16 native = to_be16(*(ptr++)); // we want from_be16, but that's the same | |||||
*(str++) = (tchar)native; | |||||
} | |||||
return (const u8 *)(strend+1); | |||||
} | |||||
size_t CStr::GetSerializedLength() const | |||||
{ | |||||
return size_t(length()*2 + 2); | |||||
} | |||||
#else | |||||
/* | |||||
CStr8 is always serialized to/from ASCII (or whatever 8-bit codepage stored | |||||
in the CStr) | |||||
*/ | |||||
u8* CStr8::Serialize(u8* buffer) const | |||||
{ | |||||
size_t len = length(); | |||||
Serialize_int_4(buffer, (u32)len); | |||||
size_t i = 0; | |||||
for (i = 0; i < len; i++) | |||||
buffer[i] = (*this)[i]; | |||||
return buffer + len; | |||||
} | |||||
const u8* CStr8::Deserialize(const u8* buffer, const u8* bufferend) | |||||
{ | |||||
u32 len; | |||||
Deserialize_int_4(buffer, len); | |||||
if (buffer + len > bufferend) | |||||
return NULL; | |||||
*this = std::string(buffer, buffer + len); | |||||
return buffer + len; | |||||
} | |||||
size_t CStr::GetSerializedLength() const | |||||
{ | |||||
return length() + 4; | |||||
} | |||||
#endif // _UNICODE | |||||
// Clean up, to keep the second pass through unidoubler happy | // Clean up, to keep the second pass through unidoubler happy | ||||
#undef tstringstream | #undef tstringstream | ||||
#undef _tstod | #undef _tstod | ||||
#undef _ttoi | #undef _ttoi | ||||
#undef _ttol | #undef _ttol | ||||
#undef _istspace | #undef _istspace | ||||
#undef _totlower | #undef _totlower | ||||
#undef _totupper | #undef _totupper | ||||
#endif // CStr_CPP_FIRST | #endif // CStr_CPP_FIRST |
Wildfire Games · Phabricator
is this one still needed?