home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / POINT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  3.8 KB  |  206 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   source\owl\point.cpp
  4. //   Implementation of mathematical TPoint, TSize, TRect classes.
  5. //----------------------------------------------------------------------------
  6. #pragma hdrignore SECTION
  7. #include <owl\owlpch.h>
  8. #include <owl\point.h>
  9.  
  10. #if !defined(SECTION) || SECTION == 1
  11.  
  12. //
  13. // Calculate the integer square root of a 32bit signed long. return a 16bit
  14. // signed. Is fairly fast, esp. compared to FP versions
  15. //
  16. int _OWLFUNC
  17. Sqrt(long val)
  18. {
  19.   if (val <= 0)
  20.     return 0;     // Throw a math exception?
  21.  
  22.   unsigned mask = 1;  // Bit mask to shift left
  23.   int best = 0;       // Best estimate so far
  24.  
  25.   for (; !(mask&0x8000); mask <<= 1)
  26.     if (((long)best+mask)*(best+mask) <= val)
  27.       best |= mask;
  28.   return best;
  29. }
  30.  
  31. //
  32. // make a duplicate of a C string using new
  33. //
  34. char* _OWLFUNC
  35. strnewdup(const char* s)
  36. {
  37.   if (!s)
  38.     s = "";
  39.   return strcpy(new char[strlen(s)+1], s);
  40. }
  41.  
  42. //
  43. // make a far duplicate of a C string using new far
  44. //
  45. #if defined(__SMALL__) || defined(__MEDIUM__)
  46. char far*  _OWLFUNC
  47. strnewdup(const char far* s)
  48. {
  49.   if (!s)
  50.     s = "";
  51.   return strcpy(new far char[strlen(s)+1], s);
  52. }
  53.  
  54. long
  55. atol(const char far* s)
  56. {
  57.   for (long val = 0; *s && isdigit(*s); s++)
  58.     val = val*10 + *s - '0';
  59.   return val;
  60. }
  61.  
  62. #endif
  63.  
  64.  
  65. TRect&
  66. TRect::Normalize()
  67. {
  68.   if (left > right)
  69.     Swap(left, right);
  70.   if (top > bottom)
  71.     Swap(top, bottom);
  72.   return *this;
  73. }
  74.  
  75. TRect&
  76. TRect::Offset(int dx, int dy)
  77. {
  78.   left += dx;
  79.   top += dy;
  80.   right += dx;
  81.   bottom += dy;
  82.   return *this;
  83. }
  84.  
  85. TRect&
  86. TRect::Inflate(int dx, int dy)
  87. {
  88.   left -= dx;
  89.   top -= dy;
  90.   right += dx;
  91.   bottom += dy;
  92.   return *this;
  93. }
  94.  
  95. TRect&
  96. TRect::operator &=(const TRect& other)
  97. {
  98.   if (!IsNull()) {
  99.     if (other.IsNull())
  100.       SetNull();
  101.     else {
  102.       left = Max(left, other.left);
  103.       top = Max(top, other.top);
  104.       right = Min(right, other.right);
  105.       bottom = Min(bottom, other.bottom);
  106.     }
  107.   }
  108.   return *this;
  109. }
  110.  
  111. TRect&
  112. TRect::operator |=(const TRect& other)
  113. {
  114.   if (!other.IsNull()) {
  115.     if (IsNull())
  116.       *this = other;
  117.     else {
  118.       left = Min(left, other.left);
  119.       top = Min(top, other.top);
  120.       right = Max(right, other.right);
  121.       bottom = Max(bottom, other.bottom);
  122.     }
  123.   }
  124.   return *this;
  125. }
  126.  
  127. #endif
  128. #if !defined(SECTION) || SECTION == 2
  129.  
  130. ostream& _OWLFUNC
  131. operator <<(ostream& os, const TRect& r)
  132. {
  133.   return os << '(' << r.left << ',' << r.top << '-'
  134.             << r.right << ',' << r.bottom << ')';
  135. }
  136.  
  137. //
  138. // streaming operators for resource Ids
  139. //
  140. ostream& _OWLFUNC
  141. operator <<(ostream& os, const TResId& id)
  142. {
  143.   BOOL  isNumeric = !id.IsString();
  144.  
  145.   if (isNumeric)
  146.     os << (long)id.Id;
  147.  
  148.   else
  149.     #if defined(__SMALL__) || defined(__MEDIUM__)
  150.       os << string(id.Id);
  151.     #else
  152.       os << id.Id;
  153.     #endif
  154.   return os;
  155. }
  156.  
  157. #endif
  158. #if !defined(SECTION) || SECTION == 3
  159.  
  160. ipstream& _OWLFUNC
  161. operator >>(ipstream& is, TRect& r)
  162. {
  163.   return is >> r.left >> r.top >> r.right >> r.bottom;
  164. }
  165.  
  166. opstream& _OWLFUNC
  167. operator <<(opstream& os, const TRect& r)
  168. {
  169.   return os << r.left << r.top << r.right << r.bottom;
  170. }
  171.  
  172. //
  173. // streaming operators for resource Ids
  174. //
  175. ipstream& _OWLFUNC
  176. operator >>(ipstream& is, TResId& id)
  177. {
  178.   BOOL  isNumeric;
  179.   is >> isNumeric;
  180.  
  181.   if (isNumeric) {
  182.     long  nid;
  183.     is >> nid;
  184.     id = int(nid);
  185.  
  186.   } else
  187.     id = is.freadString();
  188.   return is;
  189. }
  190.  
  191. opstream& _OWLFUNC
  192. operator <<(opstream& os, const TResId& id)
  193. {
  194.   BOOL  isNumeric = !id.IsString();
  195.   os << isNumeric;
  196.  
  197.   if (isNumeric)
  198.     os << (long)id.Id;
  199.  
  200.   else
  201.     os.fwriteString(id.Id);
  202.   return os;
  203. }
  204. #endif
  205.  
  206.