Code:
#include <cstdio>
void* malloc(unsigned int);
void* memcpy(void*, const void*, unsigned int);
int memcmp(const void*, const void*, unsigned int);
unsigned int strlen(const char*);
char* str2lower(char* buffer, int len)
{
for(int x=0;x<strlen(buffer);x++)
if (buffer[x] < 91 && buffer[x] > 64)
buffer[x] += 32;
return buffer;
}
char* str2upper(char* buffer)
{
for(int x=0;x<strlen(buffer);x++)
if (buffer[x] < 123 && buffer[x] > 96)
buffer[x] -= 32;
return buffer;
}
bool isValidSubstrKey(int a, unsigned int b, unsigned int len)
{
if (((a < 0) ? -a : a) > len)
return false;
if (a < 0)
return -a >= b;
return len >= a + b;
}
char* substr(FILE* fp, int a, unsigned int b)
{
int pos = ftell(fp);
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
fseek(fp, pos, SEEK_SET);
if (b == 0)
b = (a < 0) ? -a : len - a;
char* buffer = (char*) malloc(b);
if (buffer == NULL || !isValidSubstrKey(a, b, len))
return NULL;
if (fseek(fp, a, SEEK_SET) != 0)
return NULL;
fread(buffer, 1, b, fp);
buffer[b] = 0;
fseek(fp, pos, SEEK_SET);
return buffer;
}
char* substr(const char* str, int a, unsigned int b, int len)
{
if (b == 0)
b = (a < 0) ? -a : strlen(str) - a;
char* buffer = (char*) malloc(b);
if (buffer == NULL || !isValidSubstrKey(a, b, strlen(str)))
return NULL;
memcpy(buffer, &str[(a < 0) ? strlen(str) + a : a], b);
buffer[b] = 0;
return buffer;
}
int strpos(const char* str, const char* find, int offset, int len)
{
int o = - strlen(find),
x = 0;
for(;offset<strlen(str);offset++)
if (x == strlen(find))
break;
else if (find[x] != str[offset])
x = 0;
else
x++;
return (offset == strlen(str)) ? -1 : offset + o;
}
int strpos(FILE* fp, const char* find, int offset)
{
int o = - strlen(find),
p = ftell(fp),
x = 0;
char str = 0;
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
fseek(fp, offset, SEEK_SET);
for(;offset<len;offset++)
{
fread(&str, 1, sizeof(str), fp);
if (x == strlen(find))
break;
else if (find[x] != str)
x = 0;
else
x++;
}
fseek(fp, p, SEEK_SET);
return (offset == len) ? -1 : offset + o;
}
bool strcmp2(const char* a, const char* b, int len)
{
if (strlen(a) != strlen(b))
return false;
return memcmp(a, b, strlen(a)) == 0;
}
strpos interessiert dich vielleicht.