]> git.itanic.dy.fi Git - rrdd/blob - string.c
string: Add functions to skip numbers and non-numbers
[rrdd] / string.c
1 #include "string.h"
2
3 int dec_to_int(char *src, char **dst)
4 {
5         int ret;
6
7         src = skip_non_numbers(src);
8
9         ret = atoi(src);
10
11         if (dst) {
12                 while(((*src >= '0') && (*src <= '9')) && *src)
13                         src++;
14                 *dst = src;
15         }
16
17         return ret;
18 }
19
20 long long dec_to_longlong(char *src, char **dst)
21 {
22         long long ret;
23
24         src = skip_non_numbers(src);
25
26         ret = atoll(src);
27
28         if (dst) {
29                 while(((*src >= '0') && (*src <= '9')) && *src)
30                         src++;
31                 *dst = src;
32         }
33
34         return ret;
35 }
36
37 float dec_to_float(char *src, char **dst)
38 {
39         float ret;
40
41         src = skip_non_numbers(src);
42
43         ret = atof(src);
44
45         if (dst) {
46                 while((((*src >= '0') && (*src <= '9')) || (*src =='.')) && *src)
47                         src++;
48                 *dst = src;
49         }
50
51         return ret;
52 }
53
54 /*
55  * Copy white space separated stirng from *src to *word
56  *
57  * src:  source
58  * dst:  place where store the end of word pointer from source string
59  * word: destination where to copy the string
60  * size: maximum size of bytes to copy to the output buffer
61  *
62  * return the number of bytes copied
63  */
64
65 int get_word(char *src, char **dst, char *word, int size)
66 {
67         int ret = 0;
68         while(((*src == ' ') || (*src == '\t') || (*src == '\n')) && *src)
69                 src++;
70
71         while(((*src != ' ') && (*src != '\t') && (*src != '\n')) && *src && (ret < (size - 1))) {
72                 *word = *src;
73                 word++;
74                 src++;
75                 ret++;
76         }
77         *word = 0;
78
79         if (dst)
80                 *dst = src;
81
82         return ret;
83 }
84