File: FormatScanner.h

package info (click to toggle)
libfoundation1.0 1.0.84-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,132 kB
  • ctags: 1,584
  • sloc: objc: 41,781; ansic: 3,935; sh: 2,979; perl: 171; makefile: 71
file content (127 lines) | stat: -rw-r--r-- 4,242 bytes parent folder | download | duplicates (11)
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
/* 
   FormatScanner.h

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   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.
*/

#ifndef __FormatScanner_h__
#define __FormatScanner_h__

#include <stdarg.h>
#include <Foundation/NSObject.h>

@class NSString;

/*
 * A FormatScanner scans a NSString format. When it reaches a specifier
 * similar to printf(3S), it calls a handler object previously registered to
 * it. The handler object is usually inherited from the DefaultScannerHandler
 * class. The handler object maintains a mapping between format characters and
 * selectors that have to be called when a specifier is found in the format
 * string. The selector must have exactly two arguments: the first one is a
 * pointer to a va_list and the second one is the format scanner. It is the
 * responsability of handler to increase the pointer to the va_list with the
 * size of the object it handles. During the execution of the method the
 * scanner calls, the handler can ask the scanner about the flags, width,
 * precision, modifiers and the specifier character. The method should return a
 * NSString object that represents the converted object.
 *
 * FormatScanner is an abstract class. Use the PrintfFormatScanner class that
 * is used to write printf-like functions. Additional classes can be inherited
 * to write scanf-like functions.
 */

typedef enum {
    FS_ALTERNATE_FORM	= 1,
    FS_ZERO		= 2,
    FS_MINUS_SIGN	= 4,
    FS_PLUS_SIGN	= 8,
    FS_BLANK		= 16
} FormatScannerFlags;

@interface FormatScanner : NSObject
{
    int		specifierLen, specifierSize;
    char        *currentSpecifier;
    id		handler;
    unsigned	flags;
    int		width;
    int		precision;
    char	modifier;
    char	characterSpecifier;
    BOOL	allowFlags:1;
    BOOL	allowWidth:1;
    BOOL	allowPeriod:1;
    BOOL	allowPrecision:1;
    BOOL	allowModifier:1;
}

/* This method start the searching of specifiers in `format'. `context' is
   passed in handleFormatSpecifierWithContext: unmodified. */
- (BOOL)parseFormatString:(NSString*)format context:(void*)context;

/* This method is called whenever a string between two specifiers is found.
   Rewrite it in subclasses to perform whatever action you want (for example
   to collect them in a result string if you're doing printf). The method 
   should return NO if the scanning of format should stop. */
- (BOOL)handleOrdinaryString:(NSString*)string;

/* This method is called whenever a format specifier is found in `format'.
   Again, rewrite this method in subclasses to perform whatever action you
   want. The method should return NO if the scanning of the format should stop.
*/
- (BOOL)handleFormatSpecifierWithContext:(void*)context;

- (void)setFormatScannerHandler:(id)anObject;
- (id)formatScannerHandler;

- (unsigned int)flags;
- (int)width;
- (int)precision;
- (char)modifier;
- (char)characterSpecifier;
- (const char*)currentSpecifier;

- (id)setAllowFlags:(BOOL)flag;
- (id)setAllowWidth:(BOOL)flag;
- (id)setAllowPeriod:(BOOL)flag;
- (id)setAllowPrecision:(BOOL)flag;
- (id)setAllowModifier:(BOOL)flag;

/* A shorthand for sending all -setAllow* messages with !flag as argument */
- (id)setAllowOnlySpecifier:(BOOL)flag;

- (BOOL)allowFlags;
- (BOOL)allowWidth;
- (BOOL)allowPeriod;
- (BOOL)allowPrecision;
- (BOOL)allowModifier;

@end

#endif /* __FormatScanner_h__ */

/*
  Local Variables:
  c-basic-offset: 4
  tab-width: 8
  End:
*/