mirror of
https://github.com/cemu-project/idapython.git
synced 2024-11-25 02:26:55 +01:00
325 lines
7.9 KiB
OpenEdge ABL
325 lines
7.9 KiB
OpenEdge ABL
%inline %{
|
|
//<inline(py_qfile)>
|
|
/*
|
|
#<pydoc>
|
|
class qfile_t(pyidc_opaque_object_t):
|
|
"""A helper class to work with FILE related functions."""
|
|
def __init__(self):
|
|
pass
|
|
|
|
def close(self):
|
|
"""Closes the file"""
|
|
pass
|
|
|
|
def open(self, filename, mode):
|
|
"""Opens a file
|
|
@param filename: the file name
|
|
@param mode: The mode string, ala fopen() style
|
|
@return: Boolean
|
|
"""
|
|
pass
|
|
|
|
def set_linput(self, linput):
|
|
"""Links the current loader_input_t instance to a linput_t instance"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def tmpfile():
|
|
"""A static method to construct an instance using a temporary file"""
|
|
pass
|
|
|
|
def seek(self, pos, whence = SEEK_SET):
|
|
"""Set input source position
|
|
@return: the new position (not 0 as fseek!)
|
|
"""
|
|
pass
|
|
|
|
def tell(self):
|
|
"""Returns the current position"""
|
|
pass
|
|
|
|
def gets(self, len):
|
|
"""Reads a line from the input file. Returns the read line or None"""
|
|
pass
|
|
|
|
def read(self, size):
|
|
"""Reads from the file. Returns the buffer or None"""
|
|
pass
|
|
|
|
def write(self, buf):
|
|
"""Writes to the file. Returns 0 or the number of bytes written"""
|
|
pass
|
|
|
|
def readbytes(self, size, big_endian):
|
|
"""Similar to read() but it respect the endianness"""
|
|
pass
|
|
|
|
def writebytes(self, size, big_endian):
|
|
"""Similar to write() but it respect the endianness"""
|
|
pass
|
|
|
|
def flush(self):
|
|
pass
|
|
|
|
def get_char(self):
|
|
"""Reads a single character from the file. Returns None if EOF or the read character"""
|
|
pass
|
|
|
|
def put_char(self):
|
|
"""Writes a single character to the file"""
|
|
pass
|
|
|
|
def opened(self):
|
|
"""Checks if the file is opened or not"""
|
|
pass
|
|
#</pydoc>
|
|
*/
|
|
class qfile_t
|
|
{
|
|
private:
|
|
FILE *fp;
|
|
bool own;
|
|
qstring fn;
|
|
|
|
//--------------------------------------------------------------------------
|
|
void assign(const qfile_t &rhs)
|
|
{
|
|
fn = rhs.fn;
|
|
fp = rhs.fp;
|
|
own = false;
|
|
}
|
|
//--------------------------------------------------------------------------
|
|
bool _from_fp(FILE *fp)
|
|
{
|
|
if ( fp == NULL )
|
|
return false;
|
|
own = false;
|
|
fn.sprnt("<FILE * %p>", fp);
|
|
this->fp = fp;
|
|
return true;
|
|
}
|
|
inline void _from_cobject(PyObject *pycobject)
|
|
{
|
|
_from_fp((FILE *)PyCObject_AsVoidPtr(pycobject));
|
|
}
|
|
public:
|
|
int __idc_cvt_id__;
|
|
//--------------------------------------------------------------------------
|
|
qfile_t(const qfile_t &rhs)
|
|
{
|
|
assign(rhs);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
qfile_t(PyObject *pycobject = NULL)
|
|
{
|
|
fp = NULL;
|
|
own = true;
|
|
fn.qclear();
|
|
__idc_cvt_id__ = PY_ICID_OPAQUE;
|
|
if ( pycobject != NULL && PyCObject_Check(pycobject) )
|
|
_from_cobject(pycobject);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
bool opened()
|
|
{
|
|
return fp != NULL;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
void close()
|
|
{
|
|
if ( fp == NULL )
|
|
return;
|
|
if ( own )
|
|
qfclose(fp);
|
|
fp = NULL;
|
|
own = true;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
~qfile_t()
|
|
{
|
|
close();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
bool open(const char *filename, const char *mode)
|
|
{
|
|
close();
|
|
fp = qfopen(filename, mode);
|
|
if ( fp == NULL )
|
|
return false;
|
|
// Save file name
|
|
fn = filename;
|
|
own = true;
|
|
return true;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
static qfile_t *from_fp(FILE *fp)
|
|
{
|
|
if ( fp == NULL )
|
|
return NULL;
|
|
qfile_t *qf = new qfile_t();
|
|
qf->own = false;
|
|
qf->fn.sprnt("<FILE * %p>", fp);
|
|
qf->fp = fp;
|
|
return qf;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
// This method can be used to pass a FILE* from C code
|
|
static qfile_t *from_cobject(PyObject *pycobject)
|
|
{
|
|
return PyCObject_Check(pycobject) ? from_fp((FILE *)PyCObject_AsVoidPtr(pycobject)) : NULL;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
static qfile_t *tmpfile()
|
|
{
|
|
return from_fp(qtmpfile());
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
FILE *get_fp()
|
|
{
|
|
return fp;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int seek(int32 offset, int whence = SEEK_SET)
|
|
{
|
|
return qfseek(fp, offset, whence);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int32 tell()
|
|
{
|
|
return qftell(fp);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
PyObject *readbytes(int size, bool big_endian)
|
|
{
|
|
do
|
|
{
|
|
char *buf = (char *) malloc(size + 5);
|
|
if ( buf == NULL )
|
|
break;
|
|
int r = freadbytes(fp, buf, size, big_endian);
|
|
if ( r != 0 )
|
|
{
|
|
free(buf);
|
|
break;
|
|
}
|
|
PyObject *ret = PyString_FromStringAndSize(buf, r);
|
|
free(buf);
|
|
return ret;
|
|
} while ( false );
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
PyObject *read(int size)
|
|
{
|
|
do
|
|
{
|
|
char *buf = (char *) malloc(size + 5);
|
|
if ( buf == NULL )
|
|
break;
|
|
int r = qfread(fp, buf, size);
|
|
if ( r <= 0 )
|
|
{
|
|
free(buf);
|
|
break;
|
|
}
|
|
PyObject *ret = PyString_FromStringAndSize(buf, r);
|
|
free(buf);
|
|
return ret;
|
|
} while ( false );
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
PyObject *gets(int size)
|
|
{
|
|
do
|
|
{
|
|
char *buf = (char *) malloc(size + 5);
|
|
if ( buf == NULL )
|
|
break;
|
|
if ( qfgets(buf, size, fp) == NULL )
|
|
{
|
|
free(buf);
|
|
break;
|
|
}
|
|
PyObject *ret = PyString_FromString(buf);
|
|
free(buf);
|
|
return ret;
|
|
} while ( false );
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int writebytes(PyObject *py_buf, bool big_endian)
|
|
{
|
|
int sz = PyString_GET_SIZE(py_buf);
|
|
void *buf = (void *)PyString_AS_STRING(py_buf);
|
|
return fwritebytes(fp, buf, sz, big_endian);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int write(PyObject *py_buf)
|
|
{
|
|
if ( !PyString_Check(py_buf) )
|
|
return 0;
|
|
return qfwrite(fp, (void *)PyString_AS_STRING(py_buf), PyString_GET_SIZE(py_buf));
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int puts(const char *str)
|
|
{
|
|
return qfputs(str, fp);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int32 size()
|
|
{
|
|
int pos = qfseek(fp, 0, SEEK_END);
|
|
int32 r = qftell(fp);
|
|
qfseek(fp, pos, SEEK_SET);
|
|
return r;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int flush()
|
|
{
|
|
return qflush(fp);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
PyObject *filename()
|
|
{
|
|
return PyString_FromString(fn.c_str());
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
PyObject *get_char()
|
|
{
|
|
int ch = qfgetc(fp);
|
|
if ( ch == EOF )
|
|
Py_RETURN_NONE;
|
|
return Py_BuildValue("c", ch);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
int put_char(char chr)
|
|
{
|
|
return qfputc(chr, fp);
|
|
}
|
|
};
|
|
//</inline(py_qfile)>
|
|
%}
|