]> git.mxchange.org Git - flightgear.git/blob - tests/test-text.cxx
Fix various compiler warnings contributed by Norman Princeton.
[flightgear.git] / tests / test-text.cxx
1 // do some non-destructive read tests relating text files
2
3 #include <stdio.h>
4
5 static void readchars( FILE *fd ) {
6     char c;
7     while ( (c = fgetc(fd)) != EOF ) {
8         printf("c = %d", c);
9         if ( c == 10 ) {
10             printf(" LF \\n");
11         }
12         if ( c == 13 ) {
13             printf(" CR \\r");
14         }
15         printf("\n");
16     }
17 }
18
19
20 static void readlines( FILE *fd ) {
21     char line[256];
22     while ( fgets(line, 255, fd) != NULL ) {
23         int len = strlen(line);
24         printf("line = %s", line);
25         if ( len >= 2 ) {
26             printf("  char[n - 1] = %d  char[n] = %d\n",
27                    line[len-2], line[len-1]);
28         } else if ( len >= 1 ) {
29             printf("  char[n] = %d\n",line[len-1]);
30         } else {
31             printf("empty string\n");
32         }
33     }
34 }
35
36
37 int main( int argc, char **argv ) {
38     // check usage
39     if ( argc != 2 ) {
40         printf("usage: %s file\n", argv[0]);
41         return -1;
42     }
43
44     char file[256];
45     strcpy( file, argv[1] );
46     FILE *fd;
47
48     // open a file in (default) text mode
49     printf("TEXT MODE (DEFAULT) by character\n\n");
50     fd = fopen( file, "r" );
51     if ( fd != NULL ) {
52         readchars( fd );
53         fclose(fd);
54     } else {
55         printf("Cannot open %s\n", file);
56     }
57     printf("\n");
58
59     // open a file in (explicit) text mode
60     printf("TEXT MODE (EXPLICIT) by character\n\n");
61     fd = fopen( file, "rt" );
62     if ( fd != NULL ) {
63         readchars( fd );
64         fclose(fd);
65     } else {
66         printf("Cannot open %s\n", file);
67     }
68     printf("\n");
69
70     // open a file in (explicit) binary mode
71     printf("BINARY MODE (EXPLICIT) by character\n\n");
72     fd = fopen( file, "rb" );
73     if ( fd != NULL ) {
74         readchars( fd );
75         fclose(fd);
76     } else {
77         printf("Cannot open %s\n", file);
78     }
79     printf("\n");
80
81     // open a file in (default) text mode
82     printf("TEXT MODE (DEFAULT) by line\n\n");
83     fd = fopen( file, "r" );
84     if ( fd != NULL ) {
85         readlines( fd );
86         fclose(fd);
87     } else {
88         printf("Cannot open %s\n", file);
89     }
90     printf("\n");
91
92     // open a file in (explicit) text mode
93     printf("TEXT MODE (EXPLICIT) by line\n\n");
94     fd = fopen( file, "rt" );
95     if ( fd != NULL ) {
96         readlines( fd );
97         fclose(fd);
98     } else {
99         printf("Cannot open %s\n", file);
100     }
101     printf("\n");
102
103     // open a file in (explicit) binary mode
104     printf("BINARY MODE (EXPLICIT) by line\n\n");
105     fd = fopen( file, "rb" );
106     if ( fd != NULL ) {
107         readlines( fd );
108         fclose(fd);
109     } else {
110         printf("Cannot open %s\n", file);
111     }
112     printf("\n");
113
114     return 0;
115 }