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