filesystem.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <iostream>
  2. extern "C"
  3. {
  4. #include "lua.h"
  5. #include "lauxlib.h"
  6. #include "lualib.h"
  7. }
  8. #include <luabind/luabind.hpp>
  9. #include <luabind/operator.hpp>
  10. #include <boost/filesystem/operations.hpp>
  11. #include <boost/filesystem/path.hpp>
  12. #include "directory_iterator.hpp"
  13. const boost::filesystem::path&
  14. identity(const boost::filesystem::path& x)
  15. {
  16. return x;
  17. }
  18. void bind_filesystem(lua_State* L)
  19. {
  20. using namespace luabind;
  21. using namespace boost::filesystem;
  22. namespace fs = boost::filesystem;
  23. module(L, "filesystem")
  24. [
  25. class_<fs::path>("path")
  26. .def(constructor<>())
  27. .def(constructor<const char*>())
  28. .def("string", &fs::path::string)
  29. .def("native_file_string", &fs::path::native_file_string)
  30. .def("native_directory_string", &fs::path::native_directory_string)
  31. .def("root_path", &fs::path::root_path)
  32. .def("root_name", &fs::path::root_name)
  33. .def("root_directory", &fs::path::root_directory)
  34. .def("relative_path", &fs::path::relative_path)
  35. .def("leaf", &fs::path::leaf)
  36. .def("branch_path", &fs::path::branch_path)
  37. .def("empty", &fs::path::empty)
  38. .def("is_complete", &fs::path::is_complete)
  39. .def("is_directory", &fs::is_directory)
  40. .def("is_empty", &fs::is_empty)
  41. .def("has_root_path", &fs::path::has_root_path)
  42. .def("has_root_name", &fs::path::has_root_name)
  43. .def("has_root_directory", &fs::path::has_root_directory)
  44. .def("has_relative_path", &fs::path::has_relative_path)
  45. .def("has_leaf", &fs::path::has_leaf)
  46. .def("has_branch_path", &fs::path::has_branch_path)
  47. .def(const_self / const_self)
  48. .def(other<const char*>() / const_self)
  49. .def(const_self / other<const char*>())
  50. // .property("contents", &identity, return_directory_iterator)
  51. ,
  52. def("exists", &fs::exists),
  53. def("is_directory", &fs::is_directory),
  54. def("is_empty", &fs::is_empty),
  55. def("create_directory", &fs::create_directory),
  56. def("remove", &fs::remove),
  57. def("remove_all", &fs::remove_all),
  58. def("rename", &fs::rename),
  59. def("copy_file", &fs::copy_file),
  60. def("initial_path", &fs::initial_path),
  61. def("current_path", &fs::current_path),
  62. def("complete", &fs::complete),
  63. def("system_complete", &fs::system_complete)
  64. ];
  65. }
  66. int main(int argc, const char* argv[])
  67. {
  68. lua_State* L = lua_open();
  69. luaopen_base(L);
  70. luaopen_string(L);
  71. luaopen_table(L);
  72. luaopen_math(L);
  73. luaopen_io(L);
  74. luaopen_debug(L);
  75. if (argc < 2)
  76. {
  77. std::cout << "This example will bind parts of the boost.Filesystem library\n"
  78. "and then run the given lua file.\n\n"
  79. "usage: filesystem filename [args]\n";
  80. return 1;
  81. }
  82. using namespace luabind;
  83. open(L);
  84. bind_filesystem(L);
  85. object args = newtable(L);
  86. for (int i = 0; i < argc; ++i)
  87. {
  88. args[i + 1] = argv[i];
  89. }
  90. args["n"] = argc;
  91. globals(L)["args"] = args;
  92. luaL_dofile(L, argv[1]);
  93. }