CPP_Snippets/functions.cpp
2022-11-13 10:37:20 +01:00

100 lines
1.9 KiB
C++
Executable File

uint8_t utf8Ascii(uint8_t ascii) {
static uint8_t cPrev;
uint8_t c = '\0';
if (ascii < 0x7f) // Standard ASCII-set 0..0x7F, no conversion
{
cPrev = '\0';
c = ascii;
}
else
{
switch (cPrev) // Conversion depending on preceding UTF8-character
{
case 0xC2: c = ascii; break;
case 0xC3: c = ascii | 0xC0; break;
case 0x82: if (ascii==0xAC) c = 0x80; // Euro symbol special case
}
cPrev = ascii; // save last char
}
return(c);
}
void utf8Ascii(char* s) {
uint8_t c, k = 0;
char *cp = s;
while (*s != '\0')
{
c = utf8Ascii(*s++);
if (c != '\0')
*cp++ = c;
}
*cp = '\0'; // terminate the new string
}
char* string2char(String command){
if(command.length()!=0){
char *p = const_cast<char*>(command.c_str());
return p;
}
}
// constant char* to char*
char* cchar2char(String data){
char* out = new char[data.length()+1];
strcpy(out, data.c_str());
return out;
}
char * stringReplace(char *search, char *replace, char *string) {
char *tempString, *searchStart;
int len=0;
searchStart = strstr(string, search);
if(searchStart == NULL) {
return string;
}
tempString = (char*) malloc(strlen(string) * sizeof(char));
if(tempString == NULL) {
return NULL;
}
strcpy(tempString, string);
len = searchStart - string;
string[len] = '\0';
strcat(string, replace);
len += strlen(search);
strcat(string, (char*)tempString+len);
free(tempString);
return string;
}
String split(String s, char parser, int index) {
String rs="";
int parserIndex = index;
int parserCnt=0;
int rFromIndex=0, rToIndex=-1;
while (index >= parserCnt) {
rFromIndex = rToIndex+1;
rToIndex = s.indexOf(parser,rFromIndex);
if (index == parserCnt) {
//if (rToIndex < 0 || rToIndex < -1) return "";
return s.substring(rFromIndex,rToIndex);
} else parserCnt++;
}
return rs;
}