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

  1.  
  2. /* Author Anders Kjeldsen */
  3.  
  4. #ifndef GTEXTHANDLER_CPP
  5. #define GTEXTHANDLER_CPP
  6.  
  7. #include "gmisc/GTextHandler.h"
  8. #include "gsystem/GBuffer.cpp"
  9.  
  10.  
  11. GTextHandler::GTextHandler(GSTRPTR string)
  12. {
  13.     memset((GAPTR)this, 0, sizeof(GTextHandler));
  14.     int len;
  15.     if (string)
  16.     {
  17.         len = strlen(string);
  18.  
  19.         if ( InitGBuffer(len+1, "GTextHandler") )    
  20.         {
  21.             strcpy((GSTRPTR)Buffer, string);
  22.             
  23.             UCaseIsLCase = FALSE;
  24.             TextStart = (GSTRPTR)Buffer;
  25.             TextCurrent = (GSTRPTR)Buffer;
  26.         }
  27.         else
  28.         {
  29. //            WriteLog("GTextHandler(string): Was not able to allocate string-buffer\n");
  30.             TextStart = NULL;
  31.             TextCurrent = NULL;            
  32.         }
  33.     }
  34.     else
  35.     {
  36. //        WriteLog("GTextHandler(string): string == NULL\n");
  37.         TextStart = NULL;
  38.         TextCurrent = NULL;    
  39.     }
  40. }
  41.  
  42. GTextHandler::GTextHandler(GSTRPTR filename, GUWORD start, GUWORD len)
  43. {
  44.     memset((GAPTR)this, 0, sizeof(GTextHandler));
  45. //    if (Buffer) WriteLog("Buffer is something");
  46.     if ( InitGBuffer(filename, start, len, "GTextHandler") )    
  47.     {
  48.         UCaseIsLCase = FALSE;
  49.         TextStart = (GSTRPTR)Buffer;
  50.         TextCurrent = (GSTRPTR)Buffer;
  51.     }
  52.     else
  53.     {
  54. //        WriteLog("GTextHandler(string): Was not able to allocate string-buffer\n");
  55.         TextStart = NULL;
  56.         TextCurrent = NULL;    
  57.     }
  58. }
  59.  
  60. GTextHandler::~GTextHandler()
  61. {
  62. }
  63.  
  64. BOOL GTextHandler::RefreshTextPtr()
  65. {
  66.         TextStart = (GSTRPTR)Buffer;
  67.         TextCurrent = (GSTRPTR)Buffer;
  68.         return TRUE;
  69. };
  70.  
  71. BOOL GTextHandler::InitGTextHandler(GSTRPTR filename, GUWORD start, GUWORD len, GSTRPTR objtype)
  72. {
  73.     if ( InitGBuffer(filename, start, len, objtype) )
  74.     {
  75.         UCaseIsLCase = FALSE;
  76.         TextStart = (GSTRPTR)Buffer;
  77.         TextCurrent = (GSTRPTR)Buffer;
  78.         return TRUE;
  79.     }
  80.     else
  81.     {
  82. //        WriteLog("InitGTextHandler(string): Was not able to allocate string-buffer\n");
  83.         TextStart = NULL;
  84.         TextCurrent = NULL;
  85.         return FALSE;
  86.     }
  87. }
  88.  
  89. BOOL GTextHandler::InitGTextHandler(GSTRPTR string, GSTRPTR objtype)
  90. {
  91.     int len;
  92.     if (string)
  93.     {
  94.         len = strlen(string);    
  95.         if ( InitGBuffer(len+16, objtype) )
  96.         {
  97.             strcpy((GSTRPTR)Buffer, string);
  98.             
  99.             UCaseIsLCase = FALSE;
  100.             TextStart = (GSTRPTR)Buffer;
  101.             TextCurrent = (GSTRPTR)Buffer;
  102.             return TRUE;
  103.         }
  104.         else
  105.         {
  106. //            WriteLog("InitGTextHandler(string): Was not able to allocate string-buffer\n");
  107.             TextStart = NULL;
  108.             TextCurrent = NULL;
  109.             return FALSE;
  110.         }
  111.     }
  112.     else
  113.     {
  114. //        WriteLog("InitGTextHandler(string): string == NULL\n");
  115.         TextStart = NULL;
  116.         TextCurrent = NULL;
  117.         return FALSE;
  118.     }
  119. }
  120.  
  121. void GTextHandler::InitTypes()        // Should be used (Sets 0x20 as space etc.);
  122. {
  123.     CharDefs[0] = GTEXT_EOT | GTEXT_NEWLINE | GTEXT_SPACE | GTEXT_TAB | GTEXT_BREAK;
  124.     CharDefs[0x20] = GTEXT_SPACE;
  125.     CharDefs[0xa] = GTEXT_NEWLINE;
  126.     CharDefs[0x9] = GTEXT_TAB;
  127.  
  128.     GCharArea tempgca;
  129.  
  130.     tempgca.From = 'a';
  131.     tempgca.To = 'z';
  132.     SetCharDef(&tempgca, GTEXT_LETTER, GTEXT_SET_SET);
  133.  
  134.     tempgca.From = 'A';
  135.     tempgca.To = 'Z';
  136.     SetCharDef(&tempgca, GTEXT_LETTER, GTEXT_SET_SET);
  137.  
  138.     tempgca.From = '0';
  139.     tempgca.To = '9';
  140.     SetCharDef(&tempgca, GTEXT_NUMBER, GTEXT_SET_SET);
  141. }
  142.  
  143. void GTextHandler::ClearTypes()        // Clears 
  144. {
  145.     for (int i=0; i<256; i++)
  146.     {
  147.         CharDefs[i] = 0;
  148.     }
  149. }
  150.  
  151. void GTextHandler::SetUCaseIsLCase(BOOL b)
  152. {
  153.     UCaseIsLCase = b;
  154. }
  155.  
  156. BOOL GTextHandler::GetUCaseIsLCase()
  157. {
  158.     return UCaseIsLCase;
  159. }
  160.  
  161. void GTextHandler::SetCharDef(GSTRPTR str, GUSHORT flags, GUSHORT setmode)
  162. {
  163.     if (str)
  164.     {
  165.         if ( setmode == GTEXT_SET_SET )
  166.         {
  167.             while(*str)
  168.             {
  169.                 CharDefs[*str] = flags;
  170.                 str++;
  171.             }
  172.         }
  173.         else if ( setmode == GTEXT_SET_OR )
  174.         {
  175.             while(*str)
  176.             {
  177.                 CharDefs[*str] |= flags;
  178.                 str++;
  179.             }
  180.         }
  181.         else if ( setmode == GTEXT_SET_CLR )
  182.         {
  183.             flags = flags^0xffff;
  184.             while(*str)
  185.             {
  186.                 CharDefs[*str] &= flags;
  187.                 str++;
  188.             }
  189.         }
  190.     }
  191. }
  192.  
  193. void GTextHandler::SetCharDef(char x, GUSHORT flags, GUSHORT setmode)
  194. {
  195.     if ( setmode == GTEXT_SET_SET )    CharDefs[x] = flags;
  196.     else if ( setmode == GTEXT_SET_OR ) CharDefs[x] |= flags;
  197.     else if ( setmode == GTEXT_SET_CLR ) CharDefs[x] &= flags^0xffff;
  198. }
  199.  
  200. void GTextHandler::SetCharDef(GCharArea *gchararea, GUSHORT flags, GUSHORT setmode)
  201. {
  202.     if (gchararea)
  203.     {
  204.         if (gchararea->From && gchararea->To )
  205.         {
  206.             GUBYTE s,e;
  207.             s = (GUBYTE) gchararea->From;
  208.             e = (GUBYTE) gchararea->To;
  209.             if ( setmode == GTEXT_SET_SET )
  210.             {
  211.                 for (;s<=e;s++)
  212.                 {
  213.                     CharDefs[s] = flags;
  214.                 }
  215.                 gchararea++;
  216.             }
  217.             else if ( setmode == GTEXT_SET_OR )
  218.             {
  219.                 for (;s<=e;s++)
  220.                 {
  221.                     CharDefs[s] |= flags;
  222.                 }
  223.                 gchararea++;
  224.             }
  225.             else if ( setmode == GTEXT_SET_CLR )
  226.             {
  227.                 flags = flags ^ 0xffff;
  228.                 for (;s<=e;s++)
  229.                 {
  230.                     CharDefs[s] &= flags;
  231.                 }
  232.                 gchararea++;
  233.             }
  234.         }
  235.     }
  236. }
  237.  
  238. GUSHORT GTextHandler::GetCharDef()
  239. {
  240.     return CharDefs[ *TextCurrent ];
  241. }
  242.  
  243. GUBYTE GTextHandler::GetChar()
  244. {
  245.     return *TextCurrent;
  246.  
  247. }
  248.  
  249. BOOL GTextHandler::CmpChars(char a, char b)
  250. {
  251.     if ( UCaseIsLCase ) {
  252.         if ( ('A' <= a) && (a <= 'Z') ) a+=32;
  253.         if ( ('A' <= b) && (b <= 'Z') ) b+=32;
  254.     }
  255.     if (a==b) return TRUE;
  256.     else return FALSE;
  257. }
  258.  
  259. GWORD GTextHandler::CpyUntilNextIncident(GSTRPTR dest, GSTRPTR incidents)
  260. {
  261.     GUWORD index = 0;
  262.     while ( (!CmpIncidents(incidents)) && (*TextCurrent != 0) )
  263.     {
  264.         *dest++ = *TextCurrent++;
  265.         index++;
  266.     }
  267.     *dest = 0;
  268.     return index;
  269. }
  270.  
  271. GWORD GTextHandler::CpyReqType(GSTRPTR dest, GUSHORT flags, GUSHORT demand)
  272. {
  273.     if ( (CharDefs[ *TextCurrent ] & GTEXT_EOT) )
  274.     {
  275.         *dest=0;
  276.         return NULL;
  277.     }
  278.  
  279.     GSTRPTR old = dest;
  280.  
  281.     switch (demand)
  282.     {
  283.         case GTEXT_EQ:
  284.             while ( (CharDefs[*TextCurrent] == flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  285.             {
  286. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  287.                 *dest++ = *TextCurrent++;
  288.             }
  289.         break;
  290.         case GTEXT_ALL:
  291.             while ( ((CharDefs[*TextCurrent] & flags) == flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT) )
  292.             {
  293. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  294.                 *dest++ = *TextCurrent++;
  295.             }
  296.         break;
  297.  
  298.         case GTEXT_ANY:
  299.             while ( (CharDefs[*TextCurrent] & flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  300.             {
  301. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  302.                 *dest++ = *TextCurrent++;
  303.             }
  304.         break;
  305.  
  306.         case GTEXT_NOTEQ:
  307.             while ( (CharDefs[*TextCurrent] != flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  308.             {
  309. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  310.                 *dest++ = *TextCurrent++;
  311.             }
  312.         break;
  313.  
  314.         case GTEXT_NOTALL:
  315.             while ( ((CharDefs[*TextCurrent] & flags) != flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  316.             {
  317. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  318.                 *dest++ = *TextCurrent++;
  319.             }
  320.         break;
  321.  
  322.         case GTEXT_NONE:
  323.             while ( (!(CharDefs[*TextCurrent] & flags)) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  324.             {
  325. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  326.                 *dest++ = *TextCurrent++;
  327.             }
  328.         break;
  329.                 if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  330.         return NULL;
  331.     }
  332.     *dest = 0;
  333.     if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  334.     return (GUWORD)dest - (GUWORD)old;
  335. }
  336.  
  337. BOOL GTextHandler::JumpChars(GWORD x)    // no upper limit
  338. {
  339.     TextCurrent+=x;
  340.     if (TextCurrent > TextStart)
  341.     {
  342.         Jumped = x;
  343.         return TRUE;
  344.     }
  345.     Jumped = 0;
  346.     return FALSE;
  347. }
  348.  
  349. BOOL GTextHandler::JumpLines(GWORD x)        // x=0 return to start of line
  350. {
  351.     GSTRPTR old = TextCurrent;
  352.     if (x>0)
  353.     {
  354.         for (int l=0; l<x; l++)
  355.         {
  356.             while ( ! ( CharDefs[ *TextCurrent ] & GTEXT_NEWLINE ) )
  357.             {
  358.                 TextCurrent++;
  359.             }
  360.             if ( CharDefs[ *TextCurrent ] & GTEXT_EOT )
  361.             {
  362.                 Jumped = (GWORD) (TextCurrent-old);
  363.                 return FALSE;
  364.             }
  365.             TextCurrent++;
  366.         }
  367.     }
  368.     else
  369.     {
  370.         x = -x+1;
  371.         for (int l=0; l<x; l++)
  372.         {
  373.             while ( ! ( CharDefs[ *TextCurrent ] & GTEXT_NEWLINE ) )
  374.             {
  375.                 TextCurrent--;
  376.             }
  377.             if ( TextCurrent < TextStart )
  378.             {
  379.                 Jumped = (GWORD) (TextStart-old);
  380.                 return FALSE;
  381.             }
  382.             if ( CharDefs[ *TextCurrent ] & GTEXT_EOT )
  383.             {
  384.                 Jumped = (GWORD) (TextCurrent-old);
  385.                 return FALSE;
  386.             }
  387.             TextCurrent--;
  388.         }
  389.         TextCurrent+=2;
  390.     }
  391.     Jumped = (GWORD) (TextCurrent-old);
  392.     return TRUE;
  393. }
  394.  
  395. BOOL GTextHandler::JumpLine(GWORD x)
  396. {
  397.     GSTRPTR old = TextCurrent;
  398.     if (x>0)
  399.     {
  400.         TextCurrent = TextStart;
  401.         if (JumpLines(x))
  402.         {
  403.             Jumped = -(GWORD)old-Jumped;
  404.             return TRUE;
  405.         }
  406.         else
  407.         {
  408.             Jumped = -(GWORD)old-Jumped;
  409.             return FALSE;
  410.         }
  411.     }
  412.     else if (x==0)
  413.     {
  414. //        TextCurrent = TextStart;
  415. //        return (GWORD)(old-TextStart);
  416.         TextCurrent = TextStart;
  417.         Jumped = -(GWORD) (old-TextStart);
  418.         return TRUE;
  419.     }
  420.     Jumped = 0;
  421.     return FALSE;
  422. }
  423.  
  424. BOOL GTextHandler::JumpChar(GWORD x)
  425. {
  426.     GSTRPTR old = TextCurrent;
  427.     if (x>0)
  428.     {
  429.         TextCurrent = TextStart;
  430.         if (JumpChars(x))
  431.         {
  432.             Jumped = -(GWORD)old-Jumped;
  433.             return TRUE;
  434.         }
  435.         else
  436.         {
  437.             Jumped = -(GWORD)old-Jumped;
  438.             return FALSE;
  439.         }
  440.     }
  441.     else if (x==0)
  442.     {
  443.         TextCurrent = TextStart;
  444.         Jumped = -(GWORD) (old-TextStart);
  445.         return TRUE;
  446.     }
  447.     return NULL;
  448. }
  449.  
  450. BOOL GTextHandler::CmpIncidents(GSTRPTR incidents)
  451. {
  452.     GSTRPTR text;
  453.     while ( (*incidents != 0) && (*text != 0) )
  454.         {
  455.         text = TextCurrent;
  456.         while ( *incidents == *text )
  457.         {
  458. //            printf("%c", *incidents);
  459.             incidents++;
  460.             text++;
  461.         }
  462.         if ( (*incidents == '|') || (*incidents == 0) ) return TRUE;
  463.         while ( (*incidents != '|') && ( *incidents != 0) ) incidents++;
  464.         if (*incidents == '|') incidents++;
  465.     }
  466.     return FALSE;
  467. }
  468.  
  469. BOOL GTextHandler::JumpNextIncident(GSTRPTR incidents)
  470. {
  471.     GSTRPTR old = TextCurrent;
  472.     while ( (!CmpIncidents(incidents)) && !(CharDefs[*TextCurrent] & GTEXT_EOT) )
  473.     {
  474.         TextCurrent++;
  475.     }
  476.     Jumped = (GWORD) (TextCurrent-old);
  477.     if (CharDefs[*TextCurrent] & GTEXT_EOT) return FALSE;
  478.     return TRUE;
  479. //    if ( *TextCurrent == 0 ) return 0;
  480. //    else return (GWORD) (TextCurrent-old);
  481. }
  482.  
  483. BOOL GTextHandler::JumpNextReqType(GUSHORT flags, GUSHORT demand)
  484. {
  485.     GSTRPTR old = TextCurrent;
  486.     if ( (CharDefs[ *TextCurrent ] & GTEXT_EOT) ) return FALSE;
  487.  
  488.     switch (demand)
  489.     {
  490.         case GTEXT_EQ:
  491.             while ( (CharDefs[*TextCurrent] != flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT) )
  492.             {
  493. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  494. //                printf("(%c)", *TextCurrent);
  495.                 TextCurrent++;
  496.             }
  497.         break;
  498.         case GTEXT_ALL:
  499.             while ( ((CharDefs[*TextCurrent] & flags) != flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT) )
  500.             {
  501. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  502. //                printf("(%c)", *TextCurrent);
  503.                 TextCurrent++;
  504.             }
  505.         break;
  506.  
  507.         case GTEXT_ANY:
  508.             while ( (!(CharDefs[*TextCurrent] & flags)) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  509.             {
  510. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  511. //                printf("(%c)", *TextCurrent);
  512.                 TextCurrent++;
  513.             }
  514.         break;
  515.  
  516.         case GTEXT_NOTEQ:
  517.             while ( (CharDefs[*TextCurrent] == flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT) )
  518.             {
  519. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  520. //                printf("(%c)", *TextCurrent);
  521.                 TextCurrent++;
  522.             }
  523.         break;
  524.  
  525.         case GTEXT_NOTALL:
  526.             while ( ((CharDefs[*TextCurrent] & flags) == flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT) )
  527.             {
  528. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  529. //                printf("(%c)", *TextCurrent);
  530.                 TextCurrent++;
  531.             }
  532.         break;
  533.  
  534.         case GTEXT_NONE:
  535.             while ( (CharDefs[*TextCurrent] & flags) && !(CharDefs[ *TextCurrent ] & GTEXT_EOT))
  536.             {
  537. //                if (CharDefs[*TextCurrent] & GTEXT_EOT) return NULL;
  538. //                printf("(%c)", *TextCurrent);
  539.                 TextCurrent++;
  540.             }
  541.         break;
  542.  
  543.         Jumped = (GWORD) (TextCurrent-old);
  544.         return FALSE;
  545.     }
  546.     Jumped = (GWORD) (TextCurrent-old);
  547.     if (CharDefs[*TextCurrent] & GTEXT_EOT) return FALSE;
  548.     return TRUE;
  549. }
  550.  
  551. BOOL GTextHandler::JumpNextAppearance(GSTRPTR str)
  552. {
  553.     GSTRPTR old = TextCurrent;
  554. //    if ( (CharDefs[ *TextCurrent ] & GTEXT_EOT) ) return NULL;
  555. //    WriteLog("JumpNextAppearance\n");
  556. //    char temp[256];
  557.     while (! ( CharDefs[*TextCurrent] & GTEXT_EOT ) )
  558.     {
  559.         char *tcur = TextCurrent;
  560.         char *strb = str;
  561.         while ( (CmpChars(*strb, *tcur)) && !(CharDefs[ *tcur ] & GTEXT_EOT) )
  562.         {
  563.             strb++;
  564.             tcur++;
  565.         }
  566.  
  567.         if ( *strb == 0 ) {
  568. //            sprintf(temp, "before %x - after %x\n", old, TextCurrent);
  569. //            WriteLog(temp);
  570.             Jumped = TextCurrent-old;
  571.             return TRUE;
  572.         }
  573.         
  574.         TextCurrent++;
  575.     }
  576.     Jumped = (GWORD) (TextCurrent-old);
  577. //    sprintf(temp, "before %x - after %x\n", old, TextCurrent);
  578. //    WriteLog(temp);
  579.     return FALSE;
  580.         
  581. }
  582.  
  583. BOOL GTextHandler::JumpNextChar(char c)
  584. {
  585.     GSTRPTR old = TextCurrent;
  586.     while ( (!(CharDefs[ *TextCurrent ] & GTEXT_EOT)) && (! CmpChars( *TextCurrent, c) ) ) TextCurrent++;
  587.     Jumped = (GWORD) (TextCurrent-old);
  588.     if ( CharDefs[*TextCurrent] & GTEXT_EOT ) return FALSE;
  589.     return TRUE;
  590. }
  591.  
  592. #endif /* GTEXTHANDLER_CPP */