File: cf.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (191 lines) | stat: -rw-r--r-- 4,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
  !!DESCRIPTION!! print character frequencies
  !!ORIGIN!!      LCC 4.1 Testsuite
  !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
*/

/*
        cf - print character frequencies
*/

#include "common.h"

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

FILE *in;

#define INFILE   "cf.in"
#define GETCHAR() fgetc(in)

#ifndef NO_FLOATS
float f[0x100];
#else
signed f[0x100];
#endif

#ifdef NO_OLD_FUNC_DECL
int main(int argc,char **argv)
#else
main(argc, argv)
int argc;
char *argv[];
#endif
{
        int i, c, nc;
#ifndef NO_FLOATS
        float cutoff, atof();
#else
        signed cutoff;
#endif

        in = fopen(INFILE, "rb");
        if (in == NULL) {
            return EXIT_FAILURE;
        }

        if (argc <= 1)
#ifndef NO_FLOATS
                cutoff = 0.0;
#else
                cutoff = 0;
#endif
        else
#ifndef NO_FLOATS
                cutoff = atof(argv[1])/100;
#else
                cutoff = atoi(argv[1])/100;
#endif
        for (i = 0; i < 0x100; )
    {
#ifndef NO_FLOATS
                f[i++] = 0.0;
#else
        f[i++] = 0;
#endif
    }

    printf("input:\n\n");
    
    nc = 0;
    while ((c = GETCHAR()) != -1)
    {
/*        printf("[%02x]",c); */
        printf("%c",c);
        f[c] += 1;
        nc++;
    }
    printf("\n\ncount: %d\n\n",nc);

    /*
        now try to print a report in a way so that
        the order is somewhat independent from the
        target character set
    */

    printf("a-z char:freq\n\n");

    /* first round ... lowercase characters */
        for (i = 0; i < 0x100; ++i)
    {
                if ((f[i]) && ((f[i]/nc) >= cutoff))
        {
                        if ((i >= 'a') && (i <= 'z'))
            {
                                printf("%c", i);
#ifndef NO_FLOATS
                printf(":%.1f\n", 100*f[i]/nc);
#else
                printf(":%d\n", 100*f[i]/nc);
#endif
                f[i]=0;
            }
                }
    }

    printf("A-Z char:freq\n\n");

    /* second round ... uppercase characters */
        for (i = 0; i < 0x100; ++i)
    {
                if ((f[i]) && ((f[i]/nc) >= cutoff))
        {
                        if ((i >= 'A') && (i <= 'Z'))
            {
                                printf("%c", i);
#ifndef NO_FLOATS
                printf(":%.1f\n", 100*f[i]/nc);
#else
                printf(":%d\n", 100*f[i]/nc);
#endif
                f[i]=0;
            }
                }
    }

    printf("0-9 char:freq\n\n");

    /* third round ... numbers */
        for (i = 0; i < 0x100; ++i)
    {
                if ((f[i]) && ((f[i]/nc) >= cutoff))
        {
                        if ((i >= '0') && (i <= '9'))
            {
                                printf("%c", i);
#ifndef NO_FLOATS
                printf(":%.1f\n", 100*f[i]/nc);
#else
                printf(":%d\n", 100*f[i]/nc);
#endif
                f[i]=0;
            }
                }
    }

    printf("isprint char:freq\n\n");

    /* second last round ... remaining printable characters */
        for (i = 0; i < 0x100; ++i)
    {
                if ((f[i]) && ((f[i]/nc) >= cutoff))
        {
                        if(isprint(i))
            {
                                printf("%c", i);
#ifndef NO_FLOATS
                printf(":%.1f\n", 100*f[i]/nc);
#else
                printf(":%d\n", 100*f[i]/nc);
#endif
                f[i]=0;
            }
                }
    }

    printf("rest char:freq\n\n");

    /* last round ... remaining non printable characters */
        for (i = 0; i < 0x100; ++i)
    {
                if ((f[i]) && ((f[i]/nc) >= cutoff))
        {
            if(i=='\n')
            {
                                printf("newline");
            }
            else
            {
                                printf("%03o", i);
            }
#ifndef NO_FLOATS
                printf(":%.1f\n", 100*f[i]/nc);
#else
                printf(":%d\n", 100*f[i]/nc);
#endif
                }
    }
    fclose(in);
    return 0;
}