comparison fluid-mnf/misc.c @ 311:9cecc930d78f

fluid-mnf: original source from TI, defenestrated line endings and rearranged directory structure, but no *.[ch] source file content changes yet
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 29 Feb 2020 05:36:07 +0000
parents
children 1cd24530c0ae
comparison
equal deleted inserted replaced
310:ae39d76d5b7a 311:9cecc930d78f
1 /******************************************************************************
2 * FLUID (Flash Loader Utility Independent of Device)
3 *
4 * Copyright Texas Instruments, 2001.
5 * Mads Meisner-Jensen, mmj@ti.com.
6 *
7 * Miscellaneous Utility Functions
8 *
9 * $Id: misc.c 1.14 Fri, 11 Oct 2002 08:40:21 +0200 mmj $
10 *
11 ******************************************************************************/
12
13 #include "misc.h"
14 #include "fluid.h"
15 #include "trace.h"
16
17 #include <stdio.h>
18 #include <time.h>
19
20 #if defined(MSC) || defined(BCC)
21 #include "windows.h"
22 #endif
23
24
25 /******************************************************************************
26 * File And Path Name Functions
27 ******************************************************************************/
28
29 // Return the length of the directory name preceding the leafname. The
30 // terminating slash is included in the length. Length can be zero, if there
31 // is no dirname.
32 int dirname_len(const char *pathname)
33 {
34 char *p;
35 int len;
36
37 tr(TrUtility, "dirname_len('%s')\n", pathname);
38
39 if ((len = strlen(pathname)) == 0)
40 return 0;
41
42 p = (char *) (pathname + len - 1);
43
44 tr(TrUtility, "{ %d }\n", len);
45
46 while (*p != '/' && *p != '\\' && len-- > 1)
47 p--;
48
49 return len;
50 }
51
52 // Construct a full pathname from <dirname> and <leafname>. The full
53 // pathname is copied in the buffer <buf> of size <size>. On success, the
54 // full length of the pathname is returned. Otherwise, if the buffer is too
55 // small, -1 is returned. NOTE: The <dirname> is supposed to end with a
56 // slash. If not, the leafname part (if present) of the <dirname> will be
57 // removed.
58 int pathname_make(char *buf, int size, const char *dirname, const char *leafname)
59 {
60 int dir_len, leaf_len, result;
61
62 tr(TrBegin|TrUtility, "pathname_make(*, %d, '%s', '%s') {\n",
63 size, dirname, leafname);
64
65 dir_len = dirname_len(dirname);
66 leaf_len = strlen(leafname);
67
68 if (dir_len > size - leaf_len - 1)
69 result = E_BUFFER;
70 else {
71 memcpy(buf, dirname, dir_len);
72 strcpy(buf + dir_len, leafname);
73 result = dir_len + leaf_len;
74 }
75
76 tr(TrEnd|TrUtility, "} %d\n", result);
77
78 return result;
79 }
80
81
82 /******************************************************************************
83 * Utility Funcions
84 ******************************************************************************/
85
86 // Return the base 2 logarithm of <number>. Return -1 if <number> is zero.
87 unsigned int log2(unsigned int number)
88 {
89 int result = -1;
90
91 while (number > 0) {
92 number >>= 1;
93 result++;
94 }
95 return result;
96 }
97
98
99 /******************************************************************************
100 * Progress and Timer Functions
101 ******************************************************************************/
102
103 static int progress_mul;
104 static int progress_index;
105 static char progress_string[35] = "(---------------------------------)";
106 static char progress_backup[33] =
107 "\b\b\b\b\b\b\b\b" "\b\b\b\b\b\b\b\b" "\b\b\b\b\b\b\b\b" "\b\b\b\b\b\b\b\b";
108
109 void progress_begin(int n)
110 {
111 tr(TrUtility, "progress_begin(%d)\n", n);
112
113 progress_index = 0;
114
115 switch (arg_progress) {
116 case 'a':
117 // We select a progress multiplier that is a power of two. Then we
118 // generate a progress indicator that is in the range [16..32[ chars
119 // in length.
120 progress_mul = n / 32;
121 progress_mul = 1 << (log2(progress_mul) + 1);
122 n = (n + progress_mul - 1) / progress_mul;
123
124 progress_string[1 + n] = ')';
125 progress_string[1 + n + 1] = 0;
126
127 flowf(NORMAL, "%s\b%s",
128 progress_string, &progress_backup[32 - n]);
129 break;
130 case 'c':
131 break;
132 case 'd':
133 break;
134 }
135 }
136
137 void progress_update_simple(int n)
138 {
139 tr(TrUtility, "progress_update_simple(%d)\n", n);
140
141 switch (arg_progress) {
142 case 'c':
143 flowf(NORMAL, "%c", n);
144 break;
145 }
146 }
147
148 void progress_update(int n)
149 {
150 char ch;
151
152 tr(TrUtility, "progress_update(%d)\n", n);
153
154 switch (arg_progress) {
155 case 'a':
156 if (n / progress_mul > progress_index) {
157 progress_index++;
158 flowf(NORMAL, "*");
159 }
160
161 n = n % progress_mul;
162 ch = n + (n <= 9 ? '0' : 'A' - 10);
163 flowf(NORMAL, "%c\b", ch);
164 break;
165 case 'c':
166 break;
167 case 'd':
168 flowf(NORMAL, ".");
169 break;
170 }
171 }
172
173 void progress_end(int n)
174 {
175 switch (arg_progress) {
176 case 'a':
177 if (n % progress_mul)
178 flowf(NORMAL, "*) ");
179 else
180 flowf(NORMAL, ") ");
181 break;
182 case 'c':
183 case 'd':
184 flowf(NORMAL, " ");
185 break;
186 }
187 }
188
189 int stopwatch_start(void)
190 {
191 #if defined(MSC) || defined(BCC)
192 return GetTickCount();
193 #else
194 return 1000 * time(NULL);
195 #endif
196 }
197
198 int stopwatch_stop(int time_start)
199 {
200 #if defined(MSC) || defined(BCC)
201 return GetTickCount() - time_start;
202 #else
203 return 1000 * (time(NULL) - time_start);
204 #endif
205 }
206
207
208 /******************************************************************************
209 * Hexdumping
210 ******************************************************************************/
211
212 void hexdump(unsigned char *buf, int size, unsigned int addr, int unitsize)
213 {
214 int n, i;
215 char string[(8+1+1) + (1+16+1+1) + (3*16) + 1];
216 char *s;
217
218 while (size > 0)
219 {
220 s = string;
221 s += sprintf(s, "%8x ", addr); // print offset
222
223 n = (size > 16 ? 16 : size);
224
225 // print the textual representation
226 for (i = 0; i < n; i++) {
227 if (buf[i] >= ' ' && buf[i] < 127)
228 *s++ = buf[i];
229 else
230 *s++ = '.';
231 }
232 // pad textual representation with spaces
233 for (i = 0; i < 16 - n; i++) {
234 *s++ = ' ';
235 }
236 *s++ = ' ';
237
238 // print hexedecimal representation
239 for (i = 0; i < n; i += unitsize) {
240 switch (unitsize) {
241 case 1: s += sprintf(s, "%02x ", *(uint8 *) (buf+i)); break;
242 case 2: s += sprintf(s, "%04x ", *(uint16 *) (buf+i)); break;
243 case 4:
244 s += sprintf(s, "%08x ", (int) (*(uint32 *) (buf+i)));
245 break;
246 }
247 }
248 buf += 16;
249 addr += 16;
250 size -= 16;
251 puts(string);
252 }
253 }