You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2009, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef OSX
  28. #include <CoreFoundation/CoreFoundation.h>
  29. #include "talk/base/logging.h"
  30. #include "talk/base/macconversion.h"
  31. bool p_convertHostCFStringRefToCPPString(
  32. const CFStringRef cfstr, std::string& cppstr) {
  33. bool result = false;
  34. // First this must be non-null,
  35. if (NULL != cfstr) {
  36. // it must actually *be* a CFString, and not something just masquerading
  37. // as one,
  38. if (CFGetTypeID(cfstr) == CFStringGetTypeID()) {
  39. // and we must be able to get the characters out of it.
  40. // (The cfstr owns this buffer; it came from somewhere else,
  41. // so someone else gets to take care of getting rid of the cfstr,
  42. // and then this buffer will go away automatically.)
  43. unsigned length = CFStringGetLength(cfstr);
  44. char* buf = new char[1 + length];
  45. if (CFStringGetCString(cfstr, buf, 1 + length, kCFStringEncodingASCII)) {
  46. if (strlen(buf) == length) {
  47. cppstr.assign(buf);
  48. result = true;
  49. }
  50. }
  51. delete [] buf;
  52. }
  53. }
  54. return result;
  55. }
  56. bool p_convertCFNumberToInt(CFNumberRef cfn, int* i) {
  57. bool converted = false;
  58. // It must not be null.
  59. if (NULL != cfn) {
  60. // It must actually *be* a CFNumber and not something just masquerading
  61. // as one.
  62. if (CFGetTypeID(cfn) == CFNumberGetTypeID()) {
  63. CFNumberType ntype = CFNumberGetType(cfn);
  64. switch (ntype) {
  65. case kCFNumberSInt8Type:
  66. SInt8 sint8;
  67. converted = CFNumberGetValue(cfn, ntype, static_cast<void*>(&sint8));
  68. if (converted) *i = static_cast<int>(sint8);
  69. break;
  70. case kCFNumberSInt16Type:
  71. SInt16 sint16;
  72. converted = CFNumberGetValue(cfn, ntype, static_cast<void*>(&sint16));
  73. if (converted) *i = static_cast<int>(sint16);
  74. break;
  75. case kCFNumberSInt32Type:
  76. SInt32 sint32;
  77. converted = CFNumberGetValue(cfn, ntype, static_cast<void*>(&sint32));
  78. if (converted) *i = static_cast<int>(sint32);
  79. break;
  80. case kCFNumberSInt64Type:
  81. SInt64 sint64;
  82. converted = CFNumberGetValue(cfn, ntype, static_cast<void*>(&sint64));
  83. if (converted) *i = static_cast<int>(sint64);
  84. break;
  85. case kCFNumberFloat32Type:
  86. Float32 float32;
  87. converted = CFNumberGetValue(cfn, ntype,
  88. static_cast<void*>(&float32));
  89. if (converted) *i = static_cast<int>(float32);
  90. break;
  91. case kCFNumberFloat64Type:
  92. Float64 float64;
  93. converted = CFNumberGetValue(cfn, ntype,
  94. static_cast<void*>(&float64));
  95. if (converted) *i = static_cast<int>(float64);
  96. break;
  97. case kCFNumberCharType:
  98. char charvalue;
  99. converted = CFNumberGetValue(cfn, ntype,
  100. static_cast<void*>(&charvalue));
  101. if (converted) *i = static_cast<int>(charvalue);
  102. break;
  103. case kCFNumberShortType:
  104. short shortvalue;
  105. converted = CFNumberGetValue(cfn, ntype,
  106. static_cast<void*>(&shortvalue));
  107. if (converted) *i = static_cast<int>(shortvalue);
  108. break;
  109. case kCFNumberIntType:
  110. int intvalue;
  111. converted = CFNumberGetValue(cfn, ntype,
  112. static_cast<void*>(&intvalue));
  113. if (converted) *i = static_cast<int>(intvalue);
  114. break;
  115. case kCFNumberLongType:
  116. long longvalue;
  117. converted = CFNumberGetValue(cfn, ntype,
  118. static_cast<void*>(&longvalue));
  119. if (converted) *i = static_cast<int>(longvalue);
  120. break;
  121. case kCFNumberLongLongType:
  122. long long llvalue;
  123. converted = CFNumberGetValue(cfn, ntype,
  124. static_cast<void*>(&llvalue));
  125. if (converted) *i = static_cast<int>(llvalue);
  126. break;
  127. case kCFNumberFloatType:
  128. float floatvalue;
  129. converted = CFNumberGetValue(cfn, ntype,
  130. static_cast<void*>(&floatvalue));
  131. if (converted) *i = static_cast<int>(floatvalue);
  132. break;
  133. case kCFNumberDoubleType:
  134. double doublevalue;
  135. converted = CFNumberGetValue(cfn, ntype,
  136. static_cast<void*>(&doublevalue));
  137. if (converted) *i = static_cast<int>(doublevalue);
  138. break;
  139. case kCFNumberCFIndexType:
  140. CFIndex cfindex;
  141. converted = CFNumberGetValue(cfn, ntype,
  142. static_cast<void*>(&cfindex));
  143. if (converted) *i = static_cast<int>(cfindex);
  144. break;
  145. default:
  146. LOG(LS_ERROR) << "got unknown type.";
  147. break;
  148. }
  149. }
  150. }
  151. return converted;
  152. }
  153. bool p_isCFNumberTrue(CFNumberRef cfn) {
  154. // We assume it's false until proven otherwise.
  155. bool result = false;
  156. int asInt;
  157. bool converted = p_convertCFNumberToInt(cfn, &asInt);
  158. if (converted && (0 != asInt)) {
  159. result = true;
  160. }
  161. return result;
  162. }
  163. #endif // OSX