pylzma_filestreams.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Python Bindings for LZMA
  3. *
  4. * Copyright (c) 2004-2006 by Joachim Bauch, mail@joachim-bauch.de
  5. * 7-Zip Copyright (C) 1999-2005 Igor Pavlov
  6. * LZMA SDK Copyright (C) 1999-2005 Igor Pavlov
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * $Id: pylzma_filestreams.h 104 2006-01-08 18:17:14Z jojo $
  23. *
  24. */
  25. #ifndef ___PYLZMA_FILESTREAMS__H___
  26. #define ___PYLZMA_FILESTREAMS__H___
  27. #include <Python.h>
  28. #include <7zip/7zip/IStream.h>
  29. #include <7zip/Common/MyCom.h>
  30. class CFileInStream :
  31. public IInStream,
  32. public CMyUnknownImp
  33. {
  34. private:
  35. PyObject *m_file;
  36. public:
  37. MY_UNKNOWN_IMP
  38. CFileInStream(PyObject *file)
  39. {
  40. m_file = file;
  41. Py_XINCREF(m_file);
  42. }
  43. virtual ~CFileInStream()
  44. {
  45. Py_XDECREF(m_file);
  46. }
  47. STDMETHOD(Read)(void *data, UINT32 size, UINT32 *processedSize)
  48. {
  49. return ReadPart(data, size, processedSize);
  50. }
  51. STDMETHOD(ReadPart)(void *data, UINT32 size, UINT32 *processedSize)
  52. {
  53. // read from file-like object
  54. PyObject *result = PyObject_CallMethod(m_file, "read", "l", size);
  55. if (result == NULL)
  56. return E_FAIL;
  57. if (!PyString_Check(result))
  58. {
  59. PyObject *str = PyObject_Str(result);
  60. Py_XDECREF(result);
  61. if (str == NULL)
  62. return E_FAIL;
  63. result = str;
  64. }
  65. memcpy(data, PyString_AS_STRING(result), PyString_Size(result));
  66. if (processedSize)
  67. *processedSize = PyString_Size(result);
  68. Py_XDECREF(result);
  69. return S_OK;
  70. }
  71. STDMETHOD(Seek)(INT64 offset, UINT32 seekOrigin, UINT64 *newPosition)
  72. {
  73. PyObject *result = PyObject_CallMethod(m_file, "seek", "Ki", offset, seekOrigin);
  74. if (result == NULL)
  75. return E_FAIL;
  76. Py_XDECREF(result);
  77. if (newPosition)
  78. {
  79. result = PyObject_CallMethod(m_file, "tell", "");
  80. if (result == NULL)
  81. return E_FAIL;
  82. *newPosition = PyLong_AsLongLong(result);
  83. Py_XDECREF(result);
  84. }
  85. return S_OK;
  86. }
  87. };
  88. #endif