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