2017-11-02 08:00:40 -04:00
|
|
|
#ifndef KERNELMQ_INCLUDED_UTIL
|
|
|
|
#define KERNELMQ_INCLUDED_UTIL 1
|
2017-11-01 01:01:29 -04:00
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
static inline unsigned int strlen(const char *s);
|
2017-11-02 08:06:19 -04:00
|
|
|
static inline void itoa(char *buf, int base, int d);
|
2017-11-01 01:01:29 -04:00
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
unsigned int strlen(const char *const s)
|
2017-11-01 01:01:29 -04:00
|
|
|
{
|
2017-11-01 06:08:09 -04:00
|
|
|
unsigned int result = 0;
|
2017-11-01 01:01:29 -04:00
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
while (s[result]) {
|
|
|
|
++result;
|
2017-11-01 01:01:29 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
return result;
|
2017-11-01 01:01:29 -04:00
|
|
|
}
|
|
|
|
|
2017-11-02 08:06:19 -04:00
|
|
|
void itoa(char *buf, int base, int d)
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
char *p1, *p2;
|
|
|
|
unsigned long ud = d;
|
|
|
|
int divisor = 10;
|
|
|
|
|
|
|
|
// If %d is specified and D is minus, put '-' in the head.
|
|
|
|
if (base == 'd' && d < 0) {
|
|
|
|
*p++ = '-';
|
|
|
|
buf++;
|
|
|
|
ud = -d;
|
|
|
|
}
|
|
|
|
else if (base == 'x') {
|
|
|
|
divisor = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Divide UD by DIVISOR until UD == 0.
|
|
|
|
do {
|
|
|
|
int remainder = ud % divisor;
|
|
|
|
*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
|
|
|
|
}
|
|
|
|
while (ud /= divisor);
|
|
|
|
|
|
|
|
// Terminate BUF.
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
// Reverse BUF.
|
|
|
|
p1 = buf;
|
|
|
|
p2 = p - 1;
|
|
|
|
|
|
|
|
while (p1 < p2) {
|
|
|
|
char tmp = *p1;
|
|
|
|
*p1 = *p2;
|
|
|
|
*p2 = tmp;
|
|
|
|
p1++;
|
|
|
|
p2--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 01:01:29 -04:00
|
|
|
#endif
|