]> andersk Git - libfaim.git/blob - aim_util.c
Try this.
[libfaim.git] / aim_util.c
1 /*
2  *
3  *
4  *
5  */
6
7 #include <faim/aim.h>
8 #include <ctype.h>
9
10 #ifdef AIMUTIL_USEMACROS
11 /* macros in faim/aim.h */
12 #else
13 faim_shortfunc int aimutil_put8(u_char *buf, u_char data)
14 {
15   buf[0] = (u_char)data&0xff;
16   return 1;
17 }
18
19 faim_shortfunc u_char aimutil_get8(u_char *buf)
20 {
21   return buf[0];
22 }
23
24 /*
25  * Endian-ness issues here?
26  */
27 faim_shortfunc int aimutil_put16(u_char *buf, u_short data)
28 {
29   buf[0] = (u_char)(data>>8)&0xff;
30   buf[1] = (u_char)(data)&0xff;
31   return 2;
32 }
33
34 faim_shortfunc u_short aimutil_get16(u_char *buf)
35 {
36   u_short val;
37   val = (buf[0] << 8) & 0xff00;
38   val+= (buf[1]) & 0xff;
39   return val;
40 }
41
42 faim_shortfunc int aimutil_put32(u_char *buf, u_long data)
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
51 faim_shortfunc u_long aimutil_get32(u_char *buf)
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
62 faim_export faim_shortfunc int aimutil_putstr(u_char *dest, const char *src, int len)
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  */
73 faim_export int aimutil_tokslen(char *toSearch, int index, char dl)
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
98 faim_export int aimutil_itemcnt(char *toSearch, char dl)
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
116 faim_export char *aimutil_itemidx(char *toSearch, int index, char dl)
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  */
172 faim_export int aim_snlen(const char *sn)
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
203 faim_export int aim_sncmp(const char *sn1, const char *sn2)
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 }
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
249 faim_export char *aim_strsep(char **pp, const char *delim)
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.717363 seconds and 5 git commands to generate.