Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (77 sloc) 2.059 kb
467f150 Missed file
nrich@ii.net authored
1 #include <iostream>
2 #include <client/dbclient.h>
3 #include <client/gridfs.h>
4
5 extern "C" {
6 #include <lua.h>
7 #include <lauxlib.h>
8 #include <lualib.h>
9
10 #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
11 #include <compat-5.1.h>
12 #endif
13 };
14
15 #include "utils.h"
16 #include "common.h"
17
18 using namespace mongo;
19
20 namespace {
85f6ef7 @neomantra Removed support for building against mongodb < 1.6
neomantra authored
21 inline GridFSChunk* userdata_to_gridfschunk(lua_State* L, int index) {
467f150 Missed file
nrich@ii.net authored
22 void *ud = 0;
23
24 ud = luaL_checkudata(L, index, LUAMONGO_GRIDFSCHUNK);
85f6ef7 @neomantra Removed support for building against mongodb < 1.6
neomantra authored
25 GridFSChunk *chunk = *((GridFSChunk **)ud);
467f150 Missed file
nrich@ii.net authored
26
27 return chunk;
28 }
29 }
30
31 /*
32 * str = chunk:data()
33 */
34 static int gridfschunk_data(lua_State *L) {
85f6ef7 @neomantra Removed support for building against mongodb < 1.6
neomantra authored
35 GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
467f150 Missed file
nrich@ii.net authored
36 int len;
37
38 const char *data = chunk->data(len);
39
40 lua_pushlstring(L, data, len);
41
42 return 1;
43 }
44
45 /*
46 * length = chunk:len()
47 * __len
48 */
49 static int gridfschunk_len(lua_State *L) {
85f6ef7 @neomantra Removed support for building against mongodb < 1.6
neomantra authored
50 GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
467f150 Missed file
nrich@ii.net authored
51
52 int len = chunk->len();
53
54 lua_pushinteger(L, len);
55
56 return 1;
57 }
58
59
60 /*
61 * __gc
62 */
63 static int gridfschunk_gc(lua_State *L) {
85f6ef7 @neomantra Removed support for building against mongodb < 1.6
neomantra authored
64 GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
467f150 Missed file
nrich@ii.net authored
65
66 delete chunk;
67
68 return 0;
69 }
70
71 /*
72 * __tostring
73 */
74 static int gridfschunk_tostring(lua_State *L) {
85f6ef7 @neomantra Removed support for building against mongodb < 1.6
neomantra authored
75 GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
467f150 Missed file
nrich@ii.net authored
76
77 lua_pushfstring(L, "%s: %p", LUAMONGO_GRIDFSCHUNK, chunk);
78
79 return 1;
80 }
81
82 int mongo_gridfschunk_register(lua_State *L) {
83 static const luaL_Reg gridfschunk_methods[] = {
668f223 Cleaned up whitespace (spaces only)
neomantra authored
84 {"data", gridfschunk_data},
85 {"len", gridfschunk_len},
467f150 Missed file
nrich@ii.net authored
86 {NULL, NULL}
87 };
88
89 luaL_newmetatable(L, LUAMONGO_GRIDFSCHUNK);
90 luaL_register(L, 0, gridfschunk_methods);
91 lua_pushvalue(L,-1);
92 lua_setfield(L, -2, "__index");
93
94 lua_pushcfunction(L, gridfschunk_gc);
95 lua_setfield(L, -2, "__gc");
96
97 lua_pushcfunction(L, gridfschunk_tostring);
98 lua_setfield(L, -2, "__tostring");
99
100 lua_pushcfunction(L, gridfschunk_len);
101 lua_setfield(L, -2, "__len");
102
103 //luaL_register(L, LUAMONGO_GRIDFSCHUNK, gridfschunk_class_methods);
104
105 return 1;
106 }
Something went wrong with that request. Please try again.