stlab.adobe.com Adobe Systems Incorporated

macintosh_string.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /****************************************************************************************************/
00008 
00009 #ifndef ADOBE_MACINTOSH_STRING_HPP
00010 #define ADOBE_MACINTOSH_STRING_HPP
00011 
00012 /****************************************************************************************************/
00013 
00014 #include <adobe/conversion.hpp>
00015 #include <adobe/macintosh_carbon_safe.hpp>
00016 #include <adobe/macintosh_memory.hpp>
00017 #include <adobe/string.hpp>
00018 
00019 /****************************************************************************************************/
00020 
00021 namespace adobe {
00022 
00023 /****************************************************************************************************/
00024 
00025 template<>
00026 inline auto_cfstring_t explicit_cast(const std::string& x)
00027 {
00028     return auto_cfstring_t(::CFStringCreateWithCString(NULL,
00029                                                        x.c_str(),
00030                                                        kCFStringEncodingUTF8));
00031 }
00032 
00033 template<>
00034 inline auto_cfstring_t explicit_cast(const adobe::string_t& x)
00035 {
00036     return auto_cfstring_t(::CFStringCreateWithCString(NULL,
00037                                                        x.c_str(),
00038                                                        kCFStringEncodingUTF8));
00039 }
00040 
00041 template<>
00042 inline auto_cfstring_t explicit_cast(const adobe::string16_t& x)
00043 {
00044     return auto_cfstring_t(::CFStringCreateWithCharacters(NULL,
00045                                                           x.c_str(),
00046                                                           x.size()));
00047 }
00048 
00049 /****************************************************************************************************/
00050 
00051 template<>
00052 inline std::string explicit_cast(const ::CFStringRef& x)
00053 {
00054     if (x == 0)
00055         return std::string();
00056 
00057     const int         pad(16); // must be at least one - seems like a good conservative size
00058     ::CFIndex         max_size(::CFStringGetMaximumSizeForEncoding(::CFStringGetLength(x),
00059                                                                    kCFStringEncodingUTF8) + pad);
00060     std::vector<char> buffer(static_cast<std::size_t>(max_size));
00061 
00062 #ifndef NDEBUG
00063     ::Boolean result =
00064 #endif
00065 
00066     ::CFStringGetCString(x, &buffer[0], max_size, kCFStringEncodingUTF8);
00067 
00068 #ifndef NDEBUG
00069     assert(result);
00070 #endif
00071 
00072     return std::string(&buffer[0]);
00073 }
00074 
00075 /****************************************************************************************************/
00076 
00077 template<>
00078 inline adobe::string_t explicit_cast(const ::CFStringRef& x)
00079 {
00080     if (x == 0)
00081         return adobe::string_t();
00082     
00083     const int         pad(16); // must be at least one - seems like a good conservative size
00084     ::CFIndex         max_size(::CFStringGetMaximumSizeForEncoding(::CFStringGetLength(x),
00085                                                                    kCFStringEncodingUTF8) + pad);
00086     std::vector<char> buffer(static_cast<std::size_t>(max_size));
00087     
00088 #ifndef NDEBUG
00089     ::Boolean result =
00090 #endif
00091         
00092     ::CFStringGetCString(x, &buffer[0], max_size, kCFStringEncodingUTF8);
00093     
00094 #ifndef NDEBUG
00095     assert(result);
00096 #endif
00097     
00098     return adobe::string_t(&buffer[0]);
00099 }
00100 
00101 /****************************************************************************************************/
00102 
00103 template<>
00104 inline adobe::string16_t explicit_cast(const ::CFStringRef& x)
00105 {
00106     if (x == 0)
00107         return adobe::string16_t();
00108     
00109     const int         pad(16); // must be at least one - seems like a good conservative size
00110     ::CFIndex         max_size(::CFStringGetMaximumSizeForEncoding(::CFStringGetLength(x),
00111                                                                    kCFStringEncodingUTF16) + pad);
00112     std::vector<boost::uint16_t> buffer(static_cast<std::size_t>(max_size));
00113     
00114 #ifndef NDEBUG
00115     ::Boolean result =
00116 #endif
00117         
00118     ::CFStringGetCString(x, reinterpret_cast<char*>(&buffer[0]), max_size, kCFStringEncodingUTF16);
00119     
00120 #ifndef NDEBUG
00121     assert(result);
00122 #endif
00123     
00124     return adobe::string16_t(&buffer[0]);
00125 }
00126 
00127 /****************************************************************************************************/
00128 
00129 template<>
00130 inline std::string explicit_cast(const auto_cfstring_t& x)
00131 {
00132     return explicit_cast<std::string>(x.get());
00133 }
00134 
00135 /****************************************************************************************************/
00136 
00137 } // namespace adobe
00138 
00139 /****************************************************************************************************/
00140 
00141 // ADOBE_MACINTOSH_STRING_HPP
00142 #endif
00143 
00144 /****************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google