project_files/HedgewarsMobile/Classes/NSStringExtra.m
author koda
Wed, 22 Sep 2010 01:10:20 +0200
changeset 3891 f8f0d0ceb19c
child 3902 3aac7ca07b0e
permissions -rw-r--r--
initial support for savegames
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3891
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     1
/*
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     4
 *
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     8
 *
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    12
 * GNU General Public License for more details.
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    13
 *
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    17
 *
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    18
 * File created on 21/09/2010.
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    19
 */
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    20
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    21
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    22
#import "NSStringExtra.h"
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    23
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    24
@implementation NSString (extra)
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    25
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    26
-(BOOL) appendToFile:(NSString *)path {
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    27
    NSOutputStream* os = [[NSOutputStream alloc] initToFileAtPath:path append:YES];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    28
    NSData *allData = [self dataUsingEncoding:NSUTF8StringEncoding];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    29
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    30
    [os open];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    31
    [os write:[allData bytes] maxLength:[allData length]];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    32
    [os close];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    33
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    34
    [os release];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    35
    return YES;
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    36
}
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    37
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    38
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    39
// by http://iphonedevelopment.blogspot.com/2010/08/nsstring-appendtofileusingencoding.html
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    40
-(BOOL) appendToFile:(NSString *)path usingEncoding:(NSStringEncoding) encoding {
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    41
    NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path]; 
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    42
    if (fh == nil)
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    43
        return [self writeToFile:path atomically:YES encoding:encoding error:nil];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    44
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    45
    [fh truncateFileAtOffset:[fh seekToEndOfFile]];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    46
    NSData *encoded = [self dataUsingEncoding:encoding];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    47
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    48
    if (encoded == nil) 
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    49
        return NO;
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    50
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    51
    [fh writeData:encoded];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    52
    return YES;
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    53
}
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    54
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    55
@end