Adding an alphagram ivar to Word which is calculated and then stored in the keyed...
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 21 May 2008 20:54:46 +0000 (16:54 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 21 May 2008 20:54:46 +0000 (16:54 -0400)
* Dictionary/Dictionary.m: Create Word objects and archive them rather than NSStrings
* Dictionary/Dictionary.xcodeproj: Switch to C99 mode and include Word.m/h
* Scrabbalize.xcodeproj: Mysterious project updates?
* Source/Word.h: Adding alphagram ivar and getter
* Source/Word.m:
(-[dealloc]): New method
(-[initWithCoder:]): New method
(-[encodeWithCoder:]): New method
(-[alphagram]): New method
(-[createAlphagram]): New method
(-[description]): Include the alphagram in the description

Dictionary/Dictionary.m
Dictionary/Dictionary.xcodeproj/project.pbxproj
Scrabbalize.xcodeproj/project.pbxproj
Source/Word.h
Source/Word.m

index 68ce21606dedde2b0d85e6773488bf73632bd97f..4cd553bfba59c8380a1987720e1bed7dd6248461 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #import <Foundation/Foundation.h>
+#import "Word.h"
 
 int main (int argc, const char * argv[]) 
 {
@@ -31,7 +32,16 @@ int main (int argc, const char * argv[])
        file = [file stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"];
        file = [file stringByReplacingOccurrencesOfString:@"\r" withString:@"\n"];
        
-       NSArray *words = [file componentsSeparatedByString:@"\n"];
+       NSArray *strings = [file componentsSeparatedByString:@"\n"];
+       NSMutableArray *words = [[NSMutableArray alloc] init];
+       for (int i = 0; i < [strings count]; i++)
+       {
+               NSString *str = [strings objectAtIndex:i];
+               Word *word = [[Word alloc] initWithWord:str];
+               [words addObject:word];
+               NSLog(@"%@", word);
+               [word release];
+       }
        
        BOOL result = [NSKeyedArchiver archiveRootObject:words toFile:@"dictionary.ka"];
        if (result)
index a74f49891bb77a076b1910964a12b4d83419b758..7e0bc847a1305ebce327cbbd890934f33615f73e 100644 (file)
@@ -7,6 +7,7 @@
        objects = {
 
 /* Begin PBXBuildFile section */
+               1EB0367F0DE319BE00880828 /* Word.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB0367D0DE319BE00880828 /* Word.m */; };
                8DD76F9A0486AA7600D96B5E /* Dictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* Dictionary.m */; settings = {ATTRIBUTES = (); }; };
                8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
                8DD76F9F0486AA7600D96B5E /* Dictionary.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859EA3029092ED04C91782 /* Dictionary.1 */; };
@@ -28,6 +29,8 @@
 /* Begin PBXFileReference section */
                08FB7796FE84155DC02AAC07 /* Dictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Dictionary.m; sourceTree = "<group>"; };
                08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
+               1EB0367D0DE319BE00880828 /* Word.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Word.m; path = ../Source/Word.m; sourceTree = SOURCE_ROOT; };
+               1EB0367E0DE319BE00880828 /* Word.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Word.h; path = ../Source/Word.h; sourceTree = SOURCE_ROOT; };
                32A70AAB03705E1F00C91783 /* Dictionary_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dictionary_Prefix.pch; sourceTree = "<group>"; };
                8DD76FA10486AA7600D96B5E /* Dictionary */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Dictionary; sourceTree = BUILT_PRODUCTS_DIR; };
                C6859EA3029092ED04C91782 /* Dictionary.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = Dictionary.1; sourceTree = "<group>"; };
@@ -61,6 +64,8 @@
                        children = (
                                32A70AAB03705E1F00C91783 /* Dictionary_Prefix.pch */,
                                08FB7796FE84155DC02AAC07 /* Dictionary.m */,
+                               1EB0367D0DE319BE00880828 /* Word.m */,
+                               1EB0367E0DE319BE00880828 /* Word.h */,
                        );
                        name = Source;
                        sourceTree = "<group>";
                        buildActionMask = 2147483647;
                        files = (
                                8DD76F9A0486AA7600D96B5E /* Dictionary.m in Sources */,
+                               1EB0367F0DE319BE00880828 /* Word.m in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                1DEB927908733DD40010E9CD /* Debug */ = {
                        isa = XCBuildConfiguration;
                        buildSettings = {
+                               GCC_C_LANGUAGE_STANDARD = c99;
                                GCC_WARN_ABOUT_RETURN_TYPE = YES;
                                GCC_WARN_UNUSED_VARIABLE = YES;
                                PREBINDING = NO;
                                        ppc,
                                        i386,
                                );
+                               GCC_C_LANGUAGE_STANDARD = c99;
                                GCC_WARN_ABOUT_RETURN_TYPE = YES;
                                GCC_WARN_UNUSED_VARIABLE = YES;
                                PREBINDING = NO;
index ba3a5f465075054bf5bb09aba7395b1ec0a9ba23..d5f49da8c2ddb484154ede6eda38a9989a3c86cd 100644 (file)
@@ -22,7 +22,7 @@
                089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
                1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
                13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
-               1EAAC7960D16E9950058A23D /* dictionary.ka */ = {isa = PBXFileReference; lastKnownFileType = file; path = dictionary.ka; sourceTree = "<group>"; };
+               1EAAC7960D16E9950058A23D /* dictionary.ka */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = dictionary.ka; sourceTree = "<group>"; };
                1EAAC7990D16E9CE0058A23D /* AppController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppController.h; path = Source/AppController.h; sourceTree = "<group>"; };
                1EAAC79A0D16E9CE0058A23D /* AppController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppController.m; path = Source/AppController.m; sourceTree = "<group>"; };
                1EAAC8400D16F4A40058A23D /* Word.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Word.h; path = Source/Word.h; sourceTree = "<group>"; };
index d3779314fbdde877def9af01cc3c38f7ceb67043..fa286e731c196939c13526d411fc2577b5d7cc19 100644 (file)
 @interface Word : NSObject
 {
        NSString *word;
+       NSString *alphagram;
 }
 
 - (id)initWithWord:(NSString *)word;
 
 - (NSString *)word;
+- (NSString *)alphagram;
 - (int)length;
 
 @end
index 49fa0a0df6ebed8668c8405afb4beea48f7fd6d7..0474923b2987a4ccecd369950497bc83218cdc9d 100644 (file)
 
 #import "Word.h"
 
+@interface Word (Private)
+- (void)createAlphagram;
+@end
+
 
 @implementation Word
 
 {
        if (self = [super init])
        {
-               word = aWord;
+               word = [aWord retain];
        }
        return self;
 }
 
+/**
+ * Destructor
+ */
+- (void)dealloc
+{
+       [word release];
+       [alphagram release];
+       [super dealloc];
+}
+
+/**
+ * Initialize an object from a keyed archive
+ */
+- (id)initWithCoder:(NSCoder *)coder
+{
+       self = [super init];
+       word = [[coder decodeObjectForKey:@"KAWord"] retain];
+       alphagram = [[coder decodeObjectForKey:@"KAAlphagram"] retain];
+       return self;
+}
+
+/**
+ * Encodes an object for serialization in a keyed archiver
+ */
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+       [coder encodeObject:word forKey:@"KAWord"];
+       [coder encodeObject:alphagram forKey:@"KAAlphagram"];
+}
+
 /**
  * Returns the word
  */
        return [word length];
 }
 
+/**
+ * Returns the alphagram
+ */
+- (NSString *)alphagram
+{
+       return alphagram;
+}
+
+/**
+ * Generates the alphagram for the word
+ */
+- (void)createAlphagram
+{
+       char *str = (char *)[word UTF8String];
+       
+       int j;
+       for (int i = 1; i < [word length]; i++)
+       {
+               char temp = str[i];
+               for (j = i; j > 0 && temp < str[j - 1]; j--)
+               {
+                       str[j] = str[j - 1];
+               }
+               str[j] = temp;
+       }
+       
+       alphagram = [[NSString stringWithUTF8String:str] retain];
+}
+
 /**
  * Description method
  */
 - (NSString *)description
 {
-       return [NSString stringWithFormat:@"<Word:%@>", word];
+       return [NSString stringWithFormat:@"<Word:%@/%@>", word, alphagram];
 }
 
 @end