diff --git a/include/acpi.h b/include/acpi.h index 637ccd2..99a446c 100644 --- a/include/acpi.h +++ b/include/acpi.h @@ -1,3 +1,4 @@ +#pragma once enum acpi_status { ACPI_SUCCESS = 0, diff --git a/include/lib.h b/include/lib.h index 4271aa5..0651770 100644 --- a/include/lib.h +++ b/include/lib.h @@ -1,12 +1,60 @@ - -/** - * The strcmp() function compares the two strings s1 and s2. The locale is not taken into account - * (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters. - */ -int strncmp(const char *s1, const char *s2, unsigned long n); +#pragma once /** * The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory * areas s1 and s2. */ -int memcmp(const void *s1, const void *s2, unsigned long n); +extern int memcmp(const void *restrict s1, const void *restrict s2, unsigned long n); + +/** + * The memcpy() function copies n bytes from memory area src to memory area dest. + * The memory areas must not overlap. + */ +extern void *memcpy(void *restrict dest, const void *restrict src, unsigned long n); + +/** + * The memmove() function copies n bytes from memory area src to memory area dest. The memory areas + * may overlap: copying takes place as though the bytes in src are first copied into a temporary array + * that does not overlap src or dest, and the bytes are then copied from the temporary array to dest. + */ +extern void *memmove(void *dest, const void *src, unsigned long n); + +/** + * The memset() function fills the first n bytes of the memory area pointed to by s with the constant + * byte c. + */ +extern void *memset(void *restrict dest, int c, unsigned long n); + +/** + * The strcmp() function compares the two strings s1 and s2. The locale is not taken into account + * (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters. + */ +extern int strncmp(const char *restrict s1, const char *restrict s2, unsigned long n); + +/** + * @returns 1 if c is a space + */ +int isspace(int c); + +/** + * @returns 1 if c is a digit (0 - 9) + */ +int isdigit(int c); + + +char itoc(int i); +int ctoi(char c); + +int atoi(const char* s); +long int atol(const char* s); +long long int atoll(const char* s); + +char *itoa(int n, char *buffer, int radix); +char *ltoa(long int n, char *buffer, int radix); +char *utoa(unsigned int n, char *buffer, int radix); +char *ultoa(unsigned long int n, char *buffer, int radix); +char *ftoa(float f, char *buffer); + +int strtoi(const char *str, char **endptr, int base); +long int strtol(const char *str, char **endptr, int base); +long long int strtoll(const char *str, char **endptr, int base); diff --git a/src/lib.c b/src/lib.c index d072f1a..a2fa3e5 100644 --- a/src/lib.c +++ b/src/lib.c @@ -1,14 +1,223 @@ #include -int strncmp(const char *lhs, const char *rhs, unsigned long n) { +#include + +int memcmp(const void *restrict vl, const void *restrict vr, unsigned long n) { + const unsigned char *l = vl, *r = vr; + for (; n && *l == *r; n--, l++, r++); + return n ? *l-*r : 0; +} + +void *memcpy(void *restrict dest, const void *restrict src, unsigned long n) { + char *d = dest; + const char *s = src; + for (; n; n--) *d++ = *s++; + return dest; +} + +void *memmove(void *dest, const void *src, unsigned long n) { + char *d = dest; + const char *s = src; + + if (d==s) return d; + + if (d -1 && c - '0' < 10; +} + + +#define ATOX(name, type) \ + type name(const char* s) { \ + for(; isspace(*s); s++); \ + int neg = 0; \ + switch (*s) { \ + case '-': \ + neg = 1; \ + /* FALLTHRU */ \ + case '+': \ + s++; \ + break; \ + } \ + type num = 0; \ + for (; *s == '0'; s++); \ + for (; isdigit(*s); s++) { \ + num *= 10; \ + num += *s - '0'; \ + } \ + return num * (neg ? -1 : 1); \ + } + +ATOX(atoi, int) +ATOX(atol, long int) +ATOX(atoll, long long int) + +char itoc(int i) { + if(i < 10) { + return '0' + i; + } else { + return 'a' + (i - 10); + } +} + +int ctoi(char c) { + if(c < 'A') { + return c - '0'; + } else if(c < 'a') { + return c - 'A' + 10; + } else { + return c - 'a' + 10; + } +} + +#define UXTOA(type, name) \ + char *name(unsigned type n, char *buffer, int radix) { \ + if (n == 0) { \ + buffer[0] = '0'; \ + buffer[1] = '\0'; \ + return buffer; \ + } \ + char *start = buffer; \ + for (; n; n /= radix) { \ + *buffer++ = itoc(n % radix); \ + } \ + *buffer-- = '\0'; \ + while(buffer > start) { \ + char tmp = *start; \ + *start++ = *buffer; \ + *buffer-- = tmp; \ + } \ + return buffer; \ + } + +UXTOA(int, utoa) +UXTOA(long int, ultoa) + +#define XTOA(type, name) \ + char *name(type n, char* buffer, int radix) { \ + if (n == 0) { \ + buffer[0] = '0'; \ + buffer[1] = '\0'; \ + return buffer; \ + } \ + if (n < 0) { \ + *buffer++ = '-'; \ + n = -n; \ + } \ + char *start = buffer; \ + for (; n; n /= radix) { \ + *buffer++ = itoc(n % radix); \ + } \ + *buffer-- = '\0'; \ + while(buffer > start) { \ + char tmp = *start; \ + *start++ = *buffer; \ + *buffer-- = tmp; \ + } \ + return buffer; \ + } + +XTOA(int, itoa) +XTOA(long int, ltoa) + +char *ftoa(float f, char *buffer) { + char *b = buffer; + if (f < 0 || f == -0) + *buffer++ = '-'; + int n, i=0, k=0; + n = (int) f; + while (n > 0) { + f /= 10; + n = (int)f; + i++; + } + *(buffer+i) = '.'; + f *= 10; + n = (int) f; + f -= n; + while ((n > 0.1) || (i > k)) { + if (k == i) + k++; + *(buffer+k) = '0' + n; + f *= 10; + n = (int) f; + f -= n; + k++; + } + *(buffer+k) = '\0'; + return b; +} + +#define STRTOX(name, type) \ + type name(const char *s, char **endptr, int radix) { \ + *endptr = NULL; \ + for(; isspace(*s); s++); \ + int neg = 0; \ + switch (*s) { \ + case '-': \ + neg = 1; \ + /* FALLTHRU */ \ + case '+': \ + s++; \ + break; \ + } \ + if (!radix) { \ + if (*s == '0') { \ + s++; \ + if (*s == 'x' || *s == 'X') { \ + s++; \ + radix = 16; \ + } else { \ + radix = 8; \ + } \ + } else { \ + radix = 10; \ + } \ + } \ + for (; *s == '0'; s++, *endptr = (void*) s); \ + type num = 0; \ + for (; isdigit(*s); s++, *endptr = (void*) s) { \ + num *= radix; \ + num += *s - '0'; \ + } \ + return num * (neg ? -1 : 1); \ + } + +STRTOX(strtoi, int) +STRTOX(strtol, long int) +STRTOX(strtoll, long long int)