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) :
22 CFReleaser<CFStringRef> (s)
26 //----------------------------------------------------------------------
27 // CFString copy constructor
28 //----------------------------------------------------------------------
29 CFString::CFString(const CFString& rhs) :
30 CFReleaser<CFStringRef> (rhs)
35 //----------------------------------------------------------------------
36 // CFString copy constructor
37 //----------------------------------------------------------------------
39 CFString::operator=(const CFString& rhs)
46 CFString::CFString (const char *cstr, CFStringEncoding cstr_encoding) :
47 CFReleaser<CFStringRef> ()
51 reset(::CFStringCreateWithCString(kCFAllocatorDefault, cstr, cstr_encoding));
55 //----------------------------------------------------------------------
57 //----------------------------------------------------------------------
63 CFString::GetFileSystemRepresentation(std::string& s)
65 return CFString::FileSystemRepresentation(get(), s);
69 CFString::SetFileSystemRepresentation (const char *path)
71 CFStringRef new_value = NULL;
73 new_value = ::CFStringCreateWithFileSystemRepresentation (kCFAllocatorDefault, path);
80 CFString::SetFileSystemRepresentationFromCFType (CFTypeRef cf_type)
82 CFStringRef new_value = NULL;
85 CFTypeID cf_type_id = ::CFGetTypeID(cf_type);
87 if (cf_type_id == ::CFStringGetTypeID())
89 // Retain since we are using the existing object
90 new_value = (CFStringRef)::CFRetain(cf_type);
92 else if (cf_type_id == ::CFURLGetTypeID())
94 new_value = ::CFURLCopyFileSystemPath((CFURLRef)cf_type, kCFURLPOSIXPathStyle);
102 CFString::SetFileSystemRepresentationAndExpandTilde (const char *path)
104 std::string expanded_path;
105 if (CFString::GlobPath(path, expanded_path))
106 SetFileSystemRepresentation(expanded_path.c_str());
113 CFString::UTF8(std::string& str)
115 return CFString::UTF8(get(), str);
118 // Static function that puts a copy of the UTF8 contents of CF_STR into STR
119 // and returns the C string pointer that is contained in STR when successful, else
120 // NULL is returned. This allows the std::string parameter to own the extracted string,
121 // and also allows that string to be returned as a C string pointer that can be used.
124 CFString::UTF8 (CFStringRef cf_str, std::string& str)
128 const CFStringEncoding encoding = kCFStringEncodingUTF8;
129 CFIndex max_utf8_str_len = CFStringGetLength (cf_str);
130 max_utf8_str_len = CFStringGetMaximumSizeForEncoding (max_utf8_str_len, encoding);
131 if (max_utf8_str_len > 0)
133 str.resize(max_utf8_str_len);
136 if (CFStringGetCString (cf_str, &str[0], str.size(), encoding))
138 str.resize(strlen(str.c_str()));
147 // Static function that puts a copy of the file system representation of CF_STR
148 // into STR and returns the C string pointer that is contained in STR when
149 // successful, else NULL is returned. This allows the std::string parameter
150 // to own the extracted string, and also allows that string to be returned as
151 // a C string pointer that can be used.
154 CFString::FileSystemRepresentation (CFStringRef cf_str, std::string& str)
158 CFIndex max_length = ::CFStringGetMaximumSizeOfFileSystemRepresentation (cf_str);
161 str.resize(max_length);
164 if (::CFStringGetFileSystemRepresentation (cf_str, &str[0], str.size()))
166 str.erase(::strlen(str.c_str()));
178 CFString::GetLength() const
180 CFStringRef str = get();
182 return CFStringGetLength (str);
188 CFString::GlobPath(const char* path, std::string &expanded_path)
191 if (::glob (path, GLOB_TILDE, NULL, &globbuf) == 0)
193 expanded_path = globbuf.gl_pathv[0];
194 ::globfree (&globbuf);
197 expanded_path.clear();
199 return expanded_path.c_str();