]> git.sur5r.net Git - cc65/blob - samples/ascii.c
Fixed gcc compiler warning (#867)
[cc65] / samples / ascii.c
1 /* ascii.c
2 **
3 ** Shows the ASCII (or ATASCII, PETSCII) codes of typed characters.
4 **
5 ** 2003-03-09, Greg King <gngking@erols.com>
6 */
7
8 /* Define USE_STDIO, when you want to use the stdio functions.
9 ** Do not define it, when you want to use the conio functions.
10 ** NOTE:  stdin on some targets is line-bufferred.  You might need to type
11 **        a key, then tap the return(enter)-key, in order to see each code.
12 */
13 /*
14 #define USE_STDIO
15 */
16
17 #include <conio.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21
22 #define QUIT    'Q'
23
24 /* r -- row.  t -- table-column.
25 */
26 static unsigned char height, width, r, t;
27 static int c;
28
29 #ifndef USE_STDIO
30
31 # define PRINT  cprintf
32 # define PUT(c) cputc((char)(c))
33
34 /* conio doesn't echo typed characters.
35 ** So, this function does it.
36 */
37 static int GET(void) {
38         PUT(c = (int)cgetc());
39         return c;
40         }
41
42 #else
43
44 # define PRINT  printf
45 # define GET    getchar
46
47 #endif
48
49 int main(void) {
50
51 #       ifndef USE_STDIO
52         /* conio doesn't scroll!  Avoid trouble by starting at the top
53         ** of the screen, and never going "below" the bottom of the screen.
54         */
55         clrscr();
56         r = 7;                          /* allow for prompt */
57 #       endif
58
59         /* This prompt fits on the VIC-20's narrow screen.
60         */
61         PRINT("Type characters to see\r\ntheir hexadecimal code\r\nnumbers - 'Q' quits:\r\n\n");
62         screensize(&width, &height);    /* get the screen's dimensions */
63         width /= 6;                     /* get number of codes on a line */
64         cursor(true);
65         t = 0;
66         while ((c = GET()) != EOF) {
67
68 #               ifndef USE_STDIO
69                 if (r == height) {
70                         clrscr();
71                         t = 0;
72                         PUT(c); /* echo char. again because screen was erased */
73                         r = 1;
74                         }
75                 if (c == '\n')
76                         ++r;
77 #               endif
78
79                 PRINT("=$%02x ", c);
80                 if (c == QUIT)
81                         break;
82                 if (++t == width) {
83                         PRINT("\r\n");
84                         ++r;
85                         t = 0;
86                         }
87                 }
88         PRINT("\r\n");
89         return EXIT_SUCCESS;
90         }