]> andersk Git - libfaim.git/blame - aim_util.c
- Mon Mar 20 05:30:59 UTC 2000
[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
10int aimutil_put8(u_char *buf, u_char data)
11{
12 buf[0] = (u_char)data&0xff;
13 return 1;
14}
15
49c8a2fa 16u_char aimutil_get8(u_char *buf)
17{
18 return buf[0];
19}
20
9de3ca7e 21/*
22 * Endian-ness issues here?
23 */
24int aimutil_put16(u_char *buf, u_short data)
25{
26 buf[0] = (u_char)(data>>8)&0xff;
27 buf[1] = (u_char)(data)&0xff;
28 return 2;
29}
30
31u_short aimutil_get16(u_char *buf)
32{
33 u_short val;
34 val = (buf[0] << 8) & 0xff00;
35 val+= (buf[1]) & 0xff;
36 return val;
37}
38
39int aimutil_put32(u_char *buf, u_long data)
40{
41 buf[0] = (u_char)(data>>24)&0xff;
42 buf[1] = (u_char)(data>>16)&0xff;
43 buf[2] = (u_char)(data>>8)&0xff;
44 buf[3] = (u_char)(data)&0xff;
45 return 4;
46}
47
48u_long aimutil_get32(u_char *buf)
49{
50 u_long val;
51 val = (buf[0] << 24) & 0xff000000;
52 val+= (buf[1] << 16) & 0x00ff0000;
53 val+= (buf[2] << 8) & 0x0000ff00;
54 val+= (buf[3] ) & 0x000000ff;
55 return val;
56}
57
58int aimutil_putstr(u_char *dest, const u_char *src, int len)
59{
60 memcpy(dest, src, len);
61 return len;
62}
63
64/*
65 * Tokenizing functions. Used to portably replace strtok/sep.
66 * -- DMP.
67 *
68 */
69int aimutil_tokslen(char *toSearch, int index, char dl)
70{
71 int curCount = 1;
72 char *next;
73 char *last;
74 int toReturn;
75
76 last = toSearch;
77 next = strchr(toSearch, dl);
78
79 while(curCount < index && next != NULL)
80 {
81 curCount++;
82 last = next + 1;
83 next = strchr(last, dl);
84 }
85
86 if ((curCount < index) || (next == NULL))
87 toReturn = strlen(toSearch) - (curCount - 1);
88 else
89 toReturn = next - toSearch - (curCount - 1);
90
91 return toReturn;
92}
93
94int aimutil_itemcnt(char *toSearch, char dl)
95{
96 int curCount;
97 char *next;
98
99 curCount = 1;
100
101 next = strchr(toSearch, dl);
102
103 while(next != NULL)
104 {
105 curCount++;
106 next = strchr(next + 1, dl);
107 }
108
109 return curCount;
110}
111
112char *aimutil_itemidx(char *toSearch, int index, char dl)
113{
114 int curCount;
115 char *next;
116 char *last;
117 char *toReturn;
118
119 curCount = 0;
120
121 last = toSearch;
122 next = strchr(toSearch, dl);
123
124 while(curCount < index && next != NULL)
125 {
126 curCount++;
127 last = next + 1;
128 next = strchr(last, dl);
129 }
130
131 if (curCount < index)
132 {
133 toReturn = malloc(sizeof(char));
134 *toReturn = '\0';
135 }
136 next = strchr(last, dl);
137
138 if (curCount < index)
139 {
140 toReturn = malloc(sizeof(char));
141 *toReturn = '\0';
142 }
143 else
144 {
145 if (next == NULL)
146 {
147 toReturn = malloc((strlen(last) + 1) * sizeof(char));
148 strcpy(toReturn, last);
149 }
150 else
151 {
152 toReturn = malloc((next - last + 1) * sizeof(char));
153 memcpy(toReturn, last, (next - last));
154 toReturn[next - last] = '\0';
155 }
156 }
157 return toReturn;
158}
e6b05d80 159
160/*
161 * int snlen(const char *)
162 *
163 * This takes a screen name and returns its length without
164 * spaces. If there are no spaces in the SN, then the
165 * return is equal to that of strlen().
166 *
167 */
168int aim_snlen(const char *sn)
169{
170 int i = 0;
171 const char *curPtr = NULL;
172
173 if (!sn)
174 return 0;
175
176 curPtr = sn;
177 while ( (*curPtr) != (char) NULL)
178 {
179 if ((*curPtr) != ' ')
180 i++;
181 curPtr++;
182 }
183
184 return i;
185}
186
187/*
188 * int sncmp(const char *, const char *)
189 *
190 * This takes two screen names and compares them using the rules
191 * on screen names for AIM/AOL. Mainly, this means case and space
192 * insensitivity (all case differences and spacing differences are
193 * ignored).
194 *
195 * Return: 0 if equal
196 * non-0 if different
197 *
198 */
199
200int aim_sncmp(const char *sn1, const char *sn2)
201{
202 const char *curPtr1 = NULL, *curPtr2 = NULL;
203
204 if (aim_snlen(sn1) != aim_snlen(sn2))
205 return 1;
206
207 curPtr1 = sn1;
208 curPtr2 = sn2;
209 while ( (*curPtr1 != (char) NULL) && (*curPtr2 != (char) NULL) )
210 {
211 if ( (*curPtr1 == ' ') || (*curPtr2 == ' ') )
212 {
213 if (*curPtr1 == ' ')
214 curPtr1++;
215 if (*curPtr2 == ' ')
216 curPtr2++;
217 }
218 else
219 {
220 if ( toupper(*curPtr1) != toupper(*curPtr2))
221 return 1;
222 curPtr1++;
223 curPtr2++;
224 }
225 }
226
227 return 0;
228}
This page took 0.08024 seconds and 5 git commands to generate.