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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ucos-ii/] [2.91/] [common/] [cprintf_r.c] - Blame information for rev 471

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 471 julius
/*
2
  File: cprintf_r.c
3
 
4
  Copyright (C) 2004  Kustaa Nyholm
5
 
6
  This library is free software; you can redistribute it and/or
7
  modify it under the terms of the GNU Lesser General Public
8
  License as published by the Free Software Foundation; either
9
  version 2.1 of the License, or (at your option) any later version.
10
 
11
  This library is distributed in the hope that it will be useful,
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
  Lesser General Public License for more details.
15
 
16
  You should have received a copy of the GNU Lesser General Public
17
  License along with this library; if not, write to the Free Software
18
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
*/
21
 
22
#include "cprintf_r.h"
23
 
24
typedef void (*putcf) (char,void*);
25
static putcf stdout_putf;
26
static void* stdout_putp;
27
 
28
#ifdef PRINTF_LONG_SUPPORT
29
 
30
static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf)
31
{
32
  int n=0;
33
  unsigned int d=1;
34
  while (num/d >= base)
35
    d*=base;
36
  while (d!=0) {
37
    int dgt = num / d;
38
    num%=d;
39
    d/=base;
40
    if (n || dgt>0|| d==0) {
41
      *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
42
      ++n;
43
    }
44
  }
45
  *bf=0;
46
}
47
 
48
static void li2a (long num, char * bf)
49
{
50
  if (num<0) {
51
    num=-num;
52
    *bf++ = '-';
53
  }
54
  uli2a(num,10,0,bf);
55
}
56
 
57
#endif
58
 
59
static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
60
{
61
  int n=0;
62
  unsigned int d=1;
63
  while (num/d >= base)
64
    d*=base;
65
  while (d!=0) {
66
    int dgt = num / d;
67
    num%= d;
68
    d/=base;
69
    if (n || dgt>0 || d==0) {
70
      *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
71
      ++n;
72
    }
73
  }
74
  *bf=0;
75
}
76
 
77
static void i2a (int num, char * bf)
78
{
79
  if (num<0) {
80
    num=-num;
81
    *bf++ = '-';
82
  }
83
  ui2a(num,10,0,bf);
84
}
85
 
86
static int a2d(char ch)
87
{
88
  if (ch>='0' && ch<='9')
89
    return ch-'0';
90
  else if (ch>='a' && ch<='f')
91
    return ch-'a'+10;
92
  else if (ch>='A' && ch<='F')
93
    return ch-'A'+10;
94
  else return -1;
95
}
96
 
97
static char a2i(char ch, char** src,int base,int* nump)
98
{
99
  char* p= *src;
100
  int num=0;
101
  int digit;
102
  while ((digit=a2d(ch))>=0) {
103
    if (digit>base) break;
104
    num=num*base+digit;
105
    ch=*p++;
106
  }
107
  *src=p;
108
  *nump=num;
109
  return ch;
110
}
111
 
112
static void putchw(void* putp,putcf putf,int n, char z, char* bf)
113
{
114
  char fc=z? '0' : ' ';
115
  char ch;
116
  char* p=bf;
117
  while (*p++ && n > 0)
118
    n--;
119
  while (n-- > 0)
120
    putf(fc,putp);
121
  while ((ch= *bf++))
122
    putf(ch,putp);
123
}
124
 
125
void tfp_format(void* putp,putcf putf,char *fmt, va_list va)
126
{
127
  char bf[12];
128
 
129
  char ch;
130
 
131
 
132
  while ((ch=*(fmt++))) {
133
    if (ch!='%')
134
      putf(ch,putp);
135
    else {
136
      char lz=0;
137
#ifdef  PRINTF_LONG_SUPPORT
138
      char lng=0;
139
#endif
140
      int w=0;
141
      ch=*(fmt++);
142
      if (ch=='0') {
143
        ch=*(fmt++);
144
        lz=1;
145
      }
146
      if (ch>='0' && ch<='9') {
147
        ch=a2i(ch,&fmt,10,&w);
148
      }
149
#ifdef  PRINTF_LONG_SUPPORT
150
      if (ch=='l') {
151
        ch=*(fmt++);
152
        lng=1;
153
      }
154
#endif
155
      switch (ch) {
156
      case 0:
157
        goto abort;
158
      case 'u' : {
159
#ifdef  PRINTF_LONG_SUPPORT
160
        if (lng)
161
          uli2a(va_arg(va, unsigned long int),10,0,bf);
162
        else
163
#endif
164
          ui2a(va_arg(va, unsigned int),10,0,bf);
165
        putchw(putp,putf,w,lz,bf);
166
        break;
167
      }
168
      case 'd' :  {
169
#ifdef  PRINTF_LONG_SUPPORT
170
        if (lng)
171
          li2a(va_arg(va, unsigned long int),bf);
172
        else
173
#endif
174
          i2a(va_arg(va, int),bf);
175
        putchw(putp,putf,w,lz,bf);
176
        break;
177
      }
178
      case 'x': case 'X' :
179
#ifdef  PRINTF_LONG_SUPPORT
180
        if (lng)
181
          uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
182
        else
183
#endif
184
          ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
185
        putchw(putp,putf,w,lz,bf);
186
        break;
187
      case 'c' :
188
        putf((char)(va_arg(va, int)),putp);
189
        break;
190
      case 's' :
191
        putchw(putp,putf,w,0,va_arg(va, char*));
192
        break;
193
      case '%' :
194
        putf(ch,putp);
195
      default:
196
        break;
197
      }
198
    }
199
  }
200
 abort:;
201
}
202
 
203
void init_printf(void* putp,void (*putf) (char,void*))
204
{
205
  stdout_putf=putf;
206
  stdout_putp=putp;
207
}
208
 
209
void tfp_printf(char *fmt, ...)
210
{
211
  va_list va;
212
  va_start(va,fmt);
213
  tfp_format(stdout_putp,stdout_putf,fmt,va);
214
  va_end(va);
215
}
216
 
217
static void putcp(char c, void* p)
218
{
219
  *(*((char**)p))++ = c;
220
}
221
 
222
void tfp_sprintf(char* s,char *fmt, ...)
223
{
224
  va_list va;
225
  va_start(va,fmt);
226
  tfp_format(&s, putcp, fmt, va);
227
  putcp(0,&s);
228
  va_end(va);
229
}

powered by: WebSVN 2.1.0

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