Date: Thursday, March 23, 2006 @ 15:37:04
  Author: gilles
    Path: /cvsroot/carob/carob

Modified: include/BigDecimal.hpp (1.19 -> 1.20) src/BigDecimal.cpp (1.24
          -> 1.25)

Fixed bug in = operator (now allocates byte array if any)
Added copy constructor to avoid bad copies
Made use of Common::userDecimalPoint in createFromString
Const-ed operator ==
Enlighted BigDecimal(DriverSocket) constructor (removed useless this->) + made 
use of initToZero() new function


------------------------+
 include/BigDecimal.hpp |    7 +++++--
 src/BigDecimal.cpp     |   45 ++++++++++++++++++++++++---------------------
 2 files changed, 29 insertions(+), 23 deletions(-)


Index: carob/include/BigDecimal.hpp
diff -u carob/include/BigDecimal.hpp:1.19 carob/include/BigDecimal.hpp:1.20
--- carob/include/BigDecimal.hpp:1.19   Wed Mar 22 19:22:04 2006
+++ carob/include/BigDecimal.hpp        Thu Mar 23 15:37:04 2006
@@ -77,7 +77,10 @@
   BigDecimal(const int64_t);
   //TODO:BigDecimal(const float);
   //TODO:BigDecimal(const double);
-  
+
+  /** Copy constructor */
+  BigDecimal(const BigDecimal&);
+
   /**
    * Copies scale sign and unscaled_value
    */
@@ -86,7 +89,7 @@
   /**
    * Compares scale, sign and unscaled_value
    */
-  bool operator == (const BigDecimal&);
+  bool operator == (const BigDecimal&) const;
   /**
    * BigDecimal display or serialization as a string with default C locale
    * decimal point. This operator is used to send the bigdecimal as a string to
Index: carob/src/BigDecimal.cpp
diff -u carob/src/BigDecimal.cpp:1.24 carob/src/BigDecimal.cpp:1.25
--- carob/src/BigDecimal.cpp:1.24       Wed Mar 22 19:22:04 2006
+++ carob/src/BigDecimal.cpp    Thu Mar 23 15:37:04 2006
@@ -67,23 +67,21 @@
 
   std::string unscaledStr = str;
   //TODO: multi-char decimal points
-  char decimalPoint = std::use_facet<std::numpunct<char> 
>(currentLocale).decimal_point();
   int strSize = str.length();
   char currentChar = 0;
   bool decimalPointFound = false;
-  scale  = 0;
   for (int i=0; i<strSize; i++)
   {
     currentChar = unscaledStr[i];
     if (!isspace(currentChar, currentLocale))
     {
-      if (currentChar == decimalPoint)
+      if (currentChar == userDecimalPoint)
       {
         if (decimalPointFound)
         {
           //double decimal point => exception
           throw ConversionException(L"Detected more than one decimal point [" +
-              toWString(decimalPoint) + L"] in BigDecimal string ["+ 
fromString(str) + L"]", CONV_NUM_RANGE);
+              toWString(userDecimalPoint) + L"] in BigDecimal string ["+ 
fromString(str) + L"]", CONV_NUM_RANGE);
         }
         // remove the decimal point (actually replace it by a space)
         unscaledStr.replace(i, 1, " ");
@@ -182,29 +180,29 @@
 }
 #endif
 
+BigDecimal::BigDecimal(const BigDecimal& ref)
+{
+  *this = ref; //make use of = operator
+}
+
 BigDecimal::BigDecimal(const DriverSocket& input)
     throw (SocketIOException, UnexpectedException) : unscaled_value(0)
 {
+  initToZero();
   //1. Read intVal:
   //1.1 Read intValLength
   int32_t lengthRead;
   input>>lengthRead;
   //We receive the length as an int32 but internally, we have an array of 
bytes,
   //which length actually is of size_t type
-  this->byteArrayLength = (size_t)lengthRead;
-  if (this->byteArrayLength == 0)
-  {
-    //This is a zero, no byte array
-    // FIXME: for safety, create an empty byte array instead ?
-    ;
-  }
-  else
+  byteArrayLength = (size_t)lengthRead;
+  if (byteArrayLength != 0)
   {
-    this->byteArray = new java_byte[this->byteArrayLength];
+    byteArray = new java_byte[byteArrayLength];
     //1.2 Compute padding
     unsigned int idx = 0;
     int32_t wordRead = 0;
-    unsigned int padding = this->byteArrayLength%4;
+    unsigned int padding = byteArrayLength%4;
     //If there is a padding, we must read the first 'padding' bytes
     //so we are aligned for the rest of the bytes
     if (padding > 0)
@@ -212,18 +210,18 @@
       input>>wordRead;
       for (idx=0; idx<padding; idx++)
       {
-        this->byteArray[idx] = 
static_cast<java_byte>((wordRead>>(8*(padding-idx-1)))&0xFF);
+        byteArray[idx] = 
static_cast<java_byte>((wordRead>>(8*(padding-idx-1)))&0xFF);
       }
     }
     //1.3 Read the byte array from integers
     //we start from the first aligned byte
-    for (; idx<this->byteArrayLength; idx+=4)
+    for (; idx<byteArrayLength; idx+=4)
     {
       input>>wordRead;
-      this->byteArray[idx]   = static_cast<java_byte>((wordRead>>24)&0xFF);
-      this->byteArray[idx+1] = static_cast<java_byte>((wordRead>>16)&0xFF);
-      this->byteArray[idx+2] = static_cast<java_byte>((wordRead>> 8)&0xFF);
-      this->byteArray[idx+3] = static_cast<java_byte> (wordRead     &0xFF);
+      byteArray[idx]   = static_cast<java_byte>((wordRead>>24)&0xFF);
+      byteArray[idx+1] = static_cast<java_byte>((wordRead>>16)&0xFF);
+      byteArray[idx+2] = static_cast<java_byte>((wordRead>> 8)&0xFF);
+      byteArray[idx+3] = static_cast<java_byte> (wordRead     &0xFF);
     }
   }
 
@@ -268,12 +266,17 @@
   byteArrayLength = ref.byteArrayLength;
   if (byteArrayLength != 0)
   {
+    byteArray = new java_byte[byteArrayLength];
     memcpy(byteArray, ref.byteArray, byteArrayLength*sizeof(byteArray[0]));
   }
+  else
+  {
+    byteArray = NULL;
+  }
   return *this;
 }
 
-bool BigDecimal::operator == (const BigDecimal& ref)
+bool BigDecimal::operator == (const BigDecimal& ref) const
 {
   return (signum == ref.signum
       && scale == ref.scale

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to