diff options
Diffstat (limited to 'source/Host/macosx/cfcpp/CFCString.cpp')
-rw-r--r-- | source/Host/macosx/cfcpp/CFCString.cpp | 230 |
1 files changed, 98 insertions, 132 deletions
diff --git a/source/Host/macosx/cfcpp/CFCString.cpp b/source/Host/macosx/cfcpp/CFCString.cpp index 81a96b824999..0d3853c60a72 100644 --- a/source/Host/macosx/cfcpp/CFCString.cpp +++ b/source/Host/macosx/cfcpp/CFCString.cpp @@ -8,151 +8,123 @@ //===----------------------------------------------------------------------===// #include "CFCString.h" -#include <string> #include <glob.h> +#include <string> //---------------------------------------------------------------------- // CFCString constructor //---------------------------------------------------------------------- -CFCString::CFCString(CFStringRef s) : - CFCReleaser<CFStringRef> (s) -{ -} +CFCString::CFCString(CFStringRef s) : CFCReleaser<CFStringRef>(s) {} //---------------------------------------------------------------------- // CFCString copy constructor //---------------------------------------------------------------------- -CFCString::CFCString(const CFCString& rhs) : - CFCReleaser<CFStringRef> (rhs) -{ - -} +CFCString::CFCString(const CFCString &rhs) : CFCReleaser<CFStringRef>(rhs) {} //---------------------------------------------------------------------- // CFCString copy constructor //---------------------------------------------------------------------- -CFCString& -CFCString::operator=(const CFCString& rhs) -{ - if (this != &rhs) - *this = rhs; - return *this; +CFCString &CFCString::operator=(const CFCString &rhs) { + if (this != &rhs) + *this = rhs; + return *this; } -CFCString::CFCString (const char *cstr, CFStringEncoding cstr_encoding) : - CFCReleaser<CFStringRef> () -{ - if (cstr && cstr[0]) - { - reset(::CFStringCreateWithCString(kCFAllocatorDefault, cstr, cstr_encoding)); - } +CFCString::CFCString(const char *cstr, CFStringEncoding cstr_encoding) + : CFCReleaser<CFStringRef>() { + if (cstr && cstr[0]) { + reset( + ::CFStringCreateWithCString(kCFAllocatorDefault, cstr, cstr_encoding)); + } } //---------------------------------------------------------------------- // Destructor //---------------------------------------------------------------------- -CFCString::~CFCString() -{ -} +CFCString::~CFCString() {} -const char * -CFCString::GetFileSystemRepresentation(std::string& s) -{ - return CFCString::FileSystemRepresentation(get(), s); +const char *CFCString::GetFileSystemRepresentation(std::string &s) { + return CFCString::FileSystemRepresentation(get(), s); } -CFStringRef -CFCString::SetFileSystemRepresentation (const char *path) -{ - CFStringRef new_value = NULL; - if (path && path[0]) - new_value = ::CFStringCreateWithFileSystemRepresentation (kCFAllocatorDefault, path); - reset(new_value); - return get(); +CFStringRef CFCString::SetFileSystemRepresentation(const char *path) { + CFStringRef new_value = NULL; + if (path && path[0]) + new_value = + ::CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path); + reset(new_value); + return get(); } - CFStringRef -CFCString::SetFileSystemRepresentationFromCFType (CFTypeRef cf_type) -{ - CFStringRef new_value = NULL; - if (cf_type != NULL) - { - CFTypeID cf_type_id = ::CFGetTypeID(cf_type); - - if (cf_type_id == ::CFStringGetTypeID()) - { - // Retain since we are using the existing object - new_value = (CFStringRef)::CFRetain(cf_type); - } - else if (cf_type_id == ::CFURLGetTypeID()) - { - new_value = ::CFURLCopyFileSystemPath((CFURLRef)cf_type, kCFURLPOSIXPathStyle); - } +CFCString::SetFileSystemRepresentationFromCFType(CFTypeRef cf_type) { + CFStringRef new_value = NULL; + if (cf_type != NULL) { + CFTypeID cf_type_id = ::CFGetTypeID(cf_type); + + if (cf_type_id == ::CFStringGetTypeID()) { + // Retain since we are using the existing object + new_value = (CFStringRef)::CFRetain(cf_type); + } else if (cf_type_id == ::CFURLGetTypeID()) { + new_value = + ::CFURLCopyFileSystemPath((CFURLRef)cf_type, kCFURLPOSIXPathStyle); } - reset(new_value); - return get(); + } + reset(new_value); + return get(); } CFStringRef -CFCString::SetFileSystemRepresentationAndExpandTilde (const char *path) -{ - std::string expanded_path; - if (CFCString::ExpandTildeInPath(path, expanded_path)) - SetFileSystemRepresentation(expanded_path.c_str()); - else - reset(); - return get(); +CFCString::SetFileSystemRepresentationAndExpandTilde(const char *path) { + std::string expanded_path; + if (CFCString::ExpandTildeInPath(path, expanded_path)) + SetFileSystemRepresentation(expanded_path.c_str()); + else + reset(); + return get(); } -const char * -CFCString::UTF8(std::string& str) -{ - return CFCString::UTF8(get(), str); +const char *CFCString::UTF8(std::string &str) { + return CFCString::UTF8(get(), str); } // Static function that puts a copy of the UTF8 contents of CF_STR into STR -// and returns the C string pointer that is contained in STR when successful, else -// NULL is returned. This allows the std::string parameter to own the extracted string, -// and also allows that string to be returned as a C string pointer that can be used. - -const char * -CFCString::UTF8 (CFStringRef cf_str, std::string& str) -{ - if (cf_str) - { - const CFStringEncoding encoding = kCFStringEncodingUTF8; - CFIndex max_utf8_str_len = CFStringGetLength (cf_str); - max_utf8_str_len = CFStringGetMaximumSizeForEncoding (max_utf8_str_len, encoding); - if (max_utf8_str_len > 0) - { - str.resize(max_utf8_str_len); - if (!str.empty()) - { - if (CFStringGetCString (cf_str, &str[0], str.size(), encoding)) - { - str.resize(strlen(str.c_str())); - return str.c_str(); - } - } +// and returns the C string pointer that is contained in STR when successful, +// else +// NULL is returned. This allows the std::string parameter to own the extracted +// string, +// and also allows that string to be returned as a C string pointer that can be +// used. + +const char *CFCString::UTF8(CFStringRef cf_str, std::string &str) { + if (cf_str) { + const CFStringEncoding encoding = kCFStringEncodingUTF8; + CFIndex max_utf8_str_len = CFStringGetLength(cf_str); + max_utf8_str_len = + CFStringGetMaximumSizeForEncoding(max_utf8_str_len, encoding); + if (max_utf8_str_len > 0) { + str.resize(max_utf8_str_len); + if (!str.empty()) { + if (CFStringGetCString(cf_str, &str[0], str.size(), encoding)) { + str.resize(strlen(str.c_str())); + return str.c_str(); } + } } - return NULL; + } + return NULL; } -const char* -CFCString::ExpandTildeInPath(const char* path, std::string &expanded_path) -{ - glob_t globbuf; - if (::glob (path, GLOB_TILDE, NULL, &globbuf) == 0) - { - expanded_path = globbuf.gl_pathv[0]; - ::globfree (&globbuf); - } - else - expanded_path.clear(); +const char *CFCString::ExpandTildeInPath(const char *path, + std::string &expanded_path) { + glob_t globbuf; + if (::glob(path, GLOB_TILDE, NULL, &globbuf) == 0) { + expanded_path = globbuf.gl_pathv[0]; + ::globfree(&globbuf); + } else + expanded_path.clear(); - return expanded_path.c_str(); + return expanded_path.c_str(); } // Static function that puts a copy of the file system representation of CF_STR @@ -161,35 +133,29 @@ CFCString::ExpandTildeInPath(const char* path, std::string &expanded_path) // to own the extracted string, and also allows that string to be returned as // a C string pointer that can be used. -const char * -CFCString::FileSystemRepresentation (CFStringRef cf_str, std::string& str) -{ - if (cf_str) - { - CFIndex max_length = ::CFStringGetMaximumSizeOfFileSystemRepresentation (cf_str); - if (max_length > 0) - { - str.resize(max_length); - if (!str.empty()) - { - if (::CFStringGetFileSystemRepresentation (cf_str, &str[0], str.size())) - { - str.erase(::strlen(str.c_str())); - return str.c_str(); - } - } +const char *CFCString::FileSystemRepresentation(CFStringRef cf_str, + std::string &str) { + if (cf_str) { + CFIndex max_length = + ::CFStringGetMaximumSizeOfFileSystemRepresentation(cf_str); + if (max_length > 0) { + str.resize(max_length); + if (!str.empty()) { + if (::CFStringGetFileSystemRepresentation(cf_str, &str[0], + str.size())) { + str.erase(::strlen(str.c_str())); + return str.c_str(); } + } } - str.erase(); - return NULL; + } + str.erase(); + return NULL; } - -CFIndex -CFCString::GetLength() const -{ - CFStringRef str = get(); - if (str) - return CFStringGetLength (str); - return 0; +CFIndex CFCString::GetLength() const { + CFStringRef str = get(); + if (str) + return CFStringGetLength(str); + return 0; } |