ustring.cpp

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 // Project     : SDK Core
00003 // Version     : 1.0
00004 //
00005 // Category    : Helpers
00006 // Filename    : ustring.cpp
00007 // Created by  : Steinberg, 12/2005
00008 // Modified    : $Date: 2008/01/09 12:51:43 $
00009 // Description : UTF-16 String class
00010 //
00011 //-----------------------------------------------------------------------------
00012 // LICENSE
00013 // © 2008, Steinberg Media Technologies GmbH, All Rights Reserved
00014 //-----------------------------------------------------------------------------
00015 // This Software Development Kit may not be distributed in parts or its entirety  
00016 // without prior written agreement by Steinberg Media Technologies GmbH. 
00017 // This SDK must not be used to re-engineer or manipulate any technology used  
00018 // in any Steinberg or Third-party application or software module, 
00019 // unless permitted by law.
00020 // Neither the name of the Steinberg Media Technologies nor the names of its
00021 // contributors may be used to endorse or promote products derived from this 
00022 // software without specific prior written permission.
00023 // 
00024 // THIS SDK IS PROVIDED BY STEINBERG MEDIA TECHNOLOGIES GMBH "AS IS" AND
00025 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00027 // IN NO EVENT SHALL STEINBERG MEDIA TECHNOLOGIES GMBH BE LIABLE FOR ANY DIRECT, 
00028 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00029 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00030 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00031 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00032 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00033 // OF THE POSSIBILITY OF SUCH DAMAGE.
00034 //-----------------------------------------------------------------------------
00035 
00036 
00037 #include "ustring.h"
00038 
00039 #if WINDOWS
00040 #include <stdio.h>
00041 
00042 #elif TARGET_API_MAC_CARBON
00043 #include <CoreFoundation/CoreFoundation.h>
00044 
00045 #endif
00046 
00047 //------------------------------------------------------------------------
00048 namespace Steinberg {
00049 
00050 //------------------------------------------------------------------------
00052 //------------------------------------------------------------------------
00053 template <class TDstChar, class TSrcChar>
00054 void StringCopy (TDstChar* dst, int32 dstSize, const TSrcChar* src, int32 srcSize = -1)
00055 {
00056         int32 count = dstSize;
00057         if (srcSize >= 0 && srcSize < dstSize)
00058                 count = srcSize;
00059         for (int32 i = 0; i < count; i++)
00060         {
00061                 dst[i] = (TDstChar)src[i];
00062                 if (src[i] == 0)
00063                         break;
00064         }
00065         dst[dstSize - 1] = 0;
00066 }
00067 
00068 //------------------------------------------------------------------------
00070 //------------------------------------------------------------------------
00071 template <class TSrcChar>
00072 int32 StringLength (const TSrcChar* src, int32 srcSize = -1)
00073 {
00074         if (srcSize == 0)
00075                 return 0;
00076         int32 length = 0;
00077         while (src[length])
00078         {
00079                 length++;
00080                 if (srcSize > 0 && length >= srcSize)
00081                         break;
00082         }
00083         return length;
00084 }
00085 
00086 //------------------------------------------------------------------------
00087 // UString
00088 //------------------------------------------------------------------------
00089 int32 UString::getLength () const
00090 {
00091         return StringLength<char16> (thisBuffer, thisSize);
00092 }
00093 
00094 //------------------------------------------------------------------------
00095 UString& UString::assign (const char16* src, int32 srcSize)
00096 {
00097         StringCopy<char16, char16> (thisBuffer, thisSize, src, srcSize);
00098         return *this;
00099 }
00100 
00101 //------------------------------------------------------------------------
00102 UString& UString::append (const char16* src, int32 srcSize)
00103 {
00104         int32 length = getLength ();
00105         StringCopy<char16, char16> (thisBuffer + length, thisSize - length, src, srcSize);
00106         return *this;
00107 }
00108 
00109 //------------------------------------------------------------------------
00110 const UString& UString::copyTo (char16* dst, int32 dstSize) const
00111 {
00112         StringCopy<char16, char16> (dst, dstSize, thisBuffer, thisSize);
00113         return *this;
00114 }
00115 
00116 //------------------------------------------------------------------------
00117 UString& UString::fromAscii (const char* src, int32 srcSize)
00118 {
00119         StringCopy<char16, char> (thisBuffer, thisSize, src, srcSize);
00120         return *this;
00121 }
00122 
00123 //------------------------------------------------------------------------
00124 const UString& UString::toAscii (char* dst, int32 dstSize) const
00125 {
00126         StringCopy<char, char16> (dst, dstSize, thisBuffer, thisSize);
00127         return *this;
00128 }
00129 
00130 //------------------------------------------------------------------------
00131 bool UString::scanFloat (double& value) const
00132 {
00133 #if WINDOWS
00134         return swscanf ((const wchar_t*)thisBuffer, L"%lf", &value) == 1;
00135 
00136 #elif TARGET_API_MAC_CARBON
00137         CFStringRef cfStr = CFStringCreateWithBytes (0, (const UInt8 *)thisBuffer, getLength () * 2, kCFStringEncodingUTF16, false);
00138         if (cfStr)
00139         {
00140                 value = CFStringGetDoubleValue (cfStr);
00141                 CFRelease (cfStr);
00142                 return true;
00143         }
00144         return false;
00145 
00146 #else
00147         // implement me!
00148         return false;
00149 #endif
00150 }
00151 
00152 //------------------------------------------------------------------------
00153 bool UString::printFloat (double value)
00154 {
00155 #if WINDOWS
00156         return swprintf ((wchar_t*)thisBuffer, L"%.4lf", value) == 1;
00157 #elif TARGET_API_MAC_CARBON
00158         bool result = false;
00159         CFStringRef cfStr = CFStringCreateWithFormat (0, 0, CFSTR("%.4lf"), value);
00160         if (cfStr)
00161         {
00162                 memset (thisBuffer, 0, thisSize);
00163                 CFRange range = {0, CFStringGetLength (cfStr)};
00164                 CFStringGetBytes (cfStr, range, kCFStringEncodingUTF16, 0, false, (UInt8*)thisBuffer, thisSize, 0);
00165                 CFRelease (cfStr);
00166         }
00167         return result;
00168 #else
00169         // implement me!
00170         return false;
00171 #endif
00172 }
00173 
00174 //------------------------------------------------------------------------
00175 bool UString::scanInt (int64& value) const
00176 {
00177 #if WINDOWS
00178         return swscanf ((const wchar_t*)thisBuffer, L"%I64", &value) == 1;
00179 
00180 #elif TARGET_API_MAC_CARBON
00181         CFStringRef cfStr = CFStringCreateWithBytes (0, (const UInt8 *)thisBuffer, getLength () * 2, kCFStringEncodingUTF16, false);
00182         if (cfStr)
00183         {
00184                 value = CFStringGetIntValue (cfStr);
00185                 CFRelease (cfStr);
00186                 return true;
00187         }
00188         return false;
00189 
00190 #else
00191         // implement me!
00192         return false;
00193 #endif
00194 }
00195 
00196 //------------------------------------------------------------------------
00197 bool UString::printInt (int64 value)
00198 {
00199 #if WINDOWS
00200         return swprintf ((wchar_t*)thisBuffer, L"%I64", value) == 1;
00201 
00202 #elif TARGET_API_MAC_CARBON
00203         CFStringRef cfStr = CFStringCreateWithFormat (0, 0, CFSTR("%ll"), value);
00204         if (cfStr)
00205         {
00206                 memset (thisBuffer, 0, thisSize);
00207                 CFRange range = {0, CFStringGetLength (cfStr)};
00208                 CFStringGetBytes (cfStr, range, kCFStringEncodingUTF16, 0, false, (UInt8*)thisBuffer, thisSize, 0);
00209                 CFRelease (cfStr);
00210                 return true;
00211         }
00212         return false;
00213 #else
00214         // implement me!
00215         return false;
00216 #endif
00217 }
00218 
00219 } // namespace Steinberg 
Empty

Copyright ©2008 Steinberg Media Technologies. All Rights Reserved.