1 #include "library/zip.hpp"
2 #include "library/minmax.hpp"
3 #include "lua/internal.hpp"
10 lua_zip_writer(lua::state
& L
, const std::string
& filename
, unsigned compression
);
11 static size_t overcommit(const std::string
& filename
, unsigned compression
) { return 0; }
16 static int create(lua::state
& L
, lua::parameters
& P
)
21 P(filename
, P
.optional(compression
, 9));
23 compression
= clip(compression
, 0U, 9U);
25 lua::_class
<lua_zip_writer
>::create(L
, filename
, compression
);
28 int commit(lua::state
& L
, lua::parameters
& P
)
30 if(!w
) throw std::runtime_error("Zip writer already finished");
40 int rollback(lua::state
& L
, lua::parameters
& P
)
42 if(!w
) throw std::runtime_error("Zip writer already finished");
48 int close_file(lua::state
& L
, lua::parameters
& P
)
50 if(!w
) throw std::runtime_error("Zip writer already finished");
51 if(!file_open
) throw std::runtime_error("Zip writer doesn't have file open");
57 int create_file(lua::state
& L
, lua::parameters
& P
)
61 if(!w
) throw std::runtime_error("Zip writer already finished");
63 P(P
.skipped(), filename
);
69 file_open
= &w
->create_file(filename
);
72 int write(lua::state
& L
, lua::parameters
& P
)
76 if(!w
) throw std::runtime_error("Zip writer already finished");
77 if(!file_open
) throw std::runtime_error("Zip writer doesn't have file open");
79 P(P
.skipped(), _data
);
81 std::vector
<char> data(_data
.length());
82 std::copy(_data
.begin(), _data
.end(), data
.begin());
83 file_open
->write(&data
[0], data
.size());
92 std::ostream
* file_open
;
96 lua::_class
<lua_zip_writer
> LUA_class_zipwriter(lua_class_fileio
, "ZIPWRITER", {
97 {"new", lua_zip_writer::create
},
99 {"commit", &lua_zip_writer::commit
},
100 {"rollback", &lua_zip_writer::rollback
},
101 {"close_file", &lua_zip_writer::close_file
},
102 {"create_file", &lua_zip_writer::create_file
},
103 {"write", &lua_zip_writer::write
}
104 }, &lua_zip_writer::print
);
106 lua_zip_writer::lua_zip_writer(lua::state
& L
, const std::string
& filename
, unsigned compression
)
109 w
= new zip::writer(filename
, compression
);
113 int zip_enumerate(lua::state
& L
, lua::parameters
& P
)
115 std::string filename
;
118 P(filename
, P
.optional(invert
, false));
120 zip::reader
r(filename
);
136 lua::functions
LUA_zip_fns(lua_func_zip
, "zip", {
137 {"enumerate", zip_enumerate
},