add CleanText function from my mod, used when copying / pasting / loading signs
This commit is contained in:
parent
67bcd5e863
commit
e990eead10
105
src/Format.cpp
105
src/Format.cpp
@ -16,11 +16,9 @@ std::string format::URLEncode(std::string source)
|
|||||||
std::fill(dst, dst+(source.length()*3)+2, 0);
|
std::fill(dst, dst+(source.length()*3)+2, 0);
|
||||||
|
|
||||||
char *d;
|
char *d;
|
||||||
unsigned char *s;
|
for (d = dst; *d; d++) ;
|
||||||
|
|
||||||
for (d=dst; *d; d++) ;
|
for (unsigned char *s = (unsigned char *)src; *s; s++)
|
||||||
|
|
||||||
for (s=(unsigned char *)src; *s; s++)
|
|
||||||
{
|
{
|
||||||
if ((*s>='0' && *s<='9') ||
|
if ((*s>='0' && *s<='9') ||
|
||||||
(*s>='a' && *s<='z') ||
|
(*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;
|
return CleanString(std::string(dirtyData), ascii, color, newlines, numeric);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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<char> format::VideoBufferToPTI(const VideoBuffer & vidBuf)
|
std::vector<char> format::VideoBufferToPTI(const VideoBuffer & vidBuf)
|
||||||
{
|
{
|
||||||
std::vector<char> data;
|
std::vector<char> data;
|
||||||
|
@ -26,10 +26,8 @@ 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, size_t maxVisualSize, size_t maxStringLength);
|
std::string CleanString(std::string dirtyString, bool ascii, bool color, bool newlines, bool numeric = false);
|
||||||
std::string CleanString(std::string dirtyString, size_t maxStringLength = std::string::npos);
|
std::string CleanString(char * dirtyData, bool ascii, bool color, bool newlines, bool numeric = false);
|
||||||
std::string CleanString(char * dirtyData, size_t maxVisualSize, size_t maxStringLength);
|
|
||||||
std::string CleanString(char * dirtyData, size_t maxStringLength);
|
|
||||||
std::vector<char> VideoBufferToPNG(const VideoBuffer & vidBuf);
|
std::vector<char> VideoBufferToPNG(const VideoBuffer & vidBuf);
|
||||||
std::vector<char> VideoBufferToBMP(const VideoBuffer & vidBuf);
|
std::vector<char> VideoBufferToBMP(const VideoBuffer & vidBuf);
|
||||||
std::vector<char> VideoBufferToPPM(const VideoBuffer & vidBuf);
|
std::vector<char> VideoBufferToPPM(const VideoBuffer & vidBuf);
|
||||||
|
46
src/Misc.cpp
46
src/Misc.cpp
@ -20,36 +20,6 @@
|
|||||||
#include <mach-o/dyld.h>
|
#include <mach-o/dyld.h>
|
||||||
#endif
|
#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)
|
char *exe_name(void)
|
||||||
{
|
{
|
||||||
#if defined(WIN)
|
#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)
|
void save_string(FILE *f, char *str)
|
||||||
{
|
{
|
||||||
int li = strlen(str);
|
int li = strlen(str);
|
||||||
@ -203,6 +158,7 @@ int load_string(FILE *f, char *str, int max)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const static char hex[] = "0123456789ABCDEF";
|
||||||
void strcaturl(char *dst, char *src)
|
void strcaturl(char *dst, char *src)
|
||||||
{
|
{
|
||||||
char *d;
|
char *d;
|
||||||
|
@ -54,8 +54,6 @@ void strlist_free(struct strlist **list);
|
|||||||
|
|
||||||
void save_presets(int do_update);
|
void save_presets(int do_update);
|
||||||
|
|
||||||
void clean_text(char *text, int vwidth);
|
|
||||||
|
|
||||||
void load_presets(void);
|
void load_presets(void);
|
||||||
|
|
||||||
void save_string(FILE *f, char *str);
|
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);
|
void strcaturl(char *dst, const char *src);
|
||||||
|
|
||||||
std::string URLEscape(std::string source);
|
|
||||||
|
|
||||||
void strappend(char *dst, const char *src);
|
void strappend(char *dst, const char *src);
|
||||||
|
|
||||||
void *file_load(char *fn, int *size);
|
void *file_load(char *fn, int *size);
|
||||||
|
@ -2010,7 +2010,7 @@ std::vector<std::pair<std::string, int> > * Client::GetTags(int start, int count
|
|||||||
{
|
{
|
||||||
urlStream << "&Search_Query=";
|
urlStream << "&Search_Query=";
|
||||||
if(query.length())
|
if(query.length())
|
||||||
urlStream << URLEscape(query);
|
urlStream << format::URLEncode(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
|
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
|
||||||
@ -2058,17 +2058,17 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, std::string q
|
|||||||
{
|
{
|
||||||
urlStream << "&Search_Query=";
|
urlStream << "&Search_Query=";
|
||||||
if(query.length())
|
if(query.length())
|
||||||
urlStream << URLEscape(query);
|
urlStream << format::URLEncode(query);
|
||||||
if(sort == "date")
|
if(sort == "date")
|
||||||
{
|
{
|
||||||
if(query.length())
|
if(query.length())
|
||||||
urlStream << URLEscape(" ");
|
urlStream << format::URLEncode(" ");
|
||||||
urlStream << URLEscape("sort:") << URLEscape(sort);
|
urlStream << format::URLEncode("sort:") << format::URLEncode(sort);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(category.length())
|
if(category.length())
|
||||||
{
|
{
|
||||||
urlStream << "&Category=" << URLEscape(category);
|
urlStream << "&Category=" << format::URLEncode(category);
|
||||||
}
|
}
|
||||||
if(authUser.ID)
|
if(authUser.ID)
|
||||||
{
|
{
|
||||||
|
@ -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)
|
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)
|
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;
|
x = 254;
|
||||||
memcpy(tempSignText, d+p, x);
|
memcpy(tempSignText, d+p, x);
|
||||||
tempSignText[x] = 0;
|
tempSignText[x] = 0;
|
||||||
tempSign.text = tempSignText;
|
tempSign.text = format::CleanString(tempSignText, true, true, true);
|
||||||
tempSigns.push_back(tempSign);
|
tempSigns.push_back(tempSign);
|
||||||
p += x;
|
p += x;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ namespace ui
|
|||||||
void CopyTextButton::OnMouseClick(int x, int y, unsigned int button)
|
void CopyTextButton::OnMouseClick(int x, int y, unsigned int button)
|
||||||
{
|
{
|
||||||
ui::Button::OnMouseClick(x, y, button);
|
ui::Button::OnMouseClick(x, y, button);
|
||||||
ClipboardPush((char*)ButtonText.c_str());
|
ClipboardPush(ButtonText);
|
||||||
|
|
||||||
copyTextLabel->SetText("Copied!");
|
copyTextLabel->SetText("Copied!");
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
#include "Format.h"
|
||||||
#include "Point.h"
|
#include "Point.h"
|
||||||
#include "Label.h"
|
#include "Label.h"
|
||||||
#include "Keys.h"
|
#include "Keys.h"
|
||||||
@ -208,14 +209,15 @@ void Label::OnMouseClick(int x, int y, unsigned button)
|
|||||||
void Label::copySelection()
|
void Label::copySelection()
|
||||||
{
|
{
|
||||||
std::string currentText = text;
|
std::string currentText = text;
|
||||||
|
std::string copyText;
|
||||||
|
|
||||||
if(selectionIndex1 > selectionIndex0) {
|
if (selectionIndex1 > selectionIndex0)
|
||||||
ClipboardPush((char*)currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str());
|
copyText = currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str();
|
||||||
} else if(selectionIndex0 > selectionIndex1) {
|
else if(selectionIndex0 > selectionIndex1)
|
||||||
ClipboardPush((char*)currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str());
|
copyText = currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str();
|
||||||
} else {
|
else
|
||||||
ClipboardPush((char*)currentText.c_str());
|
copyText = currentText.c_str();
|
||||||
}
|
ClipboardPush(format::CleanString(copyText, false, true, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Label::OnMouseUp(int x, int y, unsigned button)
|
void Label::OnMouseUp(int x, int y, unsigned button)
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Misc.h"
|
#include "Format.h"
|
||||||
|
//#include "Misc.h"
|
||||||
#include "gui/interface/Point.h"
|
#include "gui/interface/Point.h"
|
||||||
#include "gui/interface/Textbox.h"
|
#include "gui/interface/Textbox.h"
|
||||||
#include "gui/interface/Keys.h"
|
#include "gui/interface/Keys.h"
|
||||||
@ -138,13 +139,14 @@ void Textbox::cutSelection()
|
|||||||
{
|
{
|
||||||
if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length())
|
if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length())
|
||||||
return;
|
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());
|
backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound());
|
||||||
cursor = getLowerSelectionBound();
|
cursor = getLowerSelectionBound();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ClipboardPush((char*)backingText.c_str());
|
ClipboardPush(format::CleanString(backingText, false, true, false));
|
||||||
backingText.clear();
|
backingText.clear();
|
||||||
}
|
}
|
||||||
ClearSelection();
|
ClearSelection();
|
||||||
@ -186,7 +188,7 @@ void Textbox::selectAll()
|
|||||||
|
|
||||||
void Textbox::pasteIntoSelection()
|
void Textbox::pasteIntoSelection()
|
||||||
{
|
{
|
||||||
std::string newText = ClipboardPull();
|
std::string newText = format::CleanString(ClipboardPull(), true, true, inputType != Multiline, inputType == Number || inputType == Numeric);
|
||||||
if (HasSelection())
|
if (HasSelection())
|
||||||
{
|
{
|
||||||
if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length())
|
if (getLowerSelectionBound() < 0 || getHigherSelectionBound() > (int)backingText.length())
|
||||||
@ -194,41 +196,26 @@ void Textbox::pasteIntoSelection()
|
|||||||
backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound());
|
backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound());
|
||||||
cursor = getLowerSelectionBound();
|
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;
|
int regionWidth = Size.X;
|
||||||
if(Appearance.icon)
|
if (Appearance.icon)
|
||||||
regionWidth -= 13;
|
regionWidth -= 13;
|
||||||
regionWidth -= Appearance.Margin.Left;
|
regionWidth -= Appearance.Margin.Left;
|
||||||
regionWidth -= Appearance.Margin.Right;
|
regionWidth -= Appearance.Margin.Right;
|
||||||
|
|
||||||
if(limit!=std::string::npos)
|
if (limit != std::string::npos)
|
||||||
{
|
{
|
||||||
if(limit-backingText.length() >= 0)
|
if(limit-backingText.length() >= 0)
|
||||||
newText = newText.substr(0, limit-backingText.length());
|
newText = newText.substr(0, limit-backingText.length());
|
||||||
else
|
else
|
||||||
newText = "";
|
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 pLimit = regionWidth - Graphics::textwidth((char*)backingText.c_str());
|
||||||
int cIndex = Graphics::CharIndexAtPosition((char *)newText.c_str(), pLimit, 0);
|
int cIndex = Graphics::CharIndexAtPosition((char *)newText.c_str(), pLimit, 0);
|
||||||
|
|
||||||
if(cIndex > 0)
|
if (cIndex > 0)
|
||||||
newText = newText.substr(0, cIndex);
|
newText = newText.substr(0, cIndex);
|
||||||
else
|
else
|
||||||
newText = "";
|
newText = "";
|
||||||
|
Reference in New Issue
Block a user