Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Resource.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 <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28
29#include "MutableArray.h"
30#include "Resource.h"
31#include "String.h"
32#include "Value.h"
33
34#define _Class _Resource
35
38
39#pragma mark - Object
40
44static void dealloc(Object *self) {
45
46 Resource *this = (Resource *) self;
47
48 release(this->data);
49
50 free(this->name);
51
52 super(Object, self, dealloc);
53}
54
55#pragma mark - Resource
56
61static void addResourcePath(const char *path) {
62
63 assert(path);
64 String *string = str(path);
65
66 $(_resourcePaths, addObject, string);
67 release(string);
68}
69
75
76 assert(provider);
77 Value *value = $(alloc(Value), initWithValue, provider);
78
80 release(value);
81}
82
87static Resource *initWithBytes(Resource *self, const uint8_t *bytes, size_t length, const char *name) {
88
89 Data *data = $$(Data, dataWithBytes, bytes, length);
90 self = $(self, initWithData, data, name);
91
93 return self;
94}
95
100static Resource *initWithData(Resource *self, Data *data, const char *name) {
101
102 self = (Resource *) super(Object, self, init);
103 if (self) {
104 assert(data);
105 self->data = retain(data);
106
107 assert(name);
108 self->name = strdup(name);
109 }
110
111 return self;
112}
113
118static Resource *initWithName(Resource *self, const char *name) {
119
120 Data *data = NULL;
121
122 const Array *resourceProviders = (Array *) _resourceProviders;
123 for (size_t i = 0; i < resourceProviders->count && data == NULL; i++) {
124
125 const Value *value = $(resourceProviders, objectAtIndex, i);
126 data = ((ResourceProvider) (value->value))(name);
127 }
128
129 const Array *resourcePaths = (Array *) _resourcePaths;
130 for (size_t i = 0; i < resourcePaths->count && data == NULL; i++) {
131
132 const String *resourcePath = $(resourcePaths, objectAtIndex, i);
133 String *path = str("%s%s%s", resourcePath->chars, PATH_SEPAR, name);
134
135 struct stat s;
136 if (stat(path->chars, &s) == 0 && S_ISREG(s.st_mode)) {
138 }
139
140 release(path);
141 }
142
143 if (data) {
144 self = $(self, initWithData, data, name);
145 } else {
146 self = release(self);
147 }
148
149 release(data);
150
151 return self;
152}
153
158static void removeResourcePath(const char *path) {
159
160 String *string = $$(String, stringWithCharacters, path);
161
162 $(_resourcePaths, removeObject, string);
163
164 release(string);
165}
166
172
173 Value *value = $(alloc(Value), initWithValue, provider);
174
176
177 release(value);
178}
179
184static Resource *resourceWithName(const char *name) {
185 return $(alloc(Resource), initWithName, name);
186}
187
188#pragma mark - Class lifecycle
189
193static void initialize(Class *clazz) {
194
195 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
196
197 ((ResourceInterface *) clazz->interface)->addResourcePath = addResourcePath;
198 ((ResourceInterface *) clazz->interface)->addResourceProvider = addResourceProvider;
199 ((ResourceInterface *) clazz->interface)->initWithBytes = initWithBytes;
200 ((ResourceInterface *) clazz->interface)->initWithData = initWithData;
201 ((ResourceInterface *) clazz->interface)->initWithName = initWithName;
202 ((ResourceInterface *) clazz->interface)->removeResourcePath = removeResourcePath;
203 ((ResourceInterface *) clazz->interface)->removeResourceProvider = removeResourceProvider;
204 ((ResourceInterface *) clazz->interface)->resourceWithName = resourceWithName;
205
207 assert(_resourcePaths);
208
209 const char *env = getenv("OBJECTIVELY_RESOURCE_PATH");
210 if (env) {
211
212 String *string = $$(String, stringWithCharacters, env);
214
215 for (size_t i = 0; i < paths->count; i++) {
216 addResourcePath(((String *) $(paths, objectAtIndex, i))->chars);
217 }
218
219 release(paths);
220 release(string);
221 }
222
223 addResourcePath(".");
224
226 assert(_resourceProviders);
227}
228
232static void destroy(Class *clazz) {
235}
236
242 static Class *clazz;
243 static Once once;
244
245 do_once(&once, {
246 clazz = _initialize(&(const ClassDef) {
247 .name = "Resource",
248 .superclass = _Object(),
249 .instanceSize = sizeof(Resource),
250 .interfaceOffset = offsetof(Resource, interface),
251 .interfaceSize = sizeof(ResourceInterface),
253 .destroy = destroy,
254 });
255 });
256
257 return clazz;
258}
259
260#undef _Class
261
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,...)
Mutable arrays.
static MutableArray * _resourceProviders
Definition: Resource.c:37
static void destroy(Class *clazz)
Definition: Resource.c:232
static MutableArray * _resourcePaths
Definition: Resource.c:36
static void initialize(Class *clazz)
Definition: Resource.c:193
Resources provide an abstraction for file and stream resources.
Data *(* ResourceProvider)(const char *name)
Applications may specify a provider function for loading via file system abstractions.
Definition: Resource.h:47
#define PATH_DELIM
Definition: Resource.h:32
#define PATH_SEPAR
Definition: Resource.h:33
Immutable UTF-8 strings.
Values provide Object encapsulation for C types.
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
Immutable arrays.
Definition: Array.h:56
ident objectAtIndex(const Array *self, int index)
size_t count
The count of elements.
Definition: Array.h:72
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
Immutable data buffers.
Definition: Data.h:50
Data * dataWithBytes(const uint8_t *bytes, size_t length)
Returns a new Data by copying length of bytes.
Definition: Data.c:109
Data * dataWithContentsOfFile(const char *path)
Returns a new Data with the contents of the file at path.
Definition: Data.c:127
Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
Initializes this Data by copying length of bytes.
Definition: Data.c:145
Log * initWithName(Log *self, const char *name)
Initializes this Log with the specified name.
Definition: Log.c:141
Mutable arrays.
Definition: MutableArray.h:40
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
void removeObject(MutableArray *self, const ident obj)
Removes the specified Object from this MutableArray.
Definition: MutableArray.c:270
MutableData * initWithData(MutableData *self, const Data *data)
Initializes this Data with the contents of data.
Definition: MutableData.c:124
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
Number * initWithValue(Number *self, double value)
Initializes this Number with the specified value.
Definition: Number.c:137
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Resources provide an abstraction for file and stream resources.
Definition: Resource.h:53
void removeResourceProvider(ResourceProvider provider)
Removes the specified ResourceProvider.
Definition: Resource.c:171
Resource * resourceWithName(const char *name)
Returns a new Resource with the specified name.
Definition: Resource.c:184
void addResourceProvider(ResourceProvider provider)
Adds the specified ResourceProvider.
Definition: Resource.c:74
void removeResourcePath(const char *path)
Removes the specified Resource path.
Definition: Resource.c:158
Data * data
The resource data.
Definition: Resource.h:69
void addResourcePath(const char *path)
Adds the specified Resource path.
Definition: Resource.c:61
Class * _Resource(void)
The Resource archetype.
Definition: Resource.c:241
char * name
The resource name.
Definition: Resource.h:74
Immutable UTF-8 strings.
Definition: String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
Array * componentsSeparatedByCharacters(const String *self, const char *chars)
Returns the components of this String that were separated by chars.
Definition: String.c:184
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739
String * stringWithCharacters(const char *chars)
Returns a new String by copying chars.
Definition: String.c:487
Values provide Object encapsulation for C types.
Definition: Value.h:45
ident value
The backing value.
Definition: Value.h:61