Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/macosx/cfcpp/CFCString.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- CFCString.cpp -----------------------------------------------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "CFCString.h"
10
#include <glob.h>
11
#include <string>
12
13
// CFCString constructor
14
2
CFCString::CFCString(CFStringRef s) : CFCReleaser<CFStringRef>(s) {}
15
16
// CFCString copy constructor
17
0
CFCString::CFCString(const CFCString &rhs) = default;
18
19
// CFCString copy constructor
20
0
CFCString &CFCString::operator=(const CFCString &rhs) {
21
0
  if (this != &rhs)
22
0
    *this = rhs;
23
0
  return *this;
24
0
}
25
26
CFCString::CFCString(const char *cstr, CFStringEncoding cstr_encoding)
27
10
    : CFCReleaser<CFStringRef>() {
28
10
  if (cstr && cstr[0]) {
29
10
    reset(
30
10
        ::CFStringCreateWithCString(kCFAllocatorDefault, cstr, cstr_encoding));
31
10
  }
32
10
}
33
34
// Destructor
35
12
CFCString::~CFCString() = default;
36
37
0
const char *CFCString::GetFileSystemRepresentation(std::string &s) {
38
0
  return CFCString::FileSystemRepresentation(get(), s);
39
0
}
40
41
2
CFStringRef CFCString::SetFileSystemRepresentation(const char *path) {
42
2
  CFStringRef new_value = NULL;
43
2
  if (path && path[0])
44
2
    new_value =
45
2
        ::CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path);
46
2
  reset(new_value);
47
2
  return get();
48
2
}
49
50
CFStringRef
51
0
CFCString::SetFileSystemRepresentationFromCFType(CFTypeRef cf_type) {
52
0
  CFStringRef new_value = NULL;
53
0
  if (cf_type != NULL) {
54
0
    CFTypeID cf_type_id = ::CFGetTypeID(cf_type);
55
56
0
    if (cf_type_id == ::CFStringGetTypeID()) {
57
      // Retain since we are using the existing object
58
0
      new_value = (CFStringRef)::CFRetain(cf_type);
59
0
    } else if (cf_type_id == ::CFURLGetTypeID()) {
60
0
      new_value =
61
0
          ::CFURLCopyFileSystemPath((CFURLRef)cf_type, kCFURLPOSIXPathStyle);
62
0
    }
63
0
  }
64
0
  reset(new_value);
65
0
  return get();
66
0
}
67
68
CFStringRef
69
0
CFCString::SetFileSystemRepresentationAndExpandTilde(const char *path) {
70
0
  std::string expanded_path;
71
0
  if (CFCString::ExpandTildeInPath(path, expanded_path))
72
0
    SetFileSystemRepresentation(expanded_path.c_str());
73
0
  else
74
0
    reset();
75
0
  return get();
76
0
}
77
78
0
const char *CFCString::UTF8(std::string &str) {
79
0
  return CFCString::UTF8(get(), str);
80
0
}
81
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
85
// string,
86
// and also allows that string to be returned as a C string pointer that can be
87
// used.
88
89
0
const char *CFCString::UTF8(CFStringRef cf_str, std::string &str) {
90
0
  if (cf_str) {
91
0
    const CFStringEncoding encoding = kCFStringEncodingUTF8;
92
0
    CFIndex max_utf8_str_len = CFStringGetLength(cf_str);
93
0
    max_utf8_str_len =
94
0
        CFStringGetMaximumSizeForEncoding(max_utf8_str_len, encoding);
95
0
    if (max_utf8_str_len > 0) {
96
0
      str.resize(max_utf8_str_len);
97
0
      if (!str.empty()) {
98
0
        if (CFStringGetCString(cf_str, &str[0], str.size(), encoding)) {
99
0
          str.resize(strlen(str.c_str()));
100
0
          return str.c_str();
101
0
        }
102
0
      }
103
0
    }
104
0
  }
105
0
  return NULL;
106
0
}
107
108
const char *CFCString::ExpandTildeInPath(const char *path,
109
0
                                         std::string &expanded_path) {
110
0
  glob_t globbuf;
111
0
  if (::glob(path, GLOB_TILDE, NULL, &globbuf) == 0) {
112
0
    expanded_path = globbuf.gl_pathv[0];
113
0
    ::globfree(&globbuf);
114
0
  } else
115
0
    expanded_path.clear();
116
117
0
  return expanded_path.c_str();
118
0
}
119
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.
125
126
const char *CFCString::FileSystemRepresentation(CFStringRef cf_str,
127
18
                                                std::string &str) {
128
18
  if (cf_str) {
129
18
    CFIndex max_length =
130
18
        ::CFStringGetMaximumSizeOfFileSystemRepresentation(cf_str);
131
18
    if (max_length > 0) {
132
18
      str.resize(max_length);
133
18
      if (!str.empty()) {
134
18
        if (::CFStringGetFileSystemRepresentation(cf_str, &str[0],
135
18
                                                  str.size())) {
136
18
          str.erase(::strlen(str.c_str()));
137
18
          return str.c_str();
138
18
        }
139
18
      }
140
18
    }
141
18
  }
142
0
  str.erase();
143
0
  return NULL;
144
18
}
145
146
0
CFIndex CFCString::GetLength() const {
147
0
  CFStringRef str = get();
148
0
  if (str)
149
0
    return CFStringGetLength(str);
150
0
  return 0;
151
0
}