project_files/HedgewarsMobile/Classes/NSStringExtra.m
author koda
Sat, 25 Sep 2010 03:04:22 +0200
changeset 3902 3aac7ca07b0e
parent 3891 f8f0d0ceb19c
permissions -rw-r--r--
have Saves restart net when loaded, also resumes music fix saving on hwmobile
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
3902
3aac7ca07b0e have Saves restart net when loaded, also resumes music
koda
parents: 3891
diff changeset
    38
-(BOOL) appendToFile:(NSString *)path usingStream:(NSOutputStream *)os {
3aac7ca07b0e have Saves restart net when loaded, also resumes music
koda
parents: 3891
diff changeset
    39
    NSData *allData = [self dataUsingEncoding:NSUTF8StringEncoding];
3aac7ca07b0e have Saves restart net when loaded, also resumes music
koda
parents: 3891
diff changeset
    40
    [os write:[allData bytes] maxLength:[allData length]];
3aac7ca07b0e have Saves restart net when loaded, also resumes music
koda
parents: 3891
diff changeset
    41
    return YES;
3aac7ca07b0e have Saves restart net when loaded, also resumes music
koda
parents: 3891
diff changeset
    42
}
3891
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    43
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    44
// by http://iphonedevelopment.blogspot.com/2010/08/nsstring-appendtofileusingencoding.html
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    45
-(BOOL) appendToFile:(NSString *)path usingEncoding:(NSStringEncoding) encoding {
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    46
    NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path]; 
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    47
    if (fh == nil)
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    48
        return [self writeToFile:path atomically:YES encoding:encoding error:nil];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    49
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    50
    [fh truncateFileAtOffset:[fh seekToEndOfFile]];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    51
    NSData *encoded = [self dataUsingEncoding:encoding];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    52
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    53
    if (encoded == nil) 
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    54
        return NO;
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    55
    
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    56
    [fh writeData:encoded];
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    57
    return YES;
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    58
}
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    59
f8f0d0ceb19c initial support for savegames
koda
parents:
diff changeset
    60
@end