OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [dskchk2/] [dskchk2/] [iolib.c] - Blame information for rev 260

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 hellwig
/*
2
 * iolib.c -- I/O library
3
 */
4
 
5
 
6
#include "types.h"
7
#include "stdarg.h"
8
#include "iolib.h"
9
#include "biolib.h"
10
 
11
 
12
/**************************************************************/
13
 
14
/* string functions */
15
 
16
 
17
int strlen(char *str) {
18
  int i;
19
 
20
  i = 0;
21
  while (*str++ != '\0') {
22
    i++;
23
  }
24
  return i;
25
}
26
 
27
 
28
void strcpy(char *dst, char *src) {
29
  while ((*dst++ = *src++) != '\0') ;
30
}
31
 
32
 
33
void memcpy(unsigned char *dst, unsigned char *src, unsigned int cnt) {
34
  while (cnt--) {
35
    *dst++ = *src++;
36
  }
37
}
38
 
39
 
40
/**************************************************************/
41
 
42
/* terminal I/O */
43
 
44
 
45
char getchar(void) {
46
  return getc();
47
}
48
 
49
 
50
void putchar(char c) {
51
  if (c == '\n') {
52
    putchar('\r');
53
  }
54
  putc(c);
55
}
56
 
57
 
58
void putString(char *s) {
59
  while (*s != '\0') {
60
    putchar(*s++);
61
  }
62
}
63
 
64
 
65
/**************************************************************/
66
 
67
/* get a line from the terminal */
68
 
69
 
70
void getLine(char *prompt, char *line, int max) {
71
  int index;
72
  char c;
73
 
74
  putString(prompt);
75
  putString(line);
76
  index = strlen(line);
77
  while (1) {
78
    c = getchar();
79
    switch (c) {
80
      case '\r':
81
        putchar('\n');
82
        line[index] = '\0';
83
        return;
84
      case '\b':
85
      case 0x7F:
86
        if (index == 0) {
87
          break;
88
        }
89
        putchar('\b');
90
        putchar(' ');
91
        putchar('\b');
92
        index--;
93
        break;
94
      default:
95
        if (c == '\t') {
96
          c = ' ';
97
        }
98
        if (c < 0x20 || c > 0x7E) {
99
          break;
100
        }
101
        putchar(c);
102
        line[index++] = c;
103
        break;
104
    }
105
  }
106
}
107
 
108
 
109
/**************************************************************/
110
 
111
/* scaled-down version of printf */
112
 
113
 
114
/*
115
 * Count the number of characters needed to represent
116
 * a given number in base 10.
117
 */
118
int countPrintn(long n) {
119
  long a;
120
  int res;
121
 
122
  res = 0;
123
  if (n < 0) {
124
    res++;
125
    n = -n;
126
  }
127
  a = n / 10;
128
  if (a != 0) {
129
    res += countPrintn(a);
130
  }
131
  return res + 1;
132
}
133
 
134
 
135
/*
136
 * Output a number in base 10.
137
 */
138
void printn(long n) {
139
  long a;
140
 
141
  if (n < 0) {
142
    putchar('-');
143
    n = -n;
144
  }
145
  a = n / 10;
146
  if (a != 0) {
147
    printn(a);
148
  }
149
  putchar(n % 10 + '0');
150
}
151
 
152
 
153
/*
154
 * Count the number of characters needed to represent
155
 * a given number in a given base.
156
 */
157
int countPrintu(unsigned long n, unsigned long b) {
158
  unsigned long a;
159
  int res;
160
 
161
  res = 0;
162
  a = n / b;
163
  if (a != 0) {
164
    res += countPrintu(a, b);
165
  }
166
  return res + 1;
167
}
168
 
169
 
170
/*
171
 * Output a number in a given base.
172
 */
173
void printu(unsigned long n, unsigned long b, Bool upperCase) {
174
  unsigned long a;
175
 
176
  a = n / b;
177
  if (a != 0) {
178
    printu(a, b, upperCase);
179
  }
180
  if (upperCase) {
181
    putchar("0123456789ABCDEF"[n % b]);
182
  } else {
183
    putchar("0123456789abcdef"[n % b]);
184
  }
185
}
186
 
187
 
188
/*
189
 * Output a number of filler characters.
190
 */
191
void fill(int numFillers, char filler) {
192
  while (numFillers-- > 0) {
193
    putchar(filler);
194
  }
195
}
196
 
197
 
198
/*
199
 * Formatted output with a variable argument list.
200
 */
201
void vprintf(char *fmt, va_list ap) {
202
  char c;
203
  int n;
204
  long ln;
205
  unsigned int u;
206
  unsigned long lu;
207
  char *s;
208
  Bool negFlag;
209
  char filler;
210
  int width, count;
211
 
212
  while (1) {
213
    while ((c = *fmt++) != '%') {
214
      if (c == '\0') {
215
        return;
216
      }
217
      putchar(c);
218
    }
219
    c = *fmt++;
220
    if (c == '-') {
221
      negFlag = TRUE;
222
      c = *fmt++;
223
    } else {
224
      negFlag = FALSE;
225
    }
226
    if (c == '0') {
227
      filler = '0';
228
      c = *fmt++;
229
    } else {
230
      filler = ' ';
231
    }
232
    width = 0;
233
    while (c >= '0' && c <= '9') {
234
      width *= 10;
235
      width += c - '0';
236
      c = *fmt++;
237
    }
238
    if (c == 'd') {
239
      n = va_arg(ap, int);
240
      count = countPrintn(n);
241
      if (width > 0 && !negFlag) {
242
        fill(width - count, filler);
243
      }
244
      printn(n);
245
      if (width > 0 && negFlag) {
246
        fill(width - count, filler);
247
      }
248
    } else
249
    if (c == 'u' || c == 'o' || c == 'x' || c == 'X') {
250
      u = va_arg(ap, int);
251
      count = countPrintu(u,
252
                c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10));
253
      if (width > 0 && !negFlag) {
254
        fill(width - count, filler);
255
      }
256
      printu(u,
257
             c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10),
258
             c == 'X');
259
      if (width > 0 && negFlag) {
260
        fill(width - count, filler);
261
      }
262
    } else
263
    if (c == 'l') {
264
      c = *fmt++;
265
      if (c == 'd') {
266
        ln = va_arg(ap, long);
267
        count = countPrintn(ln);
268
        if (width > 0 && !negFlag) {
269
          fill(width - count, filler);
270
        }
271
        printn(ln);
272
        if (width > 0 && negFlag) {
273
          fill(width - count, filler);
274
        }
275
      } else
276
      if (c == 'u' || c == 'o' || c == 'x' || c == 'X') {
277
        lu = va_arg(ap, long);
278
        count = countPrintu(lu,
279
                  c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10));
280
        if (width > 0 && !negFlag) {
281
          fill(width - count, filler);
282
        }
283
        printu(lu,
284
               c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10),
285
               c == 'X');
286
        if (width > 0 && negFlag) {
287
          fill(width - count, filler);
288
        }
289
      } else {
290
        putchar('l');
291
        putchar(c);
292
      }
293
    } else
294
    if (c == 's') {
295
      s = va_arg(ap, char *);
296
      count = strlen(s);
297
      if (width > 0 && !negFlag) {
298
        fill(width - count, filler);
299
      }
300
      while ((c = *s++) != '\0') {
301
        putchar(c);
302
      }
303
      if (width > 0 && negFlag) {
304
        fill(width - count, filler);
305
      }
306
    } else
307
    if (c == 'c') {
308
      c = va_arg(ap, char);
309
      putchar(c);
310
    } else {
311
      putchar(c);
312
    }
313
  }
314
}
315
 
316
 
317
/*
318
 * Formatted output.
319
 * This is a scaled-down version of the C library's
320
 * printf. Used to print diagnostic information on
321
 * the console (and optionally to a logfile).
322
 */
323
void printf(char *fmt, ...) {
324
  va_list ap;
325
 
326
  va_start(ap, fmt);
327
  vprintf(fmt, ap);
328
  va_end(ap);
329
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.