mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-01 20:12:50 +02:00
C++erific text cleaner function, also, it's a good idea to actually clean text coming from signs...
This commit is contained in:
@@ -71,6 +71,58 @@ std::string format::UnixtimeToDateMini(time_t unixtime)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string format::CleanString(std::string dirtyString, int maxStringLength)
|
||||||
|
{
|
||||||
|
return CleanString(dirtyString, std::string::npos, maxStringLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string format::CleanString(std::string dirtyString, int maxVisualSize, int maxStringLength)
|
||||||
|
{
|
||||||
|
std::string newString = dirtyString;
|
||||||
|
if(maxStringLength != std::string::npos && newString.size() > maxStringLength)
|
||||||
|
{
|
||||||
|
newString = newString.substr(0, maxStringLength);
|
||||||
|
}
|
||||||
|
if(maxVisualSize != std::string::npos && newString.size()*10 > maxVisualSize)
|
||||||
|
{
|
||||||
|
newString = newString.substr(0, maxVisualSize/10);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < newString.size(); i++){
|
||||||
|
if(!(newString[i]>=' ' && newString[i]<127)){ //Clamp to ASCII range
|
||||||
|
newString[i] = '?'; //Replace with "huh" char
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string format::CleanString(char * dirtyData, int maxStringLength)
|
||||||
|
{
|
||||||
|
return CleanString(dirtyData, std::string::npos, maxStringLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string format::CleanString(char * dirtyData, int maxVisualSize, int maxStringLength)
|
||||||
|
{
|
||||||
|
char * newData = new char[maxStringLength+1];
|
||||||
|
strncpy(newData, dirtyData, maxStringLength);
|
||||||
|
newData[maxStringLength] = 0;
|
||||||
|
|
||||||
|
std::string newString = std::string(newData);
|
||||||
|
delete[] newData;
|
||||||
|
|
||||||
|
if(maxVisualSize != std::string::npos && newString.size()*10 > maxVisualSize)
|
||||||
|
{
|
||||||
|
newString = newString.substr(0, maxVisualSize/10);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < newString.size(); i++){
|
||||||
|
if(!(newString[i]>=' ' && newString[i]<127)){ //Clamp to ASCII range
|
||||||
|
newString[i] = '?'; //Replace with "huh" char
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newString;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<char> format::VideoBufferToPTI(const VideoBuffer & vidBuf)
|
std::vector<char> format::VideoBufferToPTI(const VideoBuffer & vidBuf)
|
||||||
{
|
{
|
||||||
std::vector<char> data;
|
std::vector<char> data;
|
||||||
|
@@ -26,6 +26,10 @@ namespace format
|
|||||||
std::string URLEncode(std::string value);
|
std::string URLEncode(std::string value);
|
||||||
std::string UnixtimeToDate(time_t unixtime, std::string dateFomat = "%d %b %Y");
|
std::string UnixtimeToDate(time_t unixtime, std::string dateFomat = "%d %b %Y");
|
||||||
std::string UnixtimeToDateMini(time_t unixtime);
|
std::string UnixtimeToDateMini(time_t unixtime);
|
||||||
|
std::string CleanString(std::string dirtyString, int maxVisualSize, int maxStringLength);
|
||||||
|
std::string CleanString(std::string dirtyString, int maxStringLength = std::string::npos);
|
||||||
|
std::string CleanString(char * dirtyData, int maxVisualSize, int maxStringLength);
|
||||||
|
std::string CleanString(char * dirtyData, int maxStringLength);
|
||||||
std::vector<char> VideoBufferToPNG(const VideoBuffer & vidBuf);
|
std::vector<char> VideoBufferToPNG(const VideoBuffer & vidBuf);
|
||||||
std::vector<char> VideoBufferToPPM(const VideoBuffer & vidBuf);
|
std::vector<char> VideoBufferToPPM(const VideoBuffer & vidBuf);
|
||||||
std::vector<char> VideoBufferToPTI(const VideoBuffer & vidBuf);
|
std::vector<char> VideoBufferToPTI(const VideoBuffer & vidBuf);
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <bzlib.h>
|
#include <bzlib.h>
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
#include "Format.h"
|
||||||
#include "bson/BSON.h"
|
#include "bson/BSON.h"
|
||||||
#include "GameSave.h"
|
#include "GameSave.h"
|
||||||
#include "simulation/SimulationData.h"
|
#include "simulation/SimulationData.h"
|
||||||
@@ -501,12 +502,7 @@ void GameSave::readOPS(char * data, int dataLength)
|
|||||||
{
|
{
|
||||||
if(strcmp(bson_iterator_key(&signiter), "text")==0 && bson_iterator_type(&signiter)==BSON_STRING)
|
if(strcmp(bson_iterator_key(&signiter), "text")==0 && bson_iterator_type(&signiter)==BSON_STRING)
|
||||||
{
|
{
|
||||||
char tempString[256];
|
tempSign.text = format::CleanString(bson_iterator_string(&signiter), 255);
|
||||||
strncpy(tempString, bson_iterator_string(&signiter), 255);
|
|
||||||
tempString[255] = 0;
|
|
||||||
clean_text((char*)tempSign.text.c_str(), 158-14);
|
|
||||||
|
|
||||||
tempSign.text = tempString;
|
|
||||||
}
|
}
|
||||||
else if(strcmp(bson_iterator_key(&signiter), "justification")==0 && bson_iterator_type(&signiter)==BSON_INT)
|
else if(strcmp(bson_iterator_key(&signiter), "justification")==0 && bson_iterator_type(&signiter)==BSON_INT)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user