-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.c
More file actions
61 lines (51 loc) · 1.41 KB
/
storage.c
File metadata and controls
61 lines (51 loc) · 1.41 KB
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
#include "moonbase.h"
FILE *storage_open( const char *path, const char *mode )
{
return fopen( joinpath(base_data_path, path), mode );
}
int storage_rename( const char *oldpath, const char *newpath )
{
const char *abs_oldpath, *abs_newpath;
abs_oldpath = joinpath( base_data_path, oldpath );
abs_newpath = joinpath( base_data_path, newpath );
return rename( abs_oldpath, abs_newpath ) == 0;
}
int storage_remove( const char *path )
{
return remove( joinpath(base_data_path, path) ) == 0;
}
static int moonbase_storage_open( lua_State *s )
{
const char *relative_path, *mode;
char *absolute_path;
relative_path = luaL_checkstring( s, 1 );
mode = luaL_checkstring( s, 2 );
absolute_path = joinpath( base_data_path, relative_path );
lua_getglobal( s, "io" );
lua_getfield( s, -1, "open" );
lua_pushstring( s, absolute_path );
lua_pushstring( s, mode );
lua_call( s, 2, 1 );
return 1;
}
static int moonbase_storage_rename( lua_State *s )
{
storage_rename( luaL_checkstring(s, 1), luaL_checkstring(s, 2) );
return 0;
}
static int moonbase_storage_remove( lua_State *s )
{
storage_remove( luaL_checkstring(s, 1) );
return 0;
}
static luaL_Reg moonbase_storage_methods[] = {
{ "open", moonbase_storage_open },
{ "rename", moonbase_storage_rename },
{ "remove", moonbase_storage_remove },
{ NULL, NULL }
};
int moonbase_storage_initialize( lua_State *s )
{
luaL_newlib( s, moonbase_storage_methods );
return 1;
}