- create libsatsolverext static library
[platform/upstream/libsolv.git] / ext / tools_util.h
1
2 /*
3  * Copyright (c) 2007, Novell Inc.
4  *
5  * This program is licensed under the BSD license, read LICENSE.BSD
6  * for further information
7  */
8
9 /*
10  * util.h
11  *
12  */
13
14 #ifndef SATSOLVER_TOOLS_UTIL_H
15 #define SATSOLVER_TOOLS_UTIL_H
16
17 static char *_join_tmp;
18 static int _join_tmpl;
19
20 struct parsedata_common {
21   char *tmp;
22   int tmpl;
23   Pool *pool;
24   Repo *repo;
25 };
26
27 static inline Id
28 makeevr(Pool *pool, const char *s)
29 {
30   if (!strncmp(s, "0:", 2) && s[2])
31     s += 2;
32   return str2id(pool, s, 1);
33 }
34
35 /**
36  * split a string
37  */
38 #ifndef DISABLE_SPLIT
39 static int
40 split(char *l, char **sp, int m)
41 {
42   int i;
43   for (i = 0; i < m;)
44     {
45       while (*l == ' ')
46         l++;
47       if (!*l)
48         break;
49       sp[i++] = l;
50       while (*l && *l != ' ')
51         l++;
52       if (!*l)
53         break;
54       *l++ = 0;
55     }
56   return i;
57 }
58 #endif
59
60 /* this join does not depend on parsedata */
61 static char *
62 join2(const char *s1, const char *s2, const char *s3)
63 {
64   int l = 1;
65   char *p;
66
67   if (s1)
68     l += strlen(s1);
69   if (s2)
70     l += strlen(s2);
71   if (s3)
72     l += strlen(s3);
73   if (l > _join_tmpl)
74     {
75       _join_tmpl = l + 256;
76       if (!_join_tmp)
77         _join_tmp = malloc(_join_tmpl);
78       else
79         _join_tmp = realloc(_join_tmp, _join_tmpl);
80     }
81   p = _join_tmp;
82   if (s1)
83     {
84       strcpy(p, s1);
85       p += strlen(s1);
86     }
87   if (s2)
88     {
89       strcpy(p, s2);
90       p += strlen(s2);
91     }
92   if (s3)
93     {
94       strcpy(p, s3);
95       p += strlen(s3);
96     }
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 /* SATSOLVER_TOOLS_UTIL_H */