]> git.saurik.com Git - wxWidgets.git/commitdiff
1. Empty() now doesn't free memory - Clear() does
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 24 Nov 1998 16:08:06 +0000 (16:08 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 24 Nov 1998 16:08:06 +0000 (16:08 +0000)
2. operator<<(int), (float) and (double) added
3. vsnprintf() is used if available instead of vprintf() (buffer overflows...)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1032 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/string.h
src/common/string.cpp

index 4c1b663b9ec5c00e8ee0a10dddd703b32fd76ce4..aaf3d9f5532dc50fd3329e76d543a3896a4a5d17 100644 (file)
@@ -257,14 +257,23 @@ public:
   size_t Len() const { return GetStringData()->nDataLength; }
     /// string contains any characters?
   bool IsEmpty() const { return Len() == 0; }
-    /// reinitialize string (and free memory)
+    /// empty string contents
   void Empty()
   {
     if ( !IsEmpty() )
       Reinit();
 
+    // should be empty
     wxASSERT( GetStringData()->nDataLength == 0 );
-    wxASSERT( GetStringData()->nAllocLength == 0 );
+  }
+    /// empty the string and free memory
+  void Clear()
+  {
+    if ( !GetStringData()->IsEmpty() )
+      Reinit();
+
+    wxASSERT( GetStringData()->nDataLength == 0 );  // should be empty
+    wxASSERT( GetStringData()->nAllocLength == 0 ); // and not own any memory
   }
 
     /// Is an ascii value
@@ -372,6 +381,16 @@ public:
     //@}
   //@}
 
+  /** @name stream-like functions */
+  //@{
+        /// insert an int into string
+    wxString& operator<<(int i);
+        /// insert a float into string
+    wxString& operator<<(float f);
+        /// insert a double into string
+    wxString& operator<<(double d);
+  //@}
+  
   /** @name string comparison */
   //@{
     /**
index 70fdb635e1785ea8a705afe5808b679c184983af..84b2bb55496af39407b1ea0110f430b432ca8478 100644 (file)
@@ -879,6 +879,33 @@ int wxString::Find(const char *pszSub) const
   return (psz == NULL) ? NOT_FOUND : psz - m_pchData;
 }
 
+// ---------------------------------------------------------------------------
+// stream-like operators
+// ---------------------------------------------------------------------------
+wxString& wxString::operator<<(int i)
+{
+    wxString res;
+    res.Printf("%d", i);
+
+    return (*this) << res;
+}
+
+wxString& wxString::operator<<(float f)
+{
+    wxString res;
+    res.Printf("%f", f);
+
+    return (*this) << res;
+}
+
+wxString& wxString::operator<<(double d)
+{
+    wxString res;
+    res.Printf("%g", d);
+
+    return (*this) << res;
+}
+
 // ---------------------------------------------------------------------------
 // formatted output
 // ---------------------------------------------------------------------------
@@ -896,11 +923,53 @@ int wxString::Printf(const char *pszFormat, ...)
 
 int wxString::PrintfV(const char* pszFormat, va_list argptr)
 {
+#ifdef __WXMSW__
+  #ifdef _MSC_VER
+    #define wxVsprintf     _vsnprintf
+  #endif
+#else // guess that any Unix has snprintf() - feel free to insert additional
+      // platform/compiler tests here if this is not the case for you
+  #define wxVsprintf       vsnprintf
+#endif
+  
+#ifndef wxVsprintf
+  #pragma message("Using sprintf() because no snprintf()-like function defined")
+  #define wxVsprintf vsprintf
+#endif
+
+  // static buffer to avoid dynamic memory allocation each time
   static char s_szScratch[1024];
 
-  int iLen = vsprintf(s_szScratch, pszFormat, argptr);
+  int iLen = wxVsprintf(s_szScratch, WXSIZEOF(s_szScratch), pszFormat, argptr);
+  char *buffer;
+  if ( (size_t)iLen < WXSIZEOF(s_szScratch) ) {
+    buffer = s_szScratch;
+  }
+  else {
+      int size = WXSIZEOF(s_szScratch) * 2;
+      buffer = (char *)malloc(size);
+      while ( buffer != NULL ) {
+          iLen = wxVsprintf(buffer, WXSIZEOF(s_szScratch), pszFormat, argptr);
+          if ( iLen < size ) {
+              // ok, there was enough space
+              break;
+          }
+
+          // still not enough, double it again
+          buffer = (char *)realloc(buffer, size *= 2);
+      }
+
+      if ( !buffer ) {
+          // out of memory
+          return -1;
+      }
+  }
+
   AllocBeforeWrite(iLen);
-  strcpy(m_pchData, s_szScratch);
+  strcpy(m_pchData, buffer);
+
+  if ( buffer != s_szScratch )
+      free(buffer);
 
   return iLen;
 }