93 lines
3.4 KiB
C
93 lines
3.4 KiB
C
|
#ifndef __STDLIB_H__
|
||
|
#define __STDLIB_H__
|
||
|
|
||
|
/** Returns the absolute value of an integer.*/
|
||
|
#define abs(x) (x>0) ? (x) : (x*-1)
|
||
|
/** Returns the absolute value of a long variable.*/
|
||
|
#define labs(x) (x>0) ? (x) : (x*-1)
|
||
|
/** Returns the maximum of two numbers.*/
|
||
|
#define max(a, b) (a > b) ? a : b
|
||
|
/** Returns the minimum of two numbers.*/
|
||
|
#define min(a, b) (a < b) ? a : b
|
||
|
#define NULL 0
|
||
|
|
||
|
/** div_t is a structure of integers used by div()\n
|
||
|
Notes:\n
|
||
|
- quot = quotient;\n
|
||
|
- rem = remainder;\n */
|
||
|
typedef struct {
|
||
|
/** Quotient */
|
||
|
long quot;
|
||
|
/** Remainder */
|
||
|
long rem;
|
||
|
} div_t;
|
||
|
|
||
|
/** ldiv_t is a structure of integers used by ldiv()\n
|
||
|
Notes:\n
|
||
|
- quot = quotient;\n
|
||
|
- rem = remainder;\n */
|
||
|
typedef struct {
|
||
|
/** Quotient*/
|
||
|
long quot;
|
||
|
/** Remainder*/
|
||
|
long rem;
|
||
|
} ldiv_t;
|
||
|
|
||
|
|
||
|
// TODO: extern long double _atold (const char* string);
|
||
|
// TODO: extern double atof (const char* string); // TODO: initialize FPU
|
||
|
|
||
|
/** Convert ASCII string to INT */
|
||
|
extern int atoi (const char* string);
|
||
|
|
||
|
/** Convert ASCII string to LONG */
|
||
|
extern long atol (const char* string);
|
||
|
|
||
|
/** Convert ASCII string in hexadecimal to unsigned integer.*/
|
||
|
extern unsigned int atox (const char* string);
|
||
|
|
||
|
/** Peform a binary search\n
|
||
|
Notes:\n
|
||
|
- const void* key = A pointer to the element to look for\n
|
||
|
- const void* base = A pointer to the first element of the table\n
|
||
|
- unsigned nelem = The number of elements in the table\n
|
||
|
- unsigned width = The size of one element of the table\n
|
||
|
- int *fcmp = A user defined comparison routine\n */
|
||
|
extern void* bsearch (const void* key, const void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*));
|
||
|
|
||
|
/**Divides two integers and returns both the quotient and the remainder as a div_t structure.*/
|
||
|
extern div_t div (int numerator, int denominator);
|
||
|
|
||
|
/** Convert SIGNED INT to ASCII string */
|
||
|
extern void itoa (signed int value, char *string, int radix);
|
||
|
|
||
|
/**Divides two longs and returns both the quotient and the remainder as a ldiv_t structure.*/
|
||
|
extern ldiv_t ldiv (long numerator, long denominator);
|
||
|
|
||
|
/**Does a linear search for *key in a table\n
|
||
|
Notes:\n
|
||
|
- const void* key = A pointer to the element to look for\n
|
||
|
- const void* base = A pointer to the first element of the table\n
|
||
|
- unsigned nelem = The number of elements in the table\n
|
||
|
- unsigned width = The size of one element of the table\n
|
||
|
- int *fcmp = A user defined comparison routine\n */
|
||
|
void* lfind (const void* key, const void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*));
|
||
|
|
||
|
/** Convert SIGNED LONG to ASCII string */
|
||
|
extern void ltoa (signed long value, char *string, int radix);
|
||
|
|
||
|
/** Sorts an array using an optimized quick sort algorithm.\n
|
||
|
Notes:\n
|
||
|
- void base = A pointer to the first element of the table\n
|
||
|
- unsigned *nelem = The number of elements in the table\n
|
||
|
- unsigned width = The size of one element of the table\n
|
||
|
- int *fcmp = A user defined comparison routine\n */
|
||
|
void qsort (void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*));
|
||
|
|
||
|
/** Convert UNSIGNED INT to ASCII string */
|
||
|
extern void uitoa (unsigned int value, char *string, int radix);
|
||
|
|
||
|
/** Convert UNSIGNED LONG to ASCII string */
|
||
|
extern void ultoa (unsigned long value, char *string, int radix);
|
||
|
|
||
|
#endif
|