From d854a633d1fe573692baa8d6a2f460e1fb167f1c Mon Sep 17 00:00:00 2001 From: Johan Paulsson Date: Sun, 27 Jan 2013 17:12:47 +0000 Subject: NSFormatter subclass for validating user input of tikz code --- tikzit/src/osx/TikzFormatter.h | 29 +++++++++++++ tikzit/src/osx/TikzFormatter.m | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tikzit/src/osx/TikzFormatter.h create mode 100644 tikzit/src/osx/TikzFormatter.m 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 . +// + +#import + +@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 . +// + +#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 -- cgit v1.2.3