-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxer.m
130 lines (108 loc) · 9.23 KB
/
boxer.m
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// main.m
// boxer
//
// Created by Gavin Eadie on 11/27/12.
//
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError * errorReport;
NSFileManager * defaultFileManager = [NSFileManager defaultManager];
if (argc > 1) {
NSString * filePath = @(argv[1]);
if ([defaultFileManager fileExistsAtPath:filePath]) {
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ read the file ... │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
NSStringEncoding fileEncoding;
NSString * fileContents = [[NSString alloc] initWithContentsOfFile:filePath
usedEncoding:&fileEncoding
error:&errorReport];
if (nil == fileContents) {
NSLog(@"readFromFile error: %@", errorReport);
return 1;
}
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ break up the file into lines ... │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
NSUInteger length = [fileContents length];
NSUInteger lineAlpha = 0;
NSUInteger lineOmega = 0;
NSUInteger contentsEnd = 0;
NSMutableArray * lineArray = [NSMutableArray array];
while (lineOmega < length) {
[fileContents getParagraphStart:&lineAlpha
end:&lineOmega
contentsEnd:&contentsEnd
forRange:NSMakeRange(lineOmega, 0)];
NSRange currentRange = NSMakeRange(lineAlpha, contentsEnd - lineAlpha);
[lineArray addObject:[[fileContents substringWithRange:currentRange] mutableCopy]];
}
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ change the boxes into boxy directives ... │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
NSMutableString * unfoldedString = [[NSMutableString alloc] init];
BOOL inHeavyBox = NO;
NSRange alphaRange, omegaRange, totalRange;
NSString * replacement;
for (__strong NSString * line in lineArray) {
alphaRange = [line rangeOfString:@"+-"];
omegaRange = [line rangeOfString:@"-+"];
if (alphaRange.length > 0 && omegaRange.length > 0 &&
alphaRange.location < omegaRange.location) {
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ... found a line with "+-...-+" in it .. │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
if (inHeavyBox) {
if (NSEqualRanges(totalRange, NSUnionRange(alphaRange, omegaRange))) {
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ... if we are IN the box, and this is the same range close the box and reset │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
replacement = [[@"┗" stringByPaddingToLength:totalRange.length-1
withString:@"━"
startingAtIndex:0]
stringByAppendingString:@"┛"];
inHeavyBox = NO;
}
}
else {
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ... if we are NOT IN the box, note the box-top range and draw the box-top │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
totalRange = NSUnionRange(alphaRange, omegaRange);
replacement = [[@"┏" stringByPaddingToLength:totalRange.length-1
withString:@"━"
startingAtIndex:0]
stringByAppendingString:@"┓"];
inHeavyBox = YES;
}
line = [line stringByReplacingCharactersInRange:totalRange
withString:replacement];
}
else if (inHeavyBox) {
line = [line stringByReplacingOccurrencesOfString:@"|" withString:@"┃"];
}
[unfoldedString appendFormat:@"%@\n", line];
}
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ and write the modified file back out ... │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘*/
if (![unfoldedString writeToFile:filePath
atomically:YES
encoding:fileEncoding
error:&errorReport]) {
NSLog(@"writeToFile error: %@", errorReport);
}
}
else {
NSLog(@"File '%@' doesn't exist.", filePath);
}
}
else {
NSLog(@"No input file specified.");
}
}
return 0;
}