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