tbftss/tools/tidyHeaders.sh

242 lines
4.5 KiB
Bash
Raw Normal View History

2016-02-29 17:06:04 +01:00
#!/usr/bin/php
<?php
/*
2019-01-01 17:14:11 +01:00
Copyright (C) 2015-2019 Parallel Realities
2016-02-29 17:06:04 +01:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
$UPDATE_FILES = false;
2018-12-06 17:52:13 +01:00
function funcSort($a, $b)
{
$a = str_replace("*", "", $a);
$b = str_replace("*", "", $b);
$aParts = explode(" ", $a);
$bParts = explode(" ", $b);
return strcmp($aParts[2], $bParts[2]);
}
function updateExterns($header, $defines, $functions, $structs)
{
asort($defines);
usort($functions, "funcSort");
asort($structs);
$newHeader = [];
foreach ($header as $line)
{
$newHeader[] = $line;
}
if (count($defines) > 0)
{
$newHeader[] = "\n";
$newHeader = array_merge($newHeader, $defines);
}
if (count($functions) > 0)
{
$newHeader[] = "\n";
$newHeader = array_merge($newHeader, $functions);
}
if (count($structs) > 0)
{
$newHeader[] = "\n";
$newHeader = array_merge($newHeader, $structs);
}
return $newHeader;
}
2016-02-29 17:06:04 +01:00
function cleanHeader($headerFile)
{
global $UPDATE_FILES;
$func_pattern = "/(([A-Z0-9_]+)\\()/i";
$struct_pattern = "/([A-Z]+);/i";
2016-05-15 09:19:26 +02:00
$define_pattern = "/#define ([A-Z]+)/i";
2016-02-29 17:06:04 +01:00
$bodyFile = $headerFile;
$bodyFile[strlen($bodyFile) - 1] = 'c';
if (file_exists($bodyFile))
{
$header = file($headerFile);
$body = file_get_contents($bodyFile);
2018-12-06 17:52:13 +01:00
$isMain = strpos($body, "int main(int argc, char *argv[])");
2016-05-15 09:19:26 +02:00
$lines = [];
2018-12-06 17:52:13 +01:00
$defines = [];
$functions = [];
$structs = [];
2016-02-29 17:06:04 +01:00
$i = 0;
$hasChanges = false;
foreach ($header as $line)
{
2018-12-06 18:07:16 +01:00
if ((preg_match("/extern|define/", $line) || preg_match("/;$/", $line)))
2016-02-29 17:06:04 +01:00
{
preg_match($func_pattern, $line, $matches);
2016-05-15 09:19:26 +02:00
2016-02-29 17:06:04 +01:00
if (count($matches) == 3)
{
2018-12-06 17:52:13 +01:00
unset($header[$i]);
2016-02-29 17:06:04 +01:00
$extern = $matches[2];
2016-05-15 09:19:26 +02:00
2018-12-06 17:52:13 +01:00
if (!preg_match_all("/\b${extern}\b/", $body))
2016-05-15 09:19:26 +02:00
{
if (!$hasChanges)
{
echo "$headerFile\n";
$hasChanges = true;
}
echo "\t- $line";
}
2018-12-06 17:52:13 +01:00
else if (!in_array($line, $lines))
2016-02-29 17:06:04 +01:00
{
2018-12-06 17:52:13 +01:00
$functions[] = $line;
2016-02-29 17:06:04 +01:00
}
}
preg_match($struct_pattern, $line, $matches);
if (count($matches) == 2)
{
2018-12-06 17:52:13 +01:00
unset($header[$i]);
2016-02-29 17:06:04 +01:00
$extern = $matches[1];
2016-05-15 09:19:26 +02:00
$externs[] = $extern;
2018-12-06 17:52:13 +01:00
if (!$isMain)
2016-05-15 09:19:26 +02:00
{
2018-12-06 17:52:13 +01:00
if (!preg_match_all("/\b${extern}\b/", $body))
2016-05-15 09:19:26 +02:00
{
2018-12-06 17:52:13 +01:00
if (!$hasChanges)
{
echo "$headerFile\n";
$hasChanges = true;
}
echo "\t- $line";
2016-05-15 09:19:26 +02:00
}
2018-12-06 17:52:13 +01:00
else if (!in_array($line, $lines))
2016-05-15 09:19:26 +02:00
{
2018-12-06 17:52:13 +01:00
$structs[] = $line;
2016-05-15 09:19:26 +02:00
}
2018-12-06 17:52:13 +01:00
}
else if (!in_array($line, $lines))
{
$structs[] = $line;
2016-05-15 09:19:26 +02:00
}
}
preg_match($define_pattern, $line, $matches);
if (count($matches) == 2)
{
2018-12-06 17:52:13 +01:00
unset($header[$i]);
2016-05-15 09:19:26 +02:00
$extern = $matches[1];
$externs[] = $extern;
2016-02-29 17:06:04 +01:00
if (strstr($body, "$extern") === FALSE)
{
if (!$hasChanges)
{
echo "$headerFile\n";
$hasChanges = true;
}
echo "\t- $line";
}
2018-12-06 17:52:13 +01:00
else if (!in_array($line, $lines))
2016-05-15 09:19:26 +02:00
{
2018-12-06 17:52:13 +01:00
$defines[] = $line;
2016-05-15 09:19:26 +02:00
}
2016-02-29 17:06:04 +01:00
}
}
$i++;
}
2018-12-06 17:52:13 +01:00
do
{
$wasBlank = false;
$line = trim(end($header));
if (strlen($line) == 0)
{
array_pop($header);
$wasBlank = true;
}
}
while ($wasBlank);
$defines = array_unique($defines);
$functions = array_unique($functions);
$structs = array_unique($structs);
$header = updateExterns($header, $defines, $functions, $structs);
2016-02-29 17:06:04 +01:00
2018-12-06 17:52:13 +01:00
if ($UPDATE_FILES)
2016-02-29 17:06:04 +01:00
{
file_put_contents($headerFile, $header);
}
}
}
function recurseDir($dir)
{
2016-05-15 09:19:26 +02:00
if ($dir != "../src/json")
2016-02-29 17:06:04 +01:00
{
2016-05-15 09:19:26 +02:00
$files = array_diff(scandir($dir), array('..', '.'));
foreach ($files as $file)
2016-02-29 17:06:04 +01:00
{
2016-05-15 09:19:26 +02:00
if (is_dir("$dir/$file"))
{
recurseDir("$dir/$file");
}
2018-12-06 17:52:13 +01:00
else if (strstr($file, ".h") !== FALSE && $file != 'i18n.h')
2016-05-15 09:19:26 +02:00
{
cleanHeader("$dir/$file");
}
2016-02-29 17:06:04 +01:00
}
}
}
if (isset($argv[1]))
{
$UPDATE_FILES = ($argv[1] == "-commit");
}
recurseDir("../src");
if (!$UPDATE_FILES)
{
echo "\nNo files updated. Use -commit to update headers\n";
}
?>