1
/*****************************************************************************\
2
pcardext - Python extension for HP photocard services
4
(c) Copyright 2003-2015 HP 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
#if PY_MAJOR_VERSION >= 3
42
#define MOD_ERROR_VAL NULL
43
#define MOD_SUCCESS_VAL(val) val
44
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
45
#define PyInt_AS_LONG PyLong_AS_LONG
46
#define MOD_DEF(ob, name, doc, methods) \
47
static struct PyModuleDef moduledef = { \
48
PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
49
ob = PyModule_Create(&moduledef);
52
#define PY_String_Bytes PyBytes_FromStringAndSize
53
#define PY_AsString_Bytes PyBytes_AsStringAndSize
57
#define MOD_SUCCESS_VAL(val)
58
#define MOD_INIT(name) void init##name(void)
59
#define MOD_DEF(ob, name, doc, methods) \
60
ob = Py_InitModule3(name, methods, doc);
62
#define PY_String_Bytes PyString_FromStringAndSize
63
#define PY_AsString_Bytes PyString_AsStringAndSize
67
PyObject * readsectorFunc = NULL;
68
PyObject * writesectorFunc = NULL;
72
int ReadSector(int sector, int nsector, void *buf, int size)
79
if( nsector <= 0 || (nsector*FAT_HARDSECT) > size || nsector > FAT_BLKSIZE )
82
result = PyObject_CallFunction( readsectorFunc, "ii", sector, nsector );
88
//PyString_AsStringAndSize( result, &result_str, &len );
89
//PyBytes_AsStringAndSize( result, &result_str, &len );
90
PY_AsString_Bytes( result, &result_str, &len );
93
if( len < nsector*FAT_HARDSECT )
98
memcpy( buf, result_str, nsector*FAT_HARDSECT );
107
int WriteSector(int sector, int nsector, void *buf, int size )
111
if( writesectorFunc )
113
result = PyObject_CallFunction( writesectorFunc, "iis#", sector, nsector, buf, size );
115
return PyInt_AS_LONG( result );
122
PyObject * pcardext_mount( PyObject * self, PyObject * args )
124
if( !PyArg_ParseTuple( args, "OO", &readsectorFunc, &writesectorFunc ) )
126
return Py_BuildValue( "i", 1 );
129
if( !PyCallable_Check( readsectorFunc ) || !PyCallable_Check( writesectorFunc ) )
131
return Py_BuildValue( "i", 2 );
134
Py_INCREF( readsectorFunc );
135
Py_INCREF( writesectorFunc );
139
sprintf( buf, "print 'FatInit()=%d\n'", i );
140
PyRun_SimpleString( buf );*/
142
return Py_BuildValue( "i", i ); // ==0 ->OK, !=0 --> NG
146
PyObject * pcardext_df( PyObject * self, PyObject * args )
148
return Py_BuildValue( "i", FatFreeSpace() );
152
PyObject * pcardext_ls( PyObject * self, PyObject * args )
154
PyObject * file_list;
155
file_list = PyList_New((Py_ssize_t)0);
163
PyList_Append( file_list, Py_BuildValue( "(sci)", fa.Name, fa.Attr, fa.Size ) );
165
} while( FatDirNext( &fa ) );
171
PyObject * pcardext_cp( PyObject * self, PyObject * args )
176
if( !PyArg_ParseTuple( args, "si", &name, &fileno ) )
178
return Py_BuildValue( "i", 0 );
181
return Py_BuildValue( "i", FatReadFile( name, fileno ) );
184
PyObject * pcardext_cd( PyObject * self, PyObject * args )
188
if( !PyArg_ParseTuple( args, "s", &dir ) )
190
return Py_BuildValue( "i", 0 );
195
return Py_BuildValue( "i", 1 );
198
PyObject * pcardext_rm( PyObject * self, PyObject * args )
201
if( !PyArg_ParseTuple( args, "s", &name ) )
203
return Py_BuildValue( "i", 0 );
206
return Py_BuildValue( "i", FatDeleteFile( name ) );
209
PyObject * pcardext_umount( PyObject * self, PyObject * args )
211
return Py_BuildValue( "" );
214
PyObject * pcardext_info( PyObject * self, PyObject * args )
216
PHOTO_CARD_ATTRIBUTES pa;
217
FatDiskAttributes( &pa );
219
return Py_BuildValue( "(siiiiissi)", pa.OEMID, pa.BytesPerSector, pa.SectorsPerCluster, pa.ReservedSectors,
220
pa.RootEntries, pa.SectorsPerFat, pa.VolumeLabel, pa.SystemID,
224
PyObject * pcardext_read( PyObject * self, PyObject * args )
231
if( !PyArg_ParseTuple( args, "sii", &name, &offset, &len ) )
233
return Py_BuildValue( "s", "" );
236
buffer = alloca( len );
238
if( FatReadFileExt( name, offset, len, buffer ) == len )
240
// return PyString_FromStringAndSize( (char *)buffer, len );
241
return PY_String_Bytes( (char *)buffer, len );
242
// return PyBytes_FromStringAndSize( (char *)buffer, len );
246
return Py_BuildValue( "s", "" );
253
static PyMethodDef pcardext_methods[] =
255
{ "mount", (PyCFunction)pcardext_mount, METH_VARARGS },
256
{ "ls", (PyCFunction)pcardext_ls, METH_VARARGS },
257
{ "cp", (PyCFunction)pcardext_cp, METH_VARARGS },
258
{ "cd", (PyCFunction)pcardext_cd, METH_VARARGS },
259
{ "rm", (PyCFunction)pcardext_rm, METH_VARARGS },
260
{ "umount", (PyCFunction)pcardext_umount, METH_VARARGS },
261
{ "df", (PyCFunction)pcardext_df, METH_VARARGS },
262
{ "info", (PyCFunction)pcardext_info, METH_VARARGS },
263
{ "read", (PyCFunction)pcardext_read, METH_VARARGS },
268
static char pcardext_documentation[] = "Python extension for HP photocard services";
273
MOD_DEF(mod, "pcardext", pcardext_documentation, pcardext_methods);
275
return MOD_ERROR_VAL;
277
return MOD_SUCCESS_VAL(mod);