CHDK_DE Vorschauversion  Trunk Rev. 5215
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
loslib.c-Dateireferenz
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
+ Include-Abhängigkeitsdiagramm für loslib.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  idir_udata_t
 

Makrodefinitionen

#define loslib_c
 
#define LUA_LIB
 
#define IDIR_META   "chdk_idir_meta"
 

Funktionen

static int os_pushresult (lua_State *L, int i, const char *filename)
 
static int os_remove (lua_State *L)
 
static int os_rename (lua_State *L)
 
static void setfield (lua_State *L, const char *key, int value)
 
static void setboolfield (lua_State *L, const char *key, int value)
 
static int getboolfield (lua_State *L, const char *key)
 
static int getfield (lua_State *L, const char *key, int d)
 
static int os_date (lua_State *L)
 
static int os_time (lua_State *L)
 
static int os_difftime (lua_State *L)
 
static int os_mkdir (lua_State *L)
 
static int get_table_optbool (lua_State *L, int narg, const char *fname, int d)
 
static int os_listdir (lua_State *L)
 
static int idir_iter (lua_State *L)
 
static int idir_gc (lua_State *L)
 
static int os_idir (lua_State *L)
 
static void idir_register (lua_State *L)
 
static int os_stat (lua_State *L)
 
static int os_utime (lua_State *L)
 
LUALIB_API int luaopen_os (lua_State *L)
 

Variablen

static const luaL_Reg idir_meta_methods []
 
static const luaL_Reg syslib []
 

Makro-Dokumentation

#define IDIR_META   "chdk_idir_meta"

Definiert in Zeile 309 der Datei loslib.c.

#define loslib_c

Definiert in Zeile 21 der Datei loslib.c.

#define LUA_LIB

Definiert in Zeile 22 der Datei loslib.c.

Dokumentation der Funktionen

static int get_table_optbool ( lua_State L,
int  narg,
const char *  fname,
int  d 
)
static

Definiert in Zeile 260 der Datei loslib.c.

261 {
262  int r;
263  lua_getfield(L, narg, fname);
264  // not set - use default
265  if(lua_isnil(L,-1)) {
266  r=d;
267  } else {// otherwise, treat as bool
268  r=lua_toboolean(L,-1);
269  }
270  lua_pop(L,1);
271  return r;
272 }
static int getboolfield ( lua_State L,
const char *  key 
)
static

Definiert in Zeile 114 der Datei loslib.c.

114  {
115  int res;
116  lua_getfield(L, -1, key);
117  res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
118  lua_pop(L, 1);
119  return res;
120 }
static int getfield ( lua_State L,
const char *  key,
int  d 
)
static

Definiert in Zeile 123 der Datei loslib.c.

123  {
124  int res;
125  lua_getfield(L, -1, key);
126  if (lua_isnumber(L, -1))
127  res = (int)lua_tointeger(L, -1);
128  else {
129  if (d < 0)
130  return luaL_error(L, "field " LUA_QS " missing in date table", key);
131  res = d;
132  }
133  lua_pop(L, 1);
134  return res;
135 }
static int idir_gc ( lua_State L)
static

Definiert in Zeile 347 der Datei loslib.c.

347  {
349  if(ud->dir) {
350  closedir(ud->dir);
351  }
352  return 0;
353 }
static int idir_iter ( lua_State L)
static

Definiert in Zeile 316 der Datei loslib.c.

316  {
317  struct dirent *de;
319  // dir may be on first call if opendir failed, or previous explicit close
320  if(!ud->dir) {
321  return 0;
322  }
323  // allow explicit close by calling iterator(ud,false)
324  // need to check type because first invocation is called with nil
325  if(lua_type(L, 2) == LUA_TBOOLEAN && lua_toboolean(L,2) == 0) {
326  closedir(ud->dir);
327  ud->dir=NULL;
328  return 0;
329  }
330  while((de = readdir(ud->dir))) {
331  // if not all, skip over ignored items
332  if(!ud->all && (de->d_name[0] == '\xE5' || (strcmp(de->d_name,".")==0) || (strcmp(de->d_name,"..")==0))) {
333  continue;
334  }
335  break;
336  }
337  if(de) {
338  lua_pushstring(L, de->d_name);
339  return 1;
340  } else { // on last, close immediately to avoid keeping the handle open until GC
341  closedir(ud->dir);
342  ud->dir=NULL;
343  return 0;
344  }
345 }
static void idir_register ( lua_State L)
static

Definiert in Zeile 410 der Datei loslib.c.

410  {
413 }
LUALIB_API int luaopen_os ( lua_State L)

Definiert in Zeile 536 der Datei loslib.c.

536  {
537  idir_register(L);
539  return 1;
540 }
static int os_date ( lua_State L)
static

Definiert in Zeile 138 der Datei loslib.c.

138  {
139  const char *s = luaL_optstring(L, 1, "%c");
141  struct tm *stm;
142  if (*s == '!') { /* UTC? */
143  #if 0
144  // reyalp - we have no idea about timezones, so just eat the !
145  // and use local time
146  // TODO some cams may be timezone/dst aware ?
147  stm = gmtime(&t);
148  #endif
149  stm = localtime(&t);
150  s++; /* skip `!' */
151  }
152  else
153  stm = localtime(&t);
154  if (stm == NULL) /* invalid date? */
155  lua_pushnil(L);
156  else if (strcmp(s, "*t") == 0) {
157  lua_createtable(L, 0, 9); /* 9 = number of fields */
158  setfield(L, "sec", stm->tm_sec);
159  setfield(L, "min", stm->tm_min);
160  setfield(L, "hour", stm->tm_hour);
161  setfield(L, "day", stm->tm_mday);
162  setfield(L, "month", stm->tm_mon+1);
163  setfield(L, "year", stm->tm_year+1900);
164  setfield(L, "wday", stm->tm_wday+1);
165  setfield(L, "yday", stm->tm_yday+1);
166  setboolfield(L, "isdst", stm->tm_isdst);
167  }
168  else {
169  char cc[3];
170  luaL_Buffer b;
171  cc[0] = '%'; cc[2] = '\0';
172  luaL_buffinit(L, &b);
173  for (; *s; s++) {
174  if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
175  luaL_addchar(&b, *s);
176  else {
177  size_t reslen;
178  char buff[200]; /* should be big enough for any conversion result */
179  cc[1] = *(++s);
180  reslen = strftime(buff, sizeof(buff), cc, stm);
181  luaL_addlstring(&b, buff, reslen);
182  }
183  }
184  luaL_pushresult(&b);
185  }
186  return 1;
187 }
static int os_difftime ( lua_State L)
static

Definiert in Zeile 222 der Datei loslib.c.

222  {
223  lua_pushnumber(L, (time_t)(luaL_checknumber(L, 1) - (time_t)(luaL_optnumber(L, 2, 0))));
224  return 1;
225 }
static int os_idir ( lua_State L)
static

Definiert in Zeile 383 der Datei loslib.c.

383  {
384  DIR *dir;
385  const char *dirname = luaL_checkstring(L, 1);
386  int all=0,od_flags=OPENDIR_FL_CHDK_LFN;
387  if(lua_istable(L,2)) {
388  all=get_table_optbool(L,2,"showall",0);
389  od_flags=(get_table_optbool(L,2,"chdklfn",1))?OPENDIR_FL_CHDK_LFN:OPENDIR_FL_NONE;
390  } else {
391  all=lua_toboolean(L, 2);
392  }
393 
395 
396  idir_udata_t *ud = lua_newuserdata(L,sizeof(idir_udata_t));
397  ud->dir = opendir_chdk(dirname,od_flags); // may be null, in which case iterator will stop on first iteration
398  // no obvious way to return error status
399  ud->all = all;
400 
402  lua_setmetatable(L, -2);
403  return 2;
404 }
static int os_listdir ( lua_State L)
static

Definiert in Zeile 281 der Datei loslib.c.

281  {
282  DIR *dir;
283  struct dirent *de;
284  const char *dirname = luaL_checkstring(L, 1);
285  int all=0,od_flags=OPENDIR_FL_CHDK_LFN;
286  if(lua_istable(L,2)) {
287  all=get_table_optbool(L,2,"showall",0);
288  od_flags=(get_table_optbool(L,2,"chdklfn",1))?OPENDIR_FL_CHDK_LFN:OPENDIR_FL_NONE;
289  } else {
290  all=lua_toboolean(L, 2);
291  }
292  int i=1;
293  dir = opendir_chdk(dirname,od_flags);
294  if(!dir)
295  return os_pushresult(L, 0 , dirname);
296  lua_newtable(L);
297  while((de = readdir(dir))) {
298  if(!all && (de->d_name[0] == '\xE5' || (strcmp(de->d_name,".")==0) || (strcmp(de->d_name,"..")==0)))
299  continue;
300  lua_pushinteger(L, i);
301  lua_pushstring(L, de->d_name);
302  lua_settable(L,-3);
303  ++i;
304  }
305  closedir(dir);
306  return 1;
307 }
static int os_mkdir ( lua_State L)
static

Definiert in Zeile 250 der Datei loslib.c.

250  {
251  const char *dirname = luaL_checkstring(L, 1);
252 #if defined(HOST_LUA) && !defined(_WIN32)
253  return os_pushresult(L, mkdir(dirname,0777) == 0, dirname);
254 #else
255  return os_pushresult(L, mkdir(dirname) == 0, dirname);
256 #endif
257 }
static int os_pushresult ( lua_State L,
int  i,
const char *  filename 
)
static

Definiert in Zeile 30 der Datei loslib.c.

30  {
31  int en = errno; /* calls to Lua API may change this value */
32  if (i) {
33  lua_pushboolean(L, 1);
34  return 1;
35  }
36  else {
37  lua_pushnil(L);
38  lua_pushfstring(L, "%s: %s", filename, strerror(en));
39  lua_pushinteger(L, en);
40  return 3;
41  }
42 }
static int os_remove ( lua_State L)
static

Definiert in Zeile 53 der Datei loslib.c.

53  {
54  const char *filename = luaL_checkstring(L, 1);
55  return os_pushresult(L, remove(filename) == 0, filename);
56 }
static int os_rename ( lua_State L)
static

Definiert in Zeile 59 der Datei loslib.c.

59  {
60  const char *fromname = luaL_checkstring(L, 1);
61  const char *toname = luaL_checkstring(L, 2);
62  return os_pushresult(L, rename(fromname, toname) == 0, fromname);
63 }
static int os_stat ( lua_State L)
static

Definiert in Zeile 417 der Datei loslib.c.

417  {
418  struct stat st;
419  const char *name = luaL_checkstring(L, 1);
420  int result = stat(name,&st);
421  if (result==0) {
422  lua_createtable(L, 0, 6); /* = number of fields */
423  // don't expose the fields that aren't useful
424  // but leave them commented out for reference
425 //#ifndef CAM_DRYOS_2_3_R39
426 // setfield(L,"dev",st.st_dev); /* device ID number */
427 // setfield(L,"ino",st.st_ino); /* no inodes in fat, always -1 */
428 // setfield(L,"mode",st.st_mode); /* file mode (see below) */
429 //#endif
430 // setfield(L,"nlink",st.st_nlink); /* dryos 0, vxworks 1 */
431 // setfield(L,"uid",st.st_uid); /* no users or groups on fat */
432 // setfield(L,"gid",st.st_gid); /* " */
433 //#if !CAM_DRYOS
434 // doesn't appear useful, I wasn't able to stat any special files
435 // setfield(L,"rdev",st.st_rdev); /* device ID, only if special file */
436 //#endif
437  setfield(L,"size",st.st_size); /* size of file, in bytes */
438 //#ifndef CAM_DRYOS_2_3_R39
439 // setfield(L,"atime",st.st_atime); /* time of last access */
440 //#endif
441  setfield(L,"mtime",st.st_mtime); /* time of last modification */
442  setfield(L,"ctime",st.st_ctime); /* time of last change of file status */
443 #ifdef HOST_LUA
444 // fill in some sane values if we aren't running on the camera
445 // from chdk stdlib
446 #define DOS_ATTR_DIRECTORY 0x10 /* entry is a sub-directory */
447 #ifndef CAM_DRYOS_2_3_R39
448  setfield(L,"blksize",512);
449  setfield(L,"blocks",(st.st_size/512) + (st.st_size%512)?1:0);
450 #endif
451  if ( S_ISDIR(st.st_mode) ) {
452  setfield(L,"attrib",DOS_ATTR_DIRECTORY);
453  setboolfield(L,"is_dir",1);
454  setboolfield(L,"is_file",0);
455  }
456  else {
457  setboolfield(L,"is_dir",0);
458  setfield(L,"attrib",0);
459  if S_ISREG(st.st_mode) {
460  setboolfield(L,"is_file",1);
461  }
462  }
463 #else
464 //#ifndef CAM_DRYOS_2_3_R39
465 // setfield(L,"blksize",st.st_blksize); /* This is NOT the dos sector size. Appears to be 512 on all I've tested! */
466 // setfield(L,"blocks",st.st_blocks); /* number of blocks required to store file. May not be the same as size on disk, per above*/
467 //#endif
468  setfield(L,"attrib",st.st_attrib); /* file attribute byte (dosFs only) */
469  // for convenience
470  // note volume labels are neither file nor directory
471  setboolfield(L,"is_dir",st.st_attrib & DOS_ATTR_DIRECTORY);
472  setboolfield(L,"is_file",!(st.st_attrib & (DOS_ATTR_DIRECTORY | DOS_ATTR_VOL_LABEL)));
473 #endif
474 #if 0
475  setfield(L,"reserved1",st.reserved1);
476  setfield(L,"reserved2",st.reserved2);
477  setfield(L,"reserved3",st.reserved3);
478  setfield(L,"reserved4",st.reserved4);
479  setfield(L,"reserved5",st.reserved5);
480  setfield(L,"reserved6",st.reserved6);
481 #endif
482  return 1;
483  }
484  else {
485  int en = errno;
486  lua_pushnil(L);
487  lua_pushfstring(L, "%s: %s", name, strerror(en));
488  lua_pushinteger(L, en);
489  return 3;
490  }
491 }
static int os_time ( lua_State L)
static

Definiert in Zeile 190 der Datei loslib.c.

190  {
191  time_t t;
192  if (lua_isnoneornil(L, 1)) /* called without args? */
193  t = time(NULL); /* get current time */
194  else {
195  struct tm ts;
196  luaL_checktype(L, 1, LUA_TTABLE);
197  lua_settop(L, 1); /* make sure table is at the top */
198  ts.tm_sec = getfield(L, "sec", 0);
199  ts.tm_min = getfield(L, "min", 0);
200  ts.tm_hour = getfield(L, "hour", 12);
201  ts.tm_mday = getfield(L, "day", -1);
202  ts.tm_mon = getfield(L, "month", -1) - 1;
203  ts.tm_year = getfield(L, "year", -1) - 1900;
204  ts.tm_isdst = getboolfield(L, "isdst");
205  t = mktime(&ts);
206  }
207  if (t == (time_t)(-1))
208  lua_pushnil(L);
209  else
210  lua_pushnumber(L, (lua_Number)t);
211  return 1;
212 }
static int os_utime ( lua_State L)
static

Definiert in Zeile 496 der Datei loslib.c.

496  {
497  const char *name = luaL_checkstring(L, 1);
498  struct utimbuf t;
499  t.modtime = luaL_optnumber(L, 2, time(NULL));
500  t.actime = luaL_optnumber(L, 3, time(NULL));
501  return os_pushresult(L, utime(name,&t) == 0, name);
502 }
static void setboolfield ( lua_State L,
const char *  key,
int  value 
)
static

Definiert in Zeile 107 der Datei loslib.c.

107  {
108  if (value < 0) /* undefined? */
109  return; /* does not set field */
110  lua_pushboolean(L, value);
111  lua_setfield(L, -2, key);
112 }
static void setfield ( lua_State L,
const char *  key,
int  value 
)
static

Definiert in Zeile 102 der Datei loslib.c.

102  {
103  lua_pushinteger(L, value);
104  lua_setfield(L, -2, key);
105 }

Variablen-Dokumentation

const luaL_Reg idir_meta_methods[]
static
Initialisierung:
= {
{"__gc", idir_gc},
}

Definiert in Zeile 406 der Datei loslib.c.

const luaL_Reg syslib[]
static
Initialisierung:
= {
{"date", os_date},
{"difftime", os_difftime},
{"mkdir", os_mkdir},
{"listdir", os_listdir},
{"idir", os_idir},
{"stat", os_stat},
{"utime", os_utime},
{"remove", os_remove},
{"rename", os_rename},
{"time", os_time},
}

Definiert in Zeile 504 der Datei loslib.c.