tools_util.h 1.6 KB

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