htdocs: Allow XML output in online demo

This commit is contained in:
Tim Gerundt 2012-09-18 18:51:47 +02:00
parent 9a7d09086e
commit a5bd41dd1c
3 changed files with 69 additions and 36 deletions

View File

@ -40,7 +40,7 @@
<div class="wrap"> <div class="wrap">
<h2>Online Demo</h2> <h2>Online Demo</h2>
<form action="/demo/report/" name="f" onsubmit="return checkCodeLength();" method="post" target="_blank"> <form action="/demo/report/" name="f" onsubmit="return checkCodeLength();" method="post" target="_blank">
<p><label for="code">Enter code:</label><br /> <p><label for="code">Enter code:</label> <i class="maxChars">(max 1024 characters)</i><br />
<textarea id="code" name="code" rows="20" cols="80"> <textarea id="code" name="code" rows="20" cols="80">
void f() void f()
{ {
@ -48,7 +48,8 @@ void f()
*p = 0; *p = 0;
} }
</textarea><br /> </textarea><br />
<input type="submit" value="Check" /> (max 1024 characters)</p> <input type="submit" value="Check" />
<label><input type="checkbox" name="xml" value="1" />XML output</label></p>
</form> </form>
<h3>Examples</h3> <h3>Examples</h3>
<p>This code can be copy and pasted in the edit box above.</p> <p>This code can be copy and pasted in the edit box above.</p>

View File

@ -1,3 +1,62 @@
<?php
$isCodePosted = isset($_POST['code']) && !empty($_POST['code']);
$isXmlOutput = isset($_POST['xml']) && $_POST['xml'] == '1';
/**
* ...
* @param string $code Source code
* @return string Output lines
*/
function get_democlient_output($code) {
$postdata = http_build_query(
array(
'code' => $code
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
return @file_get_contents('http://cppcheck.sourceforge.net/cgi-bin/democlient.cgi', false, $context);
}
function cut_string($string, $length = 1024) {
if (strlen($string) > $length) {
return substr($string, 0, $length);
}
return $string;
}
//--------------------------------------------------------------------------------
// XML output...
//--------------------------------------------------------------------------------
if ($isXmlOutput) { //if XML output...
header('Content-Type: text/xml');
if (!$isCodePosted) { //if NO code posted...
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results></results>\n";
exit;
}
$output = get_democlient_output(cut_string($_POST['code']));
if ($output === false) { //if NO demo client output...
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results></results>\n";
exit;
}
echo $output;
exit;
}
//--------------------------------------------------------------------------------
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head> <head>
@ -39,33 +98,6 @@
<div class="wrap"> <div class="wrap">
<h2>Online Demo Report</h2> <h2>Online Demo Report</h2>
<?php <?php
$isCodePosted = isset($_POST['code']) && !empty($_POST['code']);
/**
* ...
* @param string $code Source code
* @return string Output lines
*/
function get_democlient_output($code) {
$postdata = http_build_query(
array(
'code' => $code
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
return @file_get_contents('http://cppcheck.sourceforge.net/cgi-bin/democlient.cgi', false, $context);
}
/** /**
* ... * ...
* @param string $output Output lines * @param string $output Output lines
@ -90,13 +122,6 @@
} }
} }
function cut_string($string, $length = 1024) {
if (strlen($string) > $length) {
return substr($string, 0, $length);
}
return $string;
}
if ($isCodePosted) { //if code posted... if ($isCodePosted) { //if code posted...
include_once '../../site/geshi/geshi.php'; include_once '../../site/geshi/geshi.php';

View File

@ -182,6 +182,13 @@ pre.cmd {
text-align: center; text-align: center;
} }
/* Max characters */
.maxChars {
margin-left: 2em;
color: #888;
font-size: smaller;
}
/* Printing */ /* Printing */
@media print { @media print {
#header { color: black; border-bottom: 1px solid black; } #header { color: black; border-bottom: 1px solid black; }