file.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef _FILE_HPP_
  2. #define _FILE_HPP_
  3. #include <algorithm>
  4. #include <glib.h>
  5. #include <list>
  6. #include <string>
  7. typedef std::list<std::string> List;
  8. template <typename Function>
  9. void __for_each_file(const std::string& dirname, const std::string& suff,
  10. const List& order_list, const List& disable_list,
  11. Function f)
  12. {
  13. GDir *dir = g_dir_open(dirname.c_str(), 0, NULL);
  14. if (dir)
  15. {
  16. const gchar *filename;
  17. while ((filename = g_dir_read_name(dir)) != NULL)
  18. {
  19. std::string fullfilename(dirname + G_DIR_SEPARATOR_S + filename);
  20. if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR))
  21. __for_each_file(fullfilename, suff, order_list, disable_list, f);
  22. else if (g_str_has_suffix(filename, suff.c_str()) &&
  23. std::find(order_list.begin(), order_list.end(),
  24. fullfilename) == order_list.end())
  25. {
  26. bool disable = std::find(disable_list.begin(),
  27. disable_list.end(),
  28. fullfilename) != disable_list.end();
  29. f(fullfilename, disable);
  30. }
  31. }
  32. g_dir_close(dir);
  33. }
  34. }
  35. template <typename Function>
  36. void for_each_file(const List& dirs_list, const std::string& suff,
  37. const List& order_list, const List& disable_list,
  38. Function f)
  39. {
  40. List::const_iterator it;
  41. for (it = order_list.begin(); it != order_list.end(); ++it)
  42. {
  43. bool disable = std::find(disable_list.begin(), disable_list.end(),
  44. *it) != disable_list.end();
  45. f(*it, disable);
  46. }
  47. for (it = dirs_list.begin(); it != dirs_list.end(); ++it)
  48. __for_each_file(*it, suff, order_list, disable_list, f);
  49. }
  50. #endif//!_FILE_HPP_