rename satsolver -> libsolv
[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 struct parsedata_common {
20   char *tmp;
21   int tmpl;
22   Pool *pool;
23   Repo *repo;
24 };
25
26 static inline Id
27 makeevr(Pool *pool, const char *s)
28 {
29   if (!strncmp(s, "0:", 2) && s[2])
30     s += 2;
31   return str2id(pool, s, 1);
32 }
33
34 /**
35  * split a string
36  */
37 #ifndef DISABLE_SPLIT
38 static int
39 split(char *l, char **sp, int m)
40 {
41   int i;
42   for (i = 0; i < m;)
43     {
44       while (*l == ' ')
45         l++;
46       if (!*l)
47         break;
48       sp[i++] = l;
49       while (*l && *l != ' ')
50         l++;
51       if (!*l)
52         break;
53       *l++ = 0;
54     }
55   return i;
56 }
57 #endif
58
59 /* this join does not depend on parsedata */
60 static char *
61 join2(const char *s1, const char *s2, const char *s3)
62 {
63   int l = 1;
64   char *p;
65
66   if (s1)
67     l += strlen(s1);
68   if (s2)
69     l += strlen(s2);
70   if (s3)
71     l += strlen(s3);
72   if (l > _join_tmpl)
73     {
74       _join_tmpl = l + 256;
75       if (!_join_tmp)
76         _join_tmp = malloc(_join_tmpl);
77       else
78         _join_tmp = realloc(_join_tmp, _join_tmpl);
79     }
80   p = _join_tmp;
81   if (s1)
82     {
83       strcpy(p, s1);
84       p += strlen(s1);
85     }
86   if (s2)
87     {
88       strcpy(p, s2);
89       p += strlen(s2);
90     }
91   if (s3)
92     {
93       strcpy(p, s3);
94       p += strlen(s3);
95     }
96   *p = 0;
97   return _join_tmp;
98 }
99
100 static inline void
101 join_freemem(void)
102 {
103   if (_join_tmp)
104     free(_join_tmp);
105   _join_tmp = 0;
106   _join_tmpl = 0;
107 }
108
109 /* util function to set a translated string */
110 static inline void repodata_set_tstr(Repodata *data, Id handle, const char *attrname, const char *lang, const char *str)
111 {
112   Id attrid;
113   attrid = str2id(data->repo->pool, join2(attrname, ":", lang), 1);
114   repodata_set_str(data, handle, attrid, str);
115 }
116
117 #endif /* LIBSOLV_TOOLS_UTIL_H */