~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/gtlua/alphabet_lua.c

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
  Copyright (c) 2007-2009 Gordon Gremme <gremme@zbh.uni-hamburg.de>
 
3
  Copyright (c) 2007-2008 Center for Bioinformatics, University of Hamburg
 
4
 
 
5
  Permission to use, copy, modify, and distribute this software for any
 
6
  purpose with or without fee is hereby granted, provided that the above
 
7
  copyright notice and this permission notice appear in all copies.
 
8
 
 
9
  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
10
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
11
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
12
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
13
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
14
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
15
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
*/
 
17
 
 
18
#include <string.h>
 
19
#include "lauxlib.h"
 
20
#include "core/alphabet.h"
 
21
#include "extended/luahelper.h"
 
22
#include "gtlua/alphabet_lua.h"
 
23
 
 
24
static int alphabet_lua_new_protein(lua_State *L)
 
25
{
 
26
  GtAlphabet **alpha;
 
27
  gt_assert(L);
 
28
  alpha = lua_newuserdata(L, sizeof *alpha);
 
29
  gt_assert(alpha);
 
30
  *alpha = gt_alphabet_new_protein();
 
31
  gt_assert(*alpha);
 
32
  luaL_getmetatable(L, ALPHABET_METATABLE);
 
33
  lua_setmetatable(L, -2);
 
34
  return 1;
 
35
}
 
36
 
 
37
static int alphabet_lua_new_empty(lua_State *L)
 
38
{
 
39
  GtAlphabet **alpha;
 
40
  gt_assert(L);
 
41
  alpha = lua_newuserdata(L, sizeof *alpha);
 
42
  gt_assert(alpha);
 
43
  *alpha = gt_alphabet_new_empty();
 
44
  gt_assert(*alpha);
 
45
  luaL_getmetatable(L, ALPHABET_METATABLE);
 
46
  lua_setmetatable(L, -2);
 
47
  return 1;
 
48
}
 
49
 
 
50
static int alphabet_lua_new_dna(lua_State *L)
 
51
{
 
52
  GtAlphabet **alpha;
 
53
  gt_assert(L);
 
54
  alpha = lua_newuserdata(L, sizeof *alpha);
 
55
  gt_assert(alpha);
 
56
  *alpha = gt_alphabet_new_dna();
 
57
  gt_assert(*alpha);
 
58
  luaL_getmetatable(L, ALPHABET_METATABLE);
 
59
  lua_setmetatable(L, -2);
 
60
  return 1;
 
61
}
 
62
 
 
63
static int alphabet_lua_add_mapping(lua_State *L)
 
64
{
 
65
  GtAlphabet **alpha;
 
66
  const char *characters;
 
67
  alpha = check_alphabet(L, 1);
 
68
  characters = luaL_checkstring(L, 2);
 
69
  luaL_argcheck(L, strlen(characters), 2,
 
70
                "mapping must contain at least one character");
 
71
  gt_alphabet_add_mapping(*alpha, characters);
 
72
  return 0;
 
73
}
 
74
 
 
75
static int alphabet_lua_add_wildcard(lua_State *L)
 
76
{
 
77
  GtAlphabet **alpha;
 
78
  const char *wildcard;
 
79
  alpha = check_alphabet(L, 1);
 
80
  wildcard = luaL_checkstring(L, 2);
 
81
  luaL_argcheck(L, strlen(wildcard) == 1, 2,
 
82
                "wildcard string must have length 0");
 
83
  gt_alphabet_add_wildcard(*alpha, wildcard[0]);
 
84
  return 0;
 
85
}
 
86
 
 
87
static int alphabet_lua_decode(lua_State *L)
 
88
{
 
89
  GtAlphabet **alpha;
 
90
  unsigned int code;
 
91
  char character;
 
92
  alpha = check_alphabet(L, 1);
 
93
  code = luaL_checkinteger(L, 2);
 
94
  /* XXX: too restrictive, does not consider wildcards */
 
95
  luaL_argcheck(L, code < gt_alphabet_size(*alpha), 2, "invalid code");
 
96
  character = gt_alphabet_decode(*alpha, code);
 
97
  lua_pushlstring(L, &character, 1);
 
98
  return 1;
 
99
}
 
100
 
 
101
static int alphabet_lua_size(lua_State *L)
 
102
{
 
103
  GtAlphabet **alpha;
 
104
  unsigned int size;
 
105
  alpha = check_alphabet(L, 1);
 
106
  size = gt_alphabet_size(*alpha);
 
107
  lua_pushinteger(L, size);
 
108
  return 1;
 
109
}
 
110
 
 
111
static int alphabet_lua_delete(lua_State *L)
 
112
{
 
113
  GtAlphabet **alpha;
 
114
  alpha = check_alphabet(L, 1);
 
115
  gt_alphabet_delete(*alpha);
 
116
  return 0;
 
117
}
 
118
 
 
119
static const struct luaL_Reg alphabet_lib_f [] = {
 
120
  { "alphabet_new_dna", alphabet_lua_new_dna },
 
121
  { "alphabet_new_protein", alphabet_lua_new_protein },
 
122
  { "alphabet_new_empty", alphabet_lua_new_empty },
 
123
  { NULL, NULL }
 
124
};
 
125
 
 
126
static const struct luaL_Reg alphabet_lib_m [] = {
 
127
  { "add_mapping", alphabet_lua_add_mapping },
 
128
  { "add_wildcard", alphabet_lua_add_wildcard },
 
129
  { "decode", alphabet_lua_decode },
 
130
  { "size", alphabet_lua_size },
 
131
  { NULL, NULL }
 
132
};
 
133
 
 
134
int gt_lua_open_alphabet(lua_State *L)
 
135
{
 
136
#ifndef NDEBUG
 
137
  int stack_size;
 
138
#endif
 
139
  gt_assert(L);
 
140
#ifndef NDEBUG
 
141
  stack_size = lua_gettop(L);
 
142
#endif
 
143
  luaL_newmetatable(L, ALPHABET_METATABLE);
 
144
  /* metatable.__index = metatable */
 
145
  lua_pushvalue(L, -1); /* duplicate the metatable */
 
146
  lua_setfield(L, -2, "__index");
 
147
  /* set its _gc field */
 
148
  lua_pushstring(L, "__gc");
 
149
  lua_pushcfunction(L, alphabet_lua_delete);
 
150
  lua_settable(L, -3);
 
151
  /* register functions */
 
152
  luaL_register(L, NULL, alphabet_lib_m);
 
153
  lua_pop(L, 1);
 
154
  luaL_register(L, "gt", alphabet_lib_f);
 
155
  lua_pop(L, 1);
 
156
  gt_assert(lua_gettop(L) == stack_size);
 
157
  return 1;
 
158
}
 
159
 
 
160
void gt_lua_alphabet_push(lua_State *L, GtAlphabet *alpha)
 
161
{
 
162
  GtAlphabet **alphaptr;
 
163
  gt_assert(L && alpha);
 
164
  alphaptr = lua_newuserdata(L, sizeof (*alphaptr));
 
165
  *alphaptr = alpha;
 
166
  luaL_getmetatable(L, ALPHABET_METATABLE);
 
167
  lua_setmetatable(L, -2);
 
168
}