~mimose/+junk/hplip-3.16.11

« back to all changes in this revision

Viewing changes to pcard/pcardext/pcardext.c

  • Committer: guoyalong
  • Date: 2017-09-20 10:13:05 UTC
  • Revision ID: guoyalong@kylinos.cn-20170920101305-82zaolzpv1qghz29
Modified debian/control & debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************\
 
2
pcardext - Python extension for HP photocard services
 
3
 
 
4
 (c) Copyright 2003-2015 HP 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
#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);
 
50
 
 
51
 
 
52
  #define PY_String_Bytes  PyBytes_FromStringAndSize
 
53
  #define PY_AsString_Bytes  PyBytes_AsStringAndSize
 
54
 
 
55
#else
 
56
  #define MOD_ERROR_VAL
 
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);
 
61
 
 
62
  #define PY_String_Bytes PyString_FromStringAndSize
 
63
  #define PY_AsString_Bytes PyString_AsStringAndSize
 
64
  
 
65
#endif
 
66
 
 
67
PyObject * readsectorFunc = NULL;
 
68
PyObject * writesectorFunc = NULL;
 
69
 
 
70
 
 
71
 
 
72
int ReadSector(int sector, int nsector, void *buf, int size)
 
73
{
 
74
    PyObject * result;
 
75
    char * result_str;
 
76
    
 
77
    if( readsectorFunc )
 
78
    {
 
79
        if( nsector <= 0 || (nsector*FAT_HARDSECT) > size || nsector > FAT_BLKSIZE )
 
80
            goto abort;
 
81
        
 
82
        result = PyObject_CallFunction( readsectorFunc, "ii", sector, nsector );
 
83
        
 
84
        if( result )
 
85
        {
 
86
            Py_ssize_t len = 0;
 
87
 
 
88
            //PyString_AsStringAndSize( result, &result_str, &len );    
 
89
            //PyBytes_AsStringAndSize( result, &result_str, &len ); 
 
90
            PY_AsString_Bytes( result, &result_str, &len );
 
91
            
 
92
 
 
93
            if( len < nsector*FAT_HARDSECT )
 
94
            {
 
95
                goto abort;
 
96
            }
 
97
            
 
98
            memcpy( buf, result_str, nsector*FAT_HARDSECT );
 
99
            return 0;
 
100
        }
 
101
    }
 
102
    
 
103
abort:    
 
104
    return 1;
 
105
}
 
106
 
 
107
int WriteSector(int sector, int nsector, void *buf, int size )
 
108
{
 
109
    PyObject * result;
 
110
    
 
111
    if( writesectorFunc )
 
112
    {
 
113
        result = PyObject_CallFunction( writesectorFunc, "iis#", sector, nsector, buf, size );
 
114
        
 
115
        return PyInt_AS_LONG( result );
 
116
    }
 
117
 
 
118
    return 1;
 
119
}
 
120
 
 
121
 
 
122
PyObject * pcardext_mount( PyObject * self, PyObject * args ) 
 
123
{
 
124
    if( !PyArg_ParseTuple( args, "OO", &readsectorFunc, &writesectorFunc ) )
 
125
    {
 
126
        return Py_BuildValue( "i", 1 );
 
127
    }
 
128
    
 
129
    if( !PyCallable_Check( readsectorFunc ) || !PyCallable_Check( writesectorFunc ) )
 
130
    {
 
131
        return Py_BuildValue( "i", 2 );
 
132
    }
 
133
        
 
134
    Py_INCREF( readsectorFunc );
 
135
    Py_INCREF( writesectorFunc );
 
136
 
 
137
    int i = FatInit();
 
138
    /*char buf[1024];
 
139
    sprintf( buf, "print 'FatInit()=%d\n'", i );
 
140
    PyRun_SimpleString( buf );*/
 
141
 
 
142
    return Py_BuildValue( "i", i ); // ==0 ->OK, !=0 --> NG
 
143
}
 
144
 
 
145
 
 
146
PyObject * pcardext_df( PyObject * self, PyObject * args ) 
 
147
{
 
148
    return Py_BuildValue( "i", FatFreeSpace() );
 
149
}
 
150
 
 
151
 
 
152
PyObject * pcardext_ls( PyObject * self, PyObject * args ) 
 
153
{
 
154
    PyObject * file_list;
 
155
    file_list = PyList_New((Py_ssize_t)0);
 
156
    FILE_ATTRIBUTES fa;
 
157
 
 
158
    FatDirBegin( &fa );
 
159
    
 
160
    do 
 
161
    {
 
162
        if( fa.Attr != 'x' )
 
163
            PyList_Append( file_list, Py_BuildValue( "(sci)",  fa.Name, fa.Attr, fa.Size ) );
 
164
        
 
165
    } while( FatDirNext( &fa ) );
 
166
 
 
167
    return file_list;
 
168
}
 
169
 
 
170
 
 
171
PyObject * pcardext_cp( PyObject * self, PyObject * args ) 
 
172
{
 
173
    char * name; 
 
174
    int fileno = 0;
 
175
    
 
176
    if( !PyArg_ParseTuple( args, "si", &name, &fileno ) )
 
177
    {
 
178
        return Py_BuildValue( "i", 0 );
 
179
    }
 
180
    
 
181
    return Py_BuildValue( "i", FatReadFile( name, fileno ) );
 
182
}
 
183
 
 
184
PyObject * pcardext_cd( PyObject * self, PyObject * args ) 
 
185
{
 
186
    char * dir;
 
187
 
 
188
    if( !PyArg_ParseTuple( args, "s", &dir ) )
 
189
    {
 
190
        return Py_BuildValue( "i", 0 );
 
191
    }
 
192
    
 
193
    FatSetCWD( dir );
 
194
    
 
195
    return Py_BuildValue( "i", 1 );
 
196
}
 
197
 
 
198
PyObject * pcardext_rm( PyObject * self, PyObject * args ) 
 
199
{
 
200
    char * name;
 
201
    if( !PyArg_ParseTuple( args, "s", &name ) )
 
202
    {
 
203
        return Py_BuildValue( "i", 0 );
 
204
    }
 
205
 
 
206
    return Py_BuildValue( "i", FatDeleteFile( name ) );
 
207
}
 
208
 
 
209
PyObject * pcardext_umount( PyObject * self, PyObject * args ) 
 
210
{
 
211
    return Py_BuildValue( "" );
 
212
}
 
213
 
 
214
PyObject * pcardext_info( PyObject * self, PyObject * args ) 
 
215
{
 
216
    PHOTO_CARD_ATTRIBUTES pa;
 
217
    FatDiskAttributes( &pa );
 
218
    
 
219
    return Py_BuildValue( "(siiiiissi)", pa.OEMID, pa.BytesPerSector, pa.SectorsPerCluster, pa.ReservedSectors,
 
220
                                        pa.RootEntries, pa.SectorsPerFat, pa.VolumeLabel, pa.SystemID,
 
221
                                        pa.WriteProtect );
 
222
}
 
223
 
 
224
PyObject * pcardext_read( PyObject * self, PyObject * args ) 
 
225
{
 
226
    char * name;
 
227
    int offset = 0;
 
228
    Py_ssize_t len = 0;
 
229
    void * buffer;
 
230
    
 
231
    if( !PyArg_ParseTuple( args, "sii", &name, &offset, &len ) )
 
232
    {
 
233
        return Py_BuildValue( "s", "" );
 
234
    }
 
235
    
 
236
    buffer = alloca( len );
 
237
    
 
238
    if( FatReadFileExt( name, offset, len, buffer ) == len )
 
239
    {
 
240
        // return PyString_FromStringAndSize( (char *)buffer, len );
 
241
        return PY_String_Bytes( (char *)buffer, len );
 
242
        // return PyBytes_FromStringAndSize( (char *)buffer, len );
 
243
    }
 
244
    else
 
245
    {
 
246
        return Py_BuildValue( "s", "" );
 
247
    }
 
248
    
 
249
    
 
250
}
 
251
 
 
252
 
 
253
static PyMethodDef pcardext_methods[] = 
 
254
{
 
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 },
 
264
    { NULL, NULL }
 
265
};  
 
266
 
 
267
 
 
268
static char pcardext_documentation[] = "Python extension for HP photocard services";
 
269
 
 
270
MOD_INIT(pcardext)  {
 
271
 
 
272
  PyObject* mod ;
 
273
  MOD_DEF(mod, "pcardext", pcardext_documentation, pcardext_methods);
 
274
  if (mod == NULL)
 
275
    return MOD_ERROR_VAL;
 
276
 
 
277
  return MOD_SUCCESS_VAL(mod);
 
278
 
 
279
}
 
280
 
 
281