Mercurial > pidgin
annotate libfaim/aim_util.c @ 1321:413121dcf7e9
[gaim-migrate @ 1331]
Heheh
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Wed, 20 Dec 2000 04:03:47 +0000 |
parents | 595ac7759563 |
children |
rev | line source |
---|---|
2 | 1 /* |
2 * | |
3 * | |
4 * | |
5 */ | |
6 | |
283
0f14e6d8a51b
[gaim-migrate @ 293]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
237
diff
changeset
|
7 #include <faim/aim.h> |
237 | 8 #include <ctype.h> |
9 | |
10 #ifdef AIMUTIL_USEMACROS | |
283
0f14e6d8a51b
[gaim-migrate @ 293]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
237
diff
changeset
|
11 /* macros in faim/aim.h */ |
237 | 12 #else |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
13 faim_shortfunc int aimutil_put8(u_char *buf, u_char data) |
2 | 14 { |
237 | 15 buf[0] = (u_char)data&0xff; |
2 | 16 return 1; |
17 } | |
18 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
19 faim_shortfunc u_char aimutil_get8(u_char *buf) |
237 | 20 { |
21 return buf[0]; | |
22 } | |
23 | |
2 | 24 /* |
25 * Endian-ness issues here? | |
26 */ | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
27 faim_shortfunc int aimutil_put16(u_char *buf, u_short data) |
2 | 28 { |
237 | 29 buf[0] = (u_char)(data>>8)&0xff; |
30 buf[1] = (u_char)(data)&0xff; | |
2 | 31 return 2; |
32 } | |
33 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
34 faim_shortfunc u_short aimutil_get16(u_char *buf) |
2 | 35 { |
237 | 36 u_short val; |
37 val = (buf[0] << 8) & 0xff00; | |
38 val+= (buf[1]) & 0xff; | |
39 return val; | |
40 } | |
41 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
42 faim_shortfunc int aimutil_put32(u_char *buf, u_long data) |
237 | 43 { |
44 buf[0] = (u_char)(data>>24)&0xff; | |
45 buf[1] = (u_char)(data>>16)&0xff; | |
46 buf[2] = (u_char)(data>>8)&0xff; | |
47 buf[3] = (u_char)(data)&0xff; | |
2 | 48 return 4; |
49 } | |
50 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
51 faim_shortfunc u_long aimutil_get32(u_char *buf) |
237 | 52 { |
53 u_long val; | |
54 val = (buf[0] << 24) & 0xff000000; | |
55 val+= (buf[1] << 16) & 0x00ff0000; | |
56 val+= (buf[2] << 8) & 0x0000ff00; | |
57 val+= (buf[3] ) & 0x000000ff; | |
58 return val; | |
59 } | |
60 #endif /* AIMUTIL_USEMACROS */ | |
61 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
62 faim_export faim_shortfunc int aimutil_putstr(u_char *dest, const char *src, int len) |
2 | 63 { |
64 memcpy(dest, src, len); | |
65 return len; | |
66 } | |
237 | 67 |
68 /* | |
69 * Tokenizing functions. Used to portably replace strtok/sep. | |
70 * -- DMP. | |
71 * | |
72 */ | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
73 faim_export int aimutil_tokslen(char *toSearch, int index, char dl) |
237 | 74 { |
75 int curCount = 1; | |
76 char *next; | |
77 char *last; | |
78 int toReturn; | |
79 | |
80 last = toSearch; | |
81 next = strchr(toSearch, dl); | |
82 | |
83 while(curCount < index && next != NULL) | |
84 { | |
85 curCount++; | |
86 last = next + 1; | |
87 next = strchr(last, dl); | |
88 } | |
89 | |
90 if ((curCount < index) || (next == NULL)) | |
91 toReturn = strlen(toSearch) - (curCount - 1); | |
92 else | |
93 toReturn = next - toSearch - (curCount - 1); | |
94 | |
95 return toReturn; | |
96 } | |
97 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
98 faim_export int aimutil_itemcnt(char *toSearch, char dl) |
237 | 99 { |
100 int curCount; | |
101 char *next; | |
102 | |
103 curCount = 1; | |
104 | |
105 next = strchr(toSearch, dl); | |
106 | |
107 while(next != NULL) | |
108 { | |
109 curCount++; | |
110 next = strchr(next + 1, dl); | |
111 } | |
112 | |
113 return curCount; | |
114 } | |
115 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
116 faim_export char *aimutil_itemidx(char *toSearch, int index, char dl) |
237 | 117 { |
118 int curCount; | |
119 char *next; | |
120 char *last; | |
121 char *toReturn; | |
122 | |
123 curCount = 0; | |
124 | |
125 last = toSearch; | |
126 next = strchr(toSearch, dl); | |
127 | |
128 while(curCount < index && next != NULL) | |
129 { | |
130 curCount++; | |
131 last = next + 1; | |
132 next = strchr(last, dl); | |
133 } | |
134 | |
135 if (curCount < index) | |
136 { | |
137 toReturn = malloc(sizeof(char)); | |
138 *toReturn = '\0'; | |
139 } | |
140 next = strchr(last, dl); | |
141 | |
142 if (curCount < index) | |
143 { | |
144 toReturn = malloc(sizeof(char)); | |
145 *toReturn = '\0'; | |
146 } | |
147 else | |
148 { | |
149 if (next == NULL) | |
150 { | |
151 toReturn = malloc((strlen(last) + 1) * sizeof(char)); | |
152 strcpy(toReturn, last); | |
153 } | |
154 else | |
155 { | |
156 toReturn = malloc((next - last + 1) * sizeof(char)); | |
157 memcpy(toReturn, last, (next - last)); | |
158 toReturn[next - last] = '\0'; | |
159 } | |
160 } | |
161 return toReturn; | |
162 } | |
163 | |
164 /* | |
165 * int snlen(const char *) | |
166 * | |
167 * This takes a screen name and returns its length without | |
168 * spaces. If there are no spaces in the SN, then the | |
169 * return is equal to that of strlen(). | |
170 * | |
171 */ | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
172 faim_export int aim_snlen(const char *sn) |
237 | 173 { |
174 int i = 0; | |
175 const char *curPtr = NULL; | |
176 | |
177 if (!sn) | |
178 return 0; | |
179 | |
180 curPtr = sn; | |
181 while ( (*curPtr) != (char) NULL) { | |
182 if ((*curPtr) != ' ') | |
183 i++; | |
184 curPtr++; | |
185 } | |
186 | |
187 return i; | |
188 } | |
189 | |
190 /* | |
191 * int sncmp(const char *, const char *) | |
192 * | |
193 * This takes two screen names and compares them using the rules | |
194 * on screen names for AIM/AOL. Mainly, this means case and space | |
195 * insensitivity (all case differences and spacing differences are | |
196 * ignored). | |
197 * | |
198 * Return: 0 if equal | |
199 * non-0 if different | |
200 * | |
201 */ | |
202 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
203 faim_export int aim_sncmp(const char *sn1, const char *sn2) |
237 | 204 { |
205 const char *curPtr1 = NULL, *curPtr2 = NULL; | |
206 | |
207 if (aim_snlen(sn1) != aim_snlen(sn2)) | |
208 return 1; | |
209 | |
210 curPtr1 = sn1; | |
211 curPtr2 = sn2; | |
212 while ( (*curPtr1 != (char) NULL) && (*curPtr2 != (char) NULL) ) { | |
213 if ( (*curPtr1 == ' ') || (*curPtr2 == ' ') ) { | |
214 if (*curPtr1 == ' ') | |
215 curPtr1++; | |
216 if (*curPtr2 == ' ') | |
217 curPtr2++; | |
218 } else { | |
219 if ( toupper(*curPtr1) != toupper(*curPtr2)) | |
220 return 1; | |
221 curPtr1++; | |
222 curPtr2++; | |
223 } | |
224 } | |
225 | |
226 return 0; | |
227 } | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
228 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
229 /* strsep Copyright (C) 1992, 1993 Free Software Foundation, Inc. |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
230 strsep is part of the GNU C Library. |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
231 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
232 The GNU C Library is free software; you can redistribute it and/or |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
233 modify it under the terms of the GNU Library General Public License as |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
234 published by the Free Software Foundation; either version 2 of the |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
235 License, or (at your option) any later version. |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
236 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
237 The GNU C Library is distributed in the hope that it will be useful, |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
238 but WITHOUT ANY WARRANTY; without even the implied warranty of |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
239 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
240 Library General Public License for more details. |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
241 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
242 You should have received a copy of the GNU Library General Public |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
243 License along with the GNU C Library; see the file COPYING.LIB. If |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
244 not, write to the Free Software Foundation, Inc., 675 Mass Ave, |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
245 Cambridge, MA 02139, USA. */ |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
246 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
247 /* Minor changes by and1000 on 15/1/97 to make it go under Nemesis */ |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
248 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
249 faim_export char *aim_strsep(char **pp, const char *delim) |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
250 { |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
251 char *p, *q; |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
252 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
253 if (!(p = *pp)) |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
254 return 0; |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
255 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
256 if ((q = strpbrk (p, delim))) |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
257 { |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
258 *pp = q + 1; |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
259 *q = '\0'; |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
260 } |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
261 else |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
262 *pp = 0; |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
263 |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
264 return p; |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
265 } |