1 //===-- CFCString.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 #include "CFCString.h"
14 //----------------------------------------------------------------------
15 // CFCString constructor
16 //----------------------------------------------------------------------
17 CFCString::CFCString(CFStringRef s) :
18 CFCReleaser<CFStringRef> (s)
22 //----------------------------------------------------------------------
23 // CFCString copy constructor
24 //----------------------------------------------------------------------
25 CFCString::CFCString(const CFCString& rhs) :
26 CFCReleaser<CFStringRef> (rhs)
31 //----------------------------------------------------------------------
32 // CFCString copy constructor
33 //----------------------------------------------------------------------
35 CFCString::operator=(const CFCString& rhs)
42 CFCString::CFCString (const char *cstr, CFStringEncoding cstr_encoding) :
43 CFCReleaser<CFStringRef> ()
47 reset(::CFStringCreateWithCString(kCFAllocatorDefault, cstr, cstr_encoding));
51 //----------------------------------------------------------------------
53 //----------------------------------------------------------------------
54 CFCString::~CFCString()
59 CFCString::GetFileSystemRepresentation(std::string& s)
61 return CFCString::FileSystemRepresentation(get(), s);
65 CFCString::SetFileSystemRepresentation (const char *path)
67 CFStringRef new_value = NULL;
69 new_value = ::CFStringCreateWithFileSystemRepresentation (kCFAllocatorDefault, path);
76 CFCString::SetFileSystemRepresentationFromCFType (CFTypeRef cf_type)
78 CFStringRef new_value = NULL;
81 CFTypeID cf_type_id = ::CFGetTypeID(cf_type);
83 if (cf_type_id == ::CFStringGetTypeID())
85 // Retain since we are using the existing object
86 new_value = (CFStringRef)::CFRetain(cf_type);
88 else if (cf_type_id == ::CFURLGetTypeID())
90 new_value = ::CFURLCopyFileSystemPath((CFURLRef)cf_type, kCFURLPOSIXPathStyle);
98 CFCString::SetFileSystemRepresentationAndExpandTilde (const char *path)
100 std::string expanded_path;
101 if (CFCString::ExpandTildeInPath(path, expanded_path))
102 SetFileSystemRepresentation(expanded_path.c_str());
109 CFCString::UTF8(std::string& str)
111 return CFCString::UTF8(get(), str);
114 // Static function that puts a copy of the UTF8 contents of CF_STR into STR
115 // and returns the C string pointer that is contained in STR when successful, else
116 // NULL is returned. This allows the std::string parameter to own the extracted string,
117 // and also allows that string to be returned as a C string pointer that can be used.
120 CFCString::UTF8 (CFStringRef cf_str, std::string& str)
124 const CFStringEncoding encoding = kCFStringEncodingUTF8;
125 CFIndex max_utf8_str_len = CFStringGetLength (cf_str);
126 max_utf8_str_len = CFStringGetMaximumSizeForEncoding (max_utf8_str_len, encoding);
127 if (max_utf8_str_len > 0)
129 str.resize(max_utf8_str_len);
132 if (CFStringGetCString (cf_str, &str[0], str.size(), encoding))
134 str.resize(strlen(str.c_str()));
144 CFCString::ExpandTildeInPath(const char* path, std::string &expanded_path)
147 if (::glob (path, GLOB_TILDE, NULL, &globbuf) == 0)
149 expanded_path = globbuf.gl_pathv[0];
150 ::globfree (&globbuf);
153 expanded_path.clear();
155 return expanded_path.c_str();
158 // Static function that puts a copy of the file system representation of CF_STR
159 // into STR and returns the C string pointer that is contained in STR when
160 // successful, else NULL is returned. This allows the std::string parameter
161 // to own the extracted string, and also allows that string to be returned as
162 // a C string pointer that can be used.
165 CFCString::FileSystemRepresentation (CFStringRef cf_str, std::string& str)
169 CFIndex max_length = ::CFStringGetMaximumSizeOfFileSystemRepresentation (cf_str);
172 str.resize(max_length);
175 if (::CFStringGetFileSystemRepresentation (cf_str, &str[0], str.size()))
177 str.erase(::strlen(str.c_str()));
189 CFCString::GetLength() const
191 CFStringRef str = get();
193 return CFStringGetLength (str);