made it work with boost 1.32
[luabind.git] / examples / filesystem / filesystem.cpp
blobc34c22aa35d8e429ef05a948a5c4c25139538911
1 #include <iostream>
3 extern "C"
5 #include "lua.h"
6 #include "lauxlib.h"
7 #include "lualib.h"
10 bool dostring(lua_State* L, const char* str)
12 if (luaL_loadbuffer(L, str, std::strlen(str), str) || lua_pcall(L, 0, 0, 0))
14 const char* a = lua_tostring(L, -1);
15 std::cout << a << "\n";
16 lua_pop(L, 1);
17 return true;
19 return false;
22 #include <luabind/luabind.hpp>
23 #include <luabind/operator.hpp>
24 #include <boost/filesystem/operations.hpp>
25 #include <boost/filesystem/path.hpp>
27 #include "directory_iterator.hpp"
29 const boost::filesystem::path&
30 identity(const boost::filesystem::path& x)
32 return x;
35 void bind_filesystem(lua_State* L)
37 using namespace luabind;
38 using namespace boost::filesystem;
40 namespace fs = boost::filesystem;
42 module(L, "filesystem")
44 class_<fs::path>("path")
45 .def(constructor<>())
46 .def(constructor<const char*>())
47 .def("string", &fs::path::string)
48 .def("native_file_string", &fs::path::native_file_string)
49 .def("native_directory_string", &fs::path::native_directory_string)
50 .def("root_path", &fs::path::root_path)
51 .def("root_name", &fs::path::root_name)
52 .def("root_directory", &fs::path::root_directory)
53 .def("relative_path", &fs::path::relative_path)
54 .def("leaf", &fs::path::leaf)
55 .def("branch_path", &fs::path::branch_path)
57 .def("empty", &fs::path::empty)
58 .def("is_complete", &fs::path::is_complete)
59 .def("is_directory", &fs::is_directory)
60 .def("is_empty", &fs::is_empty)
61 .def("has_root_path", &fs::path::has_root_path)
62 .def("has_root_name", &fs::path::has_root_name)
63 .def("has_root_directory", &fs::path::has_root_directory)
64 .def("has_relative_path", &fs::path::has_relative_path)
65 .def("has_leaf", &fs::path::has_leaf)
66 .def("has_branch_path", &fs::path::has_branch_path)
68 .def(const_self / const_self)
69 .def(other<const char*>() / const_self)
70 .def(const_self / other<const char*>())
72 .property("contents", &identity, return_directory_iterator)
75 def("exists", &fs::exists),
76 def("is_directory", &fs::is_directory),
77 def("is_empty", &fs::is_empty),
78 def("create_directory", &fs::create_directory),
79 def("remove", &fs::remove),
80 def("remove_all", &fs::remove_all),
81 def("rename", &fs::rename),
82 def("copy_file", &fs::copy_file),
84 def("initial_path", &fs::initial_path),
85 def("current_path", &fs::current_path),
86 def("complete", &fs::complete),
87 def("system_complete", &fs::system_complete)
91 int main(int argc, const char* argv[])
93 lua_State* L = lua_open();
94 luaopen_base(L);
95 luaopen_string(L);
96 luaopen_table(L);
97 luaopen_math(L);
98 luaopen_io(L);
99 luaopen_debug(L);
101 if (argc < 2)
103 std::cout << "usage: filesystem filename [args]\n";
104 return 1;
107 using namespace luabind;
109 open(L);
111 bind_filesystem(L);
113 object args = newtable(L);
115 for (int i = 0; i < argc; ++i)
117 args[i + 1] = argv[i];
120 args["n"] = argc;
122 object globals = get_globals(L);
123 globals["args"] = args;
125 lua_dofile(L, argv[1]);