b4edf432ac613bc00fefdc9c35661de20aa225d7
[coreboot.git] / src / include / string.h
1 #ifndef STRING_H
2 #define STRING_H
3
4 #include <stddef.h>
5 #include <stdlib.h>
6
7 void *memcpy(void *dest, const void *src, size_t n);
8 void *memmove(void *dest, const void *src, size_t n);
9 void *memset(void *s, int c, size_t n);
10 int memcmp(const void *s1, const void *s2, size_t n);
11 #if !defined(__PRE_RAM__)
12 int sprintf(char * buf, const char *fmt, ...);
13 #endif
14
15 // simple string functions 
16
17 static inline size_t strnlen(const char *src, size_t max) 
18
19         size_t i = 0;
20         while((*src++) && (i < max)) {
21                 i++;
22         }
23         return i;
24 }
25
26 static inline size_t strlen(const char *src)
27 {
28         size_t i = 0;
29         while(*src++) {
30                 i++;
31         }
32         return i;
33 }
34
35 static inline char *strchr(const char *s, int c)
36 {
37         for (; *s; s++) {
38                 if (*s == c)
39                         return (char *) s;
40         }       
41         return 0;
42 }
43
44 #if !defined(__PRE_RAM__)
45 static inline char *strdup(const char *s)
46 {   
47         size_t sz = strlen(s) + 1;
48         char *d = malloc(sz);
49         memcpy(d, s, sz);
50         return d;
51 }
52 #endif
53
54 static inline char *strncpy(char *to, const char *from, int count)
55 {
56         register char *ret = to;
57
58         while (count > 0) {
59                 count--;
60                 if ((*to++ = *from++) == '\0')
61                         break;
62         }
63
64         while (count > 0) {
65                 count--;
66                 *to++ = '\0';
67         }
68         return ret;
69 }
70
71 static inline int strcmp(const char *s1, const char *s2)
72 {   
73         int r;
74
75         while ((r = (*s1 - *s2)) == 0 && *s1) {
76                 s1++;
77                 s2++;
78         }
79         return r;
80 }  
81
82 static inline int strncmp(const char *s1, const char *s2, int maxlen)
83 {
84         int i;
85
86         for (i = 0; i < maxlen; i++) {
87                 if (s1[i] != s2[i])
88                         return s1[i] - s2[i];
89         }
90
91         return 0;
92 }
93
94 static inline int isspace(int c)
95 {
96         switch (c) {
97         case ' ': case '\f': case '\n':
98         case '\r': case '\t': case '\v':
99                 return 1;
100         default:
101                 return 0;
102         }
103 }
104
105 static inline int isdigit(int c)
106 {
107         return (c >= '0' && c <= '9');
108 }
109
110 static inline int isxdigit(int c)
111 {
112         return ((c >= '0' && c <= '9') ||
113                 (c >= 'a' && c <= 'f') ||
114                 (c >= 'A' && c <= 'F'));
115 }
116
117 static inline int isupper(int c)
118 {
119         return (c >= 'A' && c <= 'Z');
120 }
121
122 static inline int islower(int c)
123 {
124         return (c >= 'a' && c <= 'z');
125 }
126
127 static inline int toupper(int c)
128 {
129         if (islower(c))
130                 c -= 'a'-'A';
131         return c;
132 }
133
134 static inline int tolower(int c)
135 {
136         if (isupper(c))
137                 c -= 'A'-'a';
138         return c;
139 }
140 #endif /* STRING_H */