home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Editor / GED403R.LZX / Install / Install.run / GOLDEDDATA / developer / examples / scanner / source / html.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-05  |  5.6 KB  |  164 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   COPYIGHT
  4.  
  5.   ©1995  Dietmar  Eilert  (e-mail:  DIETMAR@TOMATE.TNG.OCHE.DE).  All  Rights
  6.   Reserved.  Code  may not be reused/reproduced without written permission of
  7.   the author.
  8.  
  9.   Dietmar Eilert
  10.   Mies-v-d-Rohe-Str.31, 52074 Aachen, Germany
  11.   E-Mail: DIETMAR@TOMATE.TNG.OCHE.DE
  12.   Tel: +49-(0)241-81665
  13.        +49-(0)2525-7776
  14.   Fax: +49-(0)241-81665
  15.  
  16.   Example: scan handler looking for HTML anchors. Known bug: can not handle
  17.   more than one anchor per line due to the scanner interface design.
  18.  
  19.   Scan handlers are plain functions (LoadSeg'ed by GED): no standard C startup
  20.   code, no library calls. String constants have to be put into the code chunk.
  21.   That's why below a string constant is (ab)used as a buffer instead of using
  22.   a static array.
  23.  
  24.   DICE-C:
  25.  
  26.   dcc html.c -// -l0 -md -ms2 -mRR -o ram:HTML
  27.  
  28.   ------------------------------------------------------------------------------
  29. */
  30.  
  31. #include <exec/types.h>
  32.  
  33. #define UPPER(a) ((a) & 95)
  34.  
  35. ULONG
  36. ScanHandlerHTML(__D0 ULONG len, __A0 char **text, __A1 ULONG *line)
  37. {
  38.     const char *version = "$VER: HTML 1.0 (5.2.95)";
  39.  
  40.     if (len >= 12) {                                  // minimum strings: <A NAME="X">
  41.                                                       //                  <A HREF="X">
  42.         UBYTE *from, *start, *last;
  43.  
  44.         UBYTE *buffer =
  45.  
  46.             "                                                                 "
  47.             "                                                                 "
  48.             "                                                                 "
  49.             "                                                                 ";
  50.  
  51.         from = *text;
  52.         last = *text + len - 12;
  53.  
  54.         while (from <= last) {
  55.  
  56.             if (*from == '<') {
  57.  
  58.                 // anchor detected ?
  59.  
  60.                 if ((UPPER(from[1]) == 'A') & (from[2] == ' ')) {
  61.  
  62.                     // 1st try: look for <A HREF="..."> or <A NAME="...">
  63.  
  64.                     for (start = from + 3; start < last; ++start) {
  65.  
  66.                         if (UPPER(*start) == 'H') {
  67.  
  68.                             if (UPPER(start[1]) == 'R') {
  69.  
  70.                                 if (UPPER(start[2]) == 'E') {
  71.  
  72.                                     if (UPPER(start[3]) == 'F') {
  73.  
  74.                                         for (start += 4; start < last; ++start) {
  75.  
  76.                                             // look for beginning of link string
  77.  
  78.                                             if (*start == 34) {
  79.  
  80.                                                 UBYTE *result = buffer;
  81.  
  82.                                                 *result++ = 'H';
  83.                                                 *result++ = 'R';
  84.                                                 *result++ = 'E';
  85.                                                 *result++ = 'F';
  86.                                                 *result++ = ' ';
  87.  
  88.                                                 for (len = 5, ++start; start < last; ++len) {
  89.  
  90.                                                     if ((*start == 34) || (len == 255)) {
  91.  
  92.                                                         *text = buffer;
  93.  
  94.                                                         return(len);
  95.                                                     }
  96.                                                     else
  97.                                                         *result++ = *start++;
  98.                                                 }
  99.  
  100.                                                 return(FALSE);
  101.                                             }
  102.                                         }
  103.                                     }
  104.                                 }
  105.                             }
  106.                         }
  107.  
  108.                         if (UPPER(*start) == 'N') {
  109.  
  110.                             if (UPPER(start[1]) == 'A') {
  111.  
  112.                                 if (UPPER(start[2]) == 'M') {
  113.  
  114.                                     if (UPPER(start[3]) == 'E') {
  115.  
  116.                                         for (start += 4; start < last; ++start) {
  117.  
  118.                                             // look for beginning of link string
  119.  
  120.                                             if (*start == 34) {
  121.  
  122.                                                 UBYTE *result = buffer;
  123.  
  124.                                                 *result++ = 'N';
  125.                                                 *result++ = 'A';
  126.                                                 *result++ = 'M';
  127.                                                 *result++ = 'E';
  128.                                                 *result++ = ' ';
  129.  
  130.                                                 for (len = 5, ++start; start < last; ++len) {
  131.  
  132.                                                     if ((*start == 34) || (len == 255)) {
  133.  
  134.                                                         *text = buffer;
  135.  
  136.                                                         return(len);
  137.                                                     }
  138.                                                     else
  139.                                                         *result++ = *start++;
  140.                                                 }
  141.  
  142.                                                 return(FALSE);
  143.                                             }
  144.                                         }
  145.                                     }
  146.                                 }
  147.                             }
  148.                         }
  149.                     }
  150.  
  151.                     break;
  152.                 }
  153.                 else
  154.                     ++from;
  155.             }
  156.             else
  157.                 ++from;
  158.         }
  159.     }
  160.  
  161.     return(FALSE);
  162. }
  163.  
  164.