summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohan Paulsson <gonz@users.sourceforge.net>2013-01-27 17:12:47 +0000
committerJohan Paulsson <gonz@users.sourceforge.net>2013-01-27 17:12:47 +0000
commitd854a633d1fe573692baa8d6a2f460e1fb167f1c (patch)
tree38867376cccf8dac02fe7cadd90917c888d62a08
parentd35a186932e95b979bb947110a2e7d2cca41fcc9 (diff)
NSFormatter subclass for validating user input of tikz code
-rw-r--r--tikzit/src/osx/TikzFormatter.h29
-rw-r--r--tikzit/src/osx/TikzFormatter.m92
2 files changed, 121 insertions, 0 deletions
diff --git a/tikzit/src/osx/TikzFormatter.h b/tikzit/src/osx/TikzFormatter.h
new file mode 100644
index 0000000..4d9ec04
--- /dev/null
+++ b/tikzit/src/osx/TikzFormatter.h
@@ -0,0 +1,29 @@
+//
+// NSTikzFormatter.h
+// TikZiT
+//
+// Created by Karl Johan Paulsson on 27/01/2013.
+// Copyright (c) 2013 Aleks Kissinger. All rights reserved.
+//
+//
+// This file is part of TikZiT.
+//
+// TikZiT is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// TikZiT is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with TikZiT. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface TikzFormatter : NSFormatter
+
+@end
diff --git a/tikzit/src/osx/TikzFormatter.m b/tikzit/src/osx/TikzFormatter.m
new file mode 100644
index 0000000..ecbb0bc
--- /dev/null
+++ b/tikzit/src/osx/TikzFormatter.m
@@ -0,0 +1,92 @@
+//
+// NSTikzFormatter.m
+// TikZiT
+//
+// Created by Karl Johan Paulsson on 27/01/2013.
+// Copyright (c) 2013 Aleks Kissinger. All rights reserved.
+//
+//
+// This file is part of TikZiT.
+//
+// TikZiT is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// TikZiT is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with TikZiT. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#import "TikzFormatter.h"
+#import "TikzGraphAssembler.h"
+
+@implementation TikzFormatter
+
+- (NSString *)stringForObjectValue:(id)obj{
+ if (![obj isKindOfClass:[NSString class]]) {
+ return @"";
+ }
+
+ return [NSString stringWithString:obj];
+}
+
+- (BOOL)getObjectValue:(out id *)obj forString:(NSString *)string errorDescription:(out NSString **)error{
+ *obj = [NSString stringWithString:string];
+
+ TikzGraphAssembler *ass = [[TikzGraphAssembler alloc] init];
+ BOOL r = [ass testTikz:string];
+
+ if (!r && error)
+ *error = NSLocalizedString(@"Invalid input, couldn't parse value.", @"tikz user input error");
+
+ return r;
+}
+
+- (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error{
+ NSRange addedRange;
+ NSString *addedString;
+
+ addedRange = NSMakeRange(origSelRange.location, proposedSelRangePtr->location - origSelRange.location);
+ addedString = [*partialStringPtr substringWithRange: addedRange];
+
+ if([addedString isEqualToString:@"{"]){
+ NSString *s = [[NSString stringWithString:*partialStringPtr] stringByAppendingString:@"}"];
+ *partialStringPtr = s;
+
+ return NO;
+ }
+
+ if([addedString isEqualToString:@"}"]){
+ NSScanner *scanner = [NSScanner scannerWithString:*partialStringPtr];
+
+ NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:@"{}"];
+ NSMutableString *strippedString = [NSMutableString stringWithCapacity:[*partialStringPtr length]];
+
+ while ([scanner isAtEnd] == NO) {
+ NSString *buffer;
+ if ([scanner scanCharactersFromSet:cs intoString:&buffer]) {
+ [strippedString appendString:buffer];
+
+ } else {
+ [scanner setScanLocation:([scanner scanLocation] + 1)];
+ }
+ }
+
+ [cs autorelease];
+ [scanner autorelease];
+ [strippedString autorelease];
+
+ if([strippedString length] % 2 == 1){
+ return NO;
+ }
+ }
+
+ return YES;
+}
+
+@end