1 //===-- CFString.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Created by Greg Clayton on 1/16/08.
12 //===----------------------------------------------------------------------===//
18 //----------------------------------------------------------------------
19 // CFString constructor
20 //----------------------------------------------------------------------
21 CFString::CFString(CFStringRef s) : CFReleaser<CFStringRef>(s) {}
23 //----------------------------------------------------------------------
24 // CFString copy constructor
25 //----------------------------------------------------------------------
26 CFString::CFString(const CFString &rhs) : CFReleaser<CFStringRef>(rhs) {}
28 //----------------------------------------------------------------------
29 // CFString copy constructor
30 //----------------------------------------------------------------------
31 CFString &CFString::operator=(const CFString &rhs) {
37 CFString::CFString(const char *cstr, CFStringEncoding cstr_encoding)
38 : CFReleaser<CFStringRef>() {
39 if (cstr && cstr[0]) {
41 ::CFStringCreateWithCString(kCFAllocatorDefault, cstr, cstr_encoding));
45 //----------------------------------------------------------------------
47 //----------------------------------------------------------------------
48 CFString::~CFString() {}
50 const char *CFString::GetFileSystemRepresentation(std::string &s) {
51 return CFString::FileSystemRepresentation(get(), s);
54 CFStringRef CFString::SetFileSystemRepresentation(const char *path) {
55 CFStringRef new_value = NULL;
58 ::CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path);
63 CFStringRef CFString::SetFileSystemRepresentationFromCFType(CFTypeRef cf_type) {
64 CFStringRef new_value = NULL;
65 if (cf_type != NULL) {
66 CFTypeID cf_type_id = ::CFGetTypeID(cf_type);
68 if (cf_type_id == ::CFStringGetTypeID()) {
69 // Retain since we are using the existing object
70 new_value = (CFStringRef)::CFRetain(cf_type);
71 } else if (cf_type_id == ::CFURLGetTypeID()) {
73 ::CFURLCopyFileSystemPath((CFURLRef)cf_type, kCFURLPOSIXPathStyle);
81 CFString::SetFileSystemRepresentationAndExpandTilde(const char *path) {
82 std::string expanded_path;
83 if (CFString::GlobPath(path, expanded_path))
84 SetFileSystemRepresentation(expanded_path.c_str());
90 const char *CFString::UTF8(std::string &str) {
91 return CFString::UTF8(get(), str);
94 // Static function that puts a copy of the UTF8 contents of CF_STR into STR
95 // and returns the C string pointer that is contained in STR when successful,
97 // NULL is returned. This allows the std::string parameter to own the extracted
99 // and also allows that string to be returned as a C string pointer that can be
102 const char *CFString::UTF8(CFStringRef cf_str, std::string &str) {
104 const CFStringEncoding encoding = kCFStringEncodingUTF8;
105 CFIndex max_utf8_str_len = CFStringGetLength(cf_str);
107 CFStringGetMaximumSizeForEncoding(max_utf8_str_len, encoding);
108 if (max_utf8_str_len > 0) {
109 str.resize(max_utf8_str_len);
111 if (CFStringGetCString(cf_str, &str[0], str.size(), encoding)) {
112 str.resize(strlen(str.c_str()));
121 // Static function that puts a copy of the file system representation of CF_STR
122 // into STR and returns the C string pointer that is contained in STR when
123 // successful, else NULL is returned. This allows the std::string parameter
124 // to own the extracted string, and also allows that string to be returned as
125 // a C string pointer that can be used.
127 const char *CFString::FileSystemRepresentation(CFStringRef cf_str,
131 ::CFStringGetMaximumSizeOfFileSystemRepresentation(cf_str);
132 if (max_length > 0) {
133 str.resize(max_length);
135 if (::CFStringGetFileSystemRepresentation(cf_str, &str[0],
137 str.erase(::strlen(str.c_str()));
147 CFIndex CFString::GetLength() const {
148 CFStringRef str = get();
150 return CFStringGetLength(str);
154 const char *CFString::GlobPath(const char *path, std::string &expanded_path) {
156 if (::glob(path, GLOB_TILDE, NULL, &globbuf) == 0) {
157 expanded_path = globbuf.gl_pathv[0];
158 ::globfree(&globbuf);
160 expanded_path.clear();
162 return expanded_path.c_str();