Minor Updates:

- DateTimeCore:
  - Specify separator between date & time fof BiuildDateTimeStr();
- UtilCore:
  - Add UrlEncode() and UrlDecode() functions
This commit is contained in:
Charl Wentzel
2019-10-06 19:38:11 +02:00
parent c2c08c527c
commit 7c77d564a7
4 changed files with 91 additions and 5 deletions

View File

@@ -213,3 +213,79 @@ char * StrSearch( const char * Haystack, const char * Needle, const int Haystac
return NULL;
}
//---------------------------------------------------------------------------
bool UrlEncode( const char * Input, char * Output, char ** EndPos )
{
int c;
char h[] = "0123456789abcdef";
char * Ipos = (char*)Input;
char * Opos = Output;
if (!Ipos || !Opos) {
if (*EndPos) *EndPos = NULL;
return false;
}
while ((c = *Ipos)) {
if ((('a' <= c) && (c <= 'z')) ||
(('A' <= c) && (c <= 'Z')) ||
(('0' <= c) && (c <= '9')) ||
(c == '-') || (c == '_') || (c == '.')) {
*Opos = *Ipos;
Opos++;
}
else if (c == ' ') {
*Opos = '+';
Opos++;
}
else {
Opos[0] = '%';
Opos[1] = h[c >> 4];
Opos[2] = h[c & 0x0f];
Opos += 3;
}
Ipos++;
}
*Opos = 0;
if (EndPos) *EndPos = Opos;
return true;
}
//---------------------------------------------------------------------------
bool UrlDecode( const char * Input, char * Output, char ** EndPos )
{
char c, c1, c2;
char * Ipos = (char*)Input;
char * Opos = Output;
if (!Ipos || !Opos) {
if (EndPos) *EndPos = Output;
return false;
}
while ((c = *Ipos)) {
if (c == '%') {
if (!(c1 = tolower(Ipos[1])) ||
!(c2 = tolower(Ipos[2])) ||
!isxdigit(c1) || !isxdigit(c2))
return false;
c1 = (c1 <= '9')? (c1 - '0') : (c1 - 'a' + 10);
c2 = (c2 <= '9')? (c2 - '0') : (c2 - 'a' + 10);
*Opos = 16*c1 + c2;
Ipos += 3;
}
else if (c == '+') {
*Opos = ' ';
Ipos++;
}
else {
*Opos = c;
Ipos++;
}
Opos++;
}
*Opos = 0;
if (EndPos) *EndPos = Opos;
return true;
}
//---------------------------------------------------------------------------