-
Notifications
You must be signed in to change notification settings - Fork 19
/
location.c
225 lines (189 loc) · 5.19 KB
/
location.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include "location.h"
#include "config.h"
struct location_type *type_start = NULL;
int location_init( void ) {
struct config_location_type *config_type = config_get()->location_types;
while( config_type ) {
struct location_type *type = malloc( sizeof(struct location_type) );
if( type ) {
struct config_location *config_location = config_get()->locations;
type->locations = NULL;
type->name = config_type->name;
while( config_location ) {
if( config_location->type == config_type ) {
struct location *location = malloc( sizeof(struct location) );
if( location ) {
location->directory = config_location->directory;
location->next = type->locations;
type->locations = location;
}
else {
fprintf( stderr, "Warning: Couldn't allocate memory for location\n" );
}
}
config_location = config_location->next;
}
type->next = type_start;
type_start = type;
}
else {
fprintf( stderr, "Warning: Couldn't allocate memory for location type\n" );
}
config_type = config_type->next;
}
/*{
struct location_type *type = type_start;
while( type ) {
struct location *location = type->locations;
printf("Type: %s\n", type->name );
while( location ) {
printf(" %s\n", location->directory );
location = location->next;
}
type = type->next;
}
}*/
return 0;
}
void location_free( void ) {
struct location_type *type = type_start;
while( type ) {
struct location_type *ttmp = type->next;
struct location *location = type->locations;
while( location ) {
struct location *ltmp = location->next;
free( location );
location = ltmp;
}
free( type );
type = ttmp;
}
}
struct location *location_get_first( const char *type ) {
struct location_type *ltype = type_start;
while( ltype ) {
if( strcasecmp( ltype->name, type ) == 0 )
return ltype->locations;
ltype = ltype->next;
}
return NULL;
}
int location_absolute( const char *filename ) {
#ifdef __WIN32__
if( filename[0] && filename[1] == ':' ) {
#else
if( filename[0] == '/' ) {
#endif
return 0;
}
return -1;
}
int location_try_directory( const char *dir, const char *filename, char *path ) {
char test[CONFIG_FILE_NAME_LENGTH];
struct stat stmp;
if( dir && filename ) {
#ifdef __WIN32__
snprintf( test, CONFIG_FILE_NAME_LENGTH, "%s\\%s", dir, filename );
#else
snprintf( test, CONFIG_FILE_NAME_LENGTH, "%s/%s", dir, filename );
#endif
if( stat( test, &stmp ) == -1 ) {
if( errno != ENOENT ) {
fprintf( stderr, "Warning: Couldn't stat file '%s': %s\n", test, strerror( errno ) );
return -1;
}
}
else {
strncpy( path, test, CONFIG_FILE_NAME_LENGTH );
return 0;
}
}
return -1;
}
int location_get_path( const char *type, const char *filename, char *path ) {
struct location *location = location_get_first( type );
if( type && filename && filename[0]) {
if( location_absolute( filename ) == 0 ) {
strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
return 0;
}
while( location ) {
if( location_try_directory( location->directory, filename, path ) == 0 ) {
return 0;
}
location = location->next;
}
/* Not found, return the original filename for later reference. */
strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
}
return -1;
}
int location_get_theme_path( const char *filename, char *path ) {
if( filename ) {
if( location_absolute( filename ) == 0 ) {
strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
return 0;
}
if( location_try_directory( config_get()->iface.theme.directory, filename, path ) == 0 ) {
return 0;
}
/* Not found, return the original filename for later reference. */
strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
}
return -1;
}
int location_get_match( const char *type, const char *filename, char *path ) {
struct location *location = location_get_first( type );
char search[CONFIG_FILE_NAME_LENGTH];
char *pos = NULL;
DIR *dir = NULL;
struct dirent *dentry;
int found = 0;
if( type && filename ) {
#ifdef __WIN32__
pos = strrchr( filename, '\\' );
#else
pos = strrchr( filename, '/' );
#endif
if( pos )
strncpy( search, pos+1, CONFIG_FILE_NAME_LENGTH );
else
strncpy( search, filename, CONFIG_FILE_NAME_LENGTH );
pos = strrchr( search, '.' );
if( pos )
*(pos + 1) = '\0';
else
strcat( search, "." );
while( location && !found ) {
if( (dir = opendir( location->directory )) ) {
while( (dentry = readdir( dir )) ) {
#ifdef __WIN32__
if( strncasecmp( dentry->d_name, search, strlen(search) ) == 0 ) {
snprintf( path, CONFIG_FILE_NAME_LENGTH, "%s\\%s", location->directory, dentry->d_name );
#else
if( strncasecmp( dentry->d_name, search, strlen(search) ) == 0 ) {
snprintf( path, CONFIG_FILE_NAME_LENGTH, "%s/%s", location->directory, dentry->d_name );
#endif
found++;
}
}
closedir( dir );
}
location = location->next;
}
}
if( found ) {
if ( found > 1 )
printf( "Warning: multiple %s assets match \"%s\", picked \"%s\"\n", type, filename, path );
return 0;
}
strcpy( path, "" );
return -1;
}