Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
StringReader.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <wchar.h>
29
30#include "StringReader.h"
31
32#define _Class _StringReader
33
34#pragma mark - Object
35
39static Object *copy(const Object *self) {
40
41 StringReader *this = (StringReader *) self;
42
43 return (Object *) $(alloc(StringReader), initWithString, this->string);
44}
45
49static void dealloc(Object *self) {
50
51 StringReader *this = (StringReader *) self;
52
53 release(this->string);
54
55 super(Object, self, dealloc);
56}
57
58#pragma mark - StringReader
59
64static StringReader *initWithCharacters(StringReader *self, const char *chars) {
65
66 String *string = str(chars);
67
68 self = $(self, initWithString, string);
69
70 release(string);
71
72 return self;
73}
74
80
81 self = (StringReader *) super(Object, self, init);
82 if (self) {
83 self->string = retain(string);
84 $(self, reset);
85 }
86 return self;
87}
88
94 Unicode c;
95
96 const int bytes = mbtowc(&c, self->head, MB_CUR_MAX);
97 if (bytes > 0) {
98 if (mode == StringReaderRead) {
99 self->head += bytes;
100 }
101 return c;
102 }
103
104 return READER_EOF;
105}
106
111static Unicode peek(StringReader *self) {
112 return $(self, next, StringReaderPeek);
113}
114
120 return $(self, next, StringReaderRead);
121}
122
127static String *readToken(StringReader *self, const Unicode *charset, Unicode *stop) {
128
129 while (true) {
130 const Unicode c = $(self, peek);
131 if (c == READER_EOF || wcschr(charset, c) == NULL) {
132 break;
133 }
134 $(self, read);
135 }
136
137 const char *start = self->head;
138
139 while (true) {
140 const Unicode c = $(self, peek);
141 if (c == READER_EOF || wcschr(charset, c)) {
142 if (stop) {
143 *stop = c;
144 }
145 break;
146 }
147 $(self, read);
148 }
149
150 if (self->head - start > 0) {
151 const Range range = {
152 .location = start - self->string->chars,
153 .length = self->head - start
154 };
155 return $(self->string, substring, range);
156 }
157
158 return NULL;
159}
160
165static void reset(StringReader *self) {
166 self->head = self->string->chars;
167}
168
169#pragma mark - Class lifecycle
170
174static void initialize(Class *clazz) {
175
176 ((ObjectInterface *) clazz->interface)->copy = copy;
177 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
178
179 ((StringReaderInterface *) clazz->interface)->initWithCharacters = initWithCharacters;
180 ((StringReaderInterface *) clazz->interface)->initWithString = initWithString;
181 ((StringReaderInterface *) clazz->interface)->next = next;
182 ((StringReaderInterface *) clazz->interface)->peek = peek;
183 ((StringReaderInterface *) clazz->interface)->read = stringReaderRead;
184 ((StringReaderInterface *) clazz->interface)->readToken = readToken;
185 ((StringReaderInterface *) clazz->interface)->reset = reset;
186}
187
193 static Class *clazz;
194 static Once once;
195
196 do_once(&once, {
197 clazz = _initialize(&(const ClassDef) {
198 .name = "StringReader",
199 .superclass = _Object(),
200 .instanceSize = sizeof(StringReader),
201 .interfaceOffset = offsetof(StringReader, interface),
202 .interfaceSize = sizeof(StringReaderInterface),
204 });
205 });
206
207 return clazz;
208}
209
210#undef _Class
static Array * init(Array *self)
Definition Array.c:420
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
Class * _Object(void)
Definition Object.c:136
static void start(Operation *self)
Definition Operation.c:171
String * str(const char *fmt,...)
Definition String.c:1084
static String * substring(const String *self, const Range range)
Definition String.c:401
UTF-8 strings.
wchar_t Unicode
The Unicode type.
Definition String.h:41
static StringReader * initWithString(StringReader *self, String *string)
static void reset(StringReader *self)
static Unicode next(StringReader *self, StringReaderMode mode)
static StringReader * initWithCharacters(StringReader *self, const char *chars)
static Unicode peek(StringReader *self)
static String * readToken(StringReader *self, const Unicode *charset, Unicode *stop)
static void dealloc(Object *self)
Class * _StringReader(void)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
static Unicode stringReaderRead(StringReader *self)
StringReaders provide convenient parsing of text based files.
StringReaderMode
@ StringReaderRead
@ StringReaderPeek
#define READER_EOF
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:95
ident interface
The interface of the Class.
Definition Class.h:105
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
A location and length into contiguous collections.
Definition Types.h:54
ssize_t location
The location.
Definition Types.h:59
UTF-8 strings.
Definition String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85
The StringReader type.
StringReader * initWithString(StringReader *self, String *string)
Initializes this StringReader with the specified String.
Unicode next(StringReader *self)
char * head
The StringReader head.
String * string
The String to read.