Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 217 lines (182 sloc) 5.118 kB
8c078fe Locations, locations, locations
steve authored
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
efdff49 Automatic game image search based on locations and ROM file name
steve authored
7 #include <dirent.h>
8c078fe Locations, locations, locations
steve authored
8 #include "location.h"
9 #include "config.h"
10
11 struct location_type *type_start = NULL;
12
13 int location_init( void ) {
14 struct config_location_type *config_type = config_get()->location_types;
15
16 while( config_type ) {
17 struct location_type *type = malloc( sizeof(struct location_type) );
18
19 if( type ) {
20 struct config_location *config_location = config_get()->locations;
21
22 type->locations = NULL;
23 type->name = config_type->name;
24
25 while( config_location ) {
26 if( config_location->type == config_type ) {
27 struct location *location = malloc( sizeof(struct location) );
28 if( location ) {
29 location->directory = config_location->directory;
30 location->next = type->locations;
31 type->locations = location;
32 }
33 else {
34 fprintf( stderr, "Warning: Couldn't allocate memory for location\n" );
35 }
36 }
37 config_location = config_location->next;
38 }
39 type->next = type_start;
40 type_start = type;
41 }
42 else {
43 fprintf( stderr, "Warning: Couldn't allocate memory for location type\n" );
44 }
45 config_type = config_type->next;
46 }
47
48 /*{
49 struct location_type *type = type_start;
50 while( type ) {
51 struct location *location = type->locations;
52 printf("Type: %s\n", type->name );
53 while( location ) {
54 printf(" %s\n", location->directory );
55 location = location->next;
56 }
57 type = type->next;
58 }
59 }*/
60
61 return 0;
62 }
63
64 void location_free( void ) {
65 struct location_type *type = type_start;
66
67 while( type ) {
68 struct location_type *ttmp = type->next;
69 struct location *location = type->locations;
70
71 while( location ) {
72 struct location *ltmp = location->next;
73 free( location );
74 location = ltmp;
75 }
76
77 free( type );
78 type = ttmp;
79 }
80 }
81
82 struct location *location_get_first( const char *type ) {
83 struct location_type *ltype = type_start;
84
85 while( ltype ) {
86 if( strcasecmp( ltype->name, type ) == 0 )
87 return ltype->locations;
88 ltype = ltype->next;
89 }
90
91 return NULL;
92 }
93
942ca42 Theme locations
steve authored
94 int location_absolute( const char *filename ) {
8c078fe Locations, locations, locations
steve authored
95 #ifdef __WIN32__
96 if( filename[0] && filename[1] == ':' ) {
97 #else
98 if( filename[0] == '/' ) {
99 #endif
100 return 0;
101 }
942ca42 Theme locations
steve authored
102 return -1;
103 }
104
105 int location_try_directory( const char *dir, const char *filename, char *path ) {
106 char test[CONFIG_FILE_NAME_LENGTH];
107
108 if( dir && filename ) {
8c078fe Locations, locations, locations
steve authored
109 #ifdef __WIN32__
942ca42 Theme locations
steve authored
110 snprintf( test, CONFIG_FILE_NAME_LENGTH, "%s\\%s", dir, filename );
8c078fe Locations, locations, locations
steve authored
111 #else
942ca42 Theme locations
steve authored
112 snprintf( test, CONFIG_FILE_NAME_LENGTH, "%s/%s", dir, filename );
8c078fe Locations, locations, locations
steve authored
113 #endif
942ca42 Theme locations
steve authored
114 if( open( test, O_RDONLY ) == -1 ) {
115 if( errno != ENOENT ) {
116 fprintf( stderr, "Warning: Couldn't read file '%s': %s\n", test, strerror( errno ) );
117 return -1;
8c078fe Locations, locations, locations
steve authored
118 }
942ca42 Theme locations
steve authored
119 }
120 else {
121 strncpy( path, test, CONFIG_FILE_NAME_LENGTH );
122 return 0;
123 }
124 }
125
126 return -1;
127 }
128
129 int location_get_path( const char *type, const char *filename, char *path ) {
130 struct location *location = location_get_first( type );
131
cfa92f0 Play videos in snap, if we've got 'em
steve authored
132 if( type && filename && filename[0]) {
942ca42 Theme locations
steve authored
133 if( location_absolute( filename ) == 0 ) {
134 strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
135 return 0;
136 }
137
138 while( location ) {
139 if( location_try_directory( location->directory, filename, path ) == 0 ) {
8c078fe Locations, locations, locations
steve authored
140 return 0;
141 }
942ca42 Theme locations
steve authored
142 location = location->next;
8c078fe Locations, locations, locations
steve authored
143 }
942ca42 Theme locations
steve authored
144
145 /* Not found, return the original filename for later reference. */
146 strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
8c078fe Locations, locations, locations
steve authored
147 }
148
942ca42 Theme locations
steve authored
149 return -1;
150 }
151
152 int location_get_theme_path( const char *filename, char *path ) {
153 if( filename ) {
154 if( location_absolute( filename ) == 0 ) {
155 strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
156 return 0;
157 }
158
159 if( location_try_directory( config_get()->iface.theme.directory, filename, path ) == 0 ) {
160 return 0;
161 }
162
163 /* Not found, return the original filename for later reference. */
164 strncpy( path, filename, CONFIG_FILE_NAME_LENGTH );
165 }
166
8c078fe Locations, locations, locations
steve authored
167 return -1;
168 }
169
efdff49 Automatic game image search based on locations and ROM file name
steve authored
170 int location_get_match( const char *type, const char *filename, char *path ) {
171 struct location *location = location_get_first( type );
172 char search[CONFIG_FILE_NAME_LENGTH];
173 char *pos = NULL;
174 DIR *dir = NULL;
175 struct dirent *dentry;
176
177 if( type && filename ) {
178 #ifdef __WIN32__
179 pos = strrchr( filename, '\\' );
180 #else
181 pos = strrchr( filename, '/' );
182 #endif
183 if( pos )
184 strncpy( search, pos+1, CONFIG_FILE_NAME_LENGTH );
185 else
186 strncpy( search, filename, CONFIG_FILE_NAME_LENGTH );
187
188 pos = strrchr( search, '.' );
189 if( pos )
190 *(pos + 1) = '\0';
593f19b Add "." to location search string if necessary, to prevent false hits
steve authored
191 else
192 strcat( search, "." );
efdff49 Automatic game image search based on locations and ROM file name
steve authored
193
194 while( location ) {
195 if( (dir = opendir( location->directory )) ) {
196 while( (dentry = readdir( dir )) ) {
197 #ifdef __WIN32__
198 if( strncasecmp( dentry->d_name, search, strlen(search) ) == 0 ) {
199 snprintf( path, CONFIG_FILE_NAME_LENGTH, "%s\\%s", location->directory, dentry->d_name );
200 #else
797638e Platform images
steve authored
201 if( strncasecmp( dentry->d_name, search, strlen(search) ) == 0 ) {
efdff49 Automatic game image search based on locations and ROM file name
steve authored
202 snprintf( path, CONFIG_FILE_NAME_LENGTH, "%s/%s", location->directory, dentry->d_name );
203 #endif
204 return 0;
205 }
206 }
a766a50 Close dir when searching
steve authored
207 closedir( dir );
efdff49 Automatic game image search based on locations and ROM file name
steve authored
208 }
209 location = location->next;
210 }
211 }
212
213 strcpy( path, "" );
214 return -1;
215 }
216
Something went wrong with that request. Please try again.