use std::string instead of c strings for sign stuff, fixes #545

This commit is contained in:
jacob1 2018-03-03 20:44:03 -05:00
parent 883484e2f9
commit f9b5c6bb1a
6 changed files with 49 additions and 60 deletions

View File

@ -984,7 +984,7 @@ void Renderer::DrawSigns()
{ {
char type = 0; char type = 0;
std::string text = signs[i].getText(sim); std::string text = signs[i].getText(sim);
sign::splitsign(signs[i].text.c_str(), &type); sign::splitsign(signs[i].text, &type);
signs[i].pos(text, x, y, w, h); signs[i].pos(text, x, y, w, h);
clearrect(x, y, w+1, h); clearrect(x, y, w+1, h);
drawrect(x, y, w+1, h, 192, 192, 192, 255); drawrect(x, y, w+1, h, 192, 192, 192, 255);

View File

@ -631,7 +631,7 @@ bool GameController::MouseDown(int x, int y, unsigned button)
if (foundSignID != -1) if (foundSignID != -1)
{ {
sign foundSign = gameModel->GetSimulation()->signs[foundSignID]; sign foundSign = gameModel->GetSimulation()->signs[foundSignID];
if (sign::splitsign(foundSign.text.c_str())) if (sign::splitsign(foundSign.text))
return false; return false;
} }
} }
@ -655,7 +655,7 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
if (foundSignID != -1) if (foundSignID != -1)
{ {
sign foundSign = gameModel->GetSimulation()->signs[foundSignID]; sign foundSign = gameModel->GetSimulation()->signs[foundSignID];
const char* str = foundSign.text.c_str(); std::string str = foundSign.text;
char type; char type;
int pos = sign::splitsign(str, &type); int pos = sign::splitsign(str, &type);
if (pos) if (pos)
@ -663,14 +663,12 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
ret = false; ret = false;
if (type == 'c' || type == 't' || type == 's') if (type == 'c' || type == 't' || type == 's')
{ {
char buff[256]; std::string link = str.substr(3, pos-3);
strcpy(buff, str+3);
buff[pos-3] = 0;
switch (type) switch (type)
{ {
case 'c': case 'c':
{ {
int saveID = format::StringToNumber<int>(std::string(buff)); int saveID = format::StringToNumber<int>(link);
if (saveID) if (saveID)
OpenSavePreview(saveID, 0, false); OpenSavePreview(saveID, 0, false);
break; break;
@ -679,12 +677,12 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
{ {
// buff is already confirmed to be a number by sign::splitsign // buff is already confirmed to be a number by sign::splitsign
std::stringstream uri; std::stringstream uri;
uri << "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=" << buff; uri << "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=" << link;
Platform::OpenURI(uri.str()); Platform::OpenURI(uri.str());
break; break;
} }
case 's': case 's':
OpenSearch(buff); OpenSearch(link);
break; break;
} }
} }

View File

@ -1744,25 +1744,23 @@ void GameView::OnTick(float dt)
int foundSignID = c->GetSignAt(mousePosition.X, mousePosition.Y); int foundSignID = c->GetSignAt(mousePosition.X, mousePosition.Y);
if (foundSignID != -1) if (foundSignID != -1)
{ {
const char* str = c->GetSignText(foundSignID).c_str();; std::string str = c->GetSignText(foundSignID);
char type = '\0'; char type = '\0';
int pos = sign::splitsign(str, &type); int pos = sign::splitsign(str, &type);
if (type == 'c' || type == 't' || type == 's') if (type == 'c' || type == 't' || type == 's')
{ {
char buff[256]; std::string linkSign = str.substr(3, pos-3);
strcpy(buff, str+3);
buff[pos-3] = 0;
std::stringstream tooltip; std::stringstream tooltip;
switch (type) switch (type)
{ {
case 'c': case 'c':
tooltip << "Go to save ID:" << buff; tooltip << "Go to save ID:" << linkSign;
break; break;
case 't': case 't':
tooltip << "Open forum thread " << buff << " in browser"; tooltip << "Open forum thread " << linkSign << " in browser";
break; break;
case 's': case 's':
tooltip << "Search for " << buff; tooltip << "Search for " << linkSign;
break; break;
} }
ToolTip(ui::Point(0, Size.Y), tooltip.str()); ToolTip(ui::Point(0, Size.Y), tooltip.str());

View File

@ -184,7 +184,7 @@ void SignWindow::DoDraw()
char type = 0; char type = 0;
Graphics * g = GetGraphics(); Graphics * g = GetGraphics();
std::string text = currentSign.getText(sim); std::string text = currentSign.getText(sim);
sign::splitsign(currentSign.text.c_str(), &type); sign::splitsign(currentSign.text, &type);
currentSign.pos(text, x, y, w, h); currentSign.pos(text, x, y, w, h);
g->clearrect(x, y, w+1, h); g->clearrect(x, y, w+1, h);
g->drawrect(x, y, w+1, h, 192, 192, 192, 255); g->drawrect(x, y, w+1, h, 192, 192, 192, 255);

View File

@ -1,3 +1,5 @@
#include <iomanip>
#include <sstream>
#include "Sign.h" #include "Sign.h"
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "simulation/Simulation.h" #include "simulation/Simulation.h"
@ -12,51 +14,45 @@ sign::sign(std::string text_, int x_, int y_, Justification justification_):
std::string sign::getText(Simulation *sim) std::string sign::getText(Simulation *sim)
{ {
char buff[256]; std::stringstream signTextNew;
char signText[256]; if (text[0] && text[0] == '{')
sprintf(signText, "%s", text.substr(0, 255).c_str());
if(signText[0] && signText[0] == '{')
{ {
if (!strcmp(signText,"{p}")) if (text == "{p}")
{ {
float pressure = 0.0f; float pressure = 0.0f;
if (x>=0 && x<XRES && y>=0 && y<YRES) if (x >= 0 && x < XRES && y >= 0 && y < YRES)
pressure = sim->pv[y/CELL][x/CELL]; pressure = sim->pv[y/CELL][x/CELL];
sprintf(buff, "Pressure: %3.2f", pressure); //...pressure signTextNew << std::fixed << std::showpoint << std::setprecision(2) << "Pressure: " << pressure;
} }
else if (!strcmp(signText,"{aheat}")) else if (text == "{aheat}")
{ {
float aheat = 0.0f; float aheat = 0.0f;
if (x>=0 && x<XRES && y>=0 && y<YRES) if (x >= 0 && x < XRES && y >= 0 && y < YRES)
aheat = sim->hv[y/CELL][x/CELL]; aheat = sim->hv[y/CELL][x/CELL];
sprintf(buff, "%3.2f", aheat-273.15); signTextNew << std::fixed << std::showpoint << std::setprecision(2) << aheat-273.15f;
} }
else if (!strcmp(signText,"{t}")) else if (text == "{t}")
{ {
if (x>=0 && x<XRES && y>=0 && y<YRES && sim->pmap[y][x]) if (x >= 0 && x < XRES && y >= 0 && y < YRES && sim->pmap[y][x])
sprintf(buff, "Temp: %4.2f", sim->parts[ID(sim->pmap[y][x])].temp-273.15); //...temperature signTextNew << std::fixed << std::showpoint << std::setprecision(2) << "Temp: " << sim->parts[ID(sim->pmap[y][x])].temp-273.15f;
else else
sprintf(buff, "Temp: 0.00"); //...temperature signTextNew << "Temp: 0.00";
} }
else else
{ {
int pos = splitsign(signText); int pos = splitsign(text);
if (pos) if (pos)
{ signTextNew << text.substr(pos+1, text.length()-pos-2);
strcpy(buff, signText+pos+1);
buff[strlen(signText)-pos-2]=0;
}
else else
strcpy(buff, signText); signTextNew << text;
} }
} }
else else
{ {
strcpy(buff, signText); signTextNew << text;
} }
return std::string(buff); return signTextNew.str();
} }
void sign::pos(std::string signText, int & x0, int & y0, int & w, int & h) void sign::pos(std::string signText, int & x0, int & y0, int & w, int & h)
@ -68,46 +64,43 @@ void sign::pos(std::string signText, int & x0, int & y0, int & w, int & h)
y0 = (y > 18) ? y - 18 : y + 4; y0 = (y > 18) ? y - 18 : y + 4;
} }
int sign::splitsign(const char* str, char * type) int sign::splitsign(std::string str, char * type)
{ {
if (str[0]=='{' && (str[1]=='c' || str[1]=='t' || str[1]=='b' || str[1]=='s')) if (str[0] == '{' && (str[1] == 'c' || str[1] == 't' || str[1] == 'b' || str[1] == 's'))
{ {
const char* p = str+2; size_t strIndex = 2;
// signs with text arguments // Signs with text arguments
if (str[1] == 's') if (str[1] == 's')
{ {
if (str[2]==':') if (str[2] == ':')
{ {
p = str+4; strIndex = 3;
while (*p && *p!='|') while (strIndex < str.length() && str[strIndex] != '|')
p++; strIndex++;
} }
else else
return 0; return 0;
} }
// signs with number arguments // Signs with number arguments
if (str[1] == 'c' || str[1] == 't') if (str[1] == 'c' || str[1] == 't')
{ {
if (str[2]==':' && str[3]>='0' && str[3]<='9') if (str[2] == ':' && str[3] >= '0' && str[3] <= '9')
{ {
p = str+4; strIndex = 4;
while (*p>='0' && *p<='9') while (str[strIndex] >= '0' && str[strIndex] <= '9')
p++; strIndex++;
} }
else else
return 0; return 0;
} }
if (*p=='|') if (str[strIndex] == '|')
{ {
int r = p-str; if (str[str.length() - 1] == '}')
while (*p)
p++;
if (p[-1] == '}')
{ {
if (type) if (type)
*type = str[1]; *type = str[1];
return r; return strIndex;
} }
} }
} }

View File

@ -17,7 +17,7 @@ public:
std::string getText(Simulation *sim); std::string getText(Simulation *sim);
void pos(std::string signText, int & x0, int & y0, int & w, int & h); void pos(std::string signText, int & x0, int & y0, int & w, int & h);
static int splitsign(const char* str, char * type = NULL); static int splitsign(std::string str, char * type = NULL);
}; };
#endif #endif