home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classsrc.pak / GEOMETRY.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  5KB  |  236 lines

  1. //----------------------------------------------------------------------------
  2. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  3. //   Implementation of geometry (TPoint, TSize, TRect) classes.
  4. //----------------------------------------------------------------------------
  5. #if !defined(_Windows)
  6. # define _Windows      // pretend we are in windows to get the headers we need
  7. #endif
  8. #include <osl/defs.h>
  9. #include <osl/geometry.h>
  10.  
  11. //
  12. // Calculate the integer square root of a 32bit signed long. return a 16bit
  13. // signed. Is fairly fast, esp. compared to FP versions
  14. //
  15. int _BIDSFUNC
  16. Sqrt(long val)
  17. {
  18.   if (val <= 0)
  19.     return 0;     // Throw a math exception?
  20.  
  21.   unsigned mask = 1;  // Bit mask to shift left
  22.   int best = 0;       // Best estimate so far
  23.  
  24.   for (; !(mask&0x8000); mask <<= 1)
  25.     if (((long)best+mask)*(best+mask) <= val)
  26.       best |= mask;
  27.   return best;
  28. }
  29.  
  30. //
  31. // Make a duplicate of a C string using new char[]
  32. //
  33. char* _BIDSFUNC
  34. strnewdup(const char* s, size_t allocSize)
  35. {
  36.   if (!s)
  37.     s = "";
  38.   int alloc = max(strlen(s)+1, allocSize);
  39.   return strcpy(new char[alloc], s);
  40. }
  41.  
  42. //
  43. // Make a far duplicate of a C string using new char[] far
  44. //
  45. #if defined(BI_DATA_NEAR)
  46.  
  47. char far*  _BIDSFUNC
  48. strnewdup(const char far* s, size_t allocSize)
  49. {
  50.   if (!s)
  51.     s = "";
  52.   int alloc = max(strlen(s)+1, allocSize);
  53.   return strcpy(new far char[alloc], s);
  54. }
  55.  
  56. long
  57. atol(const char far* s)
  58. {
  59.   for (long val = 0; *s && isdigit(*s); s++)
  60.     val = val*10 + *s - '0';
  61.   return val;
  62. }
  63.  
  64. #endif
  65.  
  66. #if !defined(BI_PLAT_WIN16)
  67. //
  68. // Make a duplicate of a wide C string using new wchar_t[]
  69. //
  70. wchar_t* _BIDSFUNC
  71. strnewdup(const wchar_t* s, size_t allocSize)
  72. {
  73.   if (!s)
  74.     s = L"";
  75.   int alloc = max((size_t)lstrlenW(s)+1, allocSize);
  76.   return lstrcpyW(new wchar_t[alloc], s);
  77. }
  78.  
  79. //
  80. // Wide string copy function. 
  81. //
  82. wchar_t* _BIDSFUNC
  83. strcpy(wchar_t* dst, const wchar_t* src)
  84. {
  85.   wchar_t* p = dst;
  86.   while ((*p++ = *src++) != 0)
  87.     ;
  88.   return dst;
  89. }
  90.  
  91. //
  92. // Wide string length function. 
  93. //
  94. size_t _BIDSFUNC
  95. strlen(const wchar_t* str)
  96. {
  97.   const wchar_t* p = str;
  98.   for (; *p; p++)
  99.     ;
  100.   return p - str;
  101. }
  102.  
  103. #endif
  104.  
  105. //----------------------------------------------------------------------------
  106.  
  107. TRect&
  108. TRect::Normalize()
  109. {
  110.   if (left > right)
  111.     Swap(left, right);
  112.   if (top > bottom)
  113.     Swap(top, bottom);
  114.   return *this;
  115. }
  116.  
  117. TRect&
  118. TRect::Offset(int dx, int dy)
  119. {
  120.   left += dx;
  121.   top += dy;
  122.   right += dx;
  123.   bottom += dy;
  124.   return *this;
  125. }
  126.  
  127. TRect&
  128. TRect::Inflate(int dx, int dy)
  129. {
  130.   left -= dx;
  131.   top -= dy;
  132.   right += dx;
  133.   bottom += dy;
  134.   return *this;
  135. }
  136.  
  137. TRect&
  138. TRect::operator &=(const TRect& other)
  139. {
  140.   if (!IsNull()) {
  141.     if (other.IsNull())
  142.       SetNull();
  143.     else {
  144.       left = Max(left, other.left);
  145.       top = Max(top, other.top);
  146.       right = Min(right, other.right);
  147.       bottom = Min(bottom, other.bottom);
  148.     }
  149.   }
  150.   return *this;
  151. }
  152.  
  153. TRect&
  154. TRect::operator |=(const TRect& other)
  155. {
  156.   if (!other.IsNull()) {
  157.     if (IsNull())
  158.       *this = other;
  159.     else {
  160.       left = Min(left, other.left);
  161.       top = Min(top, other.top);
  162.       right = Max(right, other.right);
  163.       bottom = Max(bottom, other.bottom);
  164.     }
  165.   }
  166.   return *this;
  167. }
  168.  
  169. //----------------------------------------------------------------------------
  170. // class TCmdLine implementation
  171.  
  172. const char whitespace[] = " \t";
  173. const char terminator[] = "=/ \t";  // remove /- to dissallow separating there
  174.  
  175. TCmdLine::TCmdLine(const char far* cmdLine)
  176. {
  177.   Buffer = new char[strlen(cmdLine)+1];
  178.   strcpy(Buffer, cmdLine);
  179.   Reset();
  180. }
  181.  
  182. void TCmdLine::Reset()
  183. {
  184.   Token = TokenStart = Buffer;
  185.   TokenLen = 0;
  186.   Kind = Start;
  187. }
  188.  
  189. TCmdLine::~TCmdLine()
  190. {
  191.   delete [] Buffer;
  192. }
  193.  
  194. TCmdLine::TKind TCmdLine::NextToken(bool removeCurrent)
  195. {
  196.   // Done parsing, no more tokens
  197.   //
  198.   if (Kind == Done)
  199.     return Kind;
  200.  
  201.   // Move Token ptr to next token, by copying over current token, or by ptr
  202.   // adjustment. TokenStart stays right past previous token
  203.   //
  204.   if (removeCurrent) {
  205.     strcpy(TokenStart, Token+TokenLen);
  206.     Token = TokenStart;
  207.   }
  208.   else {
  209.     Token += TokenLen;
  210.     TokenStart = Token;
  211.   }
  212.  
  213.   // Adjust token ptr to begining of token & determine kind
  214.   //
  215.   Token += strspn(Token, whitespace);  // skip leading whitespace
  216.   switch (*Token) {
  217.     case 0:
  218.       Kind = Done;
  219.       break;
  220.     case '=':
  221.       Kind = Value;
  222.       Token++;
  223.       break;
  224.     case '-':
  225.     case '/':
  226.       Kind = Option;
  227.       Token++;
  228.       break;
  229.     default:
  230.       Kind = Name;
  231.   }
  232.   Token += strspn(Token, whitespace);  // skip any more whitespace
  233.   TokenLen = strcspn(Token, terminator);
  234.   return Kind;
  235. }
  236.