InputFile.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ----------------------------------------------------------------- */
  2. /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
  3. /* developed by Sinsy Working Group */
  4. /* http://sinsy.sourceforge.net/ */
  5. /* ----------------------------------------------------------------- */
  6. /* */
  7. /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
  8. /* Department of Computer Science */
  9. /* */
  10. /* All rights reserved. */
  11. /* */
  12. /* Redistribution and use in source and binary forms, with or */
  13. /* without modification, are permitted provided that the following */
  14. /* conditions are met: */
  15. /* */
  16. /* - Redistributions of source code must retain the above copyright */
  17. /* notice, this list of conditions and the following disclaimer. */
  18. /* - Redistributions in binary form must reproduce the above */
  19. /* copyright notice, this list of conditions and the following */
  20. /* disclaimer in the documentation and/or other materials provided */
  21. /* with the distribution. */
  22. /* - Neither the name of the Sinsy working group nor the names of */
  23. /* its contributors may be used to endorse or promote products */
  24. /* derived from this software without specific prior written */
  25. /* permission. */
  26. /* */
  27. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
  28. /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  29. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  30. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  31. /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
  32. /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  33. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
  34. /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  35. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
  36. /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
  37. /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
  38. /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  39. /* POSSIBILITY OF SUCH DAMAGE. */
  40. /* ----------------------------------------------------------------- */
  41. #include "InputFile.h"
  42. #include "util_log.h"
  43. namespace sinsy
  44. {
  45. /*!
  46. constructor
  47. */
  48. InputFile::InputFile()
  49. {
  50. }
  51. /*!
  52. constructor
  53. */
  54. InputFile::InputFile(const std::string& fpath) : stream(fpath.c_str())
  55. {
  56. }
  57. /*!
  58. destructor
  59. */
  60. InputFile::~InputFile()
  61. {
  62. stream.close();
  63. }
  64. /*!
  65. read data from stream
  66. @param buffer buffer for read data
  67. @param byte byte you want to read
  68. @return read bytes (0 : end of stream)
  69. */
  70. size_t InputFile::read(void* buffer, size_t size) throw (StreamException)
  71. {
  72. if(stream.fail()) {
  73. throw StreamException("InputFile::read()");
  74. }
  75. if (stream.eof()) {
  76. return 0;
  77. }
  78. stream.read(static_cast<char*>(buffer), size);
  79. int ret = stream.gcount();
  80. if (ret <= 0) {
  81. ERR_MSG("File reading error (size: " << size << ") result of gcount = " << ret);
  82. throw StreamException("InputFile::read()");
  83. }
  84. return ret;
  85. }
  86. /*!
  87. open
  88. */
  89. void InputFile::open(const std::string& fpath)
  90. {
  91. stream.open(fpath.c_str());
  92. }
  93. /*!
  94. close
  95. */
  96. void InputFile::close()
  97. {
  98. stream.close();
  99. }
  100. /*!
  101. stream is valid or not
  102. */
  103. bool InputFile::isValid() const
  104. {
  105. return !stream.fail();
  106. }
  107. /*!
  108. read a line
  109. */
  110. bool InputFile::readLine(std::string& line)
  111. {
  112. if (!isValid() || isEof()) {
  113. return false;
  114. }
  115. getline(stream, line);
  116. return true;
  117. }
  118. }; // namespace sinsy