src/mess/zippath.c
| r3043 | r3044 | |
| 179 | 179 | |
| 180 | 180 | |
| 181 | 181 | /*------------------------------------------------- |
| 182 | is_zip_separator - special check for path |
| 183 | separators |
| 184 | -------------------------------------------------*/ |
| 185 | |
| 186 | static int is_zip_separator(char c) |
| 187 | { |
| 188 | return (c == '/') || osd_is_path_separator(c); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | /*------------------------------------------------- |
| 194 | zippath_find_sub_path - attempts to identify the |
| 195 | type of a sub path in a zip file |
| 196 | -------------------------------------------------*/ |
| 197 | |
| 198 | const zip_file_header *zippath_find_sub_path(zip_file *zipfile, const char *subpath, osd_dir_entry_type *type) |
| 199 | { |
| 200 | int i, j; |
| 201 | const zip_file_header *header; |
| 202 | |
| 203 | for (header = zip_file_first_file(zipfile); header != NULL; header = zip_file_next_file(zipfile)) |
| 204 | { |
| 205 | /* special case */ |
| 206 | if (subpath == NULL) |
| 207 | { |
| 208 | if (type != NULL) |
| 209 | *type = ENTTYPE_FILE; |
| 210 | return header; |
| 211 | } |
| 212 | |
| 213 | i = 0; |
| 214 | j = 0; |
| 215 | do |
| 216 | { |
| 217 | while(header->filename[i] == '/') |
| 218 | i++; |
| 219 | while(is_zip_separator(subpath[j])) |
| 220 | j++; |
| 221 | while((header->filename[i] != '\0') && (tolower(header->filename[i]) == tolower(subpath[j]))) |
| 222 | { |
| 223 | i++; |
| 224 | j++; |
| 225 | } |
| 226 | } |
| 227 | while((header->filename[i] == '/') && is_zip_separator(subpath[j])); |
| 228 | |
| 229 | if (subpath[j] == '\0') |
| 230 | { |
| 231 | if (header->filename[i] == '\0') |
| 232 | { |
| 233 | if (type != NULL) |
| 234 | *type = ENTTYPE_FILE; |
| 235 | return header; |
| 236 | } |
| 237 | else if ((i == 0) || (header->filename[i] == '/')) |
| 238 | { |
| 239 | if (type != NULL) |
| 240 | *type = ENTTYPE_DIR; |
| 241 | return header; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (type != NULL) |
| 247 | *type = ENTTYPE_NONE; |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |
| 253 | /*------------------------------------------------- |
| 182 | 254 | zippath_resolve - separates a ZIP path out into |
| 183 | 255 | true path and ZIP entry components |
| 184 | 256 | -------------------------------------------------*/ |
| r3043 | r3044 | |
| 209 | 281 | |
| 210 | 282 | /* stat the path */ |
| 211 | 283 | current_entry = osd_stat(astring_c(apath_trimmed)); |
| 212 | | if (current_entry == NULL) |
| 284 | |
| 285 | /* did we find anything? */ |
| 286 | if (current_entry != NULL) |
| 213 | 287 | { |
| 214 | | err = FILERR_FAILURE; |
| 215 | | goto done; |
| 288 | /* get the entry type and free the stat entry */ |
| 289 | current_entry_type = current_entry->type; |
| 290 | free(current_entry); |
| 291 | current_entry = NULL; |
| 216 | 292 | } |
| 217 | | |
| 218 | | /* get the entry type and free the stat entry */ |
| 219 | | current_entry_type = current_entry->type; |
| 220 | | free(current_entry); |
| 221 | | current_entry = NULL; |
| 222 | | |
| 223 | | /* if we have not found the file or directory, go up */ |
| 224 | | if (current_entry_type == ENTTYPE_NONE) |
| 293 | else |
| 225 | 294 | { |
| 295 | /* if we have not found the file or directory, go up */ |
| 296 | current_entry_type = ENTTYPE_NONE; |
| 226 | 297 | went_up = TRUE; |
| 227 | 298 | parent = zippath_parent(astring_alloc(), astring_c(apath)); |
| 228 | 299 | astring_free(apath); |
| r3043 | r3044 | |
| 242 | 313 | if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(astring_c(apath_trimmed)) |
| 243 | 314 | && (zip_file_open(astring_c(apath_trimmed), zipfile) == ZIPERR_NONE)) |
| 244 | 315 | { |
| 245 | | /* this was a true ZIP path - copy out the sub path */ |
| 316 | /* this was a true ZIP path - attempt to identify the type of path */ |
| 317 | zippath_find_sub_path(*zipfile, path + astring_len(apath), ¤t_entry_type); |
| 318 | if (current_entry_type == ENTTYPE_NONE) |
| 319 | { |
| 320 | err = FILERR_NOT_FOUND; |
| 321 | goto done; |
| 322 | } |
| 246 | 323 | astring_cpyc(newpath, path + astring_len(apath)); |
| 247 | | *entry_type = (astring_len(newpath) == 0) || (astring_c(newpath)[astring_len(newpath)-1] == '/') |
| 248 | | ? ENTTYPE_DIR : ENTTYPE_FILE; |
| 249 | 324 | } |
| 250 | 325 | else |
| 251 | 326 | { |
| r3043 | r3044 | |
| 256 | 331 | goto done; |
| 257 | 332 | } |
| 258 | 333 | astring_cpyc(newpath, path); |
| 259 | | *entry_type = current_entry_type; |
| 260 | 334 | } |
| 261 | 335 | |
| 262 | 336 | /* success! */ |
| 337 | *entry_type = current_entry_type; |
| 263 | 338 | err = FILERR_NONE; |
| 264 | 339 | |
| 265 | 340 | done: |
| r3043 | r3044 | |
| 384 | 459 | && !strncmp(astring_c(directory->zipprefix), header->filename, len)) |
| 385 | 460 | { |
| 386 | 461 | result = &header->filename[len]; |
| 462 | while(*result == '/') |
| 463 | result++; |
| 387 | 464 | } |
| 388 | 465 | |
| 389 | 466 | return result; |