C++erific text cleaner function, also, it's a good idea to actually clean text coming from signs...
This commit is contained in:
parent
226de5b6f8
commit
05148e8a8d
@ -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> data;
|
||||
|
@ -26,6 +26,10 @@ namespace format
|
||||
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, 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> VideoBufferToPPM(const VideoBuffer & vidBuf);
|
||||
std::vector<char> VideoBufferToPTI(const VideoBuffer & vidBuf);
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
#include <bzlib.h>
|
||||
#include "Config.h"
|
||||
#include "Format.h"
|
||||
#include "bson/BSON.h"
|
||||
#include "GameSave.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)
|
||||
{
|
||||
char tempString[256];
|
||||
strncpy(tempString, bson_iterator_string(&signiter), 255);
|
||||
tempString[255] = 0;
|
||||
clean_text((char*)tempSign.text.c_str(), 158-14);
|
||||
|
||||
tempSign.text = tempString;
|
||||
tempSign.text = format::CleanString(bson_iterator_string(&signiter), 255);
|
||||
}
|
||||
else if(strcmp(bson_iterator_key(&signiter), "justification")==0 && bson_iterator_type(&signiter)==BSON_INT)
|
||||
{
|
||||
|
Reference in New Issue
Block a user