]> andersk Git - libfaim.git/blob - aim_util.c
- Tue May 30 22:32:31 UTC 2000
[libfaim.git] / aim_util.c
1 /*
2  *
3  *
4  *
5  */
6
7 #include <faim/aim.h>
8 #include <ctype.h>
9
10 #define AIMUTIL_USEMACROS
11
12 #ifdef AIMUTIL_USEMACROS
13 /* macros in faim/aim.h */
14 #else
15 inline int aimutil_put8(u_char *buf, u_char data)
16 {
17   buf[0] = (u_char)data&0xff;
18   return 1;
19 }
20
21 inline u_char aimutil_get8(u_char *buf)
22 {
23   return buf[0];
24 }
25
26 /*
27  * Endian-ness issues here?
28  */
29 inline int aimutil_put16(u_char *buf, u_short data)
30 {
31   buf[0] = (u_char)(data>>8)&0xff;
32   buf[1] = (u_char)(data)&0xff;
33   return 2;
34 }
35
36 inline u_short aimutil_get16(u_char *buf)
37 {
38   u_short val;
39   val = (buf[0] << 8) & 0xff00;
40   val+= (buf[1]) & 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;
50   return 4;
51 }
52
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)
65 {
66   memcpy(dest, src, len);
67   return len;
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 }
This page took 0.186221 seconds and 5 git commands to generate.