]> git.karo-electronics.de Git - oswald.git/blob - ui/oswald_strings.c
Shorten charger plug "tail"
[oswald.git] / ui / oswald_strings.c
1 #include "oswald.h"
2
3 #include "oswald_strings.h"
4
5 uint16_t oswald_strlen(char *string)
6 {
7         int i=0;
8
9         if (string == NULL)
10                 return 0;
11
12         while (string[i] != 0) i++;
13
14         return i;
15 }
16
17 char *itoa(int16_t value, char* result, int base)
18 {
19         char *ptr = result, *ptr1 = result, tmp_char;
20         int16_t tmp_value;
21
22         if (result == NULL)
23                 return NULL;
24
25         // check that the base if valid
26         if (base < 2 || base > 36) {
27                 *result = '\0';
28                 return result;
29         }
30
31         do {
32                 tmp_value = value;
33                 value /= base;
34                 *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
35         } while ( value );
36
37         // Apply negative sign
38         if (tmp_value < 0)
39                 *ptr++ = '-';
40         *ptr-- = '\0';
41         while (ptr1 < ptr) {
42                 tmp_char = *ptr;
43                 *ptr--= *ptr1;
44                 *ptr1++ = tmp_char;
45         }
46         return result;
47 }
48