1 /*****************************************************************************\
2 pcardext - Python extension for HP photocard services
4 (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
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.
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.
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
25 \*****************************************************************************/
28 #include <structmember.h>
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
41 PyObject * readsectorFunc = NULL;
42 PyObject * writesectorFunc = NULL;
44 int ReadSector(int sector, int nsector, void *buf, int size)
51 if( nsector <= 0 || (nsector*FAT_HARDSECT) > size || nsector > FAT_BLKSIZE )
54 result = PyObject_CallFunction( readsectorFunc, "ii", sector, nsector );
59 PyString_AsStringAndSize( result, &result_str, &len );
61 if( len < nsector*FAT_HARDSECT )
66 memcpy( buf, result_str, nsector*FAT_HARDSECT );
75 int WriteSector(int sector, int nsector, void *buf, int size )
81 result = PyObject_CallFunction( writesectorFunc, "iis#", sector, nsector, buf, size );
83 return PyInt_AS_LONG( result );
90 PyObject * pcardext_mount( PyObject * self, PyObject * args )
92 if( !PyArg_ParseTuple( args, "OO", &readsectorFunc, &writesectorFunc ) )
94 return Py_BuildValue( "i", 1 );
97 if( !PyCallable_Check( readsectorFunc ) || !PyCallable_Check( writesectorFunc ) )
99 return Py_BuildValue( "i", 2 );
102 Py_INCREF( readsectorFunc );
103 Py_INCREF( writesectorFunc );
107 sprintf( buf, "print 'FatInit()=%d\n'", i );
108 PyRun_SimpleString( buf );*/
110 return Py_BuildValue( "i", i ); // ==0 ->OK, !=0 --> NG
114 PyObject * pcardext_df( PyObject * self, PyObject * args )
116 return Py_BuildValue( "i", FatFreeSpace() );
120 PyObject * pcardext_ls( PyObject * self, PyObject * args )
122 PyObject * file_list;
123 file_list = PyList_New((Py_ssize_t)0);
131 PyList_Append( file_list, Py_BuildValue( "(sci)", fa.Name, fa.Attr, fa.Size ) );
133 } while( FatDirNext( &fa ) );
139 PyObject * pcardext_cp( PyObject * self, PyObject * args )
144 if( !PyArg_ParseTuple( args, "si", &name, &fileno ) )
146 return Py_BuildValue( "i", 0 );
149 return Py_BuildValue( "i", FatReadFile( name, fileno ) );
152 PyObject * pcardext_cd( PyObject * self, PyObject * args )
156 if( !PyArg_ParseTuple( args, "s", &dir ) )
158 return Py_BuildValue( "i", 0 );
163 return Py_BuildValue( "i", 1 );
166 PyObject * pcardext_rm( PyObject * self, PyObject * args )
169 if( !PyArg_ParseTuple( args, "s", &name ) )
171 return Py_BuildValue( "i", 0 );
174 return Py_BuildValue( "i", FatDeleteFile( name ) );
177 PyObject * pcardext_umount( PyObject * self, PyObject * args )
179 return Py_BuildValue( "" );
182 PyObject * pcardext_info( PyObject * self, PyObject * args )
184 PHOTO_CARD_ATTRIBUTES pa;
185 FatDiskAttributes( &pa );
187 return Py_BuildValue( "(siiiiissi)", pa.OEMID, pa.BytesPerSector, pa.SectorsPerCluster, pa.ReservedSectors,
188 pa.RootEntries, pa.SectorsPerFat, pa.VolumeLabel, pa.SystemID,
192 PyObject * pcardext_read( PyObject * self, PyObject * args )
199 if( !PyArg_ParseTuple( args, "sii", &name, &offset, &len ) )
201 return Py_BuildValue( "s", "" );
204 buffer = alloca( len );
206 if( FatReadFileExt( name, offset, len, buffer ) == len )
208 return PyString_FromStringAndSize( (char *)buffer, len );
212 return Py_BuildValue( "s", "" );
219 static PyMethodDef pcardext_methods[] =
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 },
234 static char pcardext_documentation[] = "Python extension for HP photocard services";
236 void initpcardext( void )
238 PyObject * mod = Py_InitModule4( "pcardext", pcardext_methods,
239 pcardext_documentation, (PyObject*)NULL,
240 PYTHON_API_VERSION );