/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMFourCharCode.m

http://macfuse.googlecode.com/ · Objective C · 97 lines · 62 code · 17 blank · 18 comment · 8 complexity · 32df27527e42bd03216a9c940cff0b1c MD5 · raw file

  1. //
  2. // GTMFourCharCode.m
  3. // Wrapper for FourCharCodes
  4. //
  5. // Copyright 2008 Google Inc.
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. // use this file except in compliance with the License. You may obtain a copy
  9. // of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. //
  19. #import "GTMDefines.h"
  20. #import "GTMFourCharCode.h"
  21. #import <CoreServices/CoreServices.h>
  22. @implementation GTMFourCharCode
  23. + (id)stringWithFourCharCode:(FourCharCode)code {
  24. return GTMCFAutorelease(UTCreateStringForOSType(code));
  25. }
  26. + (id)fourCharCodeWithString:(NSString*)string {
  27. return [[[self alloc] initWithString:string] autorelease];
  28. }
  29. + (id)fourCharCodeWithFourCharCode:(FourCharCode)code {
  30. return [[[self alloc] initWithFourCharCode:code] autorelease];
  31. }
  32. - (id)initWithString:(NSString*)string {
  33. NSUInteger length = [string length];
  34. if (length == 0 || length > 4) {
  35. [self release];
  36. return nil;
  37. } else {
  38. return [self initWithFourCharCode:UTGetOSTypeFromString((CFStringRef)string)];
  39. }
  40. }
  41. - (id)initWithFourCharCode:(FourCharCode)code {
  42. if ((self = [super init])) {
  43. code_ = code;
  44. }
  45. return self;
  46. }
  47. - (id)initWithCoder:(NSCoder *)aDecoder {
  48. if ((self = [super init])) {
  49. code_ = [aDecoder decodeInt32ForKey:@"FourCharCode"];
  50. }
  51. return self;
  52. }
  53. - (void)encodeWithCoder:(NSCoder *)aCoder {
  54. [aCoder encodeInt32:code_ forKey:@"FourCharCode"];
  55. }
  56. - (id)copyWithZone:(NSZone *)zone {
  57. return [[[self class] alloc] initWithFourCharCode:code_];
  58. }
  59. - (BOOL)isEqual:(id)object {
  60. return [object isKindOfClass:[self class]] && [object fourCharCode] == code_;
  61. }
  62. - (NSUInteger)hash {
  63. return (NSUInteger)code_;
  64. }
  65. - (NSString *)description {
  66. return [NSString stringWithFormat:@"%@ - %@ (0x%lX)",
  67. [self class],
  68. [self stringValue],
  69. (unsigned long)code_];
  70. }
  71. - (FourCharCode)fourCharCode {
  72. return code_;
  73. }
  74. - (NSString*)stringValue {
  75. return GTMCFAutorelease(UTCreateStringForOSType(code_));
  76. }
  77. - (NSNumber*)numberValue {
  78. return [NSNumber numberWithUnsignedInt:code_];
  79. }
  80. @end