home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / htmlattr.c < prev    next >
C/C++ Source or Header  |  1998-09-08  |  6KB  |  223 lines

  1. /*
  2.  * htmlattr.c
  3.  *
  4.  * Routines for extracting HTML tag attributes.
  5.  *
  6.  *      Created: 5th December, 1997
  7.  * Version 2.00: 10th December, 1997
  8.  * Version 2.10: 8th September, 1998
  9.  *
  10.  * (C) 1997-1998 Nicholas Paul Sheppard
  11.  *
  12.  * This file is distributed under the GNU General Public License. See the
  13.  * file copying.txt for details.
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include <string.h>
  19. #include "general.h"
  20. #include "html.h"
  21.  
  22. /* internal function prototypes */
  23. char *HTMLAttrParse(char *, char *, int, char *, int);
  24. int HTMLAttrAlign(char *);
  25.  
  26.  
  27. void HTMLAttrTD(char *pszTag, HTML_TDATTR *ptd)
  28. /*
  29.  * Exract the attributes from a TD flag.
  30.  *
  31.  * char *pszTag        - the TD tag
  32.  * HTML_TDATTR *ptd    - structure to contain the attributes
  33.  */
  34. {
  35.     char szAttr[8], szValue[11];
  36.     char *pszCurrent;
  37.  
  38.     /* initialise attribute structure to defaults */
  39.     ptd->bNoWrap = 0;
  40.     ptd->iRowSpan = 1;
  41.     ptd->iColSpan = 1;
  42.     ptd->iAlign = HTML_ALIGN_LEFT;
  43.     ptd->iVAlign = HTML_ALIGN_TOP;
  44.  
  45.     pszCurrent = HTMLAttrFirst(pszTag, szAttr, sizeof(szAttr), szValue, sizeof(szValue));
  46.     while (pszCurrent != NULL) {
  47.         if (strcmpci(szAttr, "NOWRAP") == 0)
  48.             ptd->bNoWrap = 1;
  49.         else if (strcmpci(szAttr, "ROWSPAN") == 0)
  50.             ptd->iRowSpan = atoi(szValue);
  51.         else if (strcmpci(szAttr, "COLSPAN") == 0)
  52.             ptd->iColSpan = atoi(szValue);
  53.         else if (strcmpci(szAttr, "ALIGN") == 0)
  54.             ptd->iAlign = HTMLAttrAlign(szValue);
  55.         else if (strcmpci(szAttr, "VALIGN") == 0)
  56.             ptd->iVAlign = HTMLAttrAlign(szValue);
  57.         pszCurrent = HTMLAttrNext(pszCurrent, szAttr, sizeof(szAttr), szValue, sizeof(szValue));
  58.     }
  59. }
  60.  
  61.  
  62. char *HTMLAttrFirst(char *pszTag, char *pszAttr, int iMaxAttr, char *pszValue, int iMaxValue)
  63. /*
  64.  * Retrieve the first attribute string from a tag.
  65.  *
  66.  * char *pszTag        - the tag
  67.  * char *pszAttr    - string to receive attribute name
  68.  * int iMaxAttr        - maximum length of attribute name
  69.  * char *pszValue    - string to receive value
  70.  * int iMaxValue    - maximum value
  71.  *
  72.  * Returns        - pointer to the end of the attribute string
  73.  *              NULL if there are no attribtue strings
  74.  */
  75. {
  76.     char *pch;
  77.  
  78.     /* start after opening angle bracket */
  79.     pch = pszTag + 1;
  80.  
  81.     /* skip white space preceding tag identifier */
  82.     while (isspace(*pch))
  83.         pch++;
  84.  
  85.     /* skip tag identifier */
  86.     while (!isspace(*pch) && ((*pch) != '>'))
  87.         pch++;
  88.  
  89.     /* skip white space after tag identifier */
  90.     while (isspace(*pch))
  91.         pch++;
  92.  
  93.     if ((*pch) == '>')
  94.         /* no attributes */
  95.         return (NULL);
  96.     else
  97.         /* get the attribute string */
  98.         return (HTMLAttrParse(pch, pszAttr, iMaxAttr, pszValue, iMaxValue));
  99. }
  100.  
  101.  
  102. char *HTMLAttrNext(char *pszCursor, char *pszAttr, int iMaxAttr, char *pszValue, int iMaxValue)
  103. /*
  104.  * Retrieve the next attribute string from a tag.
  105.  *
  106.  * char *pszCursor    - pointer to where we're up to
  107.  * char *pszAttr    - string to receive attribute name
  108.  * int iMaxAttr        - maximum length of attribute name
  109.  * char *pszValue    - string to receive value
  110.  * int iMaxValue    - maximum value
  111.  *
  112.  * Returns        - pointer to the end of the attribute string
  113.  *              NULL if there are no more attribtue strings
  114.  */
  115. {
  116.     char *pch;
  117.  
  118.     /* skip white space up to next tag */
  119.     pch = pszCursor;
  120.     while (isspace(*pch))
  121.         pch++;
  122.  
  123.     if ((*pch) == '>')
  124.         /* no more attributes */
  125.         return (NULL);
  126.     else
  127.         /* get the attribute string */
  128.         return (HTMLAttrParse(pch, pszAttr, iMaxAttr, pszValue, iMaxValue));
  129. }
  130.  
  131.  
  132. char *HTMLAttrParse(char *pszCursor, char *pszAttr, int iMaxAttr, char *pszValue, int iMaxValue)
  133. /*
  134.  * Parse the attribute at cursor position.
  135.  *
  136.  * char *pszCursor    - pointer to where we're up to
  137.  * char *pszAttr    - string to receive attribute name
  138.  * int iMaxAttr        - maximum length of attribute name
  139.  * char *pszValue    - string to receive value
  140.  * int iMaxValue    - maximum length of value
  141.  *
  142.  * Returns        - pointer to end of attribute string
  143.  */
  144. {
  145.     char *pch1, *pch2;
  146.     int i;
  147.  
  148.     /* get attribute name */
  149.     i = 0;
  150.     pch1 = pszCursor;
  151.     pch2 = pszAttr;
  152.     while (((*pch1) != '=') && !isspace(*pch1) && ((*pch1) != '>')) {
  153.         if (i < (iMaxAttr - 1)) {
  154.             (*pch2) = (*pch1);
  155.             pch2++;
  156.         }
  157.         i++;
  158.         pch1++;
  159.     }
  160.     (*pch2) = '\0';
  161.  
  162.     pch2 = pszValue;
  163.     if ((*pch1) == '=') {
  164.         /* get value */
  165.         pch1++;
  166.         i = 0;
  167.         if ((*pch1) == '\"') {
  168.             pch1++;
  169.             while (((*pch1) != '\"') && ((*pch1) != '\0')) {
  170.                 if (i < (iMaxValue - 1)) {
  171.                     (*pch2) = (*pch1);
  172.                     pch2++;
  173.                 }
  174.                 i++;
  175.                 pch1++;
  176.             }
  177.             if ((*pch1) == '\"')
  178.                 pch1++;
  179.         } else {
  180.             while (((*pch1) != '>') && !isspace(*pch1) && ((*pch1) != '\0')) {
  181.                 if (i < (iMaxValue - 1)) {
  182.                     (*pch2) = (*pch1);
  183.                     pch2++;
  184.                 }
  185.                 i++;
  186.                 pch1++;
  187.             }
  188.         }
  189.     }
  190.     (*pch2) = '\0';
  191.  
  192.     return (pch1);
  193. }
  194.  
  195.  
  196. int HTMLAttrAlign(char *pszAlign)
  197. /*
  198.  * Return the number corresponding to the given alignment value.
  199.  *
  200.  * char *pszAlign    - the string name of an alignment
  201.  *
  202.  * Returns        - HTML_ALIGN_* as appropriate
  203.  */
  204. {
  205.     if (strcmpci(pszAlign, "LEFT") == 0)
  206.         return (HTML_ALIGN_LEFT);
  207.     if (strcmpci(pszAlign, "RIGHT") == 0)
  208.         return (HTML_ALIGN_RIGHT);
  209.     if (strcmpci(pszAlign, "TOP") == 0)
  210.         return (HTML_ALIGN_TOP);
  211.     if (strcmpci(pszAlign, "BOTTOM") == 0)
  212.         return (HTML_ALIGN_BOTTOM);
  213.     if (strcmpci(pszAlign, "MIDDLE") == 0)
  214.         return (HTML_ALIGN_MIDDLE);
  215.     if (strcmpci(pszAlign, "CENTER") == 0)
  216.         return (HTML_ALIGN_MIDDLE);
  217.     if (strcmpci(pszAlign, "CENTRE") == 0)
  218.         return (HTML_ALIGN_MIDDLE);
  219.  
  220.     /* oops! not a valid alignment */
  221.     return (HTML_ALIGN_INVALID);
  222. }
  223.