Tizen 2.1 base
[platform/upstream/hplip.git] / pcard / pcardext / pcardext.c
1 /*****************************************************************************\
2 pcardext - Python extension for HP photocard services
3  
4  (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
5
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19
20 Requires:
21 Python 2.2+
22
23 Author: Don Welch
24
25 \*****************************************************************************/
26
27 #include <Python.h>
28 #include <structmember.h>
29 #include "../fat.h"
30
31
32 /* Ref: PEP 353 (Python 2.5) */
33 #if PY_VERSION_HEX < 0x02050000
34 typedef int Py_ssize_t;
35 #define PY_SSIZE_T_MAX INT_MAX
36 #define PY_SSIZE_T_MIN INT_MIN
37 #endif
38
39 int verbose=0;
40
41 PyObject * readsectorFunc = NULL;
42 PyObject * writesectorFunc = NULL;
43
44 int ReadSector(int sector, int nsector, void *buf, int size)
45 {
46     PyObject * result;
47     char * result_str;
48     
49     if( readsectorFunc )
50     {
51         if( nsector <= 0 || (nsector*FAT_HARDSECT) > size || nsector > FAT_BLKSIZE )
52             goto abort;
53         
54         result = PyObject_CallFunction( readsectorFunc, "ii", sector, nsector );
55         
56         if( result )
57         {
58             Py_ssize_t len = 0;
59             PyString_AsStringAndSize( result, &result_str, &len );
60             
61             if( len < nsector*FAT_HARDSECT )
62             {
63                 goto abort;
64             }
65             
66             memcpy( buf, result_str, nsector*FAT_HARDSECT );
67             return 0;
68         }
69     }
70     
71 abort:    
72     return 1;
73 }
74
75 int WriteSector(int sector, int nsector, void *buf, int size )
76 {
77     PyObject * result;
78     
79     if( writesectorFunc )
80     {
81         result = PyObject_CallFunction( writesectorFunc, "iis#", sector, nsector, buf, size );
82         
83         return PyInt_AS_LONG( result );
84     }
85
86     return 1;
87 }
88
89
90 PyObject * pcardext_mount( PyObject * self, PyObject * args ) 
91 {
92     if( !PyArg_ParseTuple( args, "OO", &readsectorFunc, &writesectorFunc ) )
93     {
94         return Py_BuildValue( "i", 1 );
95     }
96     
97     if( !PyCallable_Check( readsectorFunc ) || !PyCallable_Check( writesectorFunc ) )
98     {
99         return Py_BuildValue( "i", 2 );
100     }
101         
102     Py_INCREF( readsectorFunc );
103     Py_INCREF( writesectorFunc );
104
105     int i = FatInit();
106     /*char buf[1024];
107     sprintf( buf, "print 'FatInit()=%d\n'", i );
108     PyRun_SimpleString( buf );*/
109
110     return Py_BuildValue( "i", i ); // ==0 ->OK, !=0 --> NG
111 }
112
113
114 PyObject * pcardext_df( PyObject * self, PyObject * args ) 
115 {
116     return Py_BuildValue( "i", FatFreeSpace() );
117 }
118
119
120 PyObject * pcardext_ls( PyObject * self, PyObject * args ) 
121 {
122     PyObject * file_list;
123     file_list = PyList_New((Py_ssize_t)0);
124     FILE_ATTRIBUTES fa;
125
126     FatDirBegin( &fa );
127     
128     do 
129     {
130         if( fa.Attr != 'x' )
131             PyList_Append( file_list, Py_BuildValue( "(sci)",  fa.Name, fa.Attr, fa.Size ) );
132         
133     } while( FatDirNext( &fa ) );
134
135     return file_list;
136 }
137
138
139 PyObject * pcardext_cp( PyObject * self, PyObject * args ) 
140 {
141     char * name; 
142     int fileno = 0;
143     
144     if( !PyArg_ParseTuple( args, "si", &name, &fileno ) )
145     {
146         return Py_BuildValue( "i", 0 );
147     }
148     
149     return Py_BuildValue( "i", FatReadFile( name, fileno ) );
150 }
151
152 PyObject * pcardext_cd( PyObject * self, PyObject * args ) 
153 {
154     char * dir;
155
156     if( !PyArg_ParseTuple( args, "s", &dir ) )
157     {
158         return Py_BuildValue( "i", 0 );
159     }
160     
161     FatSetCWD( dir );
162     
163     return Py_BuildValue( "i", 1 );
164 }
165
166 PyObject * pcardext_rm( PyObject * self, PyObject * args ) 
167 {
168     char * name;
169     if( !PyArg_ParseTuple( args, "s", &name ) )
170     {
171         return Py_BuildValue( "i", 0 );
172     }
173
174     return Py_BuildValue( "i", FatDeleteFile( name ) );
175 }
176
177 PyObject * pcardext_umount( PyObject * self, PyObject * args ) 
178 {
179     return Py_BuildValue( "" );
180 }
181
182 PyObject * pcardext_info( PyObject * self, PyObject * args ) 
183 {
184     PHOTO_CARD_ATTRIBUTES pa;
185     FatDiskAttributes( &pa );
186     
187     return Py_BuildValue( "(siiiiissi)", pa.OEMID, pa.BytesPerSector, pa.SectorsPerCluster, pa.ReservedSectors,
188                                         pa.RootEntries, pa.SectorsPerFat, pa.VolumeLabel, pa.SystemID,
189                                         pa.WriteProtect );
190 }
191
192 PyObject * pcardext_read( PyObject * self, PyObject * args ) 
193 {
194     char * name;
195     int offset = 0;
196     Py_ssize_t len = 0;
197     void * buffer;
198     
199     if( !PyArg_ParseTuple( args, "sii", &name, &offset, &len ) )
200     {
201         return Py_BuildValue( "s", "" );
202     }
203     
204     buffer = alloca( len );
205     
206     if( FatReadFileExt( name, offset, len, buffer ) == len )
207     {
208         return PyString_FromStringAndSize( (char *)buffer, len );
209     }
210     else
211     {
212         return Py_BuildValue( "s", "" );
213     }
214     
215     
216 }
217
218
219 static PyMethodDef pcardext_methods[] = 
220 {
221     { "mount",       (PyCFunction)pcardext_mount,  METH_VARARGS },
222     { "ls",          (PyCFunction)pcardext_ls,     METH_VARARGS },
223     { "cp",          (PyCFunction)pcardext_cp,     METH_VARARGS },
224     { "cd",          (PyCFunction)pcardext_cd,     METH_VARARGS },
225     { "rm",          (PyCFunction)pcardext_rm,     METH_VARARGS },
226     { "umount",      (PyCFunction)pcardext_umount, METH_VARARGS },
227     { "df",          (PyCFunction)pcardext_df,     METH_VARARGS },
228     { "info",        (PyCFunction)pcardext_info,   METH_VARARGS },
229     { "read",        (PyCFunction)pcardext_read,   METH_VARARGS },
230     { NULL, NULL }
231 };  
232
233
234 static char pcardext_documentation[] = "Python extension for HP photocard services";
235
236 void initpcardext( void )
237 {
238     PyObject * mod = Py_InitModule4( "pcardext", pcardext_methods, 
239                                      pcardext_documentation, (PyObject*)NULL, 
240                                      PYTHON_API_VERSION );
241                      
242     if (mod == NULL)
243       return;
244 }
245
246