comparison libfaim/aim_util.c @ 237:6ced2f1c8b24

[gaim-migrate @ 247] How cool is this, libfaim is making a comeback. I completely redid everything, as was necessary because of the updates to libfaim since gaim 0.9.7. You can sign on and send/recv IMs, but there's a bad lag between display updates that I haven't figured out how to fix yet. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Sat, 20 May 2000 00:30:53 +0000
parents 68b230f8da5f
children 0f14e6d8a51b
comparison
equal deleted inserted replaced
236:62d470738cc7 237:6ced2f1c8b24
2 * 2 *
3 * 3 *
4 * 4 *
5 */ 5 */
6 6
7 #include "aim.h" 7 #include <aim.h>
8 8 #include <ctype.h>
9 int aimutil_put8(u_char *buf, u_short data) 9
10 { 10 #define AIMUTIL_USEMACROS
11 buf[0] = data&0xff; 11
12 #ifdef AIMUTIL_USEMACROS
13 /* macros in aim.h */
14 #else
15 inline int aimutil_put8(u_char *buf, u_char data)
16 {
17 buf[0] = (u_char)data&0xff;
12 return 1; 18 return 1;
13 } 19 }
14 20
21 inline u_char aimutil_get8(u_char *buf)
22 {
23 return buf[0];
24 }
25
15 /* 26 /*
16 * Endian-ness issues here? 27 * Endian-ness issues here?
17 */ 28 */
18 int aimutil_put16(u_char *buf, u_short data) 29 inline int aimutil_put16(u_char *buf, u_short data)
19 { 30 {
20 buf[0] = (data>>8)&0xff; 31 buf[0] = (u_char)(data>>8)&0xff;
21 buf[1] = (data)&0xff; 32 buf[1] = (u_char)(data)&0xff;
22 return 2; 33 return 2;
23 } 34 }
24 35
25 int aimutil_put32(u_char *buf, u_long data) 36 inline u_short aimutil_get16(u_char *buf)
26 { 37 {
27 buf[0] = (data>>24)&0xff; 38 u_short val;
28 buf[1] = (data>>16)&0xff; 39 val = (buf[0] << 8) & 0xff00;
29 buf[2] = (data>>8)&0xff; 40 val+= (buf[1]) & 0xff;
30 buf[3] = (data)&0xff; 41 return val;
42 }
43
44 inline int aimutil_put32(u_char *buf, u_long data)
45 {
46 buf[0] = (u_char)(data>>24)&0xff;
47 buf[1] = (u_char)(data>>16)&0xff;
48 buf[2] = (u_char)(data>>8)&0xff;
49 buf[3] = (u_char)(data)&0xff;
31 return 4; 50 return 4;
32 } 51 }
33 52
34 int aimutil_putstr(u_char *dest, u_char *src, int len) 53 inline u_long aimutil_get32(u_char *buf)
54 {
55 u_long val;
56 val = (buf[0] << 24) & 0xff000000;
57 val+= (buf[1] << 16) & 0x00ff0000;
58 val+= (buf[2] << 8) & 0x0000ff00;
59 val+= (buf[3] ) & 0x000000ff;
60 return val;
61 }
62 #endif /* AIMUTIL_USEMACROS */
63
64 inline int aimutil_putstr(u_char *dest, const u_char *src, int len)
35 { 65 {
36 memcpy(dest, src, len); 66 memcpy(dest, src, len);
37 return len; 67 return len;
38 } 68 }
69
70 /*
71 * Tokenizing functions. Used to portably replace strtok/sep.
72 * -- DMP.
73 *
74 */
75 int aimutil_tokslen(char *toSearch, int index, char dl)
76 {
77 int curCount = 1;
78 char *next;
79 char *last;
80 int toReturn;
81
82 last = toSearch;
83 next = strchr(toSearch, dl);
84
85 while(curCount < index && next != NULL)
86 {
87 curCount++;
88 last = next + 1;
89 next = strchr(last, dl);
90 }
91
92 if ((curCount < index) || (next == NULL))
93 toReturn = strlen(toSearch) - (curCount - 1);
94 else
95 toReturn = next - toSearch - (curCount - 1);
96
97 return toReturn;
98 }
99
100 int aimutil_itemcnt(char *toSearch, char dl)
101 {
102 int curCount;
103 char *next;
104
105 curCount = 1;
106
107 next = strchr(toSearch, dl);
108
109 while(next != NULL)
110 {
111 curCount++;
112 next = strchr(next + 1, dl);
113 }
114
115 return curCount;
116 }
117
118 char *aimutil_itemidx(char *toSearch, int index, char dl)
119 {
120 int curCount;
121 char *next;
122 char *last;
123 char *toReturn;
124
125 curCount = 0;
126
127 last = toSearch;
128 next = strchr(toSearch, dl);
129
130 while(curCount < index && next != NULL)
131 {
132 curCount++;
133 last = next + 1;
134 next = strchr(last, dl);
135 }
136
137 if (curCount < index)
138 {
139 toReturn = malloc(sizeof(char));
140 *toReturn = '\0';
141 }
142 next = strchr(last, dl);
143
144 if (curCount < index)
145 {
146 toReturn = malloc(sizeof(char));
147 *toReturn = '\0';
148 }
149 else
150 {
151 if (next == NULL)
152 {
153 toReturn = malloc((strlen(last) + 1) * sizeof(char));
154 strcpy(toReturn, last);
155 }
156 else
157 {
158 toReturn = malloc((next - last + 1) * sizeof(char));
159 memcpy(toReturn, last, (next - last));
160 toReturn[next - last] = '\0';
161 }
162 }
163 return toReturn;
164 }
165
166 /*
167 * int snlen(const char *)
168 *
169 * This takes a screen name and returns its length without
170 * spaces. If there are no spaces in the SN, then the
171 * return is equal to that of strlen().
172 *
173 */
174 int aim_snlen(const char *sn)
175 {
176 int i = 0;
177 const char *curPtr = NULL;
178
179 if (!sn)
180 return 0;
181
182 curPtr = sn;
183 while ( (*curPtr) != (char) NULL) {
184 if ((*curPtr) != ' ')
185 i++;
186 curPtr++;
187 }
188
189 return i;
190 }
191
192 /*
193 * int sncmp(const char *, const char *)
194 *
195 * This takes two screen names and compares them using the rules
196 * on screen names for AIM/AOL. Mainly, this means case and space
197 * insensitivity (all case differences and spacing differences are
198 * ignored).
199 *
200 * Return: 0 if equal
201 * non-0 if different
202 *
203 */
204
205 int aim_sncmp(const char *sn1, const char *sn2)
206 {
207 const char *curPtr1 = NULL, *curPtr2 = NULL;
208
209 if (aim_snlen(sn1) != aim_snlen(sn2))
210 return 1;
211
212 curPtr1 = sn1;
213 curPtr2 = sn2;
214 while ( (*curPtr1 != (char) NULL) && (*curPtr2 != (char) NULL) ) {
215 if ( (*curPtr1 == ' ') || (*curPtr2 == ' ') ) {
216 if (*curPtr1 == ' ')
217 curPtr1++;
218 if (*curPtr2 == ' ')
219 curPtr2++;
220 } else {
221 if ( toupper(*curPtr1) != toupper(*curPtr2))
222 return 1;
223 curPtr1++;
224 curPtr2++;
225 }
226 }
227
228 return 0;
229 }