1 //===-- CFCString.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 // CFCString constructor
14 CFCString::CFCString(CFStringRef s
) : CFCReleaser
<CFStringRef
>(s
) {}
16 // CFCString copy constructor
17 CFCString::CFCString(const CFCString
&rhs
) = default;
19 // CFCString copy constructor
20 CFCString
&CFCString::operator=(const CFCString
&rhs
) {
26 CFCString::CFCString(const char *cstr
, CFStringEncoding cstr_encoding
)
27 : CFCReleaser
<CFStringRef
>() {
28 if (cstr
&& cstr
[0]) {
30 ::CFStringCreateWithCString(kCFAllocatorDefault
, cstr
, cstr_encoding
));
35 CFCString::~CFCString() = default;
37 const char *CFCString::GetFileSystemRepresentation(std::string
&s
) {
38 return CFCString::FileSystemRepresentation(get(), s
);
41 CFStringRef
CFCString::SetFileSystemRepresentation(const char *path
) {
42 CFStringRef new_value
= NULL
;
45 ::CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault
, path
);
51 CFCString::SetFileSystemRepresentationFromCFType(CFTypeRef cf_type
) {
52 CFStringRef new_value
= NULL
;
53 if (cf_type
!= NULL
) {
54 CFTypeID cf_type_id
= ::CFGetTypeID(cf_type
);
56 if (cf_type_id
== ::CFStringGetTypeID()) {
57 // Retain since we are using the existing object
58 new_value
= (CFStringRef
)::CFRetain(cf_type
);
59 } else if (cf_type_id
== ::CFURLGetTypeID()) {
61 ::CFURLCopyFileSystemPath((CFURLRef
)cf_type
, kCFURLPOSIXPathStyle
);
69 CFCString::SetFileSystemRepresentationAndExpandTilde(const char *path
) {
70 std::string expanded_path
;
71 if (CFCString::ExpandTildeInPath(path
, expanded_path
))
72 SetFileSystemRepresentation(expanded_path
.c_str());
78 const char *CFCString::UTF8(std::string
&str
) {
79 return CFCString::UTF8(get(), str
);
82 // Static function that puts a copy of the UTF8 contents of CF_STR into STR and
83 // returns the C string pointer that is contained in STR when successful, else
84 // NULL is returned. This allows the std::string parameter to own the extracted
86 // and also allows that string to be returned as a C string pointer that can be
89 const char *CFCString::UTF8(CFStringRef cf_str
, std::string
&str
) {
91 const CFStringEncoding encoding
= kCFStringEncodingUTF8
;
92 CFIndex max_utf8_str_len
= CFStringGetLength(cf_str
);
94 CFStringGetMaximumSizeForEncoding(max_utf8_str_len
, encoding
);
95 if (max_utf8_str_len
> 0) {
96 str
.resize(max_utf8_str_len
);
98 if (CFStringGetCString(cf_str
, &str
[0], str
.size(), encoding
)) {
99 str
.resize(strlen(str
.c_str()));
108 const char *CFCString::ExpandTildeInPath(const char *path
,
109 std::string
&expanded_path
) {
111 if (::glob(path
, GLOB_TILDE
, NULL
, &globbuf
) == 0) {
112 expanded_path
= globbuf
.gl_pathv
[0];
113 ::globfree(&globbuf
);
115 expanded_path
.clear();
117 return expanded_path
.c_str();
120 // Static function that puts a copy of the file system representation of CF_STR
121 // into STR and returns the C string pointer that is contained in STR when
122 // successful, else NULL is returned. This allows the std::string parameter to
123 // own the extracted string, and also allows that string to be returned as a C
124 // string pointer that can be used.
126 const char *CFCString::FileSystemRepresentation(CFStringRef cf_str
,
130 ::CFStringGetMaximumSizeOfFileSystemRepresentation(cf_str
);
131 if (max_length
> 0) {
132 str
.resize(max_length
);
134 if (::CFStringGetFileSystemRepresentation(cf_str
, &str
[0],
136 str
.erase(::strlen(str
.c_str()));
146 CFIndex
CFCString::GetLength() const {
147 CFStringRef str
= get();
149 return CFStringGetLength(str
);