FreeCalypso > hg > tcs211-pirelli
comparison gpf/MISC/tools.c @ 0:509db1a7b7b8
initial import: leo2moko-r1
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Mon, 01 Jun 2015 03:24:05 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:509db1a7b7b8 |
---|---|
1 /* | |
2 +------------------------------------------------------------------------------ | |
3 | File: tools.c | |
4 +------------------------------------------------------------------------------ | |
5 | Copyright 2002 Texas Instruments Berlin, AG | |
6 | All rights reserved. | |
7 | | |
8 | This file is confidential and a trade secret of Texas | |
9 | Instruments Berlin, AG | |
10 | The receipt of or possession of this file does not convey | |
11 | any rights to reproduce or disclose its contents or to | |
12 | manufacture, use, or sell anything it may describe, in | |
13 | whole, or in part, without the specific written consent of | |
14 | Texas Instruments Berlin, AG. | |
15 +----------------------------------------------------------------------------- | |
16 | Purpose : This module implements some format conversion functions. | |
17 +----------------------------------------------------------------------------- | |
18 */ | |
19 | |
20 #ifndef __TOOLS_C__ | |
21 #define __TOOLS_C__ | |
22 #endif | |
23 | |
24 /*==== INCLUDES ===================================================*/ | |
25 | |
26 #include "typedefs.h" | |
27 #include <string.h> | |
28 #include <ctype.h> | |
29 #include "vsi.h" | |
30 #include "tools.h" | |
31 | |
32 /*==== TYPES ======================================================*/ | |
33 | |
34 | |
35 /*==== CONSTANTS ==================================================*/ | |
36 | |
37 | |
38 /*==== EXTERNALS ==================================================*/ | |
39 | |
40 | |
41 /*==== VARIABLES ==================================================*/ | |
42 | |
43 | |
44 /*==== FUNCTIONS ==================================================*/ | |
45 | |
46 | |
47 | |
48 #ifndef RUN_INT_RAM | |
49 /* | |
50 +------------------------------------------------------------------------------ | |
51 | Function : GetNextToken | |
52 +------------------------------------------------------------------------------ | |
53 | Description : Exract text until next separator. | |
54 | | |
55 | Parameters : source - where to search | |
56 | token - destination for text | |
57 | seperator - sperator | |
58 | | |
59 | Return : string length of token | |
60 +------------------------------------------------------------------------------ | |
61 */ | |
62 unsigned int GetNextToken (char *source, char *token, char const *seperators) | |
63 { | |
64 unsigned int i, j, k, sep_len, src_len; | |
65 BOOL sepFound = FALSE; | |
66 | |
67 sep_len = strlen (seperators); | |
68 src_len = strlen (source); | |
69 | |
70 i = 0; | |
71 j = 0; | |
72 k = 0; | |
73 | |
74 do | |
75 { | |
76 j = 0; | |
77 sepFound = FALSE; | |
78 | |
79 while (j < sep_len) | |
80 { | |
81 if (source[i] EQ seperators[j]) | |
82 sepFound = TRUE; | |
83 j++; | |
84 } | |
85 if (sepFound) | |
86 i++; | |
87 } | |
88 while (i < src_len AND sepFound); | |
89 | |
90 sepFound = FALSE; | |
91 | |
92 while (!sepFound AND i < src_len) | |
93 { | |
94 j = 0; | |
95 while (!sepFound AND j < sep_len) | |
96 { | |
97 if (source[i] EQ seperators[j]) | |
98 sepFound = TRUE; | |
99 else | |
100 j++; | |
101 } | |
102 | |
103 if (!sepFound) | |
104 token[k++] = source[i++]; | |
105 } | |
106 token[k] = '\0'; | |
107 | |
108 return strlen (token); | |
109 } | |
110 #endif | |
111 | |
112 #ifndef RUN_FLASH | |
113 /* | |
114 +------------------------------------------------------------------------------ | |
115 | Function : HexToASCII | |
116 +------------------------------------------------------------------------------ | |
117 | Description : Convert hexadecimal value to ASCII string. | |
118 | | |
119 | Parameters : value - value to be converted | |
120 | *ptr - destination for string | |
121 | num - number of characters | |
122 | | |
123 | Return : pointer behind the end of string | |
124 +------------------------------------------------------------------------------ | |
125 */ | |
126 char *HexToASCII (ULONG value, char *ptr, int num) | |
127 { | |
128 UBYTE i; | |
129 char v; | |
130 | |
131 ptr += (num-1); | |
132 | |
133 for (i=0; i<num ;i++) | |
134 { | |
135 v = (char)(value & 0x000000f); | |
136 | |
137 value >>= 4; | |
138 | |
139 if (v > 9) | |
140 v += ('A'-10); | |
141 else | |
142 v += '0'; | |
143 | |
144 *ptr-- = v; | |
145 } | |
146 | |
147 return(ptr+num+1); | |
148 } | |
149 #endif | |
150 | |
151 #ifndef RUN_INT_RAM | |
152 /* | |
153 +------------------------------------------------------------------------------ | |
154 | Function : ASCIIToHex | |
155 +------------------------------------------------------------------------------ | |
156 | Description : Convert ASCII string to hexadecimal value. | |
157 | | |
158 | Parameters : *ptr - string to be converted | |
159 | num - number of characters | |
160 | | |
161 | Return : value | |
162 +------------------------------------------------------------------------------ | |
163 */ | |
164 unsigned int ASCIIToHex (char *ptr, int num) | |
165 { | |
166 unsigned int i; | |
167 unsigned int result = 0; | |
168 unsigned int len; | |
169 char c; | |
170 | |
171 len = strlen(ptr); | |
172 if ( len < (unsigned int)num ) | |
173 num = (int)len; | |
174 | |
175 ptr += (num-1); | |
176 | |
177 for (i=0; i < (unsigned int)num; i++) | |
178 { | |
179 if (isxdigit (*ptr) ) | |
180 { | |
181 if (*ptr > '9') | |
182 c = *ptr-- - 'A' + 10; | |
183 else | |
184 c = *ptr-- - '0'; | |
185 | |
186 /*lint -e701 suppress Info -- shift left of signed quantity */ | |
187 result += (c << (i*4)); | |
188 /*lint +e701 */ | |
189 } | |
190 } | |
191 return result; | |
192 } | |
193 #endif | |
194 | |
195 #ifndef RUN_FLASH | |
196 /* | |
197 +------------------------------------------------------------------------------ | |
198 | Function : InsertString | |
199 +------------------------------------------------------------------------------ | |
200 | Description : write a string into the buffer. If the string has less than | |
201 | 'num' characters, spaces are added. | |
202 | | |
203 | Parameters : *string - string to be written | |
204 | *ptr - pointer to destination | |
205 | num - number of characters to be filled | |
206 | | |
207 | Return : pointer to next character | |
208 +------------------------------------------------------------------------------ | |
209 */ | |
210 char *InsertString (char *string, char *ptr, int num) | |
211 { | |
212 UBYTE i; | |
213 BYTE end_detected = FALSE; | |
214 | |
215 for (i=0;i<num;i++) | |
216 { | |
217 if (end_detected) | |
218 *ptr++ = ' '; | |
219 else | |
220 { | |
221 if (string [i] == 0) | |
222 { | |
223 end_detected = TRUE; | |
224 *ptr++ = ' '; | |
225 } | |
226 else | |
227 *ptr++ = string [i]; | |
228 } | |
229 } | |
230 return(ptr); | |
231 } | |
232 #endif | |
233 | |
234 | |
235 #ifndef RUN_INT_RAM | |
236 /* | |
237 +------------------------------------------------------------------------------ | |
238 | Function : rm_path | |
239 +------------------------------------------------------------------------------ | |
240 | Description : remove path name from file name | |
241 | | |
242 | Parameters : *file - file name | |
243 | | |
244 | Return : file name without path information | |
245 | | |
246 +------------------------------------------------------------------------------ | |
247 */ | |
248 char *rm_path ( const char *file ) | |
249 { | |
250 char *end; | |
251 | |
252 /* | |
253 * to avoid crashes in case the passed parameter file is a pointer to | |
254 * anywhere it is checked if file begins with a character that is allowed | |
255 * at the beginning of file/path names (any alpanumeric character, '.', '/', | |
256 * and '\') | |
257 */ | |
258 if ( isalpha(*file) == 0 && *file != '.' && *file != '/' && *file != '\\' ) | |
259 return ((char*)"NO VALID FILENAME"); | |
260 | |
261 end = (char*)file + strlen(file); | |
262 | |
263 while ((*end != 0x2f) && (*end != 0x5c) && (end >= (char*)file)) | |
264 end--; | |
265 | |
266 return end + 1; | |
267 } | |
268 #endif |