This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHelper.mm
91 lines (72 loc) · 2.36 KB
/
FileHelper.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// FileHelper.m
// PebbleCubeSDK
//
// Created by Richard Adem on 6/01/11.
// Copyright 2011 PebbleCube. All rights reserved.
#import "FileHelper.h"
#import "JSON.h"
#import "Event.h"
#import "consts.h"
@implementation FileHelper
+ (void) Save:(NSMutableArray*) records
{
Log(@"Loading events in json");
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSError* error = nil;
NSMutableArray* dictArray = [[NSMutableArray alloc] init];
for (Event *event in records)
{
[dictArray addObject: [event info]];
}
NSString* jsonOut = [writer stringWithObject:dictArray error:&error];
if (error != nil)
{
Log(@"Save Events fail: %@", [error localizedDescription]);
}
NSString *fileName = EVENTS_JSON;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
[jsonOut writeToFile:path
atomically:YES
encoding:NSUTF8StringEncoding
error:NULL];
[writer release];
}
+ (void) Load:(NSMutableArray*) records
{
Log(@"Saving events in json");
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSError* error = nil;
NSString *fileName = EVENTS_JSON;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *jsonIn = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
Log(@"jsonIn:\n%@", jsonIn);
if (error != nil)
{
Log(@"Parsing events fail: %@", [error localizedDescription]);
error = nil;
}
NSDictionary *eventObjs = [parser objectWithString:jsonIn error:&error];
if (error != nil)
{
Log(@"Parsing events fail: %@", [error localizedDescription]);
}
for (NSDictionary *eventObj in eventObjs)
{
NSMutableDictionary* info = [[[NSMutableDictionary alloc] init] autorelease];
[info setObject:[eventObj objectForKey:@"code"] forKey:@"code"];
[info setObject:[eventObj objectForKey:@"value"] forKey:@"value"];
[info setObject:[eventObj objectForKey:@"time"] forKey:@"time"];
Event *event = [[[Event alloc] init] autorelease];
[event setInfo: info];
@synchronized(records)
{
[records addObject: event];
}
}
}
@end