]> andersk Git - libfaim.git/blame - aim_util.c
- Thu Feb 8 20:12:39 UTC 2001
[libfaim.git] / aim_util.c
CommitLineData
9de3ca7e 1/*
2 *
3 *
4 *
5 */
6
a25832e6 7#include <faim/aim.h>
e6b05d80 8#include <ctype.h>
9de3ca7e 9
f1a5efe0 10#ifdef AIMUTIL_USEMACROS
11/* macros in faim/aim.h */
12#else
78b3fb13 13faim_shortfunc int aimutil_put8(u_char *buf, u_char data)
9de3ca7e 14{
15 buf[0] = (u_char)data&0xff;
16 return 1;
17}
18
78b3fb13 19faim_shortfunc u_char aimutil_get8(u_char *buf)
49c8a2fa 20{
21 return buf[0];
22}
23
9de3ca7e 24/*
25 * Endian-ness issues here?
26 */
78b3fb13 27faim_shortfunc int aimutil_put16(u_char *buf, u_short data)
9de3ca7e 28{
29 buf[0] = (u_char)(data>>8)&0xff;
30 buf[1] = (u_char)(data)&0xff;
31 return 2;
32}
33
78b3fb13 34faim_shortfunc u_short aimutil_get16(u_char *buf)
9de3ca7e 35{
36 u_short val;
37 val = (buf[0] << 8) & 0xff00;
38 val+= (buf[1]) & 0xff;
39 return val;
40}
41
78b3fb13 42faim_shortfunc int aimutil_put32(u_char *buf, u_long data)
9de3ca7e 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;
48 return 4;
49}
50
78b3fb13 51faim_shortfunc u_long aimutil_get32(u_char *buf)
9de3ca7e 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}
f1a5efe0 60#endif /* AIMUTIL_USEMACROS */
9de3ca7e 61
78b3fb13 62faim_export faim_shortfunc int aimutil_putstr(u_char *dest, const char *src, int len)
9de3ca7e 63{
64 memcpy(dest, src, len);
65 return len;
66}
67
68/*
69 * Tokenizing functions. Used to portably replace strtok/sep.
70 * -- DMP.
71 *
72 */
78b3fb13 73faim_export int aimutil_tokslen(char *toSearch, int index, char dl)
9de3ca7e 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
78b3fb13 98faim_export int aimutil_itemcnt(char *toSearch, char dl)
9de3ca7e 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
78b3fb13 116faim_export char *aimutil_itemidx(char *toSearch, int index, char dl)
9de3ca7e 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}
e6b05d80 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 */
78b3fb13 172faim_export int aim_snlen(const char *sn)
e6b05d80 173{
174 int i = 0;
175 const char *curPtr = NULL;
176
177 if (!sn)
178 return 0;
179
180 curPtr = sn;
f1a5efe0 181 while ( (*curPtr) != (char) NULL) {
e6b05d80 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
78b3fb13 203faim_export int aim_sncmp(const char *sn1, const char *sn2)
e6b05d80 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;
f1a5efe0 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++;
e6b05d80 223 }
f1a5efe0 224 }
e6b05d80 225
226 return 0;
227}
a3619f23 228
229/* strsep Copyright (C) 1992, 1993 Free Software Foundation, Inc.
230 strsep is part of the GNU C Library.
231
232 The GNU C Library is free software; you can redistribute it and/or
233 modify it under the terms of the GNU Library General Public License as
234 published by the Free Software Foundation; either version 2 of the
235 License, or (at your option) any later version.
236
237 The GNU C Library is distributed in the hope that it will be useful,
238 but WITHOUT ANY WARRANTY; without even the implied warranty of
239 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
240 Library General Public License for more details.
241
242 You should have received a copy of the GNU Library General Public
243 License along with the GNU C Library; see the file COPYING.LIB. If
244 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
245 Cambridge, MA 02139, USA. */
246
247/* Minor changes by and1000 on 15/1/97 to make it go under Nemesis */
248
78b3fb13 249faim_export char *aim_strsep(char **pp, const char *delim)
a3619f23 250{
251 char *p, *q;
252
253 if (!(p = *pp))
254 return 0;
255
256 if ((q = strpbrk (p, delim)))
257 {
258 *pp = q + 1;
259 *q = '\0';
260 }
261 else
262 *pp = 0;
263
264 return p;
265}
This page took 0.28822 seconds and 5 git commands to generate.