Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Resource.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "Array.h"
#include "Resource.h"
#include "Pointer.h"

Go to the source code of this file.

Macros

#define _Class   _Resource
 

Functions

Class_Resource (void)
 
static void addResourcePath (const char *path)
 
static void addResourceProvider (ResourceProvider provider)
 
static void dealloc (Object *self)
 
static void destroy (Class *clazz)
 
static void initialize (Class *clazz)
 
static ResourceinitWithBytes (Resource *self, const uint8_t *bytes, size_t length, const char *name)
 
static ResourceinitWithData (Resource *self, Data *data, const char *name)
 
static ResourceinitWithName (Resource *self, const char *name)
 
static void removeResourcePath (const char *path)
 
static void removeResourceProvider (ResourceProvider provider)
 
static ResourceresourceWithName (const char *name)
 

Variables

static Array_resourcePaths
 
static Array_resourceProviders
 

Macro Definition Documentation

◆ _Class

#define _Class   _Resource

Definition at line 34 of file Resource.c.

Function Documentation

◆ _Resource()

Class * _Resource ( void  )

Definition at line 241 of file Resource.c.

241 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
static void destroy(Class *clazz)
Definition Resource.c:193
static void initialize(Class *clazz)
Definition Resource.c:201
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
Resources provide an abstraction for file and stream resources.
Definition Resource.h:53

◆ addResourcePath()

static void addResourcePath ( const char *  path)
static

Definition at line 61 of file Resource.c.

61 {
62
63 assert(path);
64 String *string = str(path);
65
66 $(_resourcePaths, addObject, string);
67 release(string);
68}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
static Array * _resourcePaths
Definition Resource.c:36
String * str(const char *fmt,...)
Definition String.c:1084
UTF-8 strings.
Definition String.h:69

◆ addResourceProvider()

static void addResourceProvider ( ResourceProvider  provider)
static

Definition at line 74 of file Resource.c.

74 {
75
76 assert(provider);
77 Pointer *pointer = $(alloc(Pointer), initWithPointer, provider, NULL);
78
79 $(_resourceProviders, addObject, pointer);
80 release(pointer);
81}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static Pointer * initWithPointer(Pointer *self, ident pointer, Consumer destroy)
Definition Pointer.c:107
static Array * _resourceProviders
Definition Resource.c:37
Pointers provide Object encapsulation for raw C pointers.
Definition Pointer.h:40

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 44 of file Resource.c.

44 {
45
46 Resource *this = (Resource *) self;
47
48 release(this->data);
49
50 free(this->name);
51
52 super(Object, self, dealloc);
53}
#define super(type, obj, method,...)
static Data * data(void)
Definition Data.c:286
static void dealloc(Object *self)
Definition Resource.c:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ destroy()

static void destroy ( Class clazz)
static
See also
Class::destroy(Class *)

Definition at line 193 of file Resource.c.

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 201 of file Resource.c.

201 {
202
203 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
204
205 ((ResourceInterface *) clazz->interface)->addResourcePath = addResourcePath;
206 ((ResourceInterface *) clazz->interface)->addResourceProvider = addResourceProvider;
207 ((ResourceInterface *) clazz->interface)->initWithBytes = initWithBytes;
208 ((ResourceInterface *) clazz->interface)->initWithData = initWithData;
209 ((ResourceInterface *) clazz->interface)->initWithName = initWithName;
210 ((ResourceInterface *) clazz->interface)->removeResourcePath = removeResourcePath;
211 ((ResourceInterface *) clazz->interface)->removeResourceProvider = removeResourceProvider;
212 ((ResourceInterface *) clazz->interface)->resourceWithName = resourceWithName;
213
215 assert(_resourcePaths);
216
217 const char *env = getenv("OBJECTIVELY_RESOURCE_PATH");
218 if (env) {
219
220 String *string = $$(String, stringWithCharacters, env);
222
223 for (size_t i = 0; i < paths->count; i++) {
224 addResourcePath(((String *) $(paths, objectAtIndex, i))->chars);
225 }
226
227 release(paths);
228 release(string);
229 }
230
231 addResourcePath(".");
232
234 assert(_resourceProviders);
235}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static Array * init(Array *self)
Definition Array.c:420
static void removeResourcePath(const char *path)
Definition Resource.c:158
static void removeResourceProvider(ResourceProvider provider)
Definition Resource.c:171
static void addResourceProvider(ResourceProvider provider)
Definition Resource.c:74
static Resource * initWithName(Resource *self, const char *name)
Definition Resource.c:118
static Resource * resourceWithName(const char *name)
Definition Resource.c:184
static Resource * initWithBytes(Resource *self, const uint8_t *bytes, size_t length, const char *name)
Definition Resource.c:87
static void addResourcePath(const char *path)
Definition Resource.c:61
static Resource * initWithData(Resource *self, Data *data, const char *name)
Definition Resource.c:100
#define PATH_DELIM
Definition Resource.h:32
static String * stringWithCharacters(const char *chars)
Definition String.c:349
static Array * componentsSeparatedByCharacters(const String *self, const char *chars)
Definition String.c:182
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72
ident interface
The interface of the Class.
Definition Class.h:105
Resource * initWithData(Resource *self, Data *data)
void removeResourcePath(const char *path)
Removes the specified Resource path.
Definition Resource.c:158
Resource * initWithName(Resource *self, const char *name)
Initializes this Resource with the specified name.
Definition Resource.c:118

◆ initWithBytes()

static Resource * initWithBytes ( Resource self,
const uint8_t *  bytes,
size_t  length,
const char *  name 
)
static

Definition at line 87 of file Resource.c.

87 {
88
89 Data *data = $$(Data, dataWithBytes, bytes, length);
90 self = $(self, initWithData, data, name);
91
93 return self;
94}
static Data * dataWithBytes(const uint8_t *bytes, size_t length)
Definition Data.c:115
Data buffers.
Definition Data.h:50

◆ initWithData()

static Resource * initWithData ( Resource self,
Data data,
const char *  name 
)
static

Definition at line 100 of file Resource.c.

100 {
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}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
Data * data
The resource data.
Definition Resource.h:69
char * name
The resource name.
Definition Resource.h:74

◆ initWithName()

static Resource * initWithName ( Resource self,
const char *  name 
)
static

Definition at line 118 of file Resource.c.

118 {
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 Pointer *pointer = $(resourceProviders, objectAtIndex, i);
126 data = ((ResourceProvider) (pointer->pointer))(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}
static Data * dataWithContentsOfFile(const char *path)
Definition Data.c:133
Data *(* ResourceProvider)(const char *name)
Applications may specify a provider function for loading via file system abstractions.
Definition Resource.h:47
#define PATH_SEPAR
Definition Resource.h:33
ident pointer
The backing pointer.
Definition Pointer.h:61
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85

◆ removeResourcePath()

static void removeResourcePath ( const char *  path)
static

Definition at line 158 of file Resource.c.

158 {
159
160 String *string = $$(String, stringWithCharacters, path);
161
162 $(_resourcePaths, removeObject, string);
163
164 release(string);
165}
static void removeObject(Array *self, const ident obj)
Definition Array.c:642

◆ removeResourceProvider()

static void removeResourceProvider ( ResourceProvider  provider)
static

Definition at line 171 of file Resource.c.

171 {
172
173 Pointer *pointer = $(alloc(Pointer), initWithPointer, provider, NULL);
174
175 $(_resourceProviders, removeObject, pointer);
176
177 release(pointer);
178}

◆ resourceWithName()

static Resource * resourceWithName ( const char *  name)
static

Definition at line 184 of file Resource.c.

184 {
185 return $(alloc(Resource), initWithName, name);
186}

Variable Documentation

◆ _resourcePaths

Array* _resourcePaths
static

Definition at line 36 of file Resource.c.

◆ _resourceProviders

Array* _resourceProviders
static

Definition at line 37 of file Resource.c.