Skip to content

Commit

Permalink
Remove ByteString::Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mniip committed May 4, 2018
1 parent e29d2c5 commit f8586ea
Show file tree
Hide file tree
Showing 26 changed files with 248 additions and 282 deletions.
24 changes: 0 additions & 24 deletions src/Format.h
Expand Up @@ -9,30 +9,6 @@ namespace format
{
const static char hex[] = "0123456789ABCDEF";

template <typename T> ByteString NumberToByteString(T number)
{
ByteString::Stream ss;
ss << number;
return ss.str();
}

template <typename T> String NumberToString(T number)
{
return String::Build(number);
}

template <typename T> T ByteStringToNumber(const ByteString & text)
{
ByteString::Stream ss(text);
T number;
return (ss >> number)?number:0;
}

template <typename T> T StringToNumber(const String & text)
{
return text.ToNumber<T>(true);
}

ByteString URLEncode(ByteString value);
ByteString UnixtimeToDate(time_t unixtime, ByteString dateFomat = "%d %b %Y");
ByteString UnixtimeToDateMini(time_t unixtime);
Expand Down
6 changes: 2 additions & 4 deletions src/PowderToySDL.cpp
Expand Up @@ -1009,7 +1009,7 @@ int main(int argc, char * argv[])

if(arguments["scale"].length())
{
tempScale = format::ByteStringToNumber<int>(arguments["scale"]);
tempScale = arguments["scale"].ToNumber<int>();
Client::Ref().SetPref("Scale", tempScale);
}

Expand Down Expand Up @@ -1181,9 +1181,7 @@ int main(int argc, char * argv[])
#ifdef DEBUG
std::cout << "Got Ptsave: id: " << saveIdPart << std::endl;
#endif
int saveId = format::ByteStringToNumber<int>(saveIdPart);
if (!saveId)
throw std::runtime_error("Invalid Save ID");
int saveId = saveIdPart.ToNumber<int>();

SaveInfo * newSave = Client::Ref().GetSave(saveId, 0);
if (!newSave)
Expand Down
188 changes: 81 additions & 107 deletions src/client/Client.cpp

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/client/GameSave.cpp
Expand Up @@ -629,7 +629,7 @@ void GameSave::readOPS(char * data, int dataLength)
int bz2ret;
if ((bz2ret = BZ2_bzBuffToBuffDecompress((char*)bsonData, &bsonDataLen, (char*)(inputData+12), inputDataLen-12, 0, 0)) != BZ_OK)
{
throw ParseException(ParseException::Corrupt, "Unable to decompress (ret " + format::NumberToString<int>(bz2ret) + ")");
throw ParseException(ParseException::Corrupt, String::Build("Unable to decompress (ret ", bz2ret, ")"));
}

set_bson_err_handler([](const char* err) { throw ParseException(ParseException::Corrupt, "BSON error when parsing save: " + ByteString(err).FromUtf8()); });
Expand Down Expand Up @@ -1406,7 +1406,7 @@ void GameSave::readPSv(char * saveDataChar, int dataLength)

int bzStatus = 0;
if ((bzStatus = BZ2_bzBuffToBuffDecompress((char *)data, (unsigned *)&size, (char *)(saveData+12), dataLength-12, 0, 0)))
throw ParseException(ParseException::Corrupt, "Cannot decompress: " + format::NumberToString(bzStatus));
throw ParseException(ParseException::Corrupt, String::Build("Cannot decompress: ", bzStatus));
dataLength = size;

#ifdef DEBUG
Expand Down Expand Up @@ -2510,7 +2510,7 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
unsigned int finalDataLen = bson_size(&b);
auto outputData = std::unique_ptr<unsigned char[]>(new unsigned char[finalDataLen*2+12]);
if (!outputData)
throw BuildException("Save error, out of memory (finalData): " + format::NumberToString<unsigned int>(finalDataLen*2+12));
throw BuildException(String::Build("Save error, out of memory (finalData): ", finalDataLen*2+12));

outputData[0] = 'O';
outputData[1] = 'P';
Expand All @@ -2528,7 +2528,7 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
unsigned int compressedSize = finalDataLen*2, bz2ret;
if ((bz2ret = BZ2_bzBuffToBuffCompress((char*)(outputData.get()+12), &compressedSize, (char*)finalData, bson_size(&b), 9, 0, 0)) != BZ_OK)
{
throw BuildException("Save error, could not compress (ret " + format::NumberToString<int>(bz2ret) + ")");
throw BuildException(String::Build("Save error, could not compress (ret ", bz2ret, ")"));
}

#ifdef DEBUG
Expand Down
10 changes: 3 additions & 7 deletions src/client/HTTP.cpp
Expand Up @@ -192,11 +192,7 @@ void http_init(char *proxy)
free(host);
free(port);
}
ByteString::Stream userAgentBuilder;
userAgentBuilder << "PowderToy/" << SAVE_VERSION << "." << MINOR_VERSION << " ";
userAgentBuilder << "(" << IDENT_PLATFORM << "; " << IDENT_BUILD << "; M" << MOD_ID << ") ";
userAgentBuilder << "TPTPP/" << SAVE_VERSION << "." << MINOR_VERSION << "." << BUILD_NUM << IDENT_RELTYPE << "." << SNAPSHOT_ID;
ByteString newUserAgent = userAgentBuilder.str();
ByteString newUserAgent = ByteString::Build("PowderToy/", SAVE_VERSION, ".", MINOR_VERSION, " (", IDENT_PLATFORM, "; ", IDENT_BUILD, "; M", MOD_ID, ") TPTPP/", SAVE_VERSION, ".", MINOR_VERSION, ".", BUILD_NUM, IDENT_RELTYPE, ".", SNAPSHOT_ID);
userAgent = new char[newUserAgent.length()+1];
std::copy(newUserAgent.begin(), newUserAgent.end(), userAgent);
userAgent[newUserAgent.length()] = 0;
Expand Down Expand Up @@ -987,7 +983,7 @@ ByteString FindBoundary(std::map<ByteString, ByteString> parts, ByteString bound
// this function used in Download class, and eventually all http requests
ByteString GetMultipartMessage(std::map<ByteString, ByteString> parts, ByteString boundary)
{
ByteString::Stream data;
ByteStringBuilder data;

// loop through each part, adding it
for (std::map<ByteString, ByteString>::iterator iter = parts.begin(); iter != parts.end(); iter++)
Expand All @@ -1014,7 +1010,7 @@ ByteString GetMultipartMessage(std::map<ByteString, ByteString> parts, ByteStrin
data << "\r\n";
}
data << "--" << boundary << "--\r\n";
return data.str();
return data.Build();
}

// add the header needed to make POSTS work
Expand Down
4 changes: 2 additions & 2 deletions src/client/requestbroker/APIRequest.cpp
Expand Up @@ -103,7 +103,7 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb)
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToByteString<int>(user.UserID).c_str());
std::strcpy(userName, ByteString::Build(user.UserID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, userName, NULL, userSession);
delete[] userSession;
Expand All @@ -122,7 +122,7 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb)
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToByteString<int>(user.UserID).c_str());
std::strcpy(userName, ByteString::Build(user.UserID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
http_auth_headers(HTTPContext, userName, NULL, userSession);
delete[] userSession;
Expand Down
15 changes: 7 additions & 8 deletions src/client/requestbroker/RequestBroker.cpp
Expand Up @@ -97,23 +97,22 @@ void RequestBroker::RenderThumbnail(GameSave * gameSave, bool decorations, bool

void RequestBroker::RetrieveThumbnail(int saveID, int saveDate, int width, int height, RequestListener * tListener)
{
ByteString::Stream urlStream;
urlStream << "http://" << STATICSERVER << "/" << saveID;
ByteStringBuilder url;
url << "http://" << STATICSERVER << "/" << saveID;
if(saveDate)
{
urlStream << "_" << saveDate;
url << "_" << saveDate;
}
urlStream << "_small.pti";
url << "_small.pti";

RetrieveImage(urlStream.str(), width, height, tListener);
RetrieveImage(url.Build(), width, height, tListener);
}

void RequestBroker::RetrieveAvatar(ByteString username, int width, int height, RequestListener * tListener)
{
ByteString::Stream urlStream;
urlStream << "http://" << STATICSERVER << "/avatars/" << username << ".pti";
ByteString url = ByteString::Build("http://", STATICSERVER, "/avatars/", username, ".pti");

RetrieveImage(urlStream.str(), width, height, tListener);
RetrieveImage(url, width, height, tListener);
}

void RequestBroker::Start(Request * request, RequestListener * tListener, int identifier)
Expand Down
4 changes: 2 additions & 2 deletions src/client/requestbroker/WebRequest.cpp
Expand Up @@ -106,7 +106,7 @@ RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToByteString<int>(user.UserID).c_str());
std::strcpy(userName, ByteString::Build(user.UserID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, userName, NULL, userSession);
delete[] userSession;
Expand All @@ -125,7 +125,7 @@ RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToByteString<int>(user.UserID).c_str());
std::strcpy(userName, ByteString::Build(user.UserID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
http_auth_headers(HTTPContext, userName, NULL, userSession);
delete[] userSession;
Expand Down
54 changes: 54 additions & 0 deletions src/common/Format.h
Expand Up @@ -25,6 +25,18 @@ namespace Format
inline FlagsOverride() {}
};

template<typename T> struct FillOverride
{
T value;
size_t fill;
inline FillOverride(T _value, size_t _fill): value(_value), fill(_fill) {}
};
template<> struct FillOverride<void>
{
String::value_type fill;
inline FillOverride(size_t _fill): fill(_fill) {}
};

template<typename T> struct WidthOverride
{
T value;
Expand Down Expand Up @@ -74,8 +86,10 @@ namespace Format
inline FlagsOverride<void, std::ios_base::scientific, std::ios_base::floatfield> Scientific() { return FlagsOverride<void, std::ios_base::scientific, std::ios_base::floatfield>(); }
inline FlagsOverride<void, std::ios_base::fmtflags{}, std::ios_base::floatfield> FloatDefault() { return FlagsOverride<void, std::ios_base::fmtflags{}, std::ios_base::floatfield>(); }

template<typename T> inline FillOverride<T> Fill(T value, String::value_type fill) { return FillOverride<T>(value, fill); }
template<typename T> inline WidthOverride<T> Width(T value, size_t width) { return WidthOverride<T>(value, width); }
template<typename T> inline PrecisionOverride<T> Precision(T value, size_t precision) { return PrecisionOverride<T>(value, precision); }
inline FillOverride<void> Fill(String::value_type fill) { return FillOverride<void>(fill); }
inline WidthOverride<void> Width(size_t width) { return WidthOverride<void>(width); }
inline PrecisionOverride<void> Precision(size_t precision) { return PrecisionOverride<void>(precision); }
};
Expand All @@ -94,16 +108,36 @@ template<std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline Byte
return b;
}

template<typename T> inline ByteStringBuilder &operator<<(ByteStringBuilder &b, Format::FillOverride<T> data)
{
size_t oldfill = b.fill;
b.fill = data.fill;
b << data.value;
b.fill = oldfill;
return b;
}
inline ByteStringBuilder &operator<<(ByteStringBuilder &b, Format::FillOverride<void> data)
{
b.fill = data.fill;
return b;
}

template<typename T> inline ByteStringBuilder &operator<<(ByteStringBuilder &b, Format::WidthOverride<T> data)
{
String::value_type oldfill = b.fill;
if(oldfill == ' ')
b.fill = '0';
size_t oldwidth = b.width;
b.width = data.width;
b << data.value;
b.width = oldwidth;
b.fill = oldfill;
return b;
}
inline ByteStringBuilder &operator<<(ByteStringBuilder &b, Format::WidthOverride<void> data)
{
if(b.fill == ' ')
b.fill = '0';
b.width = data.width;
return b;
}
Expand Down Expand Up @@ -142,16 +176,36 @@ template<std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline Stri
return b;
}

template<typename T> inline StringBuilder &operator<<(StringBuilder &b, Format::FillOverride<T> data)
{
size_t oldfill = b.fill;
b.fill = data.fill;
b << data.value;
b.fill = oldfill;
return b;
}
inline StringBuilder &operator<<(StringBuilder &b, Format::FillOverride<void> data)
{
b.fill = data.fill;
return b;
}

template<typename T> inline StringBuilder &operator<<(StringBuilder &b, Format::WidthOverride<T> data)
{
String::value_type oldfill = b.fill;
if(oldfill == ' ')
b.fill = '0';
size_t oldwidth = b.width;
b.width = data.width;
b << data.value;
b.width = oldwidth;
b.fill = oldfill;
return b;
}
inline StringBuilder &operator<<(StringBuilder &b, Format::WidthOverride<void> data)
{
if(b.fill == ' ')
b.fill = '0';
b.width = data.width;
return b;
}
Expand Down
4 changes: 1 addition & 3 deletions src/common/String.h
Expand Up @@ -168,8 +168,6 @@ class ByteString : public std::basic_string<char>
String FromUtf8(bool ignoreError = true) const;
inline String FromAscii() const;
template<typename... Ts> static ByteString Build(Ts&&... args);

using Stream = std::basic_stringstream<value_type>;
};

inline ByteString operator+(ByteString const &lhs, ByteString const &rhs) { return static_cast<std::basic_string<char> const &>(lhs) + static_cast<std::basic_string<char> const &>(rhs); }
Expand Down Expand Up @@ -405,7 +403,7 @@ class ByteStringBuilder
size_t Size() const { return buffer.size(); }
ByteString Build() const;

template<typename T> ByteStringBuilder &operator<<(T) = delete;
template<typename T> ByteStringBuilder &operator<<(T) &&= delete;

template<typename T, typename... Ts> ByteStringBuilder &Add(T &&arg, Ts&&... args)
{
Expand Down
4 changes: 2 additions & 2 deletions src/debug/ElementPopulation.cpp
Expand Up @@ -37,8 +37,8 @@ void ElementPopulationDebug::Draw()
maxAverage = (maxAverage*(1.0f-0.015f)) + (0.015f*maxVal);
scale = 255.0f/maxAverage;

maxValString = format::NumberToString<int>(maxAverage);
halfValString = format::NumberToString<int>(maxAverage/2);
maxValString = String::Build(maxAverage);
halfValString = String::Build(maxAverage/2);


g->fillrect(xStart-5, yBottom - 263, bars+10+Graphics::textwidth(maxValString)+10, 255 + 13, 0, 0, 0, 180);
Expand Down
22 changes: 11 additions & 11 deletions src/gui/colourpicker/ColourPickerActivity.cpp
Expand Up @@ -28,10 +28,10 @@ ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, ColourPicke
void TextChangedCallback(ui::Textbox * sender)
{
int r, g, b, alpha;
r = format::StringToNumber<int>(a->rValue->GetText());
g = format::StringToNumber<int>(a->gValue->GetText());
b = format::StringToNumber<int>(a->bValue->GetText());
alpha = format::StringToNumber<int>(a->aValue->GetText());
r = a->rValue->GetText().ToNumber<int>(true);
g = a->gValue->GetText().ToNumber<int>(true);
b = a->bValue->GetText().ToNumber<int>(true);
alpha = a->aValue->GetText().ToNumber<int>(true);
if (r > 255)
r = 255;
if (g > 255)
Expand Down Expand Up @@ -82,9 +82,9 @@ ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, ColourPicke
void ActionCallback(ui::Button * sender)
{
int Red, Green, Blue;
Red = format::StringToNumber<int>(a->rValue->GetText());
Green = format::StringToNumber<int>(a->gValue->GetText());
Blue = format::StringToNumber<int>(a->bValue->GetText());
Red = a->rValue->GetText().ToNumber<int>(true);
Green = a->gValue->GetText().ToNumber<int>(true);
Blue = a->bValue->GetText().ToNumber<int>(true);
ui::Colour col(Red, Green, Blue, a->currentAlpha);
if(a->callback)
a->callback->ColourPicked(col);
Expand All @@ -104,10 +104,10 @@ ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, ColourPicke

void ColourPickerActivity::UpdateTextboxes(int r, int g, int b, int a)
{
rValue->SetText(format::NumberToString<int>(r));
gValue->SetText(format::NumberToString<int>(g));
bValue->SetText(format::NumberToString<int>(b));
aValue->SetText(format::NumberToString<int>(a));
rValue->SetText(String::Build(r));
gValue->SetText(String::Build(g));
bValue->SetText(String::Build(b));
aValue->SetText(String::Build(a));
hexValue->SetText(String::Build(Format::Hex(), Format::Uppercase(), Format::Width(2), a, r, g, b));
}
void ColourPickerActivity::OnTryExit(ExitMethod method)
Expand Down
4 changes: 2 additions & 2 deletions src/gui/dialogues/SaveIDMessage.cpp
Expand Up @@ -28,8 +28,8 @@ SaveIDMessage::SaveIDMessage(int id):
copyTextLabel->Appearance.HorizontalAlign = ui::Appearance::AlignCentre;
AddComponent(copyTextLabel);

textWidth = Graphics::textwidth(format::NumberToString<int>(id).c_str());
ui::CopyTextButton * copyTextButton = new ui::CopyTextButton(ui::Point((Size.X-textWidth-10)/2, 50), ui::Point(textWidth+10, 18), format::NumberToString<int>(id), copyTextLabel);
textWidth = Graphics::textwidth(String::Build(id));
ui::CopyTextButton * copyTextButton = new ui::CopyTextButton(ui::Point((Size.X-textWidth-10)/2, 50), ui::Point(textWidth+10, 18), String::Build(id), copyTextLabel);
AddComponent(copyTextButton);

class DismissAction: public ui::ButtonAction
Expand Down

0 comments on commit f8586ea

Please sign in to comment.