Aktuální adresář: FITkit /
trunk /
mcu /
libs /
enc28j60 /
enc28j60_string.c
1 /*******************************************************************************
2
3 Copyright (C) 2010 Brno University of Technology,
4 Faculty of Information Technology
5 Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
6
7 LICENSE TERMS
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12 1. Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in
16 the documentation and/or other materials provided with the
17 distribution.
18 3. All advertising materials mentioning features or use of this software
19 or firmware must display the following acknowledgement:
20
21 This product includes software developed by the University of
22 Technology, Faculty of Information Technology, Brno and its
23 contributors.
24
25 4. Neither the name of the Company nor the names of its contributors
26 may be used to endorse or promote products derived from this
27 software without specific prior written permission.
28
29 This software or firmware is provided ``as is'', and any express or implied
30 warranties, including, but not limited to, the implied warranties of
31 merchantability and fitness for a particular purpose are disclaimed.
32 In no event shall the company or contributors be liable for any
33 direct, indirect, incidental, special, exemplary, or consequential
34 damages (including, but not limited to, procurement of substitute
35 goods or services; loss of use, data, or profits; or business
36 interruption) however caused and on any theory of liability, whether
37 in contract, strict liability, or tort (including negligence or
38 otherwise) arising in any way out of the use of this software, even
39 if advised of the possibility of such damage.
40
41 $Id$
42
43 *******************************************************************************/
44 #include "enc28j60_string.h"
45
46 /**
47
48 **/
49 inline void* enc_memcpy(void* dst, void* src, unsigned int count){
50 char* dst_tmp = (char*) dst;
51 char* src_tmp = (char*) src;
52
53 while(count--)
54 *dst_tmp++ = *src_tmp++;
55
56 return dst;
57 }
58
59 /**
60
61 **/
62 inline unsigned int enc_memcmp(void* str1, void* str2,unsigned int count){
63 char* str1_tmp = (char*) str1;
64 char* str2_tmp = (char*) str2;
65
66 while(count--){
67 if(*str1_tmp++ != *str2_tmp++)
68 return 1;
69 }
70
71 return 0;
72 }
73
74 /**
75
76 **/
77 inline void* enc_memset(void* ptr, char value, unsigned int count){
78 char* ptr_tmp = (char*) ptr;
79
80 while(count--)
81 *ptr_tmp++ = value;
82
83 return ptr;
84 }
85
86 /**
87
88 **/
89 inline unsigned int enc_strlen(void* ptr){
90 unsigned int len = 0;
91 char* ptr_tmp = (char*) ptr;
92
93 while((*ptr_tmp) != 0){
94 len++;
95 ptr_tmp++;
96 }
97 return len;
98 }
99
100 /**
101
102
103 **/
104 unsigned char str_to_ip_port(char* ptr, char max_len, unsigned long* ip, unsigned int* port){
105
106
107 while(( (! is_digit(*ptr))) & (max_len > 0)){
108 max_len--;
109 ptr++;
110
111 }
112
113 char part = 1;
114 unsigned int temp = 0;
115 unsigned long temp_port = 0;
116
117 while(max_len > 0){
118
119 switch(part){
120 case 1:
121 case 2:
122 case 3:
123
124 temp *= 10;
125 temp += (*ptr) - 48;
126
127 }
128 else{
129
130
131 part++;
132 temp = 0;
133 }
134 else{
135 return 0;
136 }
137 }
138 break;
139
140 case 4:
141
142 temp *= 10;
143 temp += (*ptr) - 48;
144 }
145 else{
146
147
148 part++;
149 temp = 0;
150 }
151 else{
152
153 *port = 0;
154 ((char*)ip)[4-part] = (char)temp;
155 return 1;
156 }
157 else
158 return 0;
159 }
160 }
161 break;
162 //port
163 case 5:
164
165 temp_port *= 10;
166 temp_port += (*ptr) - 48;
167 }
168 else{
169
170 *port = temp_port;
171 return 1;
172 }
173 else{
174 return 0;
175 }
176 }
177
178 }
179
180 //!!!!
181 ptr++;
182 max_len--;
183
184 }
185
186 //dosazen konec retezce
187 switch(part){
188 case 1:
189 case 2:
190 case 3:
191
192 case 4:
193 if(temp <= 255){
194
195 *port = 0;
196 return 1;
197 }
198 else
199 return 0;
200
201 case 5:
202
203 *port = temp_port;
204 return 1;
205 }
206 else{
207 *port = 0;
208 return 1;
209 }
210 }
211
212 return 0;
213 }
214
215 /**
216
217 **/
218 unsigned char ip_to_str(char * ptr, char max_len, unsigned long ip){
219
220 unsigned char tmp[3];
221 char counter = max_len;
222 unsigned char segment;
223
224 int i = 3;
225 while( i >= 0){
226
227 segment = ((char*)&ip)[i];
228
229 tmp[0] = (segment / 100) + 48;
230 segment %= 100;
231 tmp[1] = (segment / 10) + 48;
232 segment %= 10;
233 tmp[2] = segment + 48;
234
235 int k = 0;
236 char print = 0;
237 while(k < 3){
238
239 if(tmp[k] > '0'){ //pocatecni nuly nezapisujem
240 print = 1;
241 if(counter > 0){
242 counter--;
243 *(ptr) = tmp[k];
244 ptr++;
245 }
246 }
247 else if(print == 1){ //nuly uprostred cisla samozrejme zapisujem
248 if(counter > 0){
249 counter--;
250 *(ptr) = tmp[k];
251 ptr++;
252 }
253 }
254
255 k++;
256 }
257
258 if(print == 0){ //byly same nuly, musime zapsat alespon jednu
259 if(counter > 0){
260 counter--;
261 *(ptr) = '0';
262 ptr++;
263 }
264 }
265
266 if((i != 0) & ( counter > 0)){ //prvni 3 segmenty IP konci teckou,ctvrty ne
267 counter--;
268 *(ptr) = '.';
269 ptr++;
270 }
271
272 i--;
273 print = 0;
274
275 }
276 //ukonceni retezce
277 if(counter == 0){
278 ptr--;
279 *(ptr) = 0;
280 }
281 else
282 *(ptr) = 0;
283
284 return max_len - counter;
285 }
286