[go: up one dir, main page]

Menu

[971d76]: / module / swig / ow.i  Maximize  Restore  History

Download this file

133 lines (116 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* File: ow.i */
/* $Id$ */
%module OW
%include "typemaps.i"
%init %{
#include "owfs_config.h"
#include "ow.h"
LibSetup() ;
%}
%{
#include "owfs_config.h"
#include "ow.h"
#ifdef OW_MT
pthread_t main_threadid ;
#define IS_MAINTHREAD (main_threadid == pthread_self())
#else /* OW_MT */
#define IS_MAINTHREAD 1
#endif /* OW_MT */
char *version( ) {
return VERSION;
}
int init( const char * dev ) {
int ret = 1 ; /* Good initialization */
// LibSetup() ; /* Done in %init section */
delay_background = 1 ; // No backgrounding done
if ( OWLIB_can_init_start() || OW_ArgGeneric(dev) || LibStart() ) ret = 0 ;
OWLIB_can_init_end() ;
return ret ;
}
int put( const char * path, const char * value ) {
int s ;
int ret = 1 ; /* good result */
/* Overall flag for valid setup */
if ( value==NULL) return 0 ;
if ( OWLIB_can_access_start() ) {
ret = 0 ;
} else {
s = strlen(value) ;
if ( FS_write(path,value,s,0) ) ret = 0 ;
}
OWLIB_can_access_end() ;
return ret ;
}
char * get( const char * path ) {
struct parsedname pn ;
char * buf = NULL ;
int sz ; /* current buffer size */
int s ; /* current string length */
/* Embedded callback function */
void directory( const struct parsedname * const pn2 ) {
int sn = s+OW_FULLNAME_MAX+2 ; /* next buffer limit */
if ( sz<sn ) {
sz = sn ;
buf = realloc( buf, sn ) ;
//printf("Realloc buf pointer=%p,%p\n",buf,&buf);
}
if ( buf ) {
if ( s ) strcpy( &buf[s++], "," ) ;
FS_DirName( &buf[s], OW_FULLNAME_MAX, pn2 ) ;
if (
pn2->dev ==NULL
|| pn2->ft ==NULL
|| pn2->ft->format ==ft_subdir
|| pn2->ft->format ==ft_directory
) strcat( &buf[s], "/" );
s = strlen( buf ) ;
//printf("buf=%s len=%d\n",buf,s);
}
}
if ( OWLIB_can_access_start() ) { /* Prior init */
// buf = NULL ;
} else if ( FS_ParsedName( path, &pn ) ) { /* Parse the input string */
// buf = NULL ;
} else {
//printf("path=%s dev=%p ft=%p subdir=%p format=%d\n",pathcpy,pn.dev,pn.ft,pn.subdir,pn.ft?pn.ft->format:-1) ;
if ( pn.dev==NULL || pn.ft == NULL || pn.subdir ) { /* A directory of some kind */
//printf("Directory\n");
s=sz=0 ;
FS_dir( directory, &pn ) ;
} else { /* A regular file */
//printf("File %s\n",path);
s = FS_size_postparse(&pn) ;
//printf("File len=%d, %s\n",s,path);
if ( (buf=(char *) malloc( s+1 )) ) {
int r = FS_read_3times( buf, s, 0, &pn ) ;
if ( r<0 ) {
free(buf) ;
buf = NULL;
} else {
buf[s] = '\0' ;
// if (r!=s) printf("Mismatch path=%s read=%d len=%d\n",path,r,s);
}
}
}
//printf("End GET\n");
FS_ParsedName_destroy(&pn) ;
if(!buf) buf = strdup("") ; // have to return empty string on error
}
OWLIB_can_access_end() ;
return buf ;
}
void finish( void ) {
OWLIB_can_finish_start() ;
LibClose() ;
OWLIB_can_finish_end() ;
}
%}
%typemap(newfree) char * { if ($1) free($1) ; }
%newobject get ;
extern char *version( );
extern int init( const char * dev ) ;
extern char * get( const char * path ) ;
extern int put( const char * path, const char * value ) ;
extern void finish( void ) ;
extern int error_print;
extern int error_level ;