File: loadcw.c

package info (click to toggle)
oss4 4.2-build2020-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,320 kB
  • sloc: ansic: 239,151; cpp: 18,981; sh: 4,590; pascal: 3,863; asm: 1,189; makefile: 574; php: 53; xml: 46
file content (163 lines) | stat: -rw-r--r-- 2,413 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define TESTFILE "Vvcw"

static int ngroups = 0;

static void
printchar (unsigned char c)
{
  switch (c)
    {
    case ' ':
      ngroups++;
      if (ngroups && !(ngroups % 6))
	printf (" \n");
      else
	printf (" ");
      break;

    case 0x99:
      printf ("");
      break;
    case 0x8e:
      printf ("");
      break;
    case 0x8f:
      printf ("");
      break;
    default:
      if (c <= 'Z' && c >= 'A')
	c = c + 32;
      printf ("%c", c);
    }
}

static int
showtest (char *tname)
{
  FILE *f;
  unsigned char line[1024], *p;
  int on = 0;

  if ((f = fopen (TESTFILE, "r")) == NULL)
    {
      perror (TESTFILE);
      return -1;
    }

  while (fgets (line, sizeof (line), f))
    {
      p = &line[strlen (line) - 1];
      while (p > line)
	{
	  if (*p == '\n' || *p == '\r')
	    *p = 0;
	  p--;
	}

      if (*line == '#')
	{
	  if (on)
	    {
	      fclose (f);
	      printf ("\n");
	      return 0;
	    }

	  if (strcmp (line + 1, tname) == 0)
	    on = 1;
	}
      else if (on)
	{
	  p = line;

	  while (*p)
	    {
	      switch (*p)
		{
		default:
		  printchar (*p);
		}
	      p++;
	    }
	}

    }
  fclose (f);

  return 1;
}

static char *
randomtest (void)
{
  FILE *f;
  unsigned char line[1024], *p;
  static char name[10] = "????";
  int n = 0, x;
  char *tests[1000];

  if ((f = fopen (TESTFILE, "r")) == NULL)
    {
      perror (TESTFILE);
      return NULL;
    }

  while (fgets (line, sizeof (line), f))
    {
      if (strlen (line) < 1)
	continue;
      p = &line[strlen (line) - 1];
      while (p > line)
	{
	  if (*p == '\n' || *p == '\r')
	    *p = 0;
	  p--;
	}
      if (strlen (line) < 1)
	continue;

      if (*line == '#')
	{
	  char *tmp;
	  tmp = malloc (strlen (line + 1) + 1);
	  strcpy (tmp, line + 1);

	  if (n >= 1000)
	    {
	      fprintf (stderr, "Too many test texts\n");
	      exit (-1);
	    }

	  tests[n++] = tmp;
	}
    }
  fclose (f);

  srandom (time (0));

  x = random () % n;		/* Select one of the tests randomly */
  strcpy (name, tests[x]);

  return name;
}

int
main (int argc, char *argv[])
{
  char *thistest;

  if (argc > 1)
    exit (showtest (argv[1]));

  thistest = randomtest ();
  if (thistest == NULL)
    exit (-1);
  fprintf (stderr, "Selected test text is '%s'\n", thistest);
  fflush (stderr);
  exit (showtest (thistest));
}