improve xmacro exporter

pull/26/head
Ondřej Hruška 6 years ago
parent 8f76ce3569
commit 7746d85e5f
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 6
      app/Http/Controllers/TableController.php
  2. 21
      app/Tables/CStructArrayExporter.php
  3. 84
      app/Tables/CXMacroExporter.php
  4. 6
      resources/views/table/_panel-export.blade.php

@ -9,6 +9,7 @@ use App\Models\User;
use App\Tables\Column;
use App\Tables\CStructArrayExporter;
use App\Tables\CsvExporter;
use App\Tables\CXMacroExporter;
use App\Tables\JsonExporter;
use App\Tables\PhpExporter;
use Illuminate\Http\Request;
@ -289,6 +290,11 @@ class TableController extends Controller
$exporter = new CStructArrayExporter($tableModel);
break;
case 'xmacro':
$noq = explode(',', $request->input('noq',''));
$exporter = (new CXMacroExporter($tableModel))->noQuotesAround($noq);
break;
case 'php':
$exporter = new PhpExporter($tableModel);
break;

@ -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,12 +79,23 @@ class CStructArrayExporter extends BaseExporter
}
echo " { ";
$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 ",";
}
@ -106,7 +117,11 @@ class CStructArrayExporter extends BaseExporter
// 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':
@ -119,9 +134,5 @@ class CStructArrayExporter extends BaseExporter
break;
}
}
echo " }";
}
echo "\n};\n";
}
}

@ -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";
}
}

@ -8,6 +8,7 @@
'csv' => 'CSV table',
'csv-tab' => 'CSV table, tabbed',
'c' => 'C structs array',
'xmacro' => 'C X Macro',
] as $type => $title)
<li><a target="_blank" href="{{ route('table.export', [
@ -23,3 +24,8 @@
@endforeach
</ul>
<p>
Note: In the X Macro option, add <code>&noq=foo,bar</code> to the URL
to strip quotes from specified columns
</p>

Loading…
Cancel
Save