Chromium Updater
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.

79 lines
2.7 KiB

13 years ago
  1. //
  2. // MGMAddons.m
  3. // Chromatic
  4. //
  5. // Created by Mr. Gecko on 6/10/11.
  6. // Copyright (c) 2011 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
  7. //
  8. // Permission to use, copy, modify, and/or distribute this software for any purpose
  9. // with or without fee is hereby granted, provided that the above copyright notice
  10. // and this permission notice appear in all copies.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  13. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  15. // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  16. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  17. // ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. //
  19. #import "MGMAddons.h"
  20. @implementation NSString (MGMAddons)
  21. + (NSString *)readableMemory:(unsigned long)theBytes {
  22. double bytes = theBytes;
  23. NSString *types[] = {@"Bytes", @"KB", @"MB", @"GB", @"TB", @"PB", @"EB", @"ZB", @"YB", @"BB"};
  24. int type = 0;
  25. while (bytes>1024 && type<=9) {
  26. bytes /= 1024;
  27. type++;
  28. }
  29. return [NSString stringWithFormat:@"%.02f %@", bytes, types[type]];
  30. }
  31. + (NSString *)readableTime:(unsigned long)theSeconds {
  32. unsigned long time = theSeconds;
  33. int seconds = time%60;
  34. time = time/60;
  35. int minutes = time%60;
  36. time = time/60;
  37. int hours = time%24;
  38. unsigned long days = time/24;
  39. NSString *string;
  40. if (days!=0) {
  41. string = [NSString stringWithFormat:@"%lu day%@ %02d:%02d:%02d", days, (days==1 ? @"" : @"s"), hours, minutes, seconds];
  42. } else if (hours!=0) {
  43. string = [NSString stringWithFormat:@"%d:%02d:%02d", hours, minutes, seconds];
  44. } else {
  45. string = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
  46. }
  47. return string;
  48. }
  49. @end
  50. @implementation NSURL (MGMAddons)
  51. - (NSURL *)appendPathComponent:(NSString *)theComponent {
  52. NSMutableString *string = [NSMutableString string];
  53. if ([self scheme]!=nil)
  54. [string appendFormat:@"%@://", [self scheme]];
  55. if ([self host]!=nil)
  56. [string appendString:[self host]];
  57. if ([self port]!=0)
  58. [string appendFormat:@":%d", [self port]];
  59. if ([self path]!=nil) {
  60. if (theComponent!=nil) {
  61. [string appendString:[[self path] stringByAppendingPathComponent:theComponent]];
  62. if ([theComponent isEqual:@""] || [theComponent hasSuffix:@"/"])
  63. [string appendString:@"/"];
  64. } else {
  65. [string appendString:[self path]];
  66. if ([[self absoluteString] hasSuffix:@"/"])
  67. [string appendString:@"/"];
  68. }
  69. } else {
  70. [string appendString:[@"/" stringByAppendingPathComponent:theComponent]];
  71. }
  72. if ([self query]!=nil)
  73. [string appendFormat:@"?%@", [self query]];
  74. return [NSURL URLWithString:string];
  75. }
  76. @end