source: protocols/jabber/str.c @ 728a981

Last change on this file since 728a981 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/* --------------------------------------------------------------------------
2 *
3 * License
4 *
5 * The contents of this file are subject to the Jabber Open Source License
6 * Version 1.0 (the "JOSL").  You may not copy or use this file, in either
7 * source code or executable form, except in compliance with the JOSL. You
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
9 * http://www.opensource.org/. 
10 *
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the JOSL
13 * for the specific language governing rights and limitations under the
14 * JOSL.
15 *
16 * Copyrights
17 *
18 * Portions created by or assigned to Jabber.com, Inc. are
19 * Copyright (c) 1999-2002 Jabber.com, Inc.  All Rights Reserved.  Contact
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
21 *
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
23 *
24 * Acknowledgements
25 *
26 * Special thanks to the Jabber Open Source Contributors for their
27 * suggestions and support of Jabber.
28 *
29 * Alternatively, the contents of this file may be used under the terms of the
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
31 * the provisions of the GPL are applicable instead of those above.  If you
32 * wish to allow use of your version of this file only under the terms of the
33 * GPL and not to allow others to use your version of this file under the JOSL,
34 * indicate your decision by deleting the provisions above and replace them
35 * with the notice and other provisions required by the GPL.  If you do not
36 * delete the provisions above, a recipient may use your version of this file
37 * under either the JOSL or the GPL.
38 *
39 *
40 * --------------------------------------------------------------------------*/
41
42#include "jabber.h"
43#include <glib.h>
44
45static char *j_strcat(char *dest, char *txt)
46{
47    if(!txt) return(dest);
48
49    while(*txt)
50        *dest++ = *txt++;
51    *dest = '\0';
52
53    return(dest);
54}
55
56int j_strcmp(const char *a, const char *b)
57{
58    if(a == NULL || b == NULL)
59        return -1;
60
61    while(*a == *b && *a != '\0' && *b != '\0'){ a++; b++; }
62
63    if(*a == *b) return 0;
64
65    return -1;
66}
67
68spool spool_new(pool p)
69{
70    spool s;
71
72    s = pmalloc(p, sizeof(struct spool_struct));
73    s->p = p;
74    s->len = 0;
75    s->last = NULL;
76    s->first = NULL;
77    return s;
78}
79
80void spool_add(spool s, char *str)
81{
82    struct spool_node *sn;
83    int len;
84
85    if(str == NULL)
86        return;
87
88    len = strlen(str);
89    if(len == 0)
90        return;
91
92    sn = pmalloc(s->p, sizeof(struct spool_node));
93    sn->c = pstrdup(s->p, str);
94    sn->next = NULL;
95
96    s->len += len;
97    if(s->last != NULL)
98        s->last->next = sn;
99    s->last = sn;
100    if(s->first == NULL)
101        s->first = sn;
102}
103
104void spooler(spool s, ...)
105{
106    va_list ap;
107    char *arg = NULL;
108
109    if(s == NULL)
110        return;
111
112    VA_START(s);
113
114    /* loop till we hfit our end flag, the first arg */
115    while(1)
116    {
117        arg = va_arg(ap,char *);
118        if((spool)arg == s)
119            break;
120        else
121            spool_add(s, arg);
122    }
123
124    va_end(ap);
125}
126
127char *spool_print(spool s)
128{
129    char *ret,*tmp;
130    struct spool_node *next;
131
132    if(s == NULL || s->len == 0 || s->first == NULL)
133        return NULL;
134
135    ret = pmalloc(s->p, s->len + 1);
136    *ret = '\0';
137
138    next = s->first;
139    tmp = ret;
140    while(next != NULL)
141    {
142        tmp = j_strcat(tmp,next->c);
143        next = next->next;
144    }
145
146    return ret;
147}
148
149char *strescape(pool p, char *buf)
150{
151    int i,j,oldlen,newlen;
152    char *temp;
153
154    if (p == NULL || buf == NULL) return(NULL);
155
156    oldlen = newlen = strlen(buf);
157    for(i=0;i<oldlen;i++)
158    {
159        switch(buf[i])
160        {
161        case '&':
162            newlen+=5;
163            break;
164        case '\'':
165            newlen+=6;
166            break;
167        case '\"':
168            newlen+=6;
169            break;
170        case '<':
171            newlen+=4;
172            break;
173        case '>':
174            newlen+=4;
175            break;
176        }
177    }
178
179    if(oldlen == newlen) return buf;
180
181    temp = pmalloc(p,newlen+1);
182
183    if (temp==NULL) return(NULL);
184
185    for(i=j=0;i<oldlen;i++)
186    {
187        switch(buf[i])
188        {
189        case '&':
190            memcpy(&temp[j],"&amp;",5);
191            j += 5;
192            break;
193        case '\'':
194            memcpy(&temp[j],"&apos;",6);
195            j += 6;
196            break;
197        case '\"':
198            memcpy(&temp[j],"&quot;",6);
199            j += 6;
200            break;
201        case '<':
202            memcpy(&temp[j],"&lt;",4);
203            j += 4;
204            break;
205        case '>':
206            memcpy(&temp[j],"&gt;",4);
207            j += 4;
208            break;
209        default:
210            temp[j++] = buf[i];
211        }
212    }
213    temp[j] = '\0';
214    return temp;
215}
Note: See TracBrowser for help on using the repository browser.