home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 4 / CD_Magazyn_EXEC_nr_4.iso / Recent / dev / c / GSys.lha / gsys / gmisc / GHTMLReader.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-16  |  3.6 KB  |  168 lines

  1.  
  2. /* Author Anders Kjeldsen */
  3.  
  4. #ifndef GHTMLREADER_CPP
  5. #define GHTMLREADER_CPP
  6.  
  7. #include "gmisc/GHTMLReader.h"
  8. #include "gmisc/GTextHandler.cpp"
  9.  
  10. GHTMLReader::GHTMLReader(GSTRPTR string)
  11. {
  12.     memset((void *)this, 0, sizeof (class GHTMLReader) );
  13.  
  14. //    GTextHandler(string);
  15.     if ( InitGTextHandler(string, "GHTMLReader") )
  16.     {
  17.         UCaseIsLCase = FALSE;
  18.  
  19.         InitTypes();
  20.         SetCharDef("<>=", GTEXT_BREAK, GTEXT_SET_OR);
  21.         SetCharDef("0123456789-/", GTEXT_LETTER, GTEXT_SET_OR);
  22.     }
  23. }
  24.  
  25. GHTMLReader::GHTMLReader(GSTRPTR filename, GWORD start, GWORD len)
  26. {
  27.     memset((void *)this, 0, sizeof (class GHTMLReader) );
  28. //    GTextHandler(string);
  29.     if ( InitGTextHandler(filename, start, len, "GHTMLReader") )
  30.     {
  31.         UCaseIsLCase = FALSE;
  32.  
  33.         InitTypes();
  34.         SetCharDef("<>=", GTEXT_BREAK, GTEXT_SET_OR);
  35.         SetCharDef("0123456789-/", GTEXT_LETTER, GTEXT_SET_OR);
  36.     }
  37. }
  38.  
  39. /*
  40.  
  41. ParseAttr(STRPTR name, STRPTR value)
  42. parses the attribute-name in the string name,
  43. and the attribute-value in the strinv value, 
  44. if there is no value, the string is immediately
  45. null-terminated.
  46.  
  47. */
  48.  
  49. BOOL GHTMLReader::ParseAttr(GSTRPTR name, GSTRPTR value)
  50. {
  51.     GSTRPTR old = TextCurrent;
  52.     SetCharDef('=', GTEXT_BREAK, GTEXT_SET_SET);
  53.     SetCharDef('"', GTEXT_USER1, GTEXT_SET_SET);
  54.  
  55. // name
  56.     if ( JumpNextReqType(GTEXT_SPACE | GTEXT_TAB | GTEXT_NEWLINE, GTEXT_NONE) )
  57.     {
  58.         if ( GetChar() == '>')
  59.         {
  60.             TextCurrent++;
  61.             return FALSE;    // no more attributes
  62.         }
  63.         else
  64.         {
  65.             CpyReqType(name, GTEXT_LETTER | GTEXT_NUMBER, GTEXT_ANY);
  66.             JumpNextReqType(GTEXT_LETTER | GTEXT_NUMBER, GTEXT_NONE);
  67.         }
  68. // value
  69.         if ( JumpNextReqType(GTEXT_SPACE | GTEXT_TAB | GTEXT_NEWLINE, GTEXT_NONE) )
  70.         {
  71.             if ( GetChar() == '=' )
  72.             {
  73.                 TextCurrent++;
  74.                 if ( JumpNextReqType(GTEXT_SPACE | GTEXT_TAB | GTEXT_NEWLINE, GTEXT_NONE) )
  75.                 {
  76.                     if ( GetChar() == '"' )
  77.                     {
  78.                         TextCurrent++;
  79.                         CpyReqType(value, GTEXT_USER1, GTEXT_NONE);
  80.                         TextCurrent++;
  81.                         return TRUE;
  82.                     }
  83.                     else if ( GetCharDef() == GTEXT_BREAK )
  84.                     {
  85.                         TextCurrent++;
  86. //                        WriteLog("Unexpected Break\n");
  87.                         return FALSE;
  88.                     }
  89.                     else
  90.                     {
  91.                         CpyReqType(value, GTEXT_SPACE | GTEXT_TAB | GTEXT_NEWLINE | GTEXT_BREAK, GTEXT_NONE);
  92.                         // GTEXT_LETTER | GTEXT_NUMBER
  93.                         return TRUE;
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     *value=0;
  99. //                    WriteLog("Unexpected end of file!\n");
  100.                     return FALSE;
  101.                 }
  102.             }
  103.             else
  104.             {
  105.                 *value=0;
  106. //                WriteLog("An attribute not on the form =x, and the char is %c\n", *TextCurrent);
  107.                 return FALSE;
  108.             }
  109.         }
  110.     }
  111.     else
  112.     {
  113. //        WriteLog("End of text or similar\n");
  114.         return NULL;
  115.     }
  116.  
  117.     SetCharDef('"', GTEXT_NODEF, GTEXT_SET_SET);
  118.     SetCharDef('=', GTEXT_NODEF, GTEXT_SET_SET);
  119.     return TRUE;
  120. }
  121.  
  122. BOOL GHTMLReader::ParseTag(GSTRPTR dest)
  123. {
  124.     SetCharDef('=', GTEXT_BREAK, GTEXT_SET_SET);
  125.     JumpChars(1);
  126.     if ( GetCharDef() != GTEXT_BREAK)
  127.     {
  128.         CpyReqType(dest, GTEXT_SPACE | GTEXT_TAB | GTEXT_NEWLINE | GTEXT_BREAK, GTEXT_NONE);
  129.         if ( JumpNextReqType(GTEXT_SPACE | GTEXT_TAB | GTEXT_NEWLINE | GTEXT_BREAK, GTEXT_ANY) )
  130.         {        
  131.             return TRUE;
  132.         }
  133.         else return FALSE;
  134.     }
  135.     else return FALSE;
  136. }
  137.  
  138. BOOL GHTMLReader::ParseUntilNextTag(GSTRPTR dest)
  139. {
  140.     SetCharDef('=', GTEXT_NODEF, GTEXT_SET_SET);
  141.     CpyReqType(dest, GTEXT_BREAK, GTEXT_NONE);
  142.     if ( JumpNextReqType(GTEXT_BREAK, GTEXT_ANY) ) return TRUE;
  143.     else return FALSE;
  144.  
  145. }
  146.  
  147. BOOL GHTMLReader::JumpNextTag()
  148. {
  149.     SetCharDef('=', GTEXT_NODEF, GTEXT_SET_SET);
  150.  
  151.     while (    JumpNextReqType(GTEXT_BREAK, GTEXT_ANY) )
  152.     {
  153.         if ( GetChar() == '<' )
  154.         {
  155.             return TRUE;
  156.         }
  157.         if ( GetChar() == '>' )
  158.         {
  159.             JumpChars(1);
  160. //            WriteLog("Unexpected closing tag found! This can happen if an attribute has only one ""\n");
  161.             return TRUE;
  162.         }
  163.         JumpChars(1);
  164.     }
  165.     return FALSE;
  166. }
  167.  
  168. #endif /* GHTMLREADER_CPP */