- clean up repo parsing code
[platform/upstream/libsolv.git] / ext / tools_util.h
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * util.h
10  *
11  */
12
13 #ifndef LIBSOLV_TOOLS_UTIL_H
14 #define LIBSOLV_TOOLS_UTIL_H
15
16 static char *_join_tmp;
17 static int _join_tmpl;
18
19 static inline Id
20 makeevr(Pool *pool, const char *s)
21 {
22   if (!strncmp(s, "0:", 2) && s[2])
23     s += 2;
24   return pool_str2id(pool, s, 1);
25 }
26
27 /**
28  * split a string
29  */
30 #ifndef DISABLE_SPLIT
31 static int
32 split(char *l, char **sp, int m)
33 {
34   int i;
35   for (i = 0; i < m;)
36     {
37       while (*l == ' ')
38         l++;
39       if (!*l)
40         break;
41       sp[i++] = l;
42       while (*l && *l != ' ')
43         l++;
44       if (!*l)
45         break;
46       *l++ = 0;
47     }
48   return i;
49 }
50 #endif
51
52 /* this join does not depend on parsedata */
53 static char *
54 join2(const char *s1, const char *s2, const char *s3)
55 {
56   int l = 1;
57   char *p;
58
59   if (s1)
60     l += strlen(s1);
61   if (s2)
62     l += strlen(s2);
63   if (s3)
64     l += strlen(s3);
65   if (l > _join_tmpl)
66     {
67       _join_tmpl = l + 256;
68       if (!_join_tmp)
69         _join_tmp = malloc(_join_tmpl);
70       else
71         _join_tmp = realloc(_join_tmp, _join_tmpl);
72     }
73   p = _join_tmp;
74   if (s1)
75     {
76       strcpy(p, s1);
77       p += strlen(s1);
78     }
79   if (s2)
80     {
81       strcpy(p, s2);
82       p += strlen(s2);
83     }
84   if (s3)
85     {
86       strcpy(p, s3);
87       p += strlen(s3);
88     }
89   *p = 0;
90   return _join_tmp;
91 }
92
93 static inline void
94 join_freemem(void)
95 {
96   if (_join_tmp)
97     free(_join_tmp);
98   _join_tmp = 0;
99   _join_tmpl = 0;
100 }
101
102 /* util function to set a translated string */
103 static inline void repodata_set_tstr(Repodata *data, Id handle, const char *attrname, const char *lang, const char *str)
104 {
105   Id attrid;
106   attrid = pool_str2id(data->repo->pool, join2(attrname, ":", lang), 1);
107   repodata_set_str(data, handle, attrid, str);
108 }
109
110 #endif /* LIBSOLV_TOOLS_UTIL_H */