QNUtils.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // QNUtils.m
  3. // QiniuSDK_Mac
  4. //
  5. // Created by yangsen on 2020/3/27.
  6. // Copyright © 2020 Qiniu. All rights reserved.
  7. //
  8. #import "QNUtils.h"
  9. #include <pthread.h>
  10. #import "QNVersion.h"
  11. #if __IPHONE_OS_VERSION_MIN_REQUIRED
  12. #import <UIKit/UIKit.h>
  13. #endif
  14. @implementation QNUtils
  15. + (NSString *)sdkVersion{
  16. return kQiniuVersion;
  17. }
  18. + (NSString *)sdkLanguage{
  19. return @"Object-C";
  20. }
  21. + (int64_t)getCurrentProcessID {
  22. return [[NSProcessInfo processInfo] processIdentifier];
  23. }
  24. + (int64_t)getCurrentThreadID {
  25. __uint64_t threadId = 0;
  26. if (pthread_threadid_np(0, &threadId)) {
  27. threadId = pthread_mach_thread_np(pthread_self());
  28. }
  29. return threadId;
  30. }
  31. + (NSString *)systemName{
  32. NSString *name = nil;
  33. #if __IPHONE_OS_VERSION_MIN_REQUIRED
  34. name = [[UIDevice currentDevice] model];
  35. #else
  36. name = @"Mac OS X";
  37. #endif
  38. return name;
  39. }
  40. + (NSString *)systemVersion{
  41. NSString *version = nil;
  42. #if __IPHONE_OS_VERSION_MIN_REQUIRED
  43. version = [[UIDevice currentDevice] systemVersion];
  44. #else
  45. version = [[NSProcessInfo processInfo] operatingSystemVersionString];
  46. #endif
  47. return version;
  48. }
  49. /// 信号格数
  50. + (NSNumber *)getCurrentSignalStrength{
  51. NSNumber *strength = nil;
  52. return strength;
  53. }
  54. /// 网络类型
  55. + (NSString *)getCurrentNetworkType{
  56. NSString *type = nil;
  57. return type;
  58. }
  59. + (NSTimeInterval)currentTimestamp{
  60. return [[NSDate date] timeIntervalSince1970] * 1000;
  61. }
  62. + (NSString *)sdkDocumentDirectory{
  63. return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"/qiniu"];
  64. }
  65. + (NSString *)sdkCacheDirectory{
  66. return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"/qiniu"];
  67. }
  68. + (NSString *)formEscape:(NSString *)string{
  69. NSString *ret = string;
  70. ret = [ret stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
  71. ret = [ret stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
  72. return ret;
  73. }
  74. + (NSNumber *)dateDuration:(NSDate *)startDate endDate:(NSDate *)endDate {
  75. if (startDate && endDate) {
  76. double time = [endDate timeIntervalSinceDate:startDate] * 1000;
  77. return @(time);
  78. } else {
  79. return nil;
  80. }
  81. }
  82. + (NSNumber *)calculateSpeed:(long long)bytes totalTime:(long long)totalTime {
  83. if (bytes < 0 || totalTime == 0) {
  84. return nil;
  85. }
  86. long long speed = bytes * 1000 / totalTime;
  87. return @(speed);
  88. }
  89. + (NSString *)getIpType:(NSString *)ip host:(NSString *)host{
  90. NSString *type = host;
  91. if (!ip || ip.length == 0) {
  92. return type;
  93. }
  94. if ([ip rangeOfString:@":"].location != NSNotFound) {
  95. type = [self getIPV6StringType:ip host:host];
  96. } else if ([ip rangeOfString:@"."].location != NSNotFound){
  97. type = [self getIPV4StringType:ip host:host];
  98. }
  99. return type;
  100. }
  101. + (NSString *)getIPV4StringType:(NSString *)ipv4String host:(NSString *)host{
  102. NSString *type = nil;
  103. NSArray *ipNumberStrings = [ipv4String componentsSeparatedByString:@"."];
  104. if (ipNumberStrings.count == 4) {
  105. NSInteger firstNumber = [ipNumberStrings.firstObject integerValue];
  106. NSInteger secondNumber = [ipNumberStrings[1] integerValue];
  107. type = [NSString stringWithFormat:@"%ld-%ld",(long)firstNumber, (long)secondNumber];
  108. }
  109. type = [NSString stringWithFormat:@"%@-%@", host ?:@"", type];
  110. return type;
  111. }
  112. + (NSString *)getIPV6StringType:(NSString *)ipv6String host:(NSString *)host{
  113. NSArray *ipNumberStrings = [ipv6String componentsSeparatedByString:@":"];
  114. NSMutableArray *ipNumberStringsReal = [@[@"0000", @"0000", @"0000", @"0000",
  115. @"0000", @"0000", @"0000", @"0000"] mutableCopy];
  116. NSArray *suppleStrings = @[@"0000", @"000", @"00", @"0", @""];
  117. NSInteger i = 0;
  118. while (i < ipNumberStrings.count) {
  119. NSString *ipNumberString = ipNumberStrings[i];
  120. if (ipNumberString.length > 0) {
  121. ipNumberString = [NSString stringWithFormat:@"%@%@", suppleStrings[ipNumberString.length], ipNumberString];
  122. ipNumberStringsReal[i] = ipNumberString;
  123. } else {
  124. break;
  125. }
  126. i++;
  127. }
  128. NSInteger j = ipNumberStrings.count - 1;
  129. NSInteger indexReal = ipNumberStringsReal.count - 1;
  130. while (i < j) {
  131. NSString *ipNumberString = ipNumberStrings[j];
  132. if (ipNumberString.length > 0) {
  133. ipNumberString = [NSString stringWithFormat:@"%@%@", suppleStrings[ipNumberString.length], ipNumberString];
  134. ipNumberStringsReal[indexReal] = ipNumberString;
  135. } else {
  136. break;
  137. }
  138. j--;
  139. indexReal--;
  140. }
  141. NSString *numberInfo = [[ipNumberStringsReal subarrayWithRange:NSMakeRange(0, 4)] componentsJoinedByString:@"-"];
  142. return [NSString stringWithFormat:@"%@-%@-%@", host ?:@"", @"ipv6", numberInfo];
  143. }
  144. @end