forked from eklitzke/lua-bz2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlbz2_file_writer.c
137 lines (113 loc) · 3.61 KB
/
lbz2_file_writer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* This file implements the Lua binding to libbzip2.
*
* Copyright (c) 2008, Evan Klitzke <[email protected]>
* Copyright (c) 2012, Thomas Harning Jr <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <bzlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <assert.h>
#include "lbz2_file_writer.h"
#include "lbz2_common.h"
#define LBZ2_FILE_WRITER_MT "LBZ2_FILE_WRITER_MT"
typedef struct {
BZFILE *bz_stream;
FILE *f;
} lbz2_file_writer;
static lbz2_file_writer *lbz2_check_file_writer(lua_State *L, int index) {
return (lbz2_file_writer *)luaL_checkudata(L, index, LBZ2_FILE_WRITER_MT);
}
static int lbz2_file_writer_open(lua_State *L) {
lbz2_file_writer *writer;
int errorCode;
const char *fname = luaL_checkstring(L, 1);
int blockSize100k = luaL_optinteger(L, 2, 9);
int verbosity = luaL_optinteger(L, 3, 0);
int workFactor = luaL_optinteger(L, 4, 0);
writer = lua_newuserdata(L, sizeof(*writer));
memset(writer, 0, sizeof(*writer));
luaL_getmetatable(L, LBZ2_FILE_WRITER_MT);
lua_setmetatable(L, -2);
writer->f = fopen(fname, "wb");
if (writer->f == NULL) {
return luaL_error(L, "Failed to fopen %s", fname);
}
writer->bz_stream = BZ2_bzWriteOpen(&errorCode, writer->f, blockSize100k, verbosity, workFactor);
if (BZ_OK != errorCode) {
fclose(writer->f);
writer->f = NULL;
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
return 1;
}
static int lbz2_file_writer_close(lua_State *L) {
lbz2_file_writer *writer = lbz2_check_file_writer(L, 1);
int errorCode = BZ_OK;
if (writer->bz_stream) {
BZ2_bzWriteClose(&errorCode, writer->bz_stream, 0, NULL, NULL);
writer->bz_stream = NULL;
}
if (writer->f) {
fclose(writer->f);
writer->f = NULL;
}
lua_pushnil(L);
lua_setmetatable(L, 1);
if (BZ_OK != errorCode) {
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
static int lbz2_file_writer_write(lua_State *L) {
lbz2_file_writer *writer = lbz2_check_file_writer(L, 1);
int errorCode = BZ_OK;
size_t dataLength;
const char *data = luaL_checklstring(L, 2, &dataLength);
BZ2_bzWrite(&errorCode, writer->bz_stream, (void *)data, dataLength);
if (BZ_OK != errorCode) {
lua_pushnil(L);
lua_pushstring(L, lbz2_error(errorCode));
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
static luaL_Reg lbz2_file_writer_ops[] = {
{ "write", lbz2_file_writer_write },
{ "close", lbz2_file_writer_close },
{ NULL, NULL }
};
static luaL_Reg lbz2_file_writer_global[] = {
{ "openWrite", lbz2_file_writer_open },
{ NULL, NULL }
};
void register_lbz2_file_writer(lua_State *L) {
luaL_newmetatable(L, LBZ2_FILE_WRITER_MT);
lua_newtable(L);
luaL_register(L, NULL, lbz2_file_writer_ops);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lbz2_file_writer_close);
lua_setfield(L, -2, "__gc");
lua_pop(L, 1);
luaL_register(L, NULL, lbz2_file_writer_global);
}