From 05148e8a8db3a413edb711108fd860d3ee83154f Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sun, 9 Dec 2012 00:14:48 +0000 Subject: [PATCH] C++erific text cleaner function, also, it's a good idea to actually clean text coming from signs... --- src/Format.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ src/Format.h | 4 ++++ src/client/GameSave.cpp | 8 ++----- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/Format.cpp b/src/Format.cpp index 5f9741d7e..f7c3c092f 100644 --- a/src/Format.cpp +++ b/src/Format.cpp @@ -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 format::VideoBufferToPTI(const VideoBuffer & vidBuf) { std::vector data; diff --git a/src/Format.h b/src/Format.h index 0aae25b23..0fd63a120 100644 --- a/src/Format.h +++ b/src/Format.h @@ -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 VideoBufferToPNG(const VideoBuffer & vidBuf); std::vector VideoBufferToPPM(const VideoBuffer & vidBuf); std::vector VideoBufferToPTI(const VideoBuffer & vidBuf); diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index a4d654ff1..2dabcd342 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -4,6 +4,7 @@ #include #include #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) {