ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
SimpleSelector.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and 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 <stdlib.h>
26#include <string.h>
27
28#include <Objectively/Hash.h>
29#include <Objectively/MutableArray.h>
30#include <Objectively/String.h>
31
32#include "SimpleSelector.h"
33
34#define _Class _SimpleSelector
35
36#pragma mark - Object
37
41static void dealloc(Object *self) {
42
43 SimpleSelector *this = (SimpleSelector *) self;
44
45 free(this->pattern);
46
47 super(Object, self, dealloc);
48}
49
53static String *description(const Object *self) {
54
55 const SimpleSelector *this = (SimpleSelector *) self;
56
57 return str(this->pattern);
58}
59
63static int hash(const Object *self) {
64
65 SimpleSelector *this = (SimpleSelector *) self;
66
67 return HashForCString(HASH_SEED, this->pattern);
68}
69
73static _Bool isEqual(const Object *self, const Object *other) {
74
75 if (super(Object, self, isEqual, other)) {
76 return true;
77 }
78
79 if (other && $(other, isKindOfClass, _SimpleSelector())) {
80
81 const SimpleSelector *this = (SimpleSelector *) self;
82 const SimpleSelector *that = (SimpleSelector *) other;
83
84 return strcmp(this->pattern, that->pattern) == 0;
85 }
86
87 return false;
88}
89
90#pragma mark - SimpleSelector
91
96static SimpleSelector *initWithPattern(SimpleSelector *self, const char *pattern) {
97
98 self = (SimpleSelector *) super(Object, self, init);
99 if (self) {
100
101 self->pattern = strtrim(pattern);
102 assert(self->pattern);
103
104 assert(strlen(self->pattern));
105 }
106
107 return self;
108}
109
114 switch (c) {
115 case '\0':
117 case '*':
119 case '#':
121 case '.':
123 case ':':
125 default:
127 }
128}
129
134static Array *parse(const char *sequence) {
135
136 MutableArray *simpleSelectors = $$(MutableArray, arrayWithCapacity, 4);
137 assert(simpleSelectors);
138
139 if (sequence) {
140
141 const char *c = sequence;
142 const char *delim = sequence;
143 while (*c) {
144 const size_t size = strcspn(c, "*.#:");
145 if (size || *c == '*') {
146
147 char *pattern;
148 if (*c == '*') {
149 pattern = strdup("*");
150 assert(pattern);
151 } else {
152 pattern = calloc(1, size + 1);
153 assert(pattern);
154
155 strncpy(pattern, c, size);
156 }
157
158 SimpleSelector *simpleSelector = $(alloc(SimpleSelector), initWithPattern, pattern);
159 assert(simpleSelector);
160
161 simpleSelector->type = simpleSelectorType(*delim);
162 assert(simpleSelector->type);
163
164 delim = c + size;
165
166 $(simpleSelectors, addObject, simpleSelector);
167
168 release(simpleSelector);
169 free(pattern);
170 }
171
172 c += size;
173 c += strspn(c, "*.#:");
174 }
175 }
176
177 return (Array *) simpleSelectors;
178}
179
180#pragma mark - Class lifecycle
181
185static void initialize(Class *clazz) {
186
187 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
188 ((ObjectInterface *) clazz->interface)->description = description;
189 ((ObjectInterface *) clazz->interface)->hash = hash;
190 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
191
192 ((SimpleSelectorInterface *) clazz->interface)->initWithPattern = initWithPattern;
193 ((SimpleSelectorInterface *) clazz->interface)->parse = parse;
194}
195
200Class *_SimpleSelector(void) {
201 static Class *clazz;
202 static Once once;
203
204 do_once(&once, {
205 clazz = _initialize(&(const ClassDef) {
206 .name = "SimpleSelector",
207 .superclass = _Object(),
208 .instanceSize = sizeof(SimpleSelector),
209 .interfaceOffset = offsetof(SimpleSelector, interface),
210 .interfaceSize = sizeof(SimpleSelectorInterface),
212 });
213 });
214
215 return clazz;
216}
217
218#undef _Class
static String * description(const Object *self)
static _Bool isEqual(const Object *self, const Object *other)
static SimpleSelectorType simpleSelectorType(const char c)
static void dealloc(Object *self)
static void initialize(Class *clazz)
static int hash(const Object *self)
The SimpleSelector type.
SimpleSelectorType
The types of SimpleSelectors.
@ SimpleSelectorTypePseudo
@ SimpleSelectorTypeId
@ SimpleSelectorTypeClass
@ SimpleSelectorTypeUniversal
@ SimpleSelectorTypeType
@ SimpleSelectorTypeNone
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
SDL_Size size(const Image *self)
Definition: Image.c:181
Array * parse(const char *rules)
Parses the null-terminated C string of Selector rules into an Array of Selectors.
Definition: Selector.c:274
The SimpleSelector type.
Array * parse(const char *sequence)
Definition: Selector.c:274
SimpleSelectorType type
The SimpleSelectorType.
Class * _SimpleSelector(void)
The SimpleSelector archetype.
SimpleSelector * initWithPattern(SimpleSelector *self, const char *pattern)
Initializes this SimpleSelector.
char * pattern
The pattern, as provided by the user.