source.php

Denna kod är public domain. Om ni hittar fel eller vill ändra något i koden blir jag jätteglad om ni skickar dessa ändringar till jesper [at] fantasi [punkt] se.


<?php
  $title="K&auml;llkodsk&auml;llan";
  $currentFile=__FILE__;
  include("header.php");

  echo "<h1>K&auml;llkoder</h1>";

function langName($ext)
{
   switch (strtoupper($ext))
   {
      case "ADB": return "Ada";
      case "BAS": return "QBasic";
      case "E": return "Amiga&nbsp;E";
      case "JAVA": return "Java";
      case "JSCRIPT": return "Java&nbsp;script";
      case "PAS": return "Pascal";
      default: return strtoupper($ext);
   }
}

function noSpace($text)
{
   return str_replace("&nbsp;", "_", $text);
}

$sourceDir="source/";
$files = array();
$dir = opendir($sourceDir);

while ($file = readdir($dir))
{
   if (preg_match("/^[a-z0-9_-]+\.[a-z0-9]+$/", $file))
      array_push($files, $file);
}

natcasesort($files);

$lines = array();
$filenames = array();
$allLanguages = array();
$oldName = "";
while ($file = array_pop($files))
{
   $d = strpos($file, ".");
   $name = substr($file, 0, $d);
   $end = substr($file, $d + 1);

   array_push($allLanguages, langName($end));

   if ($_POST['key'] != NULL)
   {
      $text = html_entity_decode(file_get_contents($sourceDir.$file));
      if ($_POST['case'] == "on")
      {
         if (strpos($text, $_POST['key']) == 0)
            continue;
      }
      else
      {
         if (strpos(strtolower($text), strtolower($_POST['key'])) == 0)
            continue;
      }
   }

   if (strtolower($name) == strtolower($oldName) || $oldName == "")
   {
      if ($_POST[noSpace(langName($end))] == "on" || count($_POST) == 0)
         array_push($filenames, $file);
   }
   else
   {
      /* Print the stored files to a row in the table */
      $links = "";
      while ($lang = array_pop($filenames))
         $links = $links."<a href=\"sourceview.php?file=source/".
            $lang."\">".langName(substr($lang, strpos($lang, ".") + 1))."</a><br>";

      if ($links != "")
      {
         if (file_exists($sourceDir.strtolower($oldName)."_txt"))
            $desc = file_get_contents($sourceDir.strtolower($oldName)."_txt");
         else
            $desc = "-";

         array_push($lines, array($oldName, $links, $desc));
      }

      /* Handle the new file whos name did not match the old name. */
      if ($_POST[noSpace(langName($end))] == "on" || count($_POST) == 0)
         $filenames = array($file);
      else
         $filenames = array();
   }

   $oldName = $name;
}

$links = "";
while ($lang = array_pop($filenames))
   $links = $links."<a href=\"sourceview.php?file=source/".
            $lang."\">".langName(substr($lang, strpos($lang, ".") + 1))."</a><br>";

if ($links != "")
{
   if (file_exists($sourceDir.strtolower($oldName)."_txt"))
      $desc = file_get_contents($sourceDir.strtolower($oldName)."_txt");
   else
      $desc = "-";

   array_push($lines, array($oldName, $links, $desc));
}

$allLanguages = array_unique($allLanguages);
rsort($allLanguages);

echo "<form action=\"source.php\" method=\"post\">";
echo "<table class=\"ftable\"><tr>";

$n = 0;
while ($lang = array_pop($allLanguages))
{
   echo "<th class=\"small\"><input type=\"checkbox\" name=\"".
            noSpace($lang)."\"";
   if ($_POST[noSpace($lang)] == "on" || count($_POST) == 0)
      echo " checked";
   echo "> ".$lang."</th>";
   $n++;
}

echo "</tr><tr><th class=\"small\" colspan=\"".($n - 1)."\">Filter: ";
echo "<input type=\"text\" name=\"key\" size=\"60\" value=\"".$_POST['key']."\"> ";
echo " <input type=\"checkbox\" name=\"case\"";
if ($_POST['case'] == "on")
   echo " checked";
echo "> Case sensitive</th>";
echo "<th><img src=\"gfx/pixel.gif\" width=\"1\" height=\"24\" alt=\"\"><input type=\"submit\" value=\"Uppdatera\"></th></tr></table></form>";

?>
<p class="maincontent">
  V&auml;lj vilka spr&aring;k du &auml;r intresserad av ovan. Namnen
  p&aring; spr&aring;ken i kolumnen 'Spr&aring;k' &auml;r l&auml;nken
  till k&auml;llkoden. Du kan &auml;ven filtrera med nyckelord i
  k&auml;llkodsfilerna.
</p>

<p class="maincontent">
  Alla C-program nedan &auml;r skrivna i ren ANSI-C f&ouml;r att vara s&aring;
  plattformsoberoende som m&ouml;jligt. N&aring;gra av dem &auml;r &auml;ven
  skrivna f&ouml;r att passera testverktyget splint utan anm&auml;rkning.
  Detta g&ouml;r att de p&aring; m&aring;nga st&auml;llen &auml;r extra
  pedantiska med typkonverteringar, argument och returv&auml;rden.
</p>

<?php
ftable(-1, array("Namn", "Spr&aring;k", "Beskrivning"), array("l","l","l"));

while ($line = array_pop($lines))   
   ftline($line);

ftend();

include("footer.php");
?>