aboutsummaryrefslogtreecommitdiffstats
path: root/mk_nametoucs_tab.c
blob: 89eec886f67ba553785522896d5fe7cbae4ef450 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "gen/nametoucs_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 uint32_t prehash(const char *str)
{
  uint32_t hash = PHASHSALT;
  const uint8_t *p = (const uint8_t *)str;

  while ( *p )
    hash = (hash ^ *p++) + ((hash << 27)+(hash >> 5));

  return hash;
}

static int32_t hash_to_ucs[PHASHNKEYS];
static void read_nametoucs_tab(void)
{
  char line[256], *p;
  FILE *f = fopen("gen/nametoucs.tab", "rt");
  if ( !f )
    die("could not open gen/nametoucs.tab");
  int32_t ucs;
  uint32_t hash;
  int i;

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

  while ( fgets(line, sizeof line, f) ) {
    if ( (p = strchr(line, '\n')) )
      *p = '\0';
   
    ucs = strtol(line, NULL, 16);
    hash = _libucd_nametoucs_hash(prehash(line+6));

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

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

    hash_to_ucs[hash] = ucs;
  }

  fclose(f);
}

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

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

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


int main(void)
{
  read_nametoucs_tab();
  make_nametoucs_tab();

  return 0;
}