]> andersk Git - libfaim.git/blob - aim_util.c
f7cb012021ff01a112e89e143215fc3acd5747e3
[libfaim.git] / aim_util.c
1 /*
2  *
3  *
4  *
5  */
6
7 #include <faim/aim.h>
8
9 int aimutil_put8(u_char *buf, u_char data)
10 {
11   buf[0] = (u_char)data&0xff;
12   return 1;
13 }
14
15 u_char aimutil_get8(u_char *buf)
16 {
17   return buf[0];
18 }
19
20 /*
21  * Endian-ness issues here?
22  */
23 int aimutil_put16(u_char *buf, u_short data)
24 {
25   buf[0] = (u_char)(data>>8)&0xff;
26   buf[1] = (u_char)(data)&0xff;
27   return 2;
28 }
29
30 u_short aimutil_get16(u_char *buf)
31 {
32   u_short val;
33   val = (buf[0] << 8) & 0xff00;
34   val+= (buf[1]) & 0xff;
35   return val;
36 }
37
38 int aimutil_put32(u_char *buf, u_long data)
39 {
40   buf[0] = (u_char)(data>>24)&0xff;
41   buf[1] = (u_char)(data>>16)&0xff;
42   buf[2] = (u_char)(data>>8)&0xff;
43   buf[3] = (u_char)(data)&0xff;
44   return 4;
45 }
46
47 u_long aimutil_get32(u_char *buf)
48 {
49   u_long val;
50   val = (buf[0] << 24) & 0xff000000;
51   val+= (buf[1] << 16) & 0x00ff0000;
52   val+= (buf[2] <<  8) & 0x0000ff00;
53   val+= (buf[3]      ) & 0x000000ff;
54   return val;
55 }
56
57 int aimutil_putstr(u_char *dest, const u_char *src, int len)
58 {
59   memcpy(dest, src, len);
60   return len;
61 }
62
63 /*
64  * Tokenizing functions.  Used to portably replace strtok/sep.
65  *   -- DMP.
66  *
67  */
68 int aimutil_tokslen(char *toSearch, int index, char dl)
69 {
70   int curCount = 1;
71   char *next;
72   char *last;
73   int toReturn;
74
75   last = toSearch;
76   next = strchr(toSearch, dl);
77   
78   while(curCount < index && next != NULL)
79     {
80       curCount++;
81       last = next + 1;
82       next = strchr(last, dl);
83     }
84   
85   if ((curCount < index) || (next == NULL))
86     toReturn = strlen(toSearch) - (curCount - 1);
87   else
88     toReturn = next - toSearch - (curCount - 1);
89
90   return toReturn;
91 }
92
93 int aimutil_itemcnt(char *toSearch, char dl)
94 {
95   int curCount;
96   char *next;
97   
98   curCount = 1;
99   
100   next = strchr(toSearch, dl);
101   
102   while(next != NULL)
103     {
104       curCount++;
105       next = strchr(next + 1, dl);
106     }
107   
108   return curCount;
109 }
110
111 char *aimutil_itemidx(char *toSearch, int index, char dl)
112 {
113   int curCount;
114   char *next;
115   char *last;
116   char *toReturn;
117   
118   curCount = 0;
119   
120   last = toSearch;
121   next = strchr(toSearch, dl);
122   
123   while(curCount < index && next != NULL)
124     {
125       curCount++;
126       last = next + 1;
127       next = strchr(last, dl);
128     }
129   
130   if (curCount < index)
131     {
132       toReturn = malloc(sizeof(char));
133       *toReturn = '\0';
134     }
135   next = strchr(last, dl);
136   
137   if (curCount < index)
138     {
139       toReturn = malloc(sizeof(char));
140       *toReturn = '\0';
141     }
142   else
143     {
144       if (next == NULL)
145         {
146           toReturn = malloc((strlen(last) + 1) * sizeof(char));
147           strcpy(toReturn, last);
148         }
149       else
150         {
151           toReturn = malloc((next - last + 1) * sizeof(char));
152           memcpy(toReturn, last, (next - last));
153           toReturn[next - last] = '\0';
154         }
155     }
156   return toReturn;
157 }
This page took 0.04846 seconds and 3 git commands to generate.