PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/strigi-0.7.7/libstreams/lib/subinputstream.cpp

#
C++ | 125 lines | 94 code | 2 blank | 29 comment | 34 complexity | f4043f57a0b1df8f7181160bc77e98d7 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /* This file is part of Strigi Desktop Search
  2. *
  3. * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include <strigi/subinputstream.h>
  21. #include <strigi/strigiconfig.h>
  22. #include <iostream>
  23. #include <cassert>
  24. using namespace std;
  25. using namespace Strigi;
  26. SubInputStream::SubInputStream(InputStream *i, int64_t length)
  27. : m_offset(i->position()), m_input(i) {
  28. assert(length >= -1);
  29. // fprintf(stderr, "substream m_offset: %li\n", m_offset);
  30. m_size = length;
  31. }
  32. int32_t
  33. SubInputStream::read(const char*& start, int32_t min, int32_t max) {
  34. if (m_size != -1) {
  35. const int64_t left = m_size - m_position;
  36. if (left == 0) {
  37. m_status = Eof;
  38. return -1;
  39. }
  40. // restrict the amount of data that can be read
  41. if (min > left) min = (int32_t)left;
  42. if (max < min || max > left) {
  43. max = (int32_t)left;
  44. }
  45. }
  46. int32_t nread = m_input->read(start, min, max);
  47. assert(max < min || nread <= max);
  48. if (nread < -1) {
  49. fprintf(stderr, "substream too short.\n");
  50. m_status = Error;
  51. m_error = m_input->error();
  52. } else if (nread < min) {
  53. if (m_size == -1) {
  54. m_status = Eof;
  55. if (nread > 0) {
  56. m_position += nread;
  57. m_size = m_position;
  58. }
  59. } else {
  60. // fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! nread %i min %i max %i m_size %li\n", nread, min, max, m_size);
  61. // fprintf(stderr, "pos %li parentpos %li\n", m_position, m_input->position());
  62. // fprintf(stderr, "status: %i error: %s\n", m_input->status(), m_input->error());
  63. // we expected data but didn't get enough so that's an error
  64. m_status = Error;
  65. m_error = "Premature end of stream\n";
  66. nread = -2;
  67. }
  68. } else {
  69. m_position += nread;
  70. if (m_position == m_size) {
  71. m_status = Eof;
  72. }
  73. }
  74. return nread;
  75. }
  76. int64_t
  77. SubInputStream::reset(int64_t newpos) {
  78. assert(newpos >= 0);
  79. // fprintf(stderr, "subreset pos: %li newpos: %li m_offset: %li m_size: %li\n",
  80. // m_position, newpos, m_offset, m_size);
  81. m_position = m_input->reset(newpos + m_offset);
  82. if (m_position < m_offset) {
  83. cerr << "########### m_position " << m_position << " newpos " << newpos
  84. << endl;
  85. m_status = Error;
  86. m_error = m_input->error();
  87. } else {
  88. m_position -= m_offset;
  89. m_status = m_input->status();
  90. }
  91. return m_position;
  92. }
  93. int64_t
  94. SubInputStream::skip(int64_t ntoskip) {
  95. // fprintf(stderr, "subskip pos: %li ntoskip: %li m_offset: %li m_size: %li\n", m_position, ntoskip, m_offset, m_size);
  96. if (m_size == m_position) {
  97. m_status = Eof;
  98. return -1;
  99. }
  100. if (ntoskip == 0) return 0;
  101. if (m_size != -1) {
  102. const int64_t left = m_size - m_position;
  103. // restrict the amount of data that can be skipped
  104. if (ntoskip > left) {
  105. ntoskip = left;
  106. }
  107. }
  108. int64_t skipped = m_input->skip(ntoskip);
  109. if (m_input->status() == Error) {
  110. m_status = Error;
  111. m_error = m_input->error();
  112. } else {
  113. m_position += skipped;
  114. if (m_position == m_size) {
  115. m_status = Eof;
  116. } else if (skipped <= 0) {
  117. m_status = Error;
  118. m_error = "Premature end of stream\n";
  119. skipped = -2;
  120. }
  121. }
  122. return skipped;
  123. }