improve xmacro exporter
This commit is contained in:
@@ -22,7 +22,7 @@ class CStructArrayExporter extends BaseExporter
|
||||
return 'c';
|
||||
}
|
||||
|
||||
private function cify($name)
|
||||
protected function cify($name)
|
||||
{
|
||||
$name = preg_replace('/[^a-z0-9_]/i', '_', $name);
|
||||
if (ctype_digit($name[0])) {
|
||||
@@ -79,49 +79,60 @@ class CStructArrayExporter extends BaseExporter
|
||||
}
|
||||
|
||||
echo " { ";
|
||||
|
||||
$firstf = true;
|
||||
foreach ($fields as $field) {
|
||||
if ($firstf) {
|
||||
$firstf = false;
|
||||
} else {
|
||||
echo ", ";
|
||||
}
|
||||
|
||||
$val = 0;
|
||||
switch ($field->type) {
|
||||
case 'string':
|
||||
$val = "";
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
$val = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($row->{$field->name})) {
|
||||
$val = $row->{$field->name};
|
||||
}
|
||||
|
||||
// export to C format
|
||||
switch ($field->type) {
|
||||
case 'string':
|
||||
echo '"' . addcslashes($val, "\0..\37!@\@\177..\377") . '"';
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
echo (int)$val;
|
||||
break;
|
||||
|
||||
case 'int':
|
||||
case 'float':
|
||||
echo $val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->dumpRow($fields, $row);
|
||||
echo " }";
|
||||
}
|
||||
|
||||
echo "\n};\n";
|
||||
}
|
||||
|
||||
protected function dumpRow($fields, $row, $noQuoteCols=[], $spaces = true)
|
||||
{
|
||||
$firstf = true;
|
||||
foreach ($fields as $field) {
|
||||
if ($firstf) {
|
||||
$firstf = false;
|
||||
} else {
|
||||
if ($spaces)
|
||||
echo ", ";
|
||||
else
|
||||
echo ",";
|
||||
}
|
||||
|
||||
$val = 0;
|
||||
switch ($field->type) {
|
||||
case 'string':
|
||||
$val = "";
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
$val = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($row->{$field->name})) {
|
||||
$val = $row->{$field->name};
|
||||
}
|
||||
|
||||
// export to C format
|
||||
switch ($field->type) {
|
||||
case 'string':
|
||||
if (in_array($field->name, $noQuoteCols)) {
|
||||
echo $val;
|
||||
} else {
|
||||
echo '"' . addcslashes($val, "\0..\37!@\@\177..\377") . '"';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
echo (int)$val;
|
||||
break;
|
||||
|
||||
case 'int':
|
||||
case 'float':
|
||||
echo $val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Tables;
|
||||
|
||||
|
||||
class CXMacroExporter extends CStructArrayExporter
|
||||
{
|
||||
private $noQuoteCols = true;
|
||||
|
||||
public function noQuotesAround($cols)
|
||||
{
|
||||
$this->noQuoteCols = $cols;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string - mime type for the downloaded file
|
||||
*/
|
||||
protected function getMimeType()
|
||||
{
|
||||
return $this->wantDownload ? 'text/x-chdr' : 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string - file extension for the downloaded file
|
||||
*/
|
||||
protected function getFileExtension()
|
||||
{
|
||||
return 'h';
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the document to stdout ('php://output')
|
||||
*/
|
||||
protected function writeDocument()
|
||||
{
|
||||
$table = $this->table;
|
||||
|
||||
$any_bool = false;
|
||||
$colnames = [];
|
||||
$fields = array_map(function (Column $c) use (&$any_bool, &$colnames) {
|
||||
if ($c->type == 'bool') $any_bool = true;
|
||||
$cname = $this->cify($c->name);
|
||||
$colnames[] = $cname;
|
||||
return (object)['name' => $c->name, 'type' => $c->type, 'cname' => $cname];
|
||||
}, $this->columns);
|
||||
|
||||
$ctablename = 'X_' .strtoupper($this->cify($table->name));
|
||||
|
||||
echo "#ifndef {$ctablename}_H\n".
|
||||
"#define {$ctablename}_H\n\n";
|
||||
|
||||
echo "/*\n" . $this->getHeaderComment(' ') . "\n";
|
||||
|
||||
echo
|
||||
" Example X macro processing the table: \n".
|
||||
" \n";
|
||||
echo " #undef X\n";
|
||||
echo " #define X(".implode(', ', $colnames) .") \\\n".
|
||||
" ...do something with the args\n".
|
||||
" \n".
|
||||
" Then run it by putting $ctablename somewhere in your C or H file.\n";
|
||||
echo "*/\n\n";
|
||||
|
||||
// Now the table itself
|
||||
echo "#define ".$ctablename." \\\n";
|
||||
|
||||
$first = true;
|
||||
foreach ($this->iterateRows() as $row) {
|
||||
if ($first) {
|
||||
$first = false;
|
||||
} else {
|
||||
echo " \\\n";
|
||||
}
|
||||
|
||||
echo " X(";
|
||||
$this->dumpRow($fields, $row, $this->noQuoteCols, false);
|
||||
echo ")";
|
||||
}
|
||||
|
||||
echo "\n\n#endif /* {$ctablename}_H */\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user