home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ftes46b5.zip / ftes46b5 / src / e_line.cpp < prev    next >
C/C++ Source or Header  |  1997-05-30  |  5KB  |  233 lines

  1. /*    e_line.cpp
  2.  *
  3.  *    Copyright (c) 1994-1996, Marko Macek
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  */
  9.  
  10. #include "fte.h"
  11.  
  12. ELine::ELine(int ACount, char *AChars) {
  13.     Chars = NULL; 
  14.     Count = ACount; 
  15.     Allocate(Count); 
  16. #ifdef CONFIG_SYNTAX_HILIT
  17.     StateE = 0;
  18. #endif
  19.     if (AChars)
  20.         memcpy(Chars, AChars, Count);
  21.     else
  22.         memset(Chars, ' ', Count);
  23. }
  24.  
  25. ELine::ELine(char *AChars, int ACount) {
  26.     Chars = AChars;
  27.     Count = ACount; 
  28. #ifdef CONFIG_SYNTAX_HILIT
  29.     StateE = 0;
  30. #endif
  31. }
  32.  
  33. ELine::~ELine() { 
  34.     free(Chars); 
  35. }
  36.  
  37. int ELine::Allocate(unsigned int Bytes) { 
  38.     unsigned int Allocated;
  39.     
  40.     Allocated = (Bytes | CHAR_TRESHOLD);
  41.     if (Chars) 
  42.         Chars = (char *) realloc(Chars, Allocated); 
  43.     else
  44.         Chars = (char *) malloc(Allocated); 
  45.     if (Chars == NULL) 
  46.         return 0;
  47.     return 1;
  48. }
  49.  
  50. int EBuffer::ScreenPos(ELine *L, int Offset) {
  51.     int ExpandTabs = BFI(this, BFI_ExpandTabs);
  52.     int TabSize = BFI(this, BFI_TabSize);
  53.  
  54.     if (!ExpandTabs) {
  55.         return Offset;
  56.     } else {
  57.         char *p = L->Chars;
  58.         int Len = L->Count;
  59.         int Pos = 0;
  60.         int Ofs = Offset;
  61.  
  62.         if (Ofs > Len) {
  63.             while (Len > 0) {
  64.                 if (*p++ != '\t')
  65.                     Pos++;
  66.                 else
  67.                     Pos = NextTab(Pos, TabSize);
  68.                 Len--;
  69.             }
  70.             Pos += Ofs - L->Count;
  71.         } else {
  72.             while (Ofs > 0) {
  73.                 if (*p++ != '\t')
  74.                     Pos++;
  75.                 else
  76.                     Pos = NextTab(Pos, TabSize);
  77.                 Ofs--;
  78.             }
  79.         }
  80.         return Pos;
  81.     }
  82. }
  83.  
  84. int EBuffer::CharOffset(ELine *L, int ScreenPos) {
  85.     int ExpandTabs = BFI(this, BFI_ExpandTabs);
  86.     int TabSize = BFI(this, BFI_TabSize);
  87.  
  88.     if (!ExpandTabs) {
  89.         return ScreenPos;
  90.     } else {
  91.         int Pos = 0;
  92.         int Ofs = 0;
  93.         char *p = L->Chars;
  94.         int Len = L->Count;
  95.  
  96.         while (Len > 0) {
  97.             if (*p++ != '\t')
  98.                 Pos++;
  99.             else
  100.                 Pos = NextTab(Pos, TabSize);
  101.             if (Pos > ScreenPos)
  102.                 return Ofs;
  103.             Ofs++;
  104.             Len--;
  105.         }
  106.         return Ofs + ScreenPos - Pos;
  107.     }
  108. }
  109.  
  110. int EBuffer::Allocate(int ACount) {
  111.     PELine *L;
  112.     
  113.     L = (PELine *) realloc(LL, sizeof(PELine) * (ACount + 1));
  114.     if (L == 0 && ACount != 0)
  115.         return 0;
  116.     RAllocated = ACount;
  117.     LL = L;
  118.     return 1;
  119. }
  120.  
  121. int EBuffer::MoveRGap(int RPos) {
  122.     int GapSize = RAllocated - RCount;
  123.     
  124.     if (RGap == RPos) return 1;
  125.     if (RPos < 0 || RPos > RCount) return 0;
  126.     
  127.     if (RGap < RPos) {
  128.         if (RPos - RGap == 1) {
  129.             LL[RGap] = LL[RGap + GapSize];
  130.         } else {
  131.             memmove(LL + RGap,
  132.                 LL + RGap + GapSize,
  133.                 sizeof(PELine) * (RPos - RGap));
  134.         }
  135.     } else {
  136.         if (RGap - RPos == 1) {
  137.             LL[RPos + GapSize] = LL[RPos];
  138.         } else {
  139.             memmove(LL + RPos + GapSize,
  140.                     LL + RPos,
  141.                     sizeof(PELine) * (RGap - RPos));
  142.         }
  143.     }
  144.     RGap = RPos;
  145.     return 1;
  146. }
  147.  
  148. int EBuffer::AllocVis(int ACount) {
  149.     int *V;
  150.     
  151.     V = (int *) realloc(VV, sizeof(int) * (ACount + 1));
  152.     if (V == 0 && ACount != 0) return 0;
  153.     VAllocated = ACount;
  154.     VV = V;
  155.     return 1;
  156. }
  157.  
  158. int EBuffer::MoveVGap(int VPos) {
  159.     int GapSize = VAllocated - VCount;
  160.     
  161.     if (VGap == VPos) return 1;
  162.     if (VPos < 0 || VPos > VCount) return 0;
  163.     
  164.     if (VGap < VPos) {
  165.         if (VPos - VGap == 1) {
  166.             VV[VGap] = VV[VGap + GapSize];
  167.         } else {
  168.             memmove(VV + VGap,
  169.                     VV + VGap + GapSize,
  170.                     sizeof(PELine) * (VPos - VGap));
  171.         }
  172.     } else {
  173.         if (VGap - VPos == 1) {
  174.             VV[VPos + GapSize] = VV[VPos];
  175.         } else {
  176.             memmove(VV + VPos + GapSize,
  177.                     VV + VPos,
  178.                     sizeof(PELine) * (VGap - VPos));
  179.         }
  180.     }
  181.     VGap = VPos;
  182.     return 1;
  183. }
  184.  
  185. int EBuffer::RToV(int No) {
  186.     int L = 0, R = VCount, M, V;
  187.     
  188.     if (No > Vis(VCount - 1) + VCount - 1)   // beyond end
  189.         return -1;
  190.     if (No < VCount) // no folds before (direct match)
  191.         if (Vis(No) == 0) return No;
  192.  
  193.     while (L < R) {
  194.         M = (L + R) >> 1;
  195.         V = Vis(M) + M;
  196.         if (V == No)
  197.             return M;
  198.         else if (V > No)
  199.             R = M;
  200.         else
  201.             L = M + 1;
  202.     }
  203.     return -1;
  204. }
  205.  
  206. int EBuffer::RToVN(int No) {
  207.     int L = 0, R = VCount, M, V;
  208.     
  209.     if (No == RCount)
  210.         return VCount;
  211.     if (No > Vis(VCount - 1) + VCount - 1) 
  212.         return VCount - 1;
  213.     if (No < VCount)
  214.         if (Vis(No) == 0) return No;
  215.     
  216.     while (L < R) {
  217.         M = (L + R) >> 1;
  218.         V = Vis(M) + M;
  219.         if (V == No)
  220.             return M;
  221.         else if (V > No)
  222.             R = M;
  223.         else {
  224.             if (M == VCount - 1)
  225.                 return M;
  226.             else if (Vis(M + 1) + M + 1 > No)
  227.                 return M;
  228.             L = M + 1;
  229.         }
  230.     }
  231.     return R;
  232. }
  233.