Skip to content

Commit

Permalink
add CleanText function from my mod, used when copying / pasting / loa…
Browse files Browse the repository at this point in the history
…ding signs
  • Loading branch information
jacob1 committed Jul 10, 2015
1 parent 67bcd5e commit e990eea
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 139 deletions.
101 changes: 53 additions & 48 deletions src/Format.cpp
Expand Up @@ -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') ||
Expand Down Expand Up @@ -71,58 +69,65 @@ std::string format::UnixtimeToDateMini(time_t unixtime)
}
}

std::string format::CleanString(std::string dirtyString, size_t maxStringLength)
{
return CleanString(dirtyString, std::string::npos, maxStringLength);
}

std::string format::CleanString(std::string dirtyString, size_t maxVisualSize, size_t maxStringLength)
std::string format::CleanString(std::string dirtyString, 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)
for (size_t i = 0; i < dirtyString.size(); i++)
{
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
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 newString;
return dirtyString;
}

std::string format::CleanString(char * dirtyData, size_t maxStringLength)
std::string format::CleanString(char * dirtyData, bool ascii, bool color, bool newlines, bool numeric)
{
return CleanString(dirtyData, std::string::npos, maxStringLength);
return CleanString(std::string(dirtyData), ascii, color, newlines, numeric);
}

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<char> format::VideoBufferToPTI(const VideoBuffer & vidBuf)
{
std::vector<char> data;
Expand Down
6 changes: 2 additions & 4 deletions src/Format.h
Expand Up @@ -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<char> VideoBufferToPNG(const VideoBuffer & vidBuf);
std::vector<char> VideoBufferToBMP(const VideoBuffer & vidBuf);
std::vector<char> VideoBufferToPPM(const VideoBuffer & vidBuf);
Expand Down
46 changes: 1 addition & 45 deletions src/Misc.cpp
Expand Up @@ -20,36 +20,6 @@
#include <mach-o/dyld.h>
#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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/Misc.h
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/client/Client.cpp
Expand Up @@ -2010,7 +2010,7 @@ std::vector<std::pair<std::string, int> > * 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);
Expand Down Expand Up @@ -2058,17 +2058,17 @@ std::vector<SaveInfo*> * 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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/client/GameSave.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/interface/CopyTextButton.cpp
Expand Up @@ -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!");

Expand Down
16 changes: 9 additions & 7 deletions src/gui/interface/Label.cpp
@@ -1,5 +1,6 @@
#include <string>
#include "Config.h"
#include "Format.h"
#include "Point.h"
#include "Label.h"
#include "Keys.h"
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 10 additions & 23 deletions src/gui/interface/Textbox.cpp
Expand Up @@ -2,7 +2,8 @@
#include <iostream>
#include <stdexcept>
#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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -186,49 +188,34 @@ 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())
return;
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 = "";
Expand Down

0 comments on commit e990eea

Please sign in to comment.