aboutsummaryrefslogtreecommitdiffstats
path: root/mk_ucstoname_tab.c
blob: 1205d49e90a9ee5afae110e0d9aad881f7ecd346 (plain)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "gen/ucstoname_hash.h"

#define UCS_CNT 0x110000

const char *program;

void die(const char *msg)
{
  fprintf(stderr, "%s: %s\n", program, msg);
  exit(1);
}

static const char *int24str(int32_t n)
{
  static char str[17];

  sprintf(str, "{0x%02x,0x%02x,0x%02x}",
	  (n & 0xff),
	  ((n >> 8) & 0xff),
	  ((n >> 16) & 0xff));
  return str;
}

static int proparrayindex[UCS_CNT];
static void read_proparrayindex(void)
{
  FILE *f = fopen("gen/proparrayindex", "rt");
  if ( !f )
    die("could not open gen/proparrayindex");
  int last = 0, prev_ix = -1;
  int curr, i, ix;

  while ( fscanf(f, "%x %d\n", &curr, &ix) == 2 ) {
    for ( i = last ; i < curr ; i++ )
      proparrayindex[i] = prev_ix;
    last = curr;
    prev_ix = ix;
  }

  for ( i = last ; i < UCS_CNT ; i++ )
    proparrayindex[i] = prev_ix;

  fclose(f);
}


static int nameslistoffset[UCS_CNT];
static void read_nameslistoffset(void)
{
  FILE *f = fopen("gen/nameslist.offset", "rt");
  if ( !f )
    die("could not open gen/nameslist.offset");
  int curr, offset;
  int i;

  for ( i = 0 ; i < UCS_CNT ; i++ )
    nameslistoffset[i] = -1;

  while ( fscanf(f, "%x %d\n", &curr, &offset) == 2 )
    nameslistoffset[curr] = offset;
    
  fclose(f);
}
  

int32_t hash_to_ucs[PHASHNKEYS];
static void compute_hash_to_ucs(void)
{
  uint32_t hash;
  int i;

  for ( i = 0 ; i < PHASHNKEYS ; i++ )
    hash_to_ucs[i] = -1;

  for ( i = 0 ; i < UCS_CNT ; i++ ) {
    if ( nameslistoffset[i] != -1 ) {
      hash = _libucd_ucstoname_hash(i);

      if ( hash >= PHASHNKEYS )
	die("hash not minimal");

      if ( hash_to_ucs[hash] != -1 )
	die("hash collision");

      hash_to_ucs[hash] = i;
    }
  }
}


static void make_ucstoname_tab(void)
{
  FILE *f = fopen("gen/ucstoname_tab.c", "wt");
  if ( !f )
    die("could not create gen/ucstoname_tab.c");
  int i;
  int32_t ucs;

  fprintf(f,
	  "#include \"libucd_int.h\"\n"
	  "const struct _libucd_ucstoname_tab _libucd_ucstoname_tab[] =\n"
	  "{\n");

  for ( i = 0 ; i < PHASHNKEYS ; i++ ) {
    ucs = hash_to_ucs[i];
    fprintf(f, "\t{ %s, ", int24str(ucs));
    fprintf(f, "%s, ", int24str(nameslistoffset[ucs]));
    fprintf(f, "%d },\n", proparrayindex[ucs]);
  }
  fprintf(f, "};\n");
  fclose(f);
}


int main(void)
{
  read_proparrayindex();
  read_nameslistoffset();
  compute_hash_to_ucs();
  make_ucstoname_tab();

  return 0;
}