static bool find_files(std::string pathname, const std::string &filename, string_array &files, bool recursive) { if (!pathname.empty()) { char c = pathname[pathname.size() - 1]; if ((c != ':') && (c != '\\') && (c != '/')) pathname += "/"; } DIR *dp = opendir(pathname.c_str()); if (!dp) return false; string_array paths; for ( ; ; ) { struct dirent *ep = readdir(dp); if (!ep) break; const bool is_directory = (ep->d_type & DT_DIR) != 0; const bool is_file = (ep->d_type & DT_REG) != 0; if (ep->d_name[0] == '.') continue; std::string filename(ep->d_name); if (is_directory) { if (recursive) paths.push_back(filename); } else if (is_file) files.push_back(pathname + filename); } closedir(dp); dp = NULL; if (recursive) { for (uint i = 0; i < paths.size(); i++) { const std::string &path = paths[i]; if (!find_files(pathname + path, filename, files, true)) return false; } } return true; }
bool MyFb2File::GetGenres(string_array &genres) { bool retVal = false; int pos = 0; if (m_titleInfo.length() == 0) { m_titleInfo = GetXmlTag(titleInfoTag, pos); } if (m_titleInfo.length() == 0) { return retVal; } genres.clear(); pos = 0; QString curGenre = tr(""); while ((curGenre = GetXmlTag(genreTag, m_titleInfo, pos)).length() > 0) { int comma = curGenre.indexOf(','); if (comma != -1) { int pos = 0; QString g = tr(""); while ((comma = curGenre.indexOf(',', pos)) != -1) { int beg = pos, end = comma; while (curGenre.mid(beg, 1) == tr(" ")) { beg++; } while (curGenre.mid(end - 1, 1) == tr(" ")) { end--; } if ((end - beg) > 0) { g = curGenre.mid(beg, end - beg); genres.push_back(Utf8toAnsi(g)); } pos = comma + 1; } int beg = pos, end = curGenre.length(); while (curGenre.mid(beg, 1) == tr(" ")) { beg++; } while (curGenre.mid(end - 1, 1) == tr(" ")) { end--; } if ((end - beg) > 0) { g = curGenre.mid(beg, end - beg); genres.push_back(Utf8toAnsi(g)); } } else { genres.push_back(Utf8toAnsi(curGenre)); } retVal = true; } return retVal; }
static bool find_files(std::string pathname, const std::string &filename, string_array &files, bool recursive) { if (!pathname.empty()) { char c = pathname[pathname.size() - 1]; if ((c != ':') && (c != '\\') && (c != '/')) pathname += "\\"; } WIN32_FIND_DATAA find_data; HANDLE findHandle = FindFirstFileA((pathname + filename).c_str(), &find_data); if (findHandle == INVALID_HANDLE_VALUE) return false; do { const bool is_directory = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; const bool is_system = (find_data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0; const bool is_hidden = (find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0; std::string filename(find_data.cFileName); if ((!is_directory) && (!is_system) && (!is_hidden)) files.push_back(pathname + filename); } while (FindNextFileA(findHandle, &find_data)); FindClose(findHandle); if (recursive) { string_array paths; HANDLE findHandle = FindFirstFileA((pathname + "*").c_str(), &find_data); if (findHandle == INVALID_HANDLE_VALUE) return false; do { const bool is_directory = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; const bool is_system = (find_data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0; const bool is_hidden = (find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0; std::string filename(find_data.cFileName); if ((is_directory) && (!is_hidden) && (!is_system)) paths.push_back(filename); } while (FindNextFileA(findHandle, &find_data)); FindClose(findHandle); for (uint i = 0; i < paths.size(); i++) { const std::string &path = paths[i]; if (path[0] == '.') continue; if (!find_files(pathname + path, filename, files, true)) return false; } } return true; }