#!/usr/bin/php 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; } function cleanHeader($headerFile) { global $UPDATE_FILES; $func_pattern = "/(([A-Z0-9_]+)\\()/i"; $struct_pattern = "/([A-Z]+);/i"; $define_pattern = "/#define ([A-Z]+)/i"; $bodyFile = $headerFile; $bodyFile[strlen($bodyFile) - 1] = 'c'; if (file_exists($bodyFile)) { $header = file($headerFile); $body = file_get_contents($bodyFile); $isMain = strpos($body, "int main("); $lines = []; $defines = []; $functions = []; $structs = []; $i = 0; $hasChanges = false; foreach ($header as $line) { if ((preg_match("/extern|define/", $line) || preg_match("/;$/", $line))) { preg_match($func_pattern, $line, $matches); if (count($matches) == 3) { unset($header[$i]); $extern = $matches[2]; if (!preg_match_all("/\b${extern}\b/", $body)) { if (!$hasChanges) { echo "$headerFile\n"; $hasChanges = true; } echo "\t- $line"; } else if (!in_array($line, $lines)) { $functions[] = $line; } } preg_match($struct_pattern, $line, $matches); if (count($matches) == 2) { unset($header[$i]); $extern = $matches[1]; $externs[] = $extern; if (!$isMain) { if (!preg_match_all("/\b${extern}\b/", $body)) { if (!$hasChanges) { echo "$headerFile\n"; $hasChanges = true; } echo "\t- $line"; } else if (!in_array($line, $lines)) { $structs[] = $line; } } else if (!in_array($line, $lines)) { $structs[] = $line; } } preg_match($define_pattern, $line, $matches); if (count($matches) == 2) { unset($header[$i]); $extern = $matches[1]; $externs[] = $extern; if (strstr($body, "$extern") === FALSE) { if (!$hasChanges) { echo "$headerFile\n"; $hasChanges = true; } echo "\t- $line"; } else if (!in_array($line, $lines)) { $defines[] = $line; } } } $i++; } 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); $defines = alignDefines($defines); $header = updateExterns($header, $defines, $functions, $structs); if ($UPDATE_FILES) { file_put_contents($headerFile, $header); } } } function alignDefines($defines) { $newAligns = []; $largest = 0; $defineName; $defineValue; foreach ($defines as $define) { sscanf($define, "%*s %s", $defineName); $largest = max($largest, strlen($defineName) + 4); } foreach ($defines as $define) { sscanf($define, "%*s %s %[^\n]", $defineName, $defineValue); $newAligns[] = "#define " . str_pad($defineName, $largest, " ") . $defineValue . "\n"; } return $newAligns; } function recurseDir($dir) { if ($dir != "../src/json") { $files = array_diff(scandir($dir), array('..', '.')); foreach ($files as $file) { if (is_dir("$dir/$file")) { recurseDir("$dir/$file"); } else if (strstr($file, ".h") !== FALSE && $file != 'i18n.h') { cleanHeader("$dir/$file"); } } } } if (isset($argv[1])) { $UPDATE_FILES = ($argv[1] == "-commit"); } recurseDir("../src"); if (!$UPDATE_FILES) { echo "\nNo files updated. Use -commit to update headers\n"; } ?>