Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
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
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
Immutable UTF-8 strings.
wchar_t Unicode
The Unicode type.
Definition: String.h:41
static void initialize(Class *clazz)
Definition: StringReader.c:174
static Unicode stringReaderRead(StringReader *self)
Definition: StringReader.c:119
StringReaders provide convenient parsing of text based files.
StringReaderMode
Definition: StringReader.h:33
@ StringReaderRead
Definition: StringReader.h:35
@ StringReaderPeek
Definition: StringReader.h:34
#define READER_EOF
Definition: StringReader.h:38
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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
MutableString * initWithString(MutableString *self, const String *string)
Initializes this MutableString with the contents of string.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
void start(Operation *self)
Starts this Operation.
Definition: Operation.c:171
A location and length into contiguous collections.
Definition: Types.h:54
ssize_t location
The location.
Definition: Types.h:59
Immutable UTF-8 strings.
Definition: String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
String * initWithCharacters(String *self, const char *chars)
Initializes this String by copying chars.
Definition: String.c:310
String * substring(const String *string, const Range range)
Creates a new String from a subset of this one.
Definition: String.c:539
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739
The StringReader type.
Definition: StringReader.h:47
Unicode peek(StringReader *self)
Peeks at the next Unicode code point from this StringReader without advancing head.
Definition: StringReader.c:111
int read(StringReader *self)
Reads a single Unicode code point from this StringReader.
Unicode next(StringReader *self)
char * head
The StringReader head.
Definition: StringReader.h:63
Class * _StringReader(void)
The StringReader archetype.
Definition: StringReader.c:192
void reset(StringReader *self)
Resets this StringReader, placing the head before the beginning of the String.
Definition: StringReader.c:165
String * string
The String to read.
Definition: StringReader.h:68
int readToken(StringReader *self, const Unicode *charset, Unicode *stop)
Definition: StringReader.c:127