xode_str.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * $Id$
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Jabber
  19. * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
  20. */
  21. /*! \file
  22. * \ingroup xmpp
  23. */
  24. #include "xode.h"
  25. xode_pool xode_spool_getpool(const xode_spool s)
  26. {
  27. if(s == NULL)
  28. return NULL;
  29. return s->p;
  30. }
  31. int xode_spool_getlen(const xode_spool s)
  32. {
  33. if(s == NULL)
  34. return 0;
  35. return s->len;
  36. }
  37. void xode_spool_free(xode_spool s)
  38. {
  39. xode_pool_free(xode_spool_getpool(s));
  40. }
  41. xode_spool xode_spool_newfrompool(xode_pool p)
  42. {
  43. xode_spool s;
  44. s = xode_pool_malloc(p, sizeof(struct xode_spool_struct));
  45. s->p = p;
  46. s->len = 0;
  47. s->last = NULL;
  48. s->first = NULL;
  49. return s;
  50. }
  51. xode_spool xode_spool_new(void)
  52. {
  53. return xode_spool_newfrompool(xode_pool_heap(512));
  54. }
  55. void xode_spool_add(xode_spool s, char *str)
  56. {
  57. struct xode_spool_node *sn;
  58. int len;
  59. if(str == NULL)
  60. return;
  61. len = strlen(str);
  62. if(len == 0)
  63. return;
  64. sn = xode_pool_malloc(s->p, sizeof(struct xode_spool_node));
  65. sn->c = xode_pool_strdup(s->p, str);
  66. sn->next = NULL;
  67. s->len += len;
  68. if(s->last != NULL)
  69. s->last->next = sn;
  70. s->last = sn;
  71. if(s->first == NULL)
  72. s->first = sn;
  73. }
  74. void xode_spooler(xode_spool s, ...)
  75. {
  76. va_list ap;
  77. char *arg = NULL;
  78. if(s == NULL)
  79. return;
  80. va_start(ap, s);
  81. /* loop till we hit our end flag, the first arg */
  82. while(1)
  83. {
  84. arg = va_arg(ap,char *);
  85. if((void*)arg == (void*)s || arg == NULL)
  86. break;
  87. else
  88. xode_spool_add(s, arg);
  89. }
  90. va_end(ap);
  91. }
  92. char *xode_spool_tostr(xode_spool s)
  93. {
  94. char *ret,*tmp;
  95. struct xode_spool_node *next;
  96. if(s == NULL || s->len == 0 || s->first == NULL)
  97. return NULL;
  98. ret = xode_pool_malloc(s->p, s->len + 1);
  99. *ret = '\0';
  100. next = s->first;
  101. tmp = ret;
  102. while(next != NULL)
  103. {
  104. tmp = strcat(tmp,next->c);
  105. next = next->next;
  106. }
  107. return ret;
  108. }
  109. /* convenience :) */
  110. char *xode_spool_str(xode_pool p, ...)
  111. {
  112. va_list ap;
  113. xode_spool s;
  114. char *arg = NULL;
  115. if(p == NULL)
  116. return NULL;
  117. s = xode_spool_newfrompool(p);
  118. va_start(ap, p);
  119. /* loop till we hit our end flag, the first arg */
  120. while(1)
  121. {
  122. arg = va_arg(ap,char *);
  123. if((void*)arg == (void*)p)
  124. break;
  125. else
  126. xode_spool_add(s, arg);
  127. }
  128. va_end(ap);
  129. return xode_spool_tostr(s);
  130. }
  131. char *xode_strunescape(xode_pool p, char *buf)
  132. {
  133. int i,j=0;
  134. char *temp;
  135. if (p == NULL || buf == NULL) return(NULL);
  136. if (strchr(buf,'&') == NULL) return(buf);
  137. temp = xode_pool_malloc(p,strlen(buf)+1);
  138. if (temp == NULL) return(NULL);
  139. for(i=0;i<strlen(buf);i++)
  140. {
  141. if (buf[i]=='&')
  142. {
  143. if (strncmp(&buf[i],"&amp;",5)==0)
  144. {
  145. temp[j] = '&';
  146. i += 4;
  147. } else if (strncmp(&buf[i],"&quot;",6)==0) {
  148. temp[j] = '\"';
  149. i += 5;
  150. } else if (strncmp(&buf[i],"&apos;",6)==0) {
  151. temp[j] = '\'';
  152. i += 5;
  153. } else if (strncmp(&buf[i],"&lt;",4)==0) {
  154. temp[j] = '<';
  155. i += 3;
  156. } else if (strncmp(&buf[i],"&gt;",4)==0) {
  157. temp[j] = '>';
  158. i += 3;
  159. }
  160. } else {
  161. temp[j]=buf[i];
  162. }
  163. j++;
  164. }
  165. temp[j]='\0';
  166. return(temp);
  167. }
  168. char *xode_strescape(xode_pool p, char *buf)
  169. {
  170. int i,j,oldlen,newlen;
  171. char *temp;
  172. if (p == NULL || buf == NULL) return(NULL);
  173. oldlen = newlen = strlen(buf);
  174. for(i=0;i<oldlen;i++)
  175. {
  176. switch(buf[i])
  177. {
  178. case '&':
  179. newlen+=5;
  180. break;
  181. case '\'':
  182. newlen+=6;
  183. break;
  184. case '\"':
  185. newlen+=6;
  186. break;
  187. case '<':
  188. newlen+=4;
  189. break;
  190. case '>':
  191. newlen+=4;
  192. break;
  193. }
  194. }
  195. if(oldlen == newlen) return buf;
  196. temp = xode_pool_malloc(p,newlen+1);
  197. if (temp==NULL) return(NULL);
  198. for(i=j=0;i<oldlen;i++)
  199. {
  200. switch(buf[i])
  201. {
  202. case '&':
  203. memcpy(&temp[j],"&amp;",5);
  204. j += 5;
  205. break;
  206. case '\'':
  207. memcpy(&temp[j],"&apos;",6);
  208. j += 6;
  209. break;
  210. case '\"':
  211. memcpy(&temp[j],"&quot;",6);
  212. j += 6;
  213. break;
  214. case '<':
  215. memcpy(&temp[j],"&lt;",4);
  216. j += 4;
  217. break;
  218. case '>':
  219. memcpy(&temp[j],"&gt;",4);
  220. j += 4;
  221. break;
  222. default:
  223. temp[j++] = buf[i];
  224. }
  225. }
  226. temp[j] = '\0';
  227. return temp;
  228. }