Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / regression / tcc / 32_led.c
blob5596cbfd3e09db647b0cf534664a8410661cef47
1 /* example from http://barnyard.syr.edu/quickies/led.c */
3 /* led.c: print out number as if on 7 line led display. I.e., write integer
4 given on command line like this:
5 _ _ _
6 | _| _| |_| |_
7 | |_ _| | _| etc.
9 We assume the terminal behaves like a classical teletype. So the top
10 lines of all digits have to be printed first, then the middle lines of
11 all digits, etc.
13 By Terry R. McConnell
15 compile: cc -o led led.c
17 If you just want to link in the subroutine print_led that does all the
18 work, compile with -DNO_MAIN, and declare the following in any source file
19 that uses the call:
21 extern void print_led(unsigned long x, char *buf);
23 Bug: you cannot call repeatedly to print more than one number to a line.
24 That would require curses or some other terminal API that allows moving the
25 cursor to a previous line.
31 #include <stdlib.h>
32 #include <stdio.h>
34 #define MAX_DIGITS 32
35 #define NO_MAIN
38 /* Print the top line of the digit d into buffer.
39 Does not null terminate buffer. */
41 void topline(int d, char *p){
43 *p++ = ' ';
44 switch(d){
46 /* all these have _ on top line */
48 case 0:
49 case 2:
50 case 3:
51 case 5:
52 case 7:
53 case 8:
54 case 9:
55 *p++ = '_';
56 break;
57 default:
58 *p++=' ';
61 *p++=' ';
64 /* Print the middle line of the digit d into the buffer.
65 Does not null terminate. */
67 void midline(int d, char *p){
69 switch(d){
71 /* those that have leading | on middle line */
73 case 0:
74 case 4:
75 case 5:
76 case 6:
77 case 8:
78 case 9:
79 *p++='|';
80 break;
81 default:
82 *p++=' ';
84 switch(d){
86 /* those that have _ on middle line */
88 case 2:
89 case 3:
90 case 4:
91 case 5:
92 case 6:
93 case 8:
94 case 9:
95 *p++='_';
96 break;
97 default:
98 *p++=' ';
101 switch(d){
103 /* those that have closing | on middle line */
105 case 0:
106 case 1:
107 case 2:
108 case 3:
109 case 4:
110 case 7:
111 case 8:
112 case 9:
113 *p++='|';
114 break;
115 default:
116 *p++=' ';
121 /* Print the bottom line of the digit d. Does not null terminate. */
123 void botline(int d, char *p){
126 switch(d){
128 /* those that have leading | on bottom line */
130 case 0:
131 case 2:
132 case 6:
133 case 8:
134 *p++='|';
135 break;
136 default:
137 *p++=' ';
139 switch(d){
141 /* those that have _ on bottom line */
143 case 0:
144 case 2:
145 case 3:
146 case 5:
147 case 6:
148 case 8:
149 *p++='_';
150 break;
151 default:
152 *p++=' ';
155 switch(d){
157 /* those that have closing | on bottom line */
159 case 0:
160 case 1:
161 case 3:
162 case 4:
163 case 5:
164 case 6:
165 case 7:
166 case 8:
167 case 9:
168 *p++='|';
169 break;
170 default:
171 *p++=' ';
176 /* Write the led representation of integer to string buffer. */
178 void print_led(unsigned long x, char *buf)
181 int i=0,n;
182 static int d[MAX_DIGITS];
185 /* extract digits from x */
187 n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */
189 while(x){
190 d[n++] = (int)(x%10L);
191 if(n >= MAX_DIGITS)break;
192 x = x/10L;
195 /* print top lines of all digits */
197 for(i=n-1;i>=0;i--){
198 topline(d[i],buf);
199 buf += 3;
200 *buf++=' ';
202 *buf++='\n'; /* move teletype to next line */
204 /* print middle lines of all digits */
206 for(i=n-1;i>=0;i--){
207 midline(d[i],buf);
208 buf += 3;
209 *buf++=' ';
211 *buf++='\n';
213 /* print bottom lines of all digits */
215 for(i=n-1;i>=0;i--){
216 botline(d[i],buf);
217 buf += 3;
218 *buf++=' ';
220 *buf++='\n';
221 *buf='\0';
224 int main()
226 char buf[5*MAX_DIGITS];
227 print_led(1234567, buf);
228 printf("%s\n",buf);
230 return 0;
233 #ifndef NO_MAIN
234 int main(int argc, char **argv)
237 int i=0,n;
238 long x;
239 static int d[MAX_DIGITS];
240 char buf[5*MAX_DIGITS];
242 if(argc != 2){
243 fprintf(stderr,"led: usage: led integer\n");
244 return 1;
247 /* fetch argument from command line */
249 x = atol(argv[1]);
251 /* sanity check */
253 if(x<0){
254 fprintf(stderr,"led: %d must be non-negative\n",x);
255 return 1;
258 print_led(x,buf);
259 printf("%s\n",buf);
261 return 0;
264 #endif
266 /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/