OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <cstring> | |
12 #include <memory> | |
13 #include <sstream> | |
14 | |
15 #include <sys/utsname.h> | |
16 | |
17 #include "webrtc/base/checks.h" | |
18 #include "webrtc/base/logging.h" | |
19 #include "webrtc/base/macutils.h" | |
20 #include "webrtc/base/stringutils.h" | |
21 | |
22 namespace rtc { | |
23 | |
24 /////////////////////////////////////////////////////////////////////////////// | |
25 | |
26 bool ToUtf8(const CFStringRef str16, std::string* str8) { | |
27 if ((nullptr == str16) || (nullptr == str8)) { | |
28 return false; | |
29 } | |
30 size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16), | |
31 kCFStringEncodingUTF8) + 1; | |
32 std::unique_ptr<char[]> buffer(new char[maxlen]); | |
33 if (!buffer || !CFStringGetCString(str16, buffer.get(), maxlen, | |
34 kCFStringEncodingUTF8)) { | |
35 return false; | |
36 } | |
37 str8->assign(buffer.get()); | |
38 return true; | |
39 } | |
40 | |
41 bool ToUtf16(const std::string& str8, CFStringRef* str16) { | |
42 if (nullptr == str16) { | |
43 return false; | |
44 } | |
45 *str16 = CFStringCreateWithBytes(kCFAllocatorDefault, | |
46 reinterpret_cast<const UInt8*>(str8.data()), | |
47 str8.length(), kCFStringEncodingUTF8, | |
48 false); | |
49 return nullptr != *str16; | |
50 } | |
51 | |
52 void DecodeFourChar(UInt32 fc, std::string* out) { | |
53 std::stringstream ss; | |
54 ss << '\''; | |
55 bool printable = true; | |
56 for (int i = 3; i >= 0; --i) { | |
57 char ch = (fc >> (8 * i)) & 0xFF; | |
58 if (isprint(static_cast<unsigned char>(ch))) { | |
59 ss << ch; | |
60 } else { | |
61 printable = false; | |
62 break; | |
63 } | |
64 } | |
65 if (printable) { | |
66 ss << '\''; | |
67 } else { | |
68 ss.str(""); | |
69 ss << "0x" << std::hex << fc; | |
70 } | |
71 out->append(ss.str()); | |
72 } | |
73 | |
74 static bool GetOSVersion(int* major, int* minor, int* bugfix) { | |
75 RTC_DCHECK(major && minor && bugfix); | |
76 struct utsname uname_info; | |
77 if (uname(&uname_info) != 0) | |
78 return false; | |
79 | |
80 if (strcmp(uname_info.sysname, "Darwin") != 0) | |
81 return false; | |
82 *major = 10; | |
83 | |
84 // The market version of macOS is always 4 lower than the internal version. | |
85 int minor_version = atoi(uname_info.release); | |
86 RTC_CHECK(minor_version >= 6); | |
87 *minor = minor_version - 4; | |
88 | |
89 const char* dot = ::strchr(uname_info.release, '.'); | |
90 if (!dot) | |
91 return false; | |
92 *bugfix = atoi(dot + 1); | |
93 return true; | |
94 } | |
95 | |
96 MacOSVersionName GetOSVersionName() { | |
97 int major = 0, minor = 0, bugfix = 0; | |
98 if (!GetOSVersion(&major, &minor, &bugfix)) { | |
99 return kMacOSUnknown; | |
100 } | |
101 if (major > 10) { | |
102 return kMacOSNewer; | |
103 } | |
104 if ((major < 10) || (minor < 3)) { | |
105 return kMacOSOlder; | |
106 } | |
107 switch (minor) { | |
108 case 3: | |
109 return kMacOSPanther; | |
110 case 4: | |
111 return kMacOSTiger; | |
112 case 5: | |
113 return kMacOSLeopard; | |
114 case 6: | |
115 return kMacOSSnowLeopard; | |
116 case 7: | |
117 return kMacOSLion; | |
118 case 8: | |
119 return kMacOSMountainLion; | |
120 case 9: | |
121 return kMacOSMavericks; | |
122 } | |
123 return kMacOSNewer; | |
124 } | |
125 } // namespace rtc | |
OLD | NEW |