src/mess/zippath.c
| r3107 | r3108 | |
| 203 | 203 | |
| 204 | 204 | |
| 205 | 205 | /*------------------------------------------------- |
| 206 | next_path_char - lexes out the next path |
| 207 | character, normalizing separators as '/' |
| 208 | -------------------------------------------------*/ |
| 209 | |
| 210 | static char next_path_char(const char *s, int *pos) |
| 211 | { |
| 212 | char result; |
| 213 | |
| 214 | /* skip over any initial separators */ |
| 215 | if (*pos == 0) |
| 216 | { |
| 217 | while(is_zip_file_separator(s[*pos])) |
| 218 | (*pos)++; |
| 219 | } |
| 220 | |
| 221 | /* are we at a path separator? */ |
| 222 | if (is_zip_file_separator(s[*pos])) |
| 223 | { |
| 224 | /* skip over path separators */ |
| 225 | while(is_zip_file_separator(s[*pos])) |
| 226 | (*pos)++; |
| 227 | |
| 228 | /* normalize as '/' */ |
| 229 | result = '/'; |
| 230 | } |
| 231 | else if (s[*pos] != '\0') |
| 232 | { |
| 233 | /* return character */ |
| 234 | result = tolower(s[(*pos)++]); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | /* return NUL */ |
| 239 | result = '\0'; |
| 240 | } |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /*------------------------------------------------- |
| 206 | 248 | zippath_find_sub_path - attempts to identify the |
| 207 | 249 | type of a sub path in a zip file |
| 208 | 250 | -------------------------------------------------*/ |
| r3107 | r3108 | |
| 210 | 252 | const zip_file_header *zippath_find_sub_path(zip_file *zipfile, const char *subpath, osd_dir_entry_type *type) |
| 211 | 253 | { |
| 212 | 254 | int i, j; |
| 255 | char c1, c2, last_char; |
| 213 | 256 | const zip_file_header *header; |
| 214 | 257 | |
| 215 | 258 | for (header = zip_file_first_file(zipfile); header != NULL; header = zip_file_next_file(zipfile)) |
| r3107 | r3108 | |
| 224 | 267 | |
| 225 | 268 | i = 0; |
| 226 | 269 | j = 0; |
| 227 | | do |
| 228 | | { |
| 229 | | while(is_zip_file_separator(header->filename[i])) |
| 230 | | i++; |
| 231 | | while(is_zip_path_separator(subpath[j])) |
| 232 | | j++; |
| 233 | | while((header->filename[i] != '\0') && (tolower(header->filename[i]) == tolower(subpath[j]))) |
| 234 | | { |
| 235 | | i++; |
| 236 | | j++; |
| 237 | | } |
| 238 | | } |
| 239 | | while(is_zip_file_separator(header->filename[i]) && is_zip_path_separator(subpath[j])); |
| 270 | last_char = '/'; |
| 271 | while((c1 = next_path_char(header->filename, &i)) == (c2 = next_path_char(subpath, &j))) |
| 272 | last_char = c2; |
| 240 | 273 | |
| 241 | | if (subpath[j] == '\0') |
| 274 | if (c2 == '\0') |
| 242 | 275 | { |
| 243 | | if (header->filename[i] == '\0') |
| 276 | if (c1 == '\0') |
| 244 | 277 | { |
| 245 | 278 | if (type != NULL) |
| 246 | 279 | *type = ENTTYPE_FILE; |
| 247 | 280 | return header; |
| 248 | 281 | } |
| 249 | | else if ((i == 0) || is_zip_file_separator(header->filename[i])) |
| 282 | else if ((last_char == '/') || (c1 == '/')) |
| 250 | 283 | { |
| 251 | 284 | if (type != NULL) |
| 252 | 285 | *type = ENTTYPE_DIR; |
| r3107 | r3108 | |
| 325 | 358 | if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(astring_c(apath_trimmed)) |
| 326 | 359 | && (zip_file_open(astring_c(apath_trimmed), zipfile) == ZIPERR_NONE)) |
| 327 | 360 | { |
| 361 | i = strlen(path + astring_len(apath)); |
| 362 | while((i > 0) && is_zip_path_separator(path[astring_len(apath) + i - 1])) |
| 363 | i--; |
| 364 | astring_cpych(newpath, path + astring_len(apath), i); |
| 365 | |
| 328 | 366 | /* this was a true ZIP path - attempt to identify the type of path */ |
| 329 | | zippath_find_sub_path(*zipfile, path + astring_len(apath), ¤t_entry_type); |
| 367 | zippath_find_sub_path(*zipfile, astring_c(newpath), ¤t_entry_type); |
| 330 | 368 | if (current_entry_type == ENTTYPE_NONE) |
| 331 | 369 | { |
| 332 | 370 | err = FILERR_NOT_FOUND; |
| 333 | 371 | goto done; |
| 334 | 372 | } |
| 335 | | astring_cpyc(newpath, path + astring_len(apath)); |
| 336 | 373 | } |
| 337 | 374 | else |
| 338 | 375 | { |