source: trunk/core/module_wrappers.c @ 1505

Revision 1505, 2.5 KB checked in by reyalp, 17 months ago (diff)

merge tsvstar module code from reyalp-flt branch, + fixups for r1499, see http://chdk.setepontos.com/index.php?topic=847.msg77690#msg77690 and following posts
NOTE modules in CHDK/MODULES are now required for CHDK to work properly. On multi-partition cards, this goes on the large card.
Adds tetris and snake, originally contributed by elektro255 in http://chdk.setepontos.com/index.php?topic=2925.0

Line 
1#include "stdlib.h"
2#include "stdlib_unified.h"
3
4/*******************************************************
5// Some of base IO functions are platform dependend.
6//  This file containt wrapper unify them for modules.
7//
8//  open - different files could be used
9//  stat - different "stat" data structure used
10//  *dir - different "dirent" data structure used
11*******************************************************/
12
13
14//-------------------------------------------------------
15int safe_open (const char *name, int flags, int mode )
16{
17        int real_flags = flags & ~(STD_O_TRUNC|STD_O_CREAT);
18        if ( flags & STD_O_TRUNC ) real_flags |= O_TRUNC;
19        if ( flags & STD_O_CREAT ) real_flags |= O_CREAT;
20        return open( name, real_flags, mode);
21}
22
23//-------------------------------------------------------
24int safe_stat (const char *name, struct STD_stat *pStat)
25{
26  struct stat st;
27  int rv;
28
29  if ( pStat==0 ) return 1;
30  memset( pStat, 0, sizeof(struct STD_stat));
31  rv =  stat( name, &st );
32  if  ( rv != 0 ) return rv;
33
34  pStat->st_attrib = st.st_attrib;
35  pStat->st_size   = st.st_size;
36  pStat->st_ctime =  st.st_ctime;
37  pStat->st_mtime =  st.st_mtime;
38
39  return rv;
40}
41
42
43//-------------------------------------------------------
44STD_DIR* safe_opendir (const char* name)
45{
46        DIR* dh = opendir(name);
47        STD_DIR* std = 0;
48
49        if ( dh==0 ) return 0;
50
51        std = malloc (sizeof(STD_DIR));
52        if ( std == 0 ) {
53                closedir(dh);
54                return 0;
55        }
56
57        memset( std, 0, sizeof(STD_DIR));
58        std->dh = dh;
59
60        return std;
61}
62
63//-------------------------------------------------------
64int safe_closedir (STD_DIR* dir)
65{
66        int rv;
67
68        if ( dir==0 ) return 0;
69        rv = closedir( dir->dh );
70        free( dir );
71        return rv;
72}
73
74/*
75// rewinddir is used used nowhere. so commented for now
76//-------------------------------------------------------
77void safe_rewinddir (STD_DIR* dir )
78{
79        if ( dir!=0 )
80                rewinddir( dir->dh );
81}
82*/
83
84//-------------------------------------------------------
85struct STD_dirent* safe_readdir (STD_DIR* dir)
86{
87    struct dirent* de;
88
89        if ( dir==0 ) return 0;
90        de = readdir( dir->dh );
91
92        if ( de==0 ) return 0;
93
94#if !CAM_DRYOS
95        memset( &(dir->de), 0, sizeof(dir->de));
96#else
97        dir->de.unk1 = de->unk1;
98        dir->de.attrib = de->attrib;
99        dir->de.size  = de->size;
100        dir->de.time1 = de->time1;
101        dir->de.time2 = de->time2;
102        dir->de.time3 = de->time3;
103#endif
104        strncpy( dir->de.d_name, de->d_name, sizeof(dir->de.d_name));
105        dir->de.d_name[sizeof(dir->de.d_name)-1]=0;
106
107        return &(dir->de);
108}
Note: See TracBrowser for help on using the repository browser.