summaryrefslogtreecommitdiff
path: root/tikzit/src/osx/TikzFormatter.m
blob: 897270603a19ba3ac67ccd9020c8490b15ebeaaa (plain)
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
//
//  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];
    
	BOOL r = [TikzGraphAssembler validateTikzPropertyNameOrValue: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