cfwrappers.cpp
Upload User: zhongxx05
Upload Date: 2007-06-06
Package Size: 33641k
Code Size: 10k
Category:

Symbian

Development Platform:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #pragma once
  36. #if defined(_CARBON) || defined(_MAC_UNIX)
  37. #include "platform/mac/cfwrappers.h"
  38. #include "hxassert.h"
  39. //------------------------------------------------------------
  40. //
  41. // CHXCFString
  42. //
  43. //------------------------------------------------------------
  44. // Constructors
  45. CHXCFString::CHXCFString(void) : mCFStringRef(NULL)
  46. {
  47. UpdateDebugOnlyString();
  48. }
  49. CHXCFString::CHXCFString(CFStringRef cfs)
  50. {
  51. mCFStringRef = cfs;
  52. UpdateDebugOnlyString();
  53. }
  54. CHXCFString::CHXCFString(const CHXCFString& othercfs)
  55. {
  56. mCFStringRef = othercfs.mCFStringRef; 
  57. CFRetain(mCFStringRef); // since he'll release it and we'll release it
  58. UpdateDebugOnlyString();
  59. }
  60. CHXCFString::CHXCFString(const char *pCString, CFStringEncoding encoding)
  61. {
  62. mCFStringRef = CFStringCreateWithCString(kCFAllocatorDefault, pCString, encoding);
  63. check_nonnull(mCFStringRef);
  64. UpdateDebugOnlyString();
  65. }
  66. CHXCFString::CHXCFString(const char *pCString)
  67. {
  68. mCFStringRef = CFStringCreateWithCString(kCFAllocatorDefault, pCString, CFStringGetSystemEncoding());
  69. check_nonnull(mCFStringRef);
  70. UpdateDebugOnlyString();
  71. }
  72. CHXCFString::CHXCFString(const CHXString &str)
  73. {
  74. mCFStringRef = CFStringCreateWithCString(kCFAllocatorDefault, (const char *) str, CFStringGetSystemEncoding());
  75. check_nonnull(mCFStringRef);
  76. UpdateDebugOnlyString();
  77. }
  78. CHXCFString::CHXCFString(ConstStr255Param pPString)
  79. {
  80. mCFStringRef = CFStringCreateWithPascalString(kCFAllocatorDefault, pPString, CFStringGetSystemEncoding());
  81. check_nonnull(mCFStringRef);
  82. UpdateDebugOnlyString();
  83. }
  84. CHXCFString::CHXCFString(const UInt16* pUniChars, INT32 numChars)
  85. {
  86. mCFStringRef = CFStringCreateWithCharacters(kCFAllocatorDefault, pUniChars, numChars);
  87. check_nonnull(mCFStringRef);
  88. UpdateDebugOnlyString();
  89. }
  90. CHXCFString::CHXCFString(CFURLRef urlRef)
  91. {
  92. mCFStringRef = NULL;
  93. SetToCFURL(urlRef);
  94. }
  95. CHXCFString::CHXCFString(const HFSUniStr255& hfsUni)
  96. {
  97. mCFStringRef = CFStringCreateWithCharacters(kCFAllocatorDefault, 
  98. hfsUni.unicode, hfsUni.length);
  99. check_nonnull(mCFStringRef);
  100. UpdateDebugOnlyString();
  101. }
  102. // Type cast operators
  103. CHXCFString::operator HFSUniStr255(void) const
  104. {
  105. HFSUniStr255 hfsUni;
  106. CFIndex cfLength;
  107. cfLength = CFStringGetLength(mCFStringRef);
  108. if (cfLength <= 255)
  109. {
  110. hfsUni.length = cfLength;
  111. CFStringGetCharacters(mCFStringRef, CFRangeMake(0, cfLength), hfsUni.unicode);
  112. }
  113. else
  114. {
  115. hfsUni.length = 0;
  116. }
  117. return hfsUni;
  118. }
  119. // Assignment operators
  120. const CHXCFString& CHXCFString::operator =(const char *pCString)
  121. {
  122. ReleaseCFString();
  123. mCFStringRef = CFStringCreateWithCString(kCFAllocatorDefault, pCString, CFStringGetSystemEncoding());
  124. check_nonnull(mCFStringRef);
  125. UpdateDebugOnlyString();
  126. return *this;
  127. }
  128. const CHXCFString& CHXCFString::operator =(const CHXString& str)
  129. {
  130. ReleaseCFString();
  131. mCFStringRef = CFStringCreateWithCString(kCFAllocatorDefault, (const char *) str, CFStringGetSystemEncoding());
  132. check_nonnull(mCFStringRef);
  133. UpdateDebugOnlyString();
  134. return *this;
  135. }
  136. const CHXCFString& CHXCFString::operator =(CFStringRef cfs)
  137. {
  138. ReleaseCFString();
  139. mCFStringRef = cfs; // note we're NOT doing an additional retain, since these usually are returned by toolbox calls
  140. UpdateDebugOnlyString();
  141. return *this;
  142. }
  143. const CHXCFString& CHXCFString::operator =(const CHXCFString& othercfs)
  144. {
  145. ReleaseCFString();
  146. mCFStringRef = othercfs.mCFStringRef; 
  147. CFRetain(mCFStringRef); // since he'll release it and we'll release it
  148. UpdateDebugOnlyString();
  149. return *this;
  150. }
  151. const CHXCFString& CHXCFString::operator =(CFURLRef urlRef)
  152. {
  153. SetToCFURL(urlRef);
  154. return *this;
  155. }
  156. const CHXCFString& CHXCFString::operator =(const HFSUniStr255& hfsUni)
  157. {
  158. ReleaseCFString();
  159. mCFStringRef = CFStringCreateWithCharacters(kCFAllocatorDefault, 
  160. hfsUni.unicode, hfsUni.length);
  161. check_nonnull(mCFStringRef);
  162. UpdateDebugOnlyString();
  163. return *this;
  164. }
  165. // Destructor
  166. CHXCFString::~CHXCFString()
  167. {
  168. ReleaseCFString();
  169. }
  170. // Internal utilities
  171. void CHXCFString::ReleaseCFString()
  172. {
  173. if (mCFStringRef)
  174. {
  175. ::CFRelease(mCFStringRef);
  176. mCFStringRef = NULL;
  177. }
  178. }
  179. void CHXCFString::SetToCFURL(CFURLRef cfURL)
  180. {
  181. ReleaseCFString();
  182. CFStringRef tempStringRef;
  183. tempStringRef = ::CFURLGetString(cfURL); // we do NOT release the CFString from CFURLGetString
  184. if (tempStringRef)
  185. {
  186. mCFStringRef = ::CFStringCreateCopy(kCFAllocatorDefault, tempStringRef);
  187. }
  188. UpdateDebugOnlyString();
  189. }
  190. #ifdef _DEBUG
  191. void CHXCFString::UpdateDebugOnlyString()
  192. {
  193. if (mCFStringRef)
  194. {
  195. mStrDebugOnly.SetFromCFString(mCFStringRef, CFStringGetSystemEncoding());
  196. }
  197. else
  198. {
  199. mStrDebugOnly.Empty();
  200. }
  201. pStrDebugOnlyPeek = (const char *) mStrDebugOnly;
  202. }
  203. #endif // _DEBUG
  204. #pragma mark -
  205. //------------------------------------------------------------
  206. //
  207. // CHXCFURL
  208. //
  209. //------------------------------------------------------------
  210. // Constructors
  211. CHXCFURL::CHXCFURL(void) : mCFURLRef(NULL)
  212. {
  213. UpdateDebugOnlyString();
  214. }
  215. CHXCFURL::CHXCFURL(const char *pCString)
  216. {
  217. mCFURLRef = NULL;
  218. SetToString(pCString);
  219. }
  220. CHXCFURL::CHXCFURL(const CHXString& str)
  221. {
  222. mCFURLRef = NULL;
  223. SetToString((const char *) str);
  224. }
  225. CHXCFURL::CHXCFURL(const FSRef& fsRef)
  226. {
  227. mCFURLRef = NULL;
  228. SetToFSRef(fsRef);
  229. }
  230. CHXCFURL::CHXCFURL(const FSRef* fsRef)
  231. {
  232. mCFURLRef = NULL;
  233. SetToFSRef(*fsRef);
  234. }
  235. CHXCFURL::CHXCFURL(CFURLRef urlRef)
  236. {
  237. mCFURLRef = urlRef; // note we're NOT doing an additional retain
  238. UpdateDebugOnlyString();
  239. }
  240. // Assignment operators
  241. const CHXCFURL& CHXCFURL::operator =(const char *pCString)
  242. {
  243. SetToString(pCString);
  244. return *this;
  245. }
  246. const CHXCFURL& CHXCFURL::operator =(const CHXString& str)
  247. {
  248. SetToString((const char *) str);
  249. return *this;
  250. }
  251. const CHXCFURL& CHXCFURL::operator =(const FSRef& fsRef)
  252. {
  253. SetToFSRef(fsRef);
  254. return *this;
  255. }
  256. const CHXCFURL& CHXCFURL::operator =(const FSRef* fsRef)
  257. {
  258. SetToFSRef(*fsRef);
  259. return *this;
  260. }
  261. const CHXCFURL& CHXCFURL::operator =(CFURLRef urlRef)
  262. {
  263. ReleaseCFURL();
  264. mCFURLRef = urlRef; // note we're NOT doing an additional retain
  265. UpdateDebugOnlyString();
  266. return *this;
  267. }
  268. const CHXCFURL& CHXCFURL::operator =(const CHXCFURL& othercfurl)
  269. {
  270. ReleaseCFURL();
  271. mCFURLRef = othercfurl.mCFURLRef; // note we're retaining since he'll release it and we'll release it
  272. CFRetain(mCFURLRef);
  273. UpdateDebugOnlyString();
  274. return *this;
  275. }
  276. // Destructor
  277. CHXCFURL::~CHXCFURL()
  278. {
  279. ReleaseCFURL();
  280. }
  281. // Internal utilities
  282. void CHXCFURL::SetToString(const char *pCString)
  283. {
  284. const CFURLRef kNoBaseURL = NULL;
  285. ReleaseCFURL();
  286. // make a CFString then a CFURL from which we can get an FSRef
  287. CHXCFString cfstr(pCString, kCFStringEncodingUTF8);
  288. require(cfstr.IsSet(), CantMakeCFString);
  289. mCFURLRef = ::CFURLCreateWithString(kCFAllocatorDefault, cfstr, kNoBaseURL);
  290. if (mCFURLRef == NULL)
  291. {
  292. // failed; CFURL seems unable to deal with parameters, so try
  293. // stripping those and trying again
  294. CHXString strURL = pCString;
  295. int paramOffset = strURL.Find('?');
  296. if (paramOffset != -1)
  297. {
  298. CHXCFString cfstr2((const char *) strURL.Left(paramOffset), kCFStringEncodingUTF8);
  299. mCFURLRef = ::CFURLCreateWithString(kCFAllocatorDefault, cfstr2, kNoBaseURL);
  300. }
  301. }
  302. UpdateDebugOnlyString();
  303. CantMakeCFString:
  304. return;
  305. }
  306. void CHXCFURL::SetToFSRef(const FSRef& fsRef)
  307. {
  308. ReleaseCFURL();
  309. mCFURLRef = ::CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
  310. UpdateDebugOnlyString();
  311. return;
  312. }
  313. void CHXCFURL::ReleaseCFURL()
  314. {
  315. if (mCFURLRef)
  316. {
  317. ::CFRelease(mCFURLRef);
  318. mCFURLRef = NULL;
  319. }
  320. UpdateDebugOnlyString();
  321. }
  322. #ifdef _DEBUG
  323. void CHXCFURL::UpdateDebugOnlyString()
  324. {
  325. if (mCFURLRef)
  326. {
  327. mStrDebugOnly = ::CFURLGetString(mCFURLRef); // we do NOT release the CFString from CFURLGetString
  328. }
  329. else
  330. {
  331. mStrDebugOnly.Empty();
  332. }
  333. pStrDebugOnlyPeek = (const char *) mStrDebugOnly;
  334. }
  335. #endif // _DEBUG
  336. #endif // _CARBON