home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / TextEdit / DocumentReadWrite.m < prev    next >
Text File  |  1996-11-14  |  11KB  |  241 lines

  1. /*
  2.         DocumentReadWrite.m
  3.     Copyright (c) 1995-1996, NeXT Software, Inc.
  4.         All rights reserved.
  5.         Author: Ali Ozer
  6.  
  7.     You may freely copy, distribute and reuse the code in this example.
  8.     NeXT disclaims any warranty of any kind, expressed or implied,
  9.     as to its fitness for any particular use.
  10.  
  11.         File I/O code... We define a few convenience methods on NSAttributedString as well.
  12. */
  13.  
  14. #import <AppKit/AppKit.h>
  15. #import "Document.h"
  16. #import "Preferences.h"
  17. #import <sys/stat.h>
  18.  
  19. #define IgnoreRichText NO
  20.  
  21. #import <string.h>    // For memcmp()...
  22.  
  23. @interface NSAttributedString(EditExtensions)
  24. - (id)initWithRTF:(NSData *)data viewSize:(NSSize *)size hyphenationFactor:(float *)factor;
  25. - (id)initWithRTFDFile:(NSString *)path viewSize:(NSSize *)size hyphenationFactor:(float *)factor;
  26. - (BOOL)writeRTFDToFile:(NSString *)path updateFilenames:(BOOL)flag viewSize:(NSSize)size hyphenationFactor:(float)factor;
  27. - (NSData *)RTFFromRange:(NSRange)range viewSize:(NSSize)size hyphenationFactor:(float)factor;
  28. @end
  29.             
  30. @implementation NSAttributedString(EditExtensions)
  31.  
  32. static float defaultPadding(void) {
  33.     static float padding = -1;
  34.     if (padding < 0.0) {
  35.         NSTextContainer *container = [[NSTextContainer alloc] init];
  36.         padding = [container lineFragmentPadding];
  37.         [container release];
  38.     }
  39.     return padding;
  40. }
  41.  
  42. - (id)initWithRTF:(NSData *)data viewSize:(NSSize *)size hyphenationFactor:(float *)factor {
  43.     NSDictionary *docAttrs;
  44.     if (self = [self initWithRTF:data documentAttributes:&docAttrs]) {
  45.     NSValue *value;
  46.     NSNumber *number;
  47.         if (size && (value = [docAttrs objectForKey:@"PaperSize"])) {
  48.             *size = [value sizeValue];
  49.             /* The size has the 12 pt padding from old Edit; compensate for that... Note that we should really be getting the margins here! */
  50.             if (size->width > 0 && size->height > 0) size->width = size->width - (6.0 * 2) + (defaultPadding() * 2);
  51.         }
  52.         if (factor && (number = [docAttrs objectForKey:@"HyphenationFactor"])) *factor = [number floatValue];
  53.     }
  54.     return self; 
  55. }
  56.  
  57. - (id)initWithRTFDFile:(NSString *)path viewSize:(NSSize *)size hyphenationFactor:(float *)factor {
  58.     NSDictionary *docAttrs;
  59.     if (self = [self initWithPath:path documentAttributes:&docAttrs]) {
  60.         NSValue *value;
  61.     NSNumber *number;
  62.         if (size && (value = [docAttrs objectForKey:@"PaperSize"])) {
  63.             *size = [value sizeValue];
  64.             /* The size has the 12 pt padding from old Edit; compensate for that... Note that we should really be getting the margins here! */
  65.             if (size->width > 0 && size->height > 0) size->width = size->width - (6.0 * 2) + (defaultPadding() * 2);
  66.         }
  67.         if (factor && (number = [docAttrs objectForKey:@"HyphenationFactor"])) *factor = [number floatValue];
  68.     }
  69.     return self;
  70. }
  71.  
  72. - (BOOL)writeRTFDToFile:(NSString *)path updateFilenames:(BOOL)flag viewSize:(NSSize)size hyphenationFactor:(float)factor {
  73.     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithSize:NSMakeSize(size.width + (6.0 * 2) - (defaultPadding() * 2), size.height)], @"PaperSize", [NSNumber numberWithFloat:factor], @"HyphenationFactor", nil];
  74.     NSFileWrapper *wrapper = [self RTFDFileWrapperFromRange:NSMakeRange(0, [self length]) documentAttributes:dict];
  75.     if (wrapper) {
  76.         return [wrapper writeToFile:path atomically:YES updateFilenames:flag];
  77.     } else {
  78.         return NO;
  79.     }
  80. }
  81.  
  82. - (NSData *)RTFFromRange:(NSRange)range viewSize:(NSSize)size hyphenationFactor:(float)factor {
  83.     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithSize:NSMakeSize(size.width + (6.0 * 2) - (defaultPadding() * 2), size.height)], @"PaperSize", [NSNumber numberWithFloat:factor], @"HyphenationFactor", nil];
  84.     return [self RTFFromRange:range documentAttributes:dict];
  85. }
  86.  
  87. @end
  88.  
  89.  
  90. @implementation Document(ReadWrite)
  91.  
  92. /* Loads from the specified path, sets encoding and textStorage. Note that if the file looks like RTF or RTFD, this method will open the file in rich text mode, regardless of the setting of encoding.
  93. */
  94. - (BOOL)loadFromPath:(NSString *)fileName encoding:(int)encoding {
  95.     NSData *fileContentsAsData = nil;
  96.     NSDictionary *attrs;
  97.     BOOL isDirectory;
  98.     NSString *extension = [fileName pathExtension];
  99.     BOOL success = NO;
  100.     
  101.     if (!(attrs = [[NSFileManager defaultManager] fileAttributesAtPath:fileName traverseLink:YES])) return NO;
  102.  
  103.     isDirectory = [[attrs fileType] isEqualToString:NSFileTypeDirectory];
  104.  
  105.     if (isDirectory) {
  106.         if (![@"rtfd" isEqual:extension]) return NO;    /* If directory, should be .rtfd */
  107.         encoding = RichTextWithGraphicsStringEncoding;
  108.     } else if ([@"rtf" isEqual:extension] && !IgnoreRichText) {    /* If file looks like RTF, ignore any choice they made */
  109.         encoding = RichTextStringEncoding;
  110.     } else if (encoding == UnknownStringEncoding) {
  111.         if (fileContentsAsData = [[NSData alloc] initWithContentsOfFile:fileName]) {
  112.             unsigned len = [fileContentsAsData length];
  113.             const unsigned char *bytes = [fileContentsAsData bytes];
  114.             static const unsigned char bigUnicodeHeader[] = {0xff, 0xfe};
  115.             static const unsigned char littleUnicodeHeader[] = {0xfe, 0xff};
  116.             static const unsigned char rtfHeader[] = {'{', '\\', 'r', 't', 'f'};
  117.             /* Unicode plain text files start with the Unicode BOM char; check for that first... */
  118.             if (((len & 1) == 0) && (len >= 2) && (!memcmp(bytes, bigUnicodeHeader, 2) || !memcmp(bytes, littleUnicodeHeader, 2))) {
  119.                 encoding = NSUnicodeStringEncoding;
  120.             } else if (((len >= 6) && !memcmp(bytes, rtfHeader, 5)) && !IgnoreRichText) {
  121.                 encoding = RichTextStringEncoding;
  122.             }
  123.             if (encoding == UnknownStringEncoding) {
  124.                 encoding = [[Preferences objectForKey:PlainTextEncoding] intValue];
  125.                 if (encoding == UnknownStringEncoding) {
  126.                     encoding = [NSString defaultCStringEncoding];
  127.                 }
  128.             }
  129.         } else {
  130.             return NO;    /* File couldn't be opened, I guess */
  131.         }
  132.     }
  133.     if (encoding == RichTextWithGraphicsStringEncoding) {
  134.     NSSize size = NSZeroSize;
  135.     float factor = 0.0;
  136.         NSTextStorage *newTextStorage = [[NSTextStorage allocWithZone:[self zone]] initWithRTFDFile:fileName viewSize:&size hyphenationFactor:&factor];
  137.         if (newTextStorage) {
  138.             [self setRichText:YES];
  139.             if (size.width > 0 && size.height > 0 && ![self hasMultiplePages]) [self setViewSize:size];
  140.         [self setHyphenationFactor:factor];
  141.             [[self layoutManager] replaceTextStorage:newTextStorage];
  142.             [textStorage release];
  143.             textStorage = newTextStorage;
  144.             success = YES;
  145.         }
  146.     } else {
  147.         if (!fileContentsAsData) fileContentsAsData = [[NSData alloc] initWithContentsOfFile:fileName];
  148.         if (fileContentsAsData) {
  149.             if (encoding == RichTextStringEncoding) {
  150.                 NSSize size = NSZeroSize;
  151.         float factor = 0.0;
  152.                 NSTextStorage *newTextStorage = [[NSTextStorage allocWithZone:[self zone]] initWithRTF:fileContentsAsData viewSize:&size hyphenationFactor:&factor];
  153.                 if (newTextStorage) {
  154.                     [self setRichText:YES];
  155.                     if (size.width > 0 && size.height > 0 && ![self hasMultiplePages]) [self setViewSize:size];
  156.             [self setHyphenationFactor:factor];
  157.                     [[self layoutManager] replaceTextStorage:newTextStorage];
  158.                     [textStorage release];
  159.                     textStorage = newTextStorage;
  160.                     success = YES;
  161.                 }
  162.             } else {
  163.                 NSString *fileContents = [[NSString alloc] initWithData:fileContentsAsData encoding:encoding];
  164.                 if (fileContents) {
  165.                     [textStorage beginEditing];
  166.                     [[textStorage mutableString] setString:fileContents];
  167.                     [self setRichText:NO];
  168.                     [textStorage endEditing];
  169.                     [fileContents release];
  170.                     encodingIfPlainText = encoding;
  171.                     success = YES;
  172.                 }
  173.             }
  174.         }
  175.     }
  176.     [fileContentsAsData release];
  177.  
  178.     return success;
  179. }
  180.  
  181. - (BOOL)saveToPath:(NSString *)fileName encoding:(int)encoding updateFilenames:(BOOL)updateFileNamesFlag {
  182.     BOOL success = NO;
  183.     NSFileManager *fileManager = [NSFileManager defaultManager];
  184.     NSDictionary *curAttributes = [fileManager fileAttributesAtPath:fileName traverseLink:YES];
  185.     NSString *actualFileNameToSave = [fileName stringByResolvingSymlinksInPath];    /* Follow links to save */
  186.  
  187.     if (curAttributes) {    /* If not nil, the file exists... */
  188.         NSString *backupFileName = [actualFileNameToSave stringByAppendingString:@"~"];
  189.     /* If there was a backup file name, delete it */
  190.         if ([fileManager fileExistsAtPath:backupFileName]) {
  191.             (void)[fileManager removeFileAtPath:backupFileName handler:nil];
  192.         }
  193.         /* If the user wishes to keep backups, simply remove the old file aside */
  194.         if (![[Preferences objectForKey:DeleteBackup] boolValue]) {
  195.             (void)[fileManager movePath:actualFileNameToSave toPath:backupFileName handler:nil];
  196.     }
  197.     }
  198.     
  199.     switch (encoding) {
  200.         case RichTextWithGraphicsStringEncoding: {
  201.             success = [textStorage writeRTFDToFile:actualFileNameToSave updateFilenames:updateFileNamesFlag viewSize:[self viewSize] hyphenationFactor:[self hyphenationFactor]];
  202.             break;
  203.         }
  204.         case RichTextStringEncoding: {
  205.             NSData *data = [textStorage RTFFromRange:NSMakeRange(0, [textStorage length]) viewSize:[self viewSize] hyphenationFactor:[self hyphenationFactor]];
  206.         success = data && [data writeToFile:actualFileNameToSave atomically:YES];
  207.             break;
  208.         }
  209.         default: {
  210.             NSData *data = [[textStorage string] dataUsingEncoding:encoding];
  211.         success = data && [data writeToFile:actualFileNameToSave atomically:YES];
  212.             break;
  213.         }
  214.     }
  215.  
  216.     /* Apply the original permissions to the new file. Also make it writable if needed. */
  217.     if (success && curAttributes) {
  218.     id permissions = [curAttributes objectForKey:NSFilePosixPermissions];
  219.         if (permissions) {
  220.             if ([[Preferences objectForKey:SaveFilesWritable] boolValue]) {
  221.                 permissions = [NSNumber numberWithUnsignedLong:([permissions unsignedLongValue] | 0200)];
  222.             }
  223.         [fileManager changeFileAttributes:[NSDictionary dictionaryWithObjectsAndKeys:permissions, NSFilePosixPermissions, nil] atPath:actualFileNameToSave];
  224.         }
  225.     }
  226.     return success;
  227. }
  228.  
  229. @end
  230.  
  231.  
  232. /*
  233.  
  234.  2/21/95 aozer    Created for Edit II.
  235.  4/11/95 aozer    Added some preliminary paper size support
  236.  4/13/95 aozer    Permissions
  237.  7/21/95 aozer    Follow links correctly for saving
  238.  11/7/96 aozer    Read/write hyphenation factor in RTF files
  239.  
  240. */
  241.