Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/core/main.php b/core/main.php
- index ce88cb9..6aa032c 100644
- --- a/core/main.php
- +++ b/core/main.php
- @@ -26,7 +26,9 @@ $optionsConfig = [
- 'chronicle' => ['AI chronicle. Provide a directory name from the data directory.', 'gf'],
- 'language' => ['Resulting language. Provide a file name from the core/generators directory (without .php extension).', 'nasc'],
- 'split' => ["\t" . 'Split result by classes.', true],
- - 'join' => ["\t" . 'Join split classes into one file. Provide a directory which contains the classes.txt file.', null]
- + 'join' => ["\t" . 'Join split classes into one file. Provide a directory which contains the classes.txt file.', null],
- + 'tree' => ["\t" . 'Split result in tree structure.', false],
- + 'utf8' => ["\t" . 'Encode output in UTF-8 instead of UTF-16LE', false],
- ];
- $longopts = [];
- @@ -76,14 +78,47 @@ $parser = new Parser($data);
- $generatorClass = ucfirst($options['language']) . 'Generator';
- $generator = new $generatorClass(); /** @var GeneratorInterface $generator */
- +class readutf16le_filter extends php_user_filter {
- + function filter($in, $out, &$consumed, $closing) {
- + while ($bucket = stream_bucket_make_writeable($in)) {
- + # printf("filter: %s\n", to_hex($bucket->data));
- + $bucket->data = iconv('UTF-16LE', 'UTF-8',
- + strlen($bucket->data) && substr($bucket->data, 0, 2) == "\xff\xfe"
- + ? substr($bucket->data, 2)
- + : $bucket->data);
- + $consumed += $bucket->datalen;
- + stream_bucket_append($out, $bucket);
- + }
- + return PSFS_PASS_ON;
- + }
- +}
- +
- +stream_filter_register('readutf16le', 'readutf16le_filter');
- +
- $file = fopen($options['input'], 'r');
- +
- +stream_filter_append($file, 'readutf16le');
- +
- $line = 0;
- +function rmr($dir) {
- + $files = array_diff(scandir($dir), array('.', '..'));
- + foreach ($files as $file) {
- + $path = $dir . '/' . $file;
- + if (is_dir($path)) {
- + rmr($path);
- + } else {
- + unlink($path);
- + }
- + }
- + rmdir($dir);
- +}
- +
- if (!$options['split']) {
- $outputFile = $options['output'] . '.' . $options['language'];
- // workaround for NASC: write BOM
- - if ($options['language'] === 'nasc') {
- + if ($options['language'] === 'nasc' && !$options['utf8']) {
- file_put_contents($outputFile, pack('S', 0xFEFF));
- } else {
- file_put_contents($outputFile, '');
- @@ -92,12 +127,14 @@ if (!$options['split']) {
- mkdir($options['output']);
- } else {
- echo 'Cleaning output directory: ' . $options['output'] . "\n\n";
- - array_map('unlink', glob($options['output'] . '/*'));
- + rmr($options['output']);
- + mkdir($options['output']);
- }
- +$tree = [];
- +
- while ($file && !feof($file)) {
- $string = trim(fgets($file));
- - $string = preg_replace('/[^\s\x20-\x7E]/', '', $string); // remove non-ASCII characters
- $line++;
- if (!$string) {
- @@ -131,17 +168,36 @@ while ($file && !feof($file)) {
- $outputFile = $options['output'] . '.' . $options['language'];
- // workaround for NASC: convert to UTF-16LE BOM
- - if ($options['language'] === 'nasc') {
- + if ($options['language'] === 'nasc' && !$options['utf8']) {
- file_put_contents($outputFile, iconv('UTF-8', 'UTF-16LE', $code), FILE_APPEND);
- } else {
- file_put_contents($outputFile, $code, FILE_APPEND);
- }
- } else {
- - $outputFile = $options['output'] . '/' . $name . '.' . $options['language'];
- - file_put_contents($options['output'] . '/classes.txt', $name . '.' . $options['language'] . "\n", FILE_APPEND);
- + if ($options['tree']) {
- + if ($class->getSuper() !== null) {
- + $tree[$class->getName()] = $class->getSuper();
- + }
- + $filename = '';
- + $current = $class->getName();
- + for (;;) {
- + $current = $tree[$current];
- + if (!$current) break;
- + $filename = $current . '/' . $filename;
- + }
- + $dirname = $options['output'] . '/' . $filename;
- + if (!is_dir($dirname)) {
- + mkdir($dirname, 0777, true);
- + }
- + $filename = $filename . $class->getName();
- + } else {
- + $filename = $name;
- + }
- + $outputFile = $options['output'] . '/' . $filename . '.' . $options['language'];
- + file_put_contents($options['output'] . '/classes.txt', $filename . '.' . $options['language'] . "\n", FILE_APPEND);
- // workaround for NASC: convert to UTF-16LE BOM
- - if ($options['language'] === 'nasc') {
- + if ($options['language'] === 'nasc' && !$options['utf8']) {
- file_put_contents($outputFile, pack('S', 0xFEFF) . iconv('UTF-8', 'UTF-16LE', $code));
- } else {
- file_put_contents($outputFile, $code);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement