File Info

Rev. 173
Size 1,995 bytes
Time 2020-12-19 21:38:41
Author mateuszviste
Log Message

quick hack so titles with high-ascii chars are displayed acceptably well

Content

<html>
<head>
  <title>AMB library</title>
  <meta name="author" content="Mateusz Viste">
  <meta name="robots" content="index, follow">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="style.css">
</head>

<body>

<h1>AMB library</h1>
<p class="copyr">a selection of free/gratis AMB books</p>

<?php

function getamafile($ambfname, $amafname) {
  if (! is_file($ambfname)) return(FALSE);
  $fd = fopen($ambfname, "rb");
  if ($fd === FALSE) return(FALSE);
  // read header (AMB1)
  if (fread($fd, 4) !== 'AMB1') return(FALSE);
  // read number of ama files inside
  $fcount = reset(unpack('v', fread($fd, 2)));
  if ($fcount === FALSE) return(FALSE);
  // read index until AMA file is found
  $offset = 0;
  $amalen = 0;
  $amafile = '';
  for ($i = 0; $i < $fcount; $i++) {
    $amafile = rtrim(fread($fd, 12), "\0");
    $offset = reset(unpack('V', fread($fd, 4)));
    $amalen = reset(unpack('v', fread($fd, 2)));
    $bsum = reset(unpack('v', fread($fd, 2)));
    if (strcasecmp($amafile, $amafname) == 0) break;
  }
  if ($i >= $fcount) return(FALSE); // not found
  // jump to offset and read file
  fseek($fd, $offset);
  $result = fread($fd, $amalen);
  fclose($fd);
  return($result);
}

function codepageprocess($s) {
  $res = '';
  $ss = str_split($s);
  foreach ($ss as $c) {
    if (ord($c) < 128) {
      $res .= $c;
    } else if (ord($c) == 158) {
      $res .= 'ś';
    } else {
      $res .= '[' . ord($c) . ']';
    }
  }
  return($res);
}


$flist = glob("lib/*.amb");

?>

<table>

<?php
foreach ($flist as $f) {
  $title = trim(getamafile($f, 'title'));
  if ($title === FALSE) $title = basename($f, '.amb');
  $title = codepageprocess($title);
  echo "<tr><td>{$title}</td><td style=\"text-align: right;\"><a href=\"{$f}\">download</a> or <a href=\"phpamb.php?fname={$f}\">read online</a></td></tr>\n";
}

?>

</table>

<p>&lt;&lt; <a href="..">Back to the home page</a></p>

</body>
</html>
Show on old repository browser