ensure options are applied before the headers are set
[m6w6/ext-http] / gen_utf8.php
1 #!/usr/bin/env php
2 <?php
3
4 error_reporting(E_ALL);
5 set_error_handler(function($c, $e, $f, $l) {
6 throw new Exception("$e in $f on line $l");
7 });
8
9 $i18n = $argc >= 2 ? $argv[1] : "/usr/share/i18n/locales/i18n";
10
11 $f = fopen($i18n, "r");
12 $c = false;
13 $a = false;
14
15 ob_start(null, 0xffff);
16 while (!feof($f)) {
17 $line = fgets($f);
18 if (!$c && $line !== "LC_CTYPE\n") {
19 continue;
20 }
21 $c = true;
22 if ($line === "END LC_CTYPE\n") {
23 break;
24 }
25 switch($line{0}) {
26 case "%":
27 break;
28 case "\n":
29 if ($a) {
30 break 2;
31 }
32 break;
33 case " ":
34 if ($a) {
35 foreach (explode(";", trim($line, "\n/ ;")) as $ranges) {
36 $range = explode("..", $ranges);
37 $step = 0;
38 $end = 0;
39 switch (count($range)) {
40 case 3:
41 list($sstart, $sstep, $send) = $range;
42 sscanf($sstart, "<U%X>", $start);
43 sscanf($sstep, "(%d)", $step);
44 sscanf($send, "<U%X>", $end);
45
46 break;
47 case 2:
48 list($sstart, $send) = $range;
49 $step = 1;
50 sscanf($sstart, "<U%X>", $start);
51 sscanf($send, "<U%X>", $end);
52 break;
53 case 1:
54 list($sstart) = $range;
55 sscanf($sstart, "<U%X>", $start);
56 break;
57 }
58 print "\t{";
59 if ($start >= 0xffff) {
60 printf("0x%08X, ", $start);
61 if ($end) {
62 printf("0x%08X, ", $end);
63 } else {
64 print(" 0, ");
65 }
66 } else {
67 printf(" 0x%04X, ", $start);
68 if ($end) {
69 printf(" 0x%04X, ", $end);
70 } else {
71 print(" 0, ");
72 }
73 }
74 printf("%d},\n", $step);
75 }
76 }
77 break;
78 default:
79 if ($a) {
80 break 2;
81 } elseif ($line === "alpha /\n") {
82 $a = true;
83 }
84 break;
85 }
86 }
87
88 file_put_contents("php_http_utf8.h",
89 preg_replace('/(\/\* BEGIN::UTF8TABLE \*\/\n).*(\n\s*\/\* END::UTF8TABLE \*\/)/s', '$1'. ob_get_contents() .'$2',
90 file_get_contents("php_http_utf8.h")));