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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
FormatScanner.m
Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
All rights reserved.
Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
This file is part of the Foundation Extensions Library.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
We disclaim all warranties with regard to this software, including all
implied warranties of merchantability and fitness, in no event shall
we be liable for any special, indirect or consequential damages or any
damages whatsoever resulting from loss of use, data or profits, whether in
an action of contract, negligence or other tortious action, arising out of
or in connection with the use or performance of this software.
*/
#include <ctype.h>
#include "common.h"
#import <Foundation/NSDictionary.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSCharacterSet.h>
#include <extensions/FormatScanner.h>
@implementation FormatScanner
enum { SPECIFIER_SIZE = 1000 }; /* This value should be sufficient */
- init
{
currentSpecifier = Malloc(specifierSize = SPECIFIER_SIZE);
boolFlags.allowFlags = YES;
boolFlags.allowWidth = YES;
boolFlags.allowPeriod = YES;
boolFlags.allowPrecision = YES;
boolFlags.allowModifier = YES;
return self;
}
- (void)dealloc
{
Free(currentSpecifier);
[handler release];
[super dealloc];
}
- (void)setFormatScannerHandler:(id)anObject
{
[anObject retain];
[handler release];
handler = anObject;
}
- setAllowOnlySpecifier:(BOOL)flag
{
boolFlags.allowFlags = !flag;
boolFlags.allowWidth = !flag;
boolFlags.allowPeriod = !flag;
boolFlags.allowPrecision = !flag;
boolFlags.allowModifier = !flag;
return self;
}
- setAllowFlags:(BOOL)flag { boolFlags.allowFlags = flag; return self; }
- setAllowWidth:(BOOL)flag { boolFlags.allowWidth = flag; return self; }
- setAllowPeriod:(BOOL)flag { boolFlags.allowPeriod = flag; return self; }
- setAllowPrecision:(BOOL)flag { boolFlags.allowPrecision = flag; return self;}
- setAllowModifier:(BOOL)flag { boolFlags.allowModifier = flag; return self; }
- (id)formatScannerHandler { return handler; }
- (unsigned int)flags { return flags; }
- (int)width { return width; }
- (int)precision { return precision; }
- (char)modifier { return modifier; }
- (char)characterSpecifier { return characterSpecifier; }
- (const char*)currentSpecifier { return currentSpecifier; }
- (BOOL)allowFlags { return boolFlags.allowFlags; }
- (BOOL)allowWidth { return boolFlags.allowWidth; }
- (BOOL)allowPeriod { return boolFlags.allowPeriod; }
- (BOOL)allowPrecision { return boolFlags.allowPrecision; }
- (BOOL)allowModifier { return boolFlags.allowModifier; }
#define CHECK_END \
if(i >= length) { \
/* An unterminated specifier. Break the loop. */ \
[self handleOrdinaryString: \
[NSString stringWithCString:currentSpecifier]]; \
goto _return; \
}
/* Scans the format string looking after specifiers. Doesn't handle '*' as a
valid width or precision. */
- (BOOL)parseFormatString:(NSString*)format context:(void*)context
{
NSRange searchRange, foundRange;
int i = 0, length = [format length];
unichar ch;
NSCharacterSet* decimals = [NSCharacterSet decimalDigitCharacterSet];
*currentSpecifier = 0;
specifierLen = 0;
while(i < length) {
searchRange.location = i;
searchRange.length = length - i;
foundRange = [format rangeOfString:@"%" options:0 range:searchRange];
if (foundRange.length == 0)
foundRange.location = length;
searchRange.length = foundRange.location - searchRange.location;
if (searchRange.length) {
if (![self handleOrdinaryString:
[format substringWithRange:searchRange]])
return NO;
}
i = foundRange.location;
CHECK_END
i++;
strcpy(currentSpecifier, "%");
specifierLen = 1;
CHECK_END
flags = width = precision = modifier = characterSpecifier = 0;
/* Check for flags. */
if(boolFlags.allowFlags) {
for(; i < length; i++) {
ch = [format characterAtIndex:i];
switch(ch) {
case '#': strcat(currentSpecifier, "#");
flags |= FS_ALTERNATE_FORM;
break;
case '0': strcat(currentSpecifier, "0");
flags |= FS_ZERO;
break;
case '-': strcat(currentSpecifier, "-");
flags |= FS_MINUS_SIGN;
break;
case '+': strcat(currentSpecifier, "+");
flags |= FS_PLUS_SIGN;
break;
case ' ': strcat(currentSpecifier, " ");
flags |= FS_BLANK;
break;
default: goto quit;
}
if(++specifierLen == specifierSize)
currentSpecifier = Realloc(currentSpecifier,
specifierSize += SPECIFIER_SIZE);
}
quit:
CHECK_END
}
/* Check for width. */
if(boolFlags.allowWidth) {
for(; i < length; i++) {
char str[2] = { 0, 0 };
ch = [format characterAtIndex:i];
if (![decimals characterIsMember:ch])
break;
str[0] = ch;
strcat(currentSpecifier, str);
if(++specifierLen == specifierSize)
currentSpecifier = Realloc(currentSpecifier,
specifierSize += SPECIFIER_SIZE);
width = 10 * width + (ch - '0');
}
CHECK_END
}
/* Check for period. */
if(boolFlags.allowPeriod) {
ch = [format characterAtIndex:i];
if(ch == '.') {
char str[2] = { ch, 0 };
strcat(currentSpecifier, str);
if(++specifierLen == specifierSize)
currentSpecifier = Realloc(currentSpecifier,
specifierSize += SPECIFIER_SIZE);
i++;
CHECK_END
}
}
/* Check for precision. */
if(boolFlags.allowPrecision) {
for(; i < length; i++) {
char str[2] = { 0, 0 };
ch = [format characterAtIndex:i];
if (![decimals characterIsMember:ch])
break;
str[0] = ch;
strcat(currentSpecifier, str);
if(++specifierLen == specifierSize)
currentSpecifier = Realloc(currentSpecifier,
specifierSize += SPECIFIER_SIZE);
precision = 10 * precision + (ch - '0');
}
CHECK_END
}
/* Check for data-width modifier. */
if(boolFlags.allowModifier) {
ch = [format characterAtIndex:i];
if(ch == 'h' || ch == 'l') {
char str[2] = { ch, 0 };
strcat(currentSpecifier, str);
if(++specifierLen == specifierSize)
currentSpecifier = Realloc(currentSpecifier,
specifierSize += SPECIFIER_SIZE);
modifier = ch;
i++;
CHECK_END
}
}
/* Finally, the conversion character. */
{
char str[2] = { 0, 0 };
ch = [format characterAtIndex:i];
str[0] = ch;
strcat(currentSpecifier, str);
if(++specifierLen == specifierSize)
currentSpecifier = Realloc(currentSpecifier,
specifierSize += SPECIFIER_SIZE);
characterSpecifier = ch;
}
if (![self handleFormatSpecifierWithContext:context])
return NO;
i++;
*currentSpecifier = 0;
specifierLen = 0;
CHECK_END
}
_return:
return YES;
}
- (BOOL)handleOrdinaryString:(NSString*)string
{
return YES;
}
- (BOOL)handleFormatSpecifierWithContext:(void*)context
{
return YES;
}
@end /* FormatScanner */
|