Skip to content

Commit

Permalink
Switch from std::string to String/ByteString in most of the code
Browse files Browse the repository at this point in the history
Also switch SimulationData from weird arrays to std::vector
  • Loading branch information
mniip committed Apr 30, 2018
1 parent 4912674 commit ff27d69
Show file tree
Hide file tree
Showing 187 changed files with 1,838 additions and 1,714 deletions.
16 changes: 8 additions & 8 deletions src/Format.cpp
@@ -1,6 +1,6 @@

#include <ctime>
#include <string>
#include "common/String.h"
#include <stdexcept>
#include <iostream>
#include <iterator>
Expand All @@ -9,7 +9,7 @@
#include "Format.h"
#include "graphics/Graphics.h"

std::string format::URLEncode(std::string source)
ByteString format::URLEncode(ByteString source)
{
char * src = (char *)source.c_str();
char * dst = new char[(source.length()*3)+2];
Expand All @@ -33,23 +33,23 @@ std::string format::URLEncode(std::string source)
}
*d = 0;

std::string finalString(dst);
ByteString finalString(dst);
delete[] dst;
return finalString;
}

std::string format::UnixtimeToDate(time_t unixtime, std::string dateFormat)
ByteString format::UnixtimeToDate(time_t unixtime, ByteString dateFormat)
{
struct tm * timeData;
char buffer[128];

timeData = localtime(&unixtime);

strftime(buffer, 128, dateFormat.c_str(), timeData);
return std::string(buffer);
return ByteString(buffer);
}

std::string format::UnixtimeToDateMini(time_t unixtime)
ByteString format::UnixtimeToDateMini(time_t unixtime)
{
time_t currentTime = time(NULL);
struct tm currentTimeData = *localtime(&currentTime);
Expand All @@ -69,7 +69,7 @@ std::string format::UnixtimeToDateMini(time_t unixtime)
}
}

std::string format::CleanString(std::string dirtyString, bool ascii, bool color, bool newlines, bool numeric)
String format::CleanString(String dirtyString, bool ascii, bool color, bool newlines, bool numeric)
{
for (size_t i = 0; i < dirtyString.size(); i++)
{
Expand Down Expand Up @@ -226,7 +226,7 @@ struct PNGChunk

//char[4] CRC();

PNGChunk(int length, std::string name)
PNGChunk(int length, ByteString name)
{
if (name.length()!=4)
throw std::runtime_error("Invalid chunk name");
Expand Down
32 changes: 23 additions & 9 deletions src/Format.h
@@ -1,6 +1,6 @@
#pragma once

#include <sstream>
#include "common/String.h"
#include <vector>

class VideoBuffer;
Expand All @@ -9,24 +9,38 @@ namespace format
{
const static char hex[] = "0123456789ABCDEF";

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

template <typename T> T StringToNumber(const std::string & text)
template <typename T> String NumberToString(T number)
{
std::stringstream ss(text);
String::Stream ss;
ss << number;
return ss.str();
}

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)
{
String::Stream ss(text);
T number;
return (ss >> number)?number:0;
}

std::string URLEncode(std::string value);
std::string UnixtimeToDate(time_t unixtime, std::string dateFomat = "%d %b %Y");
std::string UnixtimeToDateMini(time_t unixtime);
std::string CleanString(std::string dirtyString, bool ascii, bool color, bool newlines, bool numeric = false);
ByteString URLEncode(ByteString value);
ByteString UnixtimeToDate(time_t unixtime, ByteString dateFomat = "%d %b %Y");
ByteString UnixtimeToDateMini(time_t unixtime);
String CleanString(String dirtyString, bool ascii, bool color, bool newlines, bool numeric = false);
std::vector<char> VideoBufferToPNG(const VideoBuffer & vidBuf);
std::vector<char> VideoBufferToBMP(const VideoBuffer & vidBuf);
std::vector<char> VideoBufferToPPM(const VideoBuffer & vidBuf);
Expand Down
2 changes: 0 additions & 2 deletions src/Misc.h
Expand Up @@ -2,8 +2,6 @@
#define UTILS_H
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>

//Linear interpolation
Expand Down
8 changes: 4 additions & 4 deletions src/Platform.cpp
Expand Up @@ -20,9 +20,9 @@
namespace Platform
{

std::string ExecutableName()
ByteString ExecutableName()
{
std::string ret;
ByteString ret;
#if defined(WIN)
char *name = (char *)malloc(64);
DWORD max = 64, res;
Expand Down Expand Up @@ -73,7 +73,7 @@ std::string ExecutableName()

void DoRestart()
{
std::string exename = ExecutableName();
ByteString exename = ExecutableName();
if (exename.length())
{
#ifdef WIN
Expand All @@ -85,7 +85,7 @@ void DoRestart()
exit(-1);
}

void OpenURI(std::string uri)
void OpenURI(ByteString uri)
{
#if defined(WIN)
ShellExecute(0, "OPEN", uri.c_str(), NULL, NULL, 0);
Expand Down
6 changes: 3 additions & 3 deletions src/Platform.h
@@ -1,14 +1,14 @@
#ifndef PLATFORM_H
#define PLATFORM_H

#include <string>
#include "common/String.h"

namespace Platform
{
std::string ExecutableName();
ByteString ExecutableName();
void DoRestart();

void OpenURI(std::string uri);
void OpenURI(ByteString uri);

void Millisleep(long int t);
long unsigned int GetTime();
Expand Down
5 changes: 2 additions & 3 deletions src/PowderToy.h
@@ -1,9 +1,8 @@
#pragma once
#include <string>

void EngineProcess();
void ClipboardPush(std::string text);
std::string ClipboardPull();
void ClipboardPush(ByteString text);
ByteString ClipboardPull();
int GetModifiers();
bool LoadWindowPosition(int scale);
void SetCursorEnabled(int enabled);
Expand Down
25 changes: 12 additions & 13 deletions src/PowderToyRenderer.cpp
Expand Up @@ -2,11 +2,10 @@

#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>

#include "common/String.h"
#include "Config.h"
#include "Format.h"
#include "gui/interface/Engine.h"
Expand All @@ -18,16 +17,16 @@


void EngineProcess() {}
void ClipboardPush(std::string) {}
std::string ClipboardPull() { return ""; }
void ClipboardPush(ByteString) {}
ByteString ClipboardPull() { return ""; }
int GetModifiers() { return 0; }
void SetCursorEnabled(int enabled) {}
unsigned int GetTicks() { return 0; }

void readFile(std::string filename, std::vector<char> & storage)
void readFile(ByteString filename, std::vector<char> & storage)
{
std::ifstream fileStream;
fileStream.open(std::string(filename).c_str(), std::ios::binary);
fileStream.open(filename.c_str(), std::ios::binary);
if(fileStream.is_open())
{
fileStream.seekg(0, std::ios::end);
Expand All @@ -45,10 +44,10 @@ void readFile(std::string filename, std::vector<char> & storage)
}
}

void writeFile(std::string filename, std::vector<char> & fileData)
void writeFile(ByteString filename, std::vector<char> & fileData)
{
std::ofstream fileStream;
fileStream.open(std::string(filename).c_str(), std::ios::binary);
fileStream.open(filename.c_str(), std::ios::binary);
if(fileStream.is_open())
{
fileStream.write(&fileData[0], fileData.size());
Expand All @@ -59,13 +58,13 @@ void writeFile(std::string filename, std::vector<char> & fileData)
int main(int argc, char *argv[])
{
ui::Engine * engine;
std::string outputPrefix, inputFilename;
ByteString outputPrefix, inputFilename;
std::vector<char> inputFile;
std::string ppmFilename, ptiFilename, ptiSmallFilename, pngFilename, pngSmallFilename;
ByteString ppmFilename, ptiFilename, ptiSmallFilename, pngFilename, pngSmallFilename;
std::vector<char> ppmFile, ptiFile, ptiSmallFile, pngFile, pngSmallFile;

inputFilename = std::string(argv[1]);
outputPrefix = std::string(argv[2]);
inputFilename = argv[1];
outputPrefix = argv[2];

ppmFilename = outputPrefix+".ppm";
ptiFilename = outputPrefix+".pti";
Expand All @@ -88,7 +87,7 @@ int main(int argc, char *argv[])
catch (ParseException e)
{
//Render the save again later or something? I don't know
if (e.what() == "Save from newer version")
if (ByteString(e.what()).FromUtf8() == "Save from newer version")
throw e;
}

Expand Down

0 comments on commit ff27d69

Please sign in to comment.