diff --git a/src/Format.cpp b/src/Format.cpp index a7099a90e..da03d57d9 100644 --- a/src/Format.cpp +++ b/src/Format.cpp @@ -16,11 +16,9 @@ std::string format::URLEncode(std::string source) std::fill(dst, dst+(source.length()*3)+2, 0); char *d; - unsigned char *s; + for (d = dst; *d; d++) ; - for (d=dst; *d; d++) ; - - for (s=(unsigned char *)src; *s; s++) + for (unsigned char *s = (unsigned char *)src; *s; s++) { if ((*s>='0' && *s<='9') || (*s>='a' && *s<='z') || @@ -71,58 +69,65 @@ std::string format::UnixtimeToDateMini(time_t unixtime) } } -std::string format::CleanString(std::string dirtyString, size_t maxStringLength) +std::string format::CleanString(std::string dirtyString, bool ascii, bool color, bool newlines, bool numeric) { - return CleanString(dirtyString, std::string::npos, maxStringLength); + for (size_t i = 0; i < dirtyString.size(); i++) + { + switch(dirtyString[i]) + { + case '\b': + if (color) + { + dirtyString.erase(i, 2); + i--; + } + else + i++; + break; + case '\x0E': + if (color) + { + dirtyString.erase(i, 1); + i--; + } + break; + case '\x0F': + if (color) + { + dirtyString.erase(i, 4); + i--; + } + else + i += 3; + break; + case '\r': + case '\n': + if (newlines) + dirtyString[i] = ' '; + break; + default: + // if less than ascii 20 or greater than ascii 126, delete + if (numeric && (dirtyString[i] < '0' || dirtyString[i] > '9')) + { + dirtyString.erase(i, 1); + i--; + } + else if (ascii && (dirtyString[i] < ' ' || dirtyString[i] > '~')) + { + dirtyString.erase(i, 1); + i--; + } + break; + } + } + return dirtyString; } -std::string format::CleanString(std::string dirtyString, size_t maxVisualSize, size_t maxStringLength) +std::string format::CleanString(char * dirtyData, bool ascii, bool color, bool newlines, bool numeric) { - 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 (unsigned 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; + return CleanString(std::string(dirtyData), ascii, color, newlines, numeric); } -std::string format::CleanString(char * dirtyData, size_t maxStringLength) -{ - return CleanString(dirtyData, std::string::npos, maxStringLength); -} - -std::string format::CleanString(char * dirtyData, size_t maxVisualSize, size_t 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 (unsigned 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 d5f1176d2..82e958000 100644 --- a/src/Format.h +++ b/src/Format.h @@ -26,10 +26,8 @@ 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, size_t maxVisualSize, size_t maxStringLength); - std::string CleanString(std::string dirtyString, size_t maxStringLength = std::string::npos); - std::string CleanString(char * dirtyData, size_t maxVisualSize, size_t maxStringLength); - std::string CleanString(char * dirtyData, size_t maxStringLength); + std::string CleanString(std::string dirtyString, bool ascii, bool color, bool newlines, bool numeric = false); + std::string CleanString(char * dirtyData, bool ascii, bool color, bool newlines, bool numeric = false); std::vector VideoBufferToPNG(const VideoBuffer & vidBuf); std::vector VideoBufferToBMP(const VideoBuffer & vidBuf); std::vector VideoBufferToPPM(const VideoBuffer & vidBuf); diff --git a/src/Misc.cpp b/src/Misc.cpp index b60277475..4bcc3d9e3 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -20,36 +20,6 @@ #include #endif -const static char hex[] = "0123456789ABCDEF"; -std::string URLEscape(std::string source) -{ - char * src = (char *)source.c_str(); - char * dst = (char *)calloc((source.length()*3)+2, 1); - char *d; - unsigned char *s; - - for (d=dst; *d; d++) ; - - for (s=(unsigned char *)src; *s; s++) - { - if ((*s>='0' && *s<='9') || - (*s>='a' && *s<='z') || - (*s>='A' && *s<='Z')) - *(d++) = *s; - else - { - *(d++) = '%'; - *(d++) = hex[*s>>4]; - *(d++) = hex[*s&15]; - } - } - *d = 0; - - std::string finalString(dst); - free(dst); - return finalString; -} - char *exe_name(void) { #if defined(WIN) @@ -162,21 +132,6 @@ void strlist_free(struct strlist **list) } } -void clean_text(char *text, unsigned int vwidth) -{ - if (strlen(text)*10 > vwidth) - { - text[vwidth/10] = 0; - } - for (unsigned i = 0; i < strlen(text); i++) - { - if (! (text[i]>=' ' && text[i]<127)) - { - text[i] = ' '; - } - } -} - void save_string(FILE *f, char *str) { int li = strlen(str); @@ -203,6 +158,7 @@ int load_string(FILE *f, char *str, int max) return 0; } +const static char hex[] = "0123456789ABCDEF"; void strcaturl(char *dst, char *src) { char *d; diff --git a/src/Misc.h b/src/Misc.h index 8c7f2d3eb..d2c6cc8dd 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -54,8 +54,6 @@ void strlist_free(struct strlist **list); void save_presets(int do_update); -void clean_text(char *text, int vwidth); - void load_presets(void); void save_string(FILE *f, char *str); @@ -64,8 +62,6 @@ int load_string(FILE *f, char *str, int max); void strcaturl(char *dst, const char *src); -std::string URLEscape(std::string source); - void strappend(char *dst, const char *src); void *file_load(char *fn, int *size); diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 42ef06ad0..39a483008 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -2010,7 +2010,7 @@ std::vector > * Client::GetTags(int start, int count { urlStream << "&Search_Query="; if(query.length()) - urlStream << URLEscape(query); + urlStream << format::URLEncode(query); } data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength); @@ -2058,17 +2058,17 @@ std::vector * Client::SearchSaves(int start, int count, std::string q { urlStream << "&Search_Query="; if(query.length()) - urlStream << URLEscape(query); + urlStream << format::URLEncode(query); if(sort == "date") { if(query.length()) - urlStream << URLEscape(" "); - urlStream << URLEscape("sort:") << URLEscape(sort); + urlStream << format::URLEncode(" "); + urlStream << format::URLEncode("sort:") << format::URLEncode(sort); } } if(category.length()) { - urlStream << "&Category=" << URLEscape(category); + urlStream << "&Category=" << format::URLEncode(category); } if(authUser.ID) { diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 3d6d521e6..e6cd7031b 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -517,7 +517,7 @@ void GameSave::readOPS(char * data, int dataLength) { if(strcmp(bson_iterator_key(&signiter), "text")==0 && bson_iterator_type(&signiter)==BSON_STRING) { - tempSign.text = format::CleanString(bson_iterator_string(&signiter), 255); + tempSign.text = format::CleanString(bson_iterator_string(&signiter), true, true, true).substr(0, 255); } else if(strcmp(bson_iterator_key(&signiter), "justification")==0 && bson_iterator_type(&signiter)==BSON_INT) { @@ -1713,7 +1713,7 @@ void GameSave::readPSv(char * data, int dataLength) x = 254; memcpy(tempSignText, d+p, x); tempSignText[x] = 0; - tempSign.text = tempSignText; + tempSign.text = format::CleanString(tempSignText, true, true, true); tempSigns.push_back(tempSign); p += x; } diff --git a/src/gui/interface/CopyTextButton.cpp b/src/gui/interface/CopyTextButton.cpp index 74083ffc9..14ffb5436 100644 --- a/src/gui/interface/CopyTextButton.cpp +++ b/src/gui/interface/CopyTextButton.cpp @@ -19,7 +19,7 @@ namespace ui void CopyTextButton::OnMouseClick(int x, int y, unsigned int button) { ui::Button::OnMouseClick(x, y, button); - ClipboardPush((char*)ButtonText.c_str()); + ClipboardPush(ButtonText); copyTextLabel->SetText("Copied!"); diff --git a/src/gui/interface/Label.cpp b/src/gui/interface/Label.cpp index 78a3b20a9..f15733e7b 100644 --- a/src/gui/interface/Label.cpp +++ b/src/gui/interface/Label.cpp @@ -1,5 +1,6 @@ #include #include "Config.h" +#include "Format.h" #include "Point.h" #include "Label.h" #include "Keys.h" @@ -208,14 +209,15 @@ void Label::OnMouseClick(int x, int y, unsigned button) void Label::copySelection() { std::string currentText = text; + std::string copyText; - if(selectionIndex1 > selectionIndex0) { - ClipboardPush((char*)currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str()); - } else if(selectionIndex0 > selectionIndex1) { - ClipboardPush((char*)currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str()); - } else { - ClipboardPush((char*)currentText.c_str()); - } + if (selectionIndex1 > selectionIndex0) + copyText = currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str(); + else if(selectionIndex0 > selectionIndex1) + copyText = currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str(); + else + copyText = currentText.c_str(); + ClipboardPush(format::CleanString(copyText, false, true, false)); } void Label::OnMouseUp(int x, int y, unsigned button) diff --git a/src/gui/interface/Textbox.cpp b/src/gui/interface/Textbox.cpp index 29c68a252..24804bc70 100644 --- a/src/gui/interface/Textbox.cpp +++ b/src/gui/interface/Textbox.cpp @@ -2,7 +2,8 @@ #include #include #include "Config.h" -#include "Misc.h" +#include "Format.h" +//#include "Misc.h" #include "gui/interface/Point.h" #include "gui/interface/Textbox.h" #include "gui/interface/Keys.h" @@ -138,13 +139,14 @@ void Textbox::cutSelection() { if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length()) return; - ClipboardPush((char*)backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()).c_str()); + std::string toCopy = backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()); + ClipboardPush(format::CleanString(toCopy, false, true, false)); backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); cursor = getLowerSelectionBound(); } else { - ClipboardPush((char*)backingText.c_str()); + ClipboardPush(format::CleanString(backingText, false, true, false)); backingText.clear(); } ClearSelection(); @@ -186,7 +188,7 @@ void Textbox::selectAll() void Textbox::pasteIntoSelection() { - std::string newText = ClipboardPull(); + std::string newText = format::CleanString(ClipboardPull(), true, true, inputType != Multiline, inputType == Number || inputType == Numeric); if (HasSelection()) { if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length()) @@ -194,41 +196,26 @@ void Textbox::pasteIntoSelection() backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); cursor = getLowerSelectionBound(); } - for (std::string::iterator iter = newText.begin(), end = newText.end(); iter != end; ++iter) - { - if (!CharacterValid(*iter)) - { - if (inputType == All || inputType == Multiline) - { - if(*iter == '\n' || *iter == '\r') - *iter = ' '; - else - *iter = '?'; - } - else - *iter = '0'; - } - } int regionWidth = Size.X; - if(Appearance.icon) + if (Appearance.icon) regionWidth -= 13; regionWidth -= Appearance.Margin.Left; regionWidth -= Appearance.Margin.Right; - if(limit!=std::string::npos) + if (limit != std::string::npos) { if(limit-backingText.length() >= 0) newText = newText.substr(0, limit-backingText.length()); else newText = ""; } - else if(!multiline && Graphics::textwidth((char*)std::string(backingText+newText).c_str()) > regionWidth) + else if (!multiline && Graphics::textwidth((char*)std::string(backingText+newText).c_str()) > regionWidth) { int pLimit = regionWidth - Graphics::textwidth((char*)backingText.c_str()); int cIndex = Graphics::CharIndexAtPosition((char *)newText.c_str(), pLimit, 0); - if(cIndex > 0) + if (cIndex > 0) newText = newText.substr(0, cIndex); else newText = "";