Fix rv2xml

It turns out that I needed to understand why my regex was failing in
order to figure out why the XML wasn't writing out. The XML writer was
being initialized improperly, wasn't having some settings set, some of
the regex was wrong, lines needed to be trimmed, and logic needed to be
backported for rom/disk handling.
This commit is contained in:
Matt Nadareski
2016-04-04 03:18:09 -07:00
parent 69987febe8
commit 2c4e0ba588

View File

@@ -47,26 +47,28 @@ function rv2xml ($file)
ini_set('max_execution_time', 0); // Set the execution time to infinite. This is a bad idea in production.
// Set the complex regex patterns
$headerPattern = "/(^.*?) \($/";
$romPattern = "/^\s+((?:rom)|(?:disk)) \( (name) \"(.*?)\" (?:(size) (.*?) )?(?:(crc) (.*?))?(?:(md5) (.*?) )?(?:(sha1) (.*?) )?\)/";
$itemPattern = "/^\s+(.*?) \"(.*?)\"/";
$endPattern = "/^\s*\)\s*$/";
$headerPattern = @"/(.*?) \($/m";
$itemPattern = @"/^(.*?) (.*)/";
$endPattern = @"/^\)\s*$/";
// Create the XMLWriter
$xmlw = new XMLWriter();
$xmlw = new XMLWriter;
$xmlw->openMemory();
$xmlw->setIndent(true);
$xmlw->setIndentString("\t");
$xmlw->startDocument();
$xmlw->startElement("datafile");
$block = false;
$parent = false;
for ($k = 0; k < sizeof($file); $k++)
for ($k = 0; $k < sizeof($file); $k++)
{
$line = $file[$k];
$line = trim($file[$k]);
// If the line is the header or a game
if (preg_match($headerPattern, $line, $matches) != 0)
if (preg_match($headerPattern, $line, $matches) !== 0)
{
if ($matches[1] == "clrmamepro" || matches[1] == "romvault")
if ($matches[1] == "clrmamepro" || $matches[1] == "romvault")
{
$xmlw->startElement("header");
$parent = true;
@@ -80,26 +82,78 @@ function rv2xml ($file)
}
// If the line is a rom or disk and we're in a block
elseif (preg_match($romPattern, $line, $matches) != 0 && $block)
elseif ((strpos(trim($line), "rom (") === 0 || strpos(trim($line), "disk (") === 0) && $block)
{
$xmlw->startElement($matches[1]);
$line = explode(" ", $line);
$xmlw->startElement($line[0]);
// Loop over all attributes and add them if possible
for ($i = 1; $i < sizeof($matches); $i++)
$quote = false; $attrib = ""; $val = "";
for ($i = 2; $i < sizeof($line); $i++)
{
if ($i + 2 < sizeof($matches))
// Get the number of quotes
preg_match_all(@"/\"/", $line[$i], $quotes);
$quotes = sizeof($quotes[0]);
// Even number of quotes, not in a quote, not in attribute
if ($quotes % 2 == 0 && !$quote && $attrib == "")
{
$xmlw->writeAttribute($matches[$i + 1], $matches[$i + 2]);
$i++;
$attrib = str_replace("\"", "", $line[$i]);
}
// Even number of quotes, not in a quote, in attribute
elseif ($quotes % 2 == 0 && !$quote && $attrib != "")
{
$xmlw->writeAttribute($attrib, str_replace("\"", "", $line[$i]));
$attrib = "";
}
// Even number of quotes, in a quote, not in attribute
elseif ($quotes % 2 == 0 && $quote && $attrib == "")
{
// Attributes can't have quoted names
}
// Even number of quotes, in a quote, in attribute
elseif ($quotes % 2 == 0 && $quote && $attrib != "")
{
$val .= " ".$line[$i];
}
// Odd number of quotes, not in a quote, not in attribute
elseif ($quotes % 2 == 1 && !$quote && $attrib == "")
{
// Attributes can't have quoted names
}
// Odd number of quotes, not in a quote, in attribute
elseif ($quotes % 2 == 1 && !$quote && $attrib != "")
{
$val = str_replace("\"", "", $line[$i]);
$quote = true;
}
// Odd number of quotes, in a quote, not in attribute
elseif ($quotes % 2 == 1 && $quote && $attrib == "")
{
$quote = false;
}
// Odd number of quotes, in a quote, in attribute
elseif ($quotes % 2 == 1 && $quote && $attrib != "")
{
$val .= " ".str_replace("\"", "", $line[$i]);
$xmlw->writeAttribute($attrib, $val);
$quote = false;
$attrib = "";
$val = "";
}
}
$xmlw->endElement();
}
// If the line is anything but a rom or disk and we're in a block
elseif (preg_match($itemPattern, $line, $matches) != 0 && $block)
elseif (preg_match($itemPattern, $line, $matches) !== 0 && $block)
{
if ($matches[1] == "name" && $header)
$matches[2] = str_replace("\"", "", $matches[2]);
if ($matches[1] == "name" && $parent)
{
$xmlw->writeAttribute($matches[1], $matches[2]);
$xmlw->writeElement("description", $matches[2]);
@@ -111,16 +165,22 @@ function rv2xml ($file)
}
// If we find an end bracket that's not associated with anything else, the block is done
elseif (preg_match($endPattern, $line) != 0 && $block)
elseif (preg_match($endPattern, $line) !== 0 && $block)
{
$block = false;
$xmlw->endElement();
}
// Somehow didn't match anything
else
{
//echo "<pre>".$line."</pre><br/>\n";
}
}
$xmlw->endElement();
$xmlw->endDocument();
echo $xmlw->outputMemory();
//return $xmlw->outputMemory();
return $xmlw->outputMemory();
}
?>