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;
179 date_to_rm = NEWS_FOREVER;
182 if (lua_istable(L, 1)) {
186 while (lua_next(L, -2)) {
188 if (lua_istable(L, -1)) {
189 faction = title = body = NULL;
192 date_to_rm = NEWS_FOREVER;
194 lua_getfield( L, -1,
"faction" );
195 if (!lua_isnil(L,-1))
196 faction = luaL_checkstring(L,-1);
199 lua_getfield( L, -1,
"head" );
200 if (!lua_isnil(L,-1))
201 title = luaL_checkstring(L,-1);
204 lua_getfield( L, -1,
"body" );
205 if (!lua_isnil(L,-1))
206 body = luaL_checkstring(L,-1);
209 lua_getfield( L, -1,
"date" );
210 if (!lua_isnil(L,-1)) {
211 if (lua_isnumber(L,-1))
212 date = lua_tonumber(L,-1);
218 lua_getfield( L, -1,
"date_to_rm" );
219 if (!lua_isnil(L,-1)) {
220 if (lua_isnumber(L,-1))
221 date_to_rm = lua_tonumber(L,-1);
227 lua_getfield( L, -1,
"priority" );
228 if (!lua_isnil(L,-1))
229 priority = luaL_checkinteger(L,-1);
232 if (title && body && faction)
233 news_add( title, body, faction, NULL, date, date_to_rm, priority );
235 WARN(_(
"Bad arguments"));
251 if (!(lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3))) {
252 WARN(_(
"\nBad arguments, use "
253 "addArticle(\"Faction\",\"Title\",\"Content\",[date,[date_to_rm]])"));
257 faction = luaL_checkstring(L, 1);
258 title = luaL_checkstring(L, 2);
259 body = luaL_checkstring(L, 3);
260 priority = luaL_optinteger(L, 6, 5);
263 if (!lua_isnoneornil(L,4)) {
267 date_to_rm = luaL_checklong(L, 4);
269 if (!lua_isnoneornil(L,5)) {
273 date = luaL_checklong(L, 5);
276 if (title && body && faction) {
277 LuaNews_t n_article =
news_add( title, body, faction, NULL, date, date_to_rm, priority );
281 NLUA_ERROR(L,_(
"Bad arguments"));
300 if (lua_istable(L, 1)) {
302 while (lua_next(L, -2)) {
304 news_rm( *Larticle );
310 news_rm( *Larticle );
345 const char *characteristic = NULL;
349 if (lua_isnoneornil(L, 1))
351 else if (lua_isnumber(L, 1))
352 date = (ntime_t)lua_tonumber(L, 1);
353 else if (lua_isstring(L, 1))
354 characteristic = lua_tostring(L, 1);
356 NLUA_INVALID_PARAMETER(L);
363 if ((n->title == NULL) || (n->desc == NULL) || (n->faction == NULL))
366 if (print_all || date == n->date
368 && ((strcmp( n->title, characteristic ) == 0)
369 || (strcmp( n->desc, characteristic ) == 0)
370 || (strcmp( n->faction, characteristic ) == 0)
371 || (n->tag != NULL && strcmp( n->tag, characteristic ) == 0 )))) {
373 lua_rawseti(L, -2, narticle++);
392 const LuaNews_t *a1, *a2;
395 lua_pushboolean(L, *a1 == *a2);
408 lua_pushstring(L, article_ptr->
title);
421 lua_pushstring(L, article_ptr->
desc);
434 lua_pushstring(L, article_ptr->
faction);
447 lua_pushinteger(L, (lua_Integer)article_ptr->
date);
462 if (lua_istable(L, 1)) {
463 const char *tag = luaL_checkstring(L, 2);
468 while (lua_next(L, -2)) {
470 WARN(_(
"Bad argument to news.date(), must be article or a table of articles"));
474 if (article_ptr == NULL) {
475 WARN(_(
"Article not valid"));
478 article_ptr->
tag = strdup(tag);
485 WARN(_(
"Bad argument to news.date(), must be article or a table of articles"));
489 if (article_ptr == NULL) {
490 WARN(_(
"Article not valid"));
494 tag = luaL_checkstring(L, 2);
495 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.