00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
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
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
00215 return false;
00216 #endif
00217 }
00218
00219 }