-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.h
81 lines (73 loc) · 2.07 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* # `utils.h`
* A variety of utility functions.
*
* Some of the functions are duplicates of well known C functions that are not
* standard.
*
* ### License
*
* Author: Werner Stoop
* This is free and unencumbered software released into the public domain.
* See http://unlicense.org/ for more details.
*
* ## API
* ### Macros
*/
/**
* #### `MY_MIN(a,b)`
* Macro that returns the smallest of its parameters.
*
* As with all macros, `a` and `b` should not have side effects.
*/
#define MY_MIN(a,b) (((a)<(b))?(a):(b))
/**
* #### `MY_MAX(a,b)`
* Macro that returns the largest of its parameters.
*
* As with all macros, `a` and `b` should not have side effects.
*/
#define MY_MAX(a,b) (((a)>(b))?(a):(b))
/**
* ### Functions
*/
/**
* #### `int my_stricmp(const char *p, const char *q)`
* Compares two strings `p` and `q` case insensitively.
*
* It returns 0 if the strings are the same, a positive number if `p` comes after `q`,
* and negative if `p` comes before `q`.
*/
int my_stricmp(const char *p, const char *q);
/**
* #### `char *my_strdup(const char *s)`
* Creates a duplicate of a string `s` in a dynamic memory buffer.
*
* The returned buffer needs to be `free()`d after use. It may return
* `NULL` if the memory allocation fails.
*/
char *my_strdup(const char *s);
/**
* #### `char *my_strlower (char *p)`
* Converts a string `p` to lowercase in place.
* It returns `p`.
*/
char *my_strlower (char *p);
/**
* #### `char *my_strupper (char *p)`
* Converts a string `p` to uppercase in place.
* It returns `p`.
*/
char *my_strupper (char *p);
/**
* #### `char *my_strtok_r(char *str, const char *delim, char **saveptr)`
* Works the same as `strtok_r(3)` for platforms which lack it.
*/
char *my_strtok_r(char *str, const char *delim, char **saveptr);
/**
* #### `char *my_readfile (const char *fn)`
* Reads an entire file identified by `fn` into a dynamically allocated memory buffer.\n
* The returned buffer needs to be `free()`d afterwards.\n
* It returns `NULL` if the file could not be read.
*/
char *my_readfile (const char *fn);