74 return (LuaNews_t*) lua_touserdata(L,ind);
87 luaL_typerror(L, ind, NEWS_METATABLE);
99 LuaNews_t *la = (LuaNews_t*) lua_newuserdata(L,
sizeof(LuaNews_t));
101 luaL_getmetatable(L, NEWS_METATABLE);
102 lua_setmetatable(L, -2);
116 if (lua_getmetatable(L,ind)==0)
118 lua_getfield(L, LUA_REGISTRYINDEX, NEWS_METATABLE);
121 if (lua_rawequal(L, -1, -2))
140 NLUA_ERROR(L, _(
"Article is invalid."));
169 const char *title, *body, *faction;
170 ntime_t date, date_to_rm;
178 date_to_rm = NEWS_FOREVER;
181 if (lua_istable(L, 1)) {
185 while (lua_next(L, -2)) {
187 if (lua_istable(L, -1)) {
188 faction = title = body = NULL;
191 date_to_rm = NEWS_FOREVER;
193 lua_getfield( L, -1,
"faction" );
194 if (!lua_isnil(L,-1))
195 faction = luaL_checkstring(L,-1);
198 lua_getfield( L, -1,
"head" );
199 if (!lua_isnil(L,-1))
200 title = luaL_checkstring(L,-1);
203 lua_getfield( L, -1,
"body" );
204 if (!lua_isnil(L,-1))
205 body = luaL_checkstring(L,-1);
208 lua_getfield( L, -1,
"date" );
209 if (!lua_isnil(L,-1)) {
210 if (lua_isnumber(L,-1))
211 date = lua_tonumber(L,-1);
217 lua_getfield( L, -1,
"date_to_rm" );
218 if (!lua_isnil(L,-1)) {
219 if (lua_isnumber(L,-1))
220 date_to_rm = lua_tonumber(L,-1);
226 lua_getfield( L, -1,
"priority" );
227 if (!lua_isnil(L,-1))
228 priority = luaL_checkinteger(L,-1);
231 if (title && body && faction)
232 news_add( title, body, faction, NULL, date, date_to_rm, priority );
234 WARN(_(
"Bad arguments"));
250 if (!(lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3))) {
251 WARN(_(
"\nBad arguments, use "
252 "addArticle(\"Faction\",\"Title\",\"Content\",[date,[date_to_rm]])"));
256 faction = luaL_checkstring(L, 1);
257 title = luaL_checkstring(L, 2);
258 body = luaL_checkstring(L, 3);
259 priority = luaL_optinteger(L, 6, 5);
262 if (!lua_isnoneornil(L,4)) {
266 date_to_rm = luaL_checklong(L, 4);
268 if (!lua_isnoneornil(L,5)) {
272 date = luaL_checklong(L, 5);
275 if (title && body && faction) {
276 LuaNews_t n_article =
news_add( title, body, faction, NULL, date, date_to_rm, priority );
280 return NLUA_ERROR(L,_(
"Bad arguments"));
299 if (lua_istable(L, 1)) {
301 while (lua_next(L, -2)) {
303 news_rm( *Larticle );
309 news_rm( *Larticle );
344 const char *characteristic = NULL;
348 if (lua_isnoneornil(L, 1))
350 else if (lua_isnumber(L, 1))
351 date = (ntime_t)lua_tonumber(L, 1);
352 else if (lua_isstring(L, 1))
353 characteristic = lua_tostring(L, 1);
355 NLUA_INVALID_PARAMETER(L,1);
362 if ((n->title == NULL) || (n->desc == NULL) || (n->faction == NULL))
365 if (print_all || date == n->date
367 && ((strcmp( n->title, characteristic ) == 0)
368 || (strcmp( n->desc, characteristic ) == 0)
369 || (strcmp( n->faction, characteristic ) == 0)
370 || (n->tag != NULL && strcmp( n->tag, characteristic ) == 0 )))) {
372 lua_rawseti(L, -2, narticle++);
391 const LuaNews_t *a1, *a2;
394 lua_pushboolean(L, *a1 == *a2);
407 lua_pushstring(L, article_ptr->
title);
420 lua_pushstring(L, article_ptr->
desc);
433 lua_pushstring(L, article_ptr->
faction);
446 lua_pushinteger(L, (lua_Integer)article_ptr->
date);
461 if (lua_istable(L, 1)) {
462 const char *tag = luaL_checkstring(L, 2);
467 while (lua_next(L, -2)) {
469 WARN(_(
"Bad argument to news.date(), must be article or a table of articles"));
473 if (article_ptr == NULL) {
474 WARN(_(
"Article not valid"));
477 article_ptr->
tag = strdup(tag);
484 WARN(_(
"Bad argument to news.date(), must be article or a table of articles"));
488 if (article_ptr == NULL) {
489 WARN(_(
"Article not valid"));
493 tag = luaL_checkstring(L, 2);
494 article_ptr->
tag = strdup( tag );
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
void bar_regen(void)
Regenerates the bar list.
Header file with generic functions and naev-specifics.
int news_add(const char *title, const char *content, const char *faction, const char *tag, ntime_t date, ntime_t date_to_rm, int priority)
makes a new article and puts it into the list
news_t * news_get(int id)
gets the article with id ID, else NULL
int * generate_news(int faction)
Generates news from newslist from specific faction AND Generic news.
static int newsL_bind(lua_State *L)
Tags a news article or a table of articles with a string. Lua function parameter: Article a Article t...
static int newsL_eq(lua_State *L)
Check news articles for equality.
int lua_isnews(lua_State *L, int ind)
Checks to see if ind is a news.
static const luaL_Reg news_methods[]
int nlua_loadNews(nlua_env env)
Loads the news library.
static int newsL_rm(lua_State *L)
Frees a news article or a table of articles. Lua function parameter: News n News article to free.
static int newsL_get(lua_State *L)
Gets all matching news articles in a table.
static int newsL_desc(lua_State *L)
Gets the news article description. Lua function parameter: Article a article to get the desc of Lua r...
LuaNews_t * luaL_checknews(lua_State *L, int ind)
Gets news at index or raises error if there is no news at index.
static int newsL_add(lua_State *L)
Lua bindings to interact with the news.
news_t * luaL_validnews(lua_State *L, int ind)
Makes sure the news is valid or raises a Lua error.
LuaNews_t * lua_pushnews(lua_State *L, LuaNews_t news)
Pushes a news on the stack.
LuaNews_t * lua_tonews(lua_State *L, int ind)
Gets news at index.
static int newsL_title(lua_State *L)
Gets the news article title. Lua function parameter: Article a article to get the title of Lua return...
static int newsL_date(lua_State *L)
Gets the news article date. Lua function parameter: Article a article to get the date of Lua return p...
static int newsL_faction(lua_State *L)
Gets the news article faction. Lua function parameter: Article a article to get the faction of Lua re...
int lua_istime(lua_State *L, int ind)
Checks to see if ind is a time.
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
ntime_t ntime_get(void)
Gets the current time.
Represents a news article.