-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteResLoader.m
More file actions
86 lines (84 loc) · 2.23 KB
/
SpriteResLoader.m
File metadata and controls
86 lines (84 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 1995-2008 Jeffrey T. Read
*
* This file is part of SpriteCore.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
*/
#import <SpriteCore/SpriteResLoader.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@implementation SpriteResLoader
-initOn: (SpriteApp *)anApp named: (char *)aName {
self = [super init];
name = aName;
parent = anApp;
return self;
}
-free {
[super free];
}
-(unsigned int)fetchResource: (char *)aResName to: (void **)aPtr
{
unsigned char *path = [parent resourcePath];
unsigned char *fullpath;
void *data;
struct stat st;
int fd;
int result;
if(path == NULL)
{
path = "./";
}
size_t plen = strlen(path);
size_t nlen = strlen(name);
size_t rlen = strlen(aResName);
fullpath = (unsigned char *)malloc(plen + nlen + rlen + 7);
fullpath[0] = '\0';
strncat(fullpath,path,plen);
if(path[plen - 1] != '/')
{
fullpath[plen] = '/';
fullpath[plen + 1] = '\0';
}
strncat(fullpath,name,nlen);
strncat(fullpath,".res/",5);
strncat(fullpath,aResName,rlen);
result = stat(fullpath,&st);
if(result != 0) goto res_err1;
data = malloc(st.st_size);
fd = open(fullpath,O_RDONLY);
if(fd < 0) goto res_err2;
result = read(fd,data,st.st_size);
if(result < 0) goto res_err2;
free(fullpath);
*aPtr = data;
return st.st_size;
res_err2:
free(data);
res_err1:
free(fullpath);
*aPtr = NULL;
return 0;
}
@end