std::string get_file_extension(const std::string& filename) { std::string::const_reverse_iterator rit; std::string result; // search for the first dot from the end of the string for (rit=filename.rbegin(); rit!=filename.rend(); ++rit) { if (is_path_separator(*rit)) return result; else if (*rit == '.') break; } if (rit != filename.rend()) { std::copy(std::string::const_iterator(rit.base()), filename.end(), std::back_inserter(result)); } return result; }
std::string get_file_title(const std::string& filename) { std::string::const_reverse_iterator rit; std::string::const_iterator last_dot = filename.end(); std::string result; for (rit=filename.rbegin(); rit!=filename.rend(); ++rit) { if (is_path_separator(*rit)) break; else if (*rit == '.' && last_dot == filename.end()) last_dot = rit.base()-1; } for (std::string::const_iterator it(rit.base()); it!=filename.end(); ++it) { if (it == last_dot) break; else result.push_back(*it); } return result; }
std::string &zippath_combine(std::string &dst, const char *path1, const char *path2) { if (!strcmp(path2, ".")) { dst.assign(path1); } else if (!strcmp(path2, "..")) { zippath_parent(dst, path1); } else if (osd_is_absolute_path(path2)) { dst.assign(path2); } else if ((path1[0] != '\0') && !is_path_separator(path1[strlen(path1) - 1])) { dst.assign(path1).append(PATH_SEPARATOR).append(path2); } else { dst.assign(path1).append(path2); } return dst; }
static file_error zippath_resolve(const char *path, osd_dir_entry_type &entry_type, zip_file *&zipfile, std::string &newpath) { file_error err; osd_directory_entry *current_entry = NULL; osd_dir_entry_type current_entry_type; int went_up = FALSE; int i; newpath.clear(); /* be conservative */ entry_type = ENTTYPE_NONE; zipfile = NULL; std::string apath(path); std::string apath_trimmed; do { /* trim the path of trailing path separators */ i = apath.length(); while (i > 1 && is_path_separator(apath[i - 1])) i--; apath = apath.substr(0, i); apath_trimmed.assign(apath); /* stat the path */ current_entry = osd_stat(apath_trimmed.c_str()); /* did we find anything? */ if (current_entry != NULL) { /* get the entry type and free the stat entry */ current_entry_type = current_entry->type; osd_free(current_entry); current_entry = NULL; } else { /* if we have not found the file or directory, go up */ current_entry_type = ENTTYPE_NONE; went_up = TRUE; std::string parent; apath.assign(zippath_parent(parent, apath.c_str())); } } while (current_entry_type == ENTTYPE_NONE && !is_root(apath.c_str())); /* if we did not find anything, then error out */ if (current_entry_type == ENTTYPE_NONE) { err = FILERR_NOT_FOUND; goto done; } /* is this file a ZIP file? */ if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(apath_trimmed.c_str()) && (zip_file_open(apath_trimmed.c_str(), &zipfile) == ZIPERR_NONE)) { i = strlen(path + apath.length()); while (i > 0 && is_zip_path_separator(path[apath.length() + i - 1])) i--; newpath.assign(path + apath.length(), i); /* this was a true ZIP path - attempt to identify the type of path */ zippath_find_sub_path(zipfile, newpath.c_str(), ¤t_entry_type); if (current_entry_type == ENTTYPE_NONE) { err = FILERR_NOT_FOUND; goto done; } } else { /* this was a normal path */ if (went_up) { err = FILERR_NOT_FOUND; goto done; } newpath.assign(path); } /* success! */ entry_type = current_entry_type; err = FILERR_NONE; done: return err; }
static int is_zip_path_separator(char c) { return is_zip_file_separator(c) || is_path_separator(c); }
void file_utils::trim_trailing_seperator(dynamic_string &path) { if ((path.get_len()) && (is_path_separator(path.back()))) path.truncate(path.get_len() - 1); }