Permalink
Browse files

Fix conversion. Add JSON format.

  • Loading branch information...
1 parent 4af4c25 commit 3638c9dc59d975bcbdd44053a2a3b67873fae21d @p2 committed Feb 4, 2014
@@ -521,11 +521,10 @@
C05733CC08A9546B00998B17 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
- ARCHS = "$(NATIVE_ARCH_ACTUAL)";
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "./**";
- MACOSX_DEPLOYMENT_TARGET = 10.5;
+ MACOSX_DEPLOYMENT_TARGET = 10.6;
OTHER_CFLAGS = (
"-DCSV_STRING_EXPORTING",
"-DDEBUG",
@@ -541,19 +540,19 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "./**";
- MACOSX_DEPLOYMENT_TARGET = 10.5;
+ MACOSX_DEPLOYMENT_TARGET = 10.6;
+ OTHER_CFLAGS = "-DCSV_STRING_EXPORTING";
SDKROOT = macosx;
};
name = Release;
};
EE714642113568CA00418030 /* Debug Plus */ = {
isa = XCBuildConfiguration;
buildSettings = {
- ARCHS = "$(NATIVE_ARCH_ACTUAL)";
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "./**";
- MACOSX_DEPLOYMENT_TARGET = 10.5;
+ MACOSX_DEPLOYMENT_TARGET = 10.6;
OTHER_CFLAGS = (
"-DCSV_STRING_EXPORTING",
"-DDEBUG",
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -46,6 +46,7 @@
+ (PPStringFormat *) csvFormat;
+ (PPStringFormat *) tabFormat;
++ (PPStringFormat *) jsonFormat;
+ (PPStringFormat *) flatXMLFormat;
+ (PPStringFormat *) sqlFormat;
@@ -76,11 +76,10 @@ - (id) copyWithZone:(NSZone *)zone
return copy;
}
-#pragma mark -
-#pragma mark NSCoding
+#pragma mark - NSCoding
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [self init]) {
@@ -112,11 +111,10 @@ - (void) encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:headerFormat forKey:@"headerFormat"];
[aCoder encodeObject:valueFormat forKey:@"valueFormat"];
}
-#pragma mark -
-#pragma mark Formatting
+#pragma mark - Formatting
- (NSString *) stringForRows:(NSArray *)csvRows andColumns:(NSArray *)columns;
{
NSMutableString *string = nil;
@@ -129,7 +127,6 @@ - (NSString *) stringForRows:(NSArray *)csvRows andColumns:(NSArray *)columns;
// add rows (if there are any)
if ([csvRows count] > 0) {
NSMutableArray *keys = [NSMutableArray arrayWithCapacity:[columns count]];
- NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
// extract the keys (actually: names) from the columns
for (CSVColumn *column in columns) {
@@ -138,24 +135,22 @@ - (NSString *) stringForRows:(NSArray *)csvRows andColumns:(NSArray *)columns;
}
// loop the rows
- NSUInteger i = 0;
- for (CSVRow *row in csvRows) {
- if (!row.isHeaderRow) {
- [string appendString:[valueFormat rowForKeys:keys values:[row valuesForColumns:columns]]]; // value row
- i++;
- }
- else if (exportHeaders) {
- [string appendString:[headerFormat rowForKeys:keys values:[row valuesForColumns:columns]]]; // header row
- i++;
- }
-
- // let's clean the pool from time to time
- if (0 == i % 50) {
- [myPool release];
- myPool = [[NSAutoreleasePool alloc] init];
+ @autoreleasepool {
+ for (CSVRow *row in csvRows) {
+ if (!row.isHeaderRow) {
+ NSString *rowStr = [valueFormat rowForKeys:keys values:[row valuesForColumns:columns]]; // value row
+ if (rowStr) {
+ [string appendString:rowStr];
+ }
+ }
+ else if (exportHeaders) {
+ NSString *rowStr = [headerFormat rowForKeys:keys values:[row valuesForColumns:columns]]; // header row
+ if (rowStr) {
+ [string appendString:rowStr];
+ }
+ }
}
}
- [myPool release];
}
// append the suffix
@@ -176,11 +171,10 @@ - (NSString *) rowForColumnKeys:(NSArray *)keys values:(NSArray *)values;
{
return [valueFormat rowForKeys:keys values:values];
}
-#pragma mark -
-#pragma mark Loading and Saving from/to file
+#pragma mark - Loading and Saving from/to file
+ (PPStringFormat *) formatFromFile:(NSURL *)aFileURL error:(NSError **)outError
{
NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
@@ -249,11 +243,10 @@ - (BOOL) deleteFile:(NSError **)outError
return YES;
}
-#pragma mark -
-#pragma mark Predefined formats
+#pragma mark - Predefined formats
+ (PPStringFormat *) csvFormat
{
PPStringFormat *myself = [[PPStringFormat alloc] initWithName:@"CSV"];
@@ -418,11 +411,47 @@ + (PPStringFormat *) sqlFormat
return [myself autorelease];
}
-#pragma mark -
+
++ (PPStringFormat *) jsonFormat
+{
+ PPStringFormat *myself = [[PPStringFormat alloc] initWithName:@"json"];
+ myself.systemFormat = YES;
+ myself.name = @"JSON";
+ myself.type = @"json";
+ myself.formatDescription = @"A format that exports your file to JSON";
+ myself.exportHeaders = NO;
+
+ // Setup CSV properties
+ NSArray *keyPairs = [PPStringFormatTransformPair transformPairsFromTo:
+ @"\"", @"\\\"",
+ @"'", @"\\'",
+ @"\\", @"\\\\", nil];
+ NSArray *valuePairs = [PPStringFormatTransformPair transformPairsFromTo:
+ @"\"", @"\\\"",
+ @"'", @"\\'",
+ @"\\", @"\\\\", nil];
+
+ PPStringFormatEntity *valueEntity = [PPStringFormatEntity formatEntity];
+ valueEntity.separator = @",\n\t\t";
+ valueEntity.stringFormat = @"\"$key\":\"$value\"";
+ valueEntity.numberFormat = @"\"$key\":$value";
+ valueEntity.keyTransforms = keyPairs;
+ valueEntity.valueTransforms = valuePairs;
+
+ PPStringFormatRow *row = [PPStringFormatRow formatRow];
+ row.format = @"\t{\n\t\t@values\n\t},\n";
+ row.valueFormat = valueEntity;
+
+ myself.prefix = @"{\"rows\":[\n";
+ myself.suffix = @"]";
+ myself.valueFormat = row;
+
+ return [myself autorelease];
+}
-#pragma mark Utilities
+#pragma mark - Utilities
- (NSString *) description
{
return [NSString stringWithFormat:@"%@ <%p> %@ (system: %i)", NSStringFromClass([self class]), self, name, systemFormat];
@@ -10,6 +10,9 @@
#import <Cocoa/Cocoa.h>
+/**
+ * A string format entity applies transformations to one CSV row.
+ */
@interface PPStringFormatEntity : NSObject <NSCopying, NSCoding> {
NSString *separator; // will be put between each key/value pair
NSString *stringFormat; // e.g. <$key>$value</$key>
@@ -26,7 +29,9 @@
+ (PPStringFormatEntity *) formatEntity;
+/**
+ * Applies the receiver's transformations to the keys (i.e. CSV header names) and values (i.e. CSV row values).
+ */
- (NSString *) stringForKeys:(NSArray *)keys values:(NSArray *)values;
-
@end
@@ -48,11 +48,10 @@ - (void) dealloc
[super dealloc];
}
-#pragma mark -
-#pragma mark NSCoding
+#pragma mark - NSCoding
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [self init]) {
@@ -74,11 +73,10 @@ - (void) encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:keyTransforms forKey:@"keyTransforms"];
[aCoder encodeObject:valueTransforms forKey:@"valueTransforms"];
}
-#pragma mark -
-#pragma mark Formatting
+#pragma mark - Formatting
- (NSString *) stringForKeys:(NSArray *)keys values:(NSArray *)values
{
NSString *string = @"";
@@ -60,5 +60,4 @@
- (IBAction) loadFormatPlugins:(NSError **)outError;
- (BOOL) installFormat:(NSURL *)path error:(NSError **)outError;
-
@end
@@ -48,7 +48,7 @@ @implementation PPStringFormatManager
@synthesize previewPanelController;
-#pragma mark Singleton Overrides
+#pragma mark - Singleton Overrides
static PPStringFormatManager *managerInstance = nil;
+ (PPStringFormatManager *) sharedManager
@@ -126,11 +126,10 @@ - (void) dealloc // will never be called anyway! (Singleton)
[super dealloc];
}
-#pragma mark -
-#pragma mark KVC
+#pragma mark - KVC
- (NSMutableArray *) formats
{
if (nil == formats) {
@@ -154,6 +153,7 @@ - (NSArray *) systemFormats
self.systemFormats = [NSArray arrayWithObjects:
[PPStringFormat csvFormat],
[PPStringFormat tabFormat],
+ [PPStringFormat jsonFormat],
[PPStringFormat flatXMLFormat],
[PPStringFormat sqlFormat],
nil];
@@ -171,16 +171,12 @@ - (void) setSystemFormats:(NSArray *)newFormats
- (PPStringFormat *) selectedFormat
{
NSArray *selectedObjects = [formatController selectedObjects];
- if ([selectedObjects count] > 0) {
- return [selectedObjects objectAtIndex:0];
- }
- return nil;
+ return [selectedObjects firstObject];
}
-#pragma mark -
-#pragma mark Awakening and Closing
+#pragma mark - Awakening and Closing
- (void) awakeFromNib
{
[super awakeFromNib];
@@ -215,17 +211,17 @@ - (void) windowWillClose:(NSNotification *)notification
}
}
}
-#pragma mark -
-#pragma mark Table and Text Delegate
+#pragma mark - Table and Text Delegate
- (void) tableViewSelectionDidChange:(NSNotification *)aNotification
{
// change the subview accordingly
if (formatTable == aNotification.object) {
- if ([self selectedFormat]) {
- BOOL isSystemFormat = [self selectedFormat].isSystemFormat;
+ PPStringFormat *selected = [self selectedFormat];
+ if (selected) {
+ BOOL isSystemFormat = selected.isSystemFormat;
[[detailContainer subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSView *newSubview = isSystemFormat ? systemFormatDetails : userFormatDetails;
@@ -241,11 +237,10 @@ - (void) controlTextDidEndEditing:(NSNotification *)aNotification
{
[self updatePreview:nil];
}
-#pragma mark -
-#pragma mark Actions
+#pragma mark - Actions
- (IBAction) askToRemoveFormat:(id)sender
{
formatToBeRemoved = [[formatController selectedObjects] objectAtIndex:0]; // Error checking, anyone??
@@ -299,11 +294,11 @@ - (IBAction) updatePreview:(id)sender
NSDocumentController *docController = [NSDocumentController sharedDocumentController];
MyDocument *frontDoc = [docController currentDocument];
CSVDocument *csvDoc = frontDoc.csvDocument;
- if (nil != csvDoc) {
+ if (csvDoc) {
// get current format
PPStringFormat *frontFormat = [self selectedFormat];
- if (nil != frontFormat) {
+ if (frontFormat) {
NSIndexSet *testSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 8)];
NSError *myError = nil;
NSString *previewString = [csvDoc stringInFormat:frontFormat withColumns:csvDoc.activeColumns forRowIndexes:testSet includeHeaders:YES error:&myError];
@@ -424,11 +419,10 @@ - (BOOL) installFormat:(NSURL *)source error:(NSError **)outError
}
return NO;
}
-#pragma mark -
-#pragma mark Showing/Hiding
+#pragma mark - Showing/Hiding
+ (void) show:(id)sender
{
PPStringFormatManager *manager = [PPStringFormatManager sharedManager];
@@ -444,11 +438,10 @@ - (NSString *) windowFrameAutosaveName
{
return @"PPStringFormatManager";
}
-#pragma mark -
-#pragma mark Utilities
+#pragma mark - Utilities
- (NSString *) formatPluginPath
{
return [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/CSVManipulator/Formats"];
@@ -18,8 +18,6 @@
@property (nonatomic, copy) NSString *panelTitle;
@property (nonatomic, retain) IBOutlet NSTextView *previewField;
-
- (void) previewString:(NSString *)string;
-
@end
Oops, something went wrong.

0 comments on commit 3638c9d

Please sign in to comment.