[announce] Implement util functions

This commit is contained in:
RD42 2023-10-25 14:46:26 +08:00
parent 5a71f0b9fa
commit 77acff8b2c
2 changed files with 245 additions and 35 deletions

View File

@ -1,55 +1,265 @@
void Util_UrlUnencode()
#include <stdio.h>
#include <string.h>
//----------------------------------------------------
void Util_UrlUnencode(char *enc)
{
// TODO: Util_UrlUnencode L: 08049864
char *write_pos = enc;
while(*enc)
{
if(*enc=='%')
{
*write_pos = (*++enc>'/'&&*enc<':')?((*enc-('0'))<<4):((*enc-('7'))<<4);
*write_pos |= (*++enc>'/'&&*enc<':')?(*enc-'0'):(*enc-'7');
}
else if (*enc=='+')
*write_pos= ' ';
else
*write_pos= *enc;
write_pos++; enc++;
}
*write_pos='\0';
}
void Util_toupper()
//----------------------------------------------------
char Util_toupper(char c) {return ((c>(char)0x60) && (c<(char)0x7b))? c-0x20:c;}
//----------------------------------------------------
char *Util_stristr(const char *String, const char *Pattern)
{
// TODO: Util_toupper L: 08049948
char *pptr, *sptr, *start;
for (start = (char *)String; *start != 0; start++)
{
/* find start of pattern in string */
for ( ; ((*start!=0) && (Util_toupper(*start) != Util_toupper(*Pattern))); start++)
;
if (0 == *start)
return NULL;
pptr = (char *)Pattern;
sptr = (char *)start;
while (Util_toupper(*sptr) == Util_toupper(*pptr))
{
sptr++;
pptr++;
/* if end of pattern then pattern was found */
if (0 == *pptr)
return (start);
}
}
return NULL;
}
void Util_stristr()
//----------------------------------------------------
void Util_strupr(char *string)
{
// TODO: Util_stristr W: 00401920 L: 0804997A
char *p = string;
while(*p) {
*p = Util_toupper(*p);
p++;
}
}
void Util_strupr()
//----------------------------------------------------
int Util_wildcmp(char *wild, char *string)
{
// TODO: Util_strupr L: 08049A40
char *cp, *mp;
while((*string) && (*wild != '*'))
{
if((*wild != *string) && (*wild != '?'))
{
return 0;
}
wild++;
string++;
}
while (*string)
{
if (*wild == '*')
{
if (!*++wild)
{
return 1;
}
mp = wild;
cp = string+1;
}
else if ((*wild == *string) || (*wild == '?'))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while (*wild == '*')
{
wild++;
}
return !*wild;
}
void Util_wildcmp()
//----------------------------------------------------
int Util_strnicmp(const char *s1, const char *s2, size_t n)
{
// TODO: Util_wildcmp L: 08049A72
if (n == 0) return 0;
do
{
if (Util_toupper((unsigned char)*s1) != Util_toupper((unsigned char)*s2++))
return (int)Util_toupper((unsigned char)*s1) - (int)Util_toupper((unsigned char)*--s2);
if (*s1++ == 0)
break;
} while (--n != 0);
return 0;
}
void Util_strnicmp()
//----------------------------------------------------
char *Util_strrev(char *str)
{
// TODO: Util_strnicmp L: 08049B42
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
void Util_strrev()
//----------------------------------------------------
char * Util_itoa(int v, char *s, int r)
{
// TODO: Util_strrev L: 08049BDC
int i,neg = 0;
char *p = s;
char *q = s;
if (r < 0 || r > 35) {
*s = 0;
return (s);
}
if (r == 0) r = 10;
if (v == 0) {
*p++ = '0';
*p = 0;
return (s);
}
if (v < 0) {
neg = 1;
v = -v;
}
while (v > 0) {
i = v % r;
if (i > 9) i += 7;
*p++ = '0' + i;
v /= r;
}
if (neg) *p++ = '-';
*p-- = 0;
q = s;
while (p > q) {
i = *q;
*q++ = *p;
*p-- = i;
}
return (s);
}
void Util_itoa()
//----------------------------------------------------
char * Base64Encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
void Util_Base64Encode( char *cpInput, char *cpOutput )
{
// TODO: Util_itoa L: 08049C60
int nIdx[ 4 ];
while ( '\0' != *cpInput )
{
nIdx[0] = ((*cpInput) & 0xFC)>>2;
nIdx[1] = ((*cpInput) & 0x03)<<4;
cpInput++;
if ( '\0' != *cpInput )
{
nIdx[1] |= ((*cpInput) & 0xF0)>>4;
nIdx[2] = ((*cpInput) & 0x0F)<<2;
cpInput++;
if ( '\0' != (*cpInput) )
{
nIdx[2] |= ((*cpInput) & 0xC0) >> 6;
nIdx[3] = (*cpInput) & 0x3F;
cpInput++;
}
else
nIdx[3] = 64;
}
else
{
nIdx[2] = 64;
nIdx[3] = 64;
}
*(cpOutput+0) = *(Base64Encoding + nIdx[0]);
*(cpOutput+1) = *(Base64Encoding + nIdx[1]);
*(cpOutput+2) = *(Base64Encoding + nIdx[2]);
*(cpOutput+3) = *(Base64Encoding + nIdx[3]);
cpOutput += 4;
}
*cpOutput = '\0';
return;
}
void Util_Base64Encode()
//----------------------------------------------------
void FilterInvalidNickChars(char * szString)
{
// TODO: Util_Base64Encode L: 08049D8C
while(*szString) {
if(*szString < ' ' || *szString > 'z') {
*szString = '_';
}
szString++;
}
}
void FilterInvalidNickChars()
//----------------------------------------------------
int CanFileBeOpenedForReading(char * filename)
{
// TODO: FilterInvalidNickChars L: 08049E90
FILE *f;
if(f=fopen(filename,"r")) {
fclose(f);
return 1;
}
return 0;
}
void CanFileBeOpenedForReading()
{
// TODO: CanFileBeOpenedForReading L: 08049EBA
}
//----------------------------------------------------

View File

@ -1,12 +1,12 @@
void Util_UrlUnencode();
void Util_toupper();
void Util_stristr();
void Util_strupr();
void Util_wildcmp();
void Util_strnicmp();
void Util_strrev();
void Util_itoa();
void Util_Base64Encode();
void FilterInvalidNickChars();
void CanFileBeOpenedForReading();
void Util_UrlUnencode(char *enc);
char Util_toupper(char c);
char *Util_stristr(const char *String, const char *Pattern);
void Util_strupr(char *string);
int Util_wildcmp(char *wild, char *string);
int Util_strnicmp(const char *s1, const char *s2, size_t n);
char *Util_strrev(char *str);
char * Util_itoa(int v, char *s, int r);
void Util_Base64Encode( char *cpInput, char *cpOutput );
//----------------------------------------------------