1 #include "library/zip.hpp"
2 #include "lua/internal.hpp"
9 lua_zip_writer(lua_state
& L
, const std::string
& filename
, unsigned compression
);
14 int commit(lua_state
& L
, const std::string
& fname
)
17 throw std::runtime_error("Zip writer already finished");
25 int rollback(lua_state
& L
, const std::string
& fname
)
28 throw std::runtime_error("Zip writer already finished");
32 int close_file(lua_state
& L
, const std::string
& fname
)
35 throw std::runtime_error("Zip writer already finished");
37 throw std::runtime_error("Zip writer doesn't have file open");
41 int create_file(lua_state
& L
, const std::string
& fname
)
44 throw std::runtime_error("Zip writer already finished");
45 std::string filename
= L
.get_string(2, "ZIPWRITER::create_file");
50 file_open
= &w
->create_file(filename
);
52 int write(lua_state
& L
, const std::string
& fname
)
55 throw std::runtime_error("Zip writer already finished");
57 throw std::runtime_error("Zip writer doesn't have file open");
58 std::string _data
= L
.get_string(2, "ZIPWRITER::write");
59 std::vector
<char> data(_data
.length());
60 std::copy(_data
.begin(), _data
.end(), data
.begin());
61 file_open
->write(&data
[0], data
.size());
69 std::ostream
* file_open
;
73 lua_class
<lua_zip_writer
> class_zipwriter("ZIPWRITER");
75 lua_zip_writer::lua_zip_writer(lua_state
& L
, const std::string
& filename
, unsigned compression
)
78 w
= new zip::writer(filename
, compression
);
80 objclass
<lua_zip_writer
>().bind_multi(L
, {
81 {"commit", &lua_zip_writer::commit
},
82 {"rollback", &lua_zip_writer::rollback
},
83 {"close_file", &lua_zip_writer::close_file
},
84 {"create_file", &lua_zip_writer::create_file
},
85 {"write", &lua_zip_writer::write
}
89 function_ptr_luafun
lua_zip(lua_func_zip
, "zip.create", [](lua_state
& L
,
90 const std::string
& fname
) -> int {
91 unsigned compression
= 9;
92 std::string filename
= L
.get_string(1, fname
.c_str());
93 L
.get_numeric_argument
<unsigned>(2, compression
, fname
.c_str());
98 lua_class
<lua_zip_writer
>::create(L
, filename
, compression
);
102 function_ptr_luafun
lua_enumerate_zip(lua_func_zip
, "zip.enumerate", [](lua_state
& L
,
103 const std::string
& fname
) -> int {
104 std::string filename
= L
.get_string(1, fname
.c_str());
106 if(L
.type(2) != LUA_TNONE
&& L
.type(2) != LUA_TNIL
)
107 invert
= L
.toboolean(2);
108 zip::reader
r(filename
);