cursorstreamwrapper.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Tencent is pleased to support the open source community by making RapidJSON
  2. // available.
  3. //
  4. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
  5. // rights reserved.
  6. //
  7. // Licensed under the MIT License (the "License"); you may not use this file
  8. // except in compliance with the License. You may obtain a copy of the License
  9. // at
  10. //
  11. // http://opensource.org/licenses/MIT
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. #ifndef RAPIDJSON_CURSORSTREAMWRAPPER_H_
  19. #define RAPIDJSON_CURSORSTREAMWRAPPER_H_
  20. #include "stream.h"
  21. #if defined(__GNUC__)
  22. RAPIDJSON_DIAG_PUSH
  23. RAPIDJSON_DIAG_OFF(effc++)
  24. #endif
  25. #if defined(_MSC_VER) && _MSC_VER <= 1800
  26. RAPIDJSON_DIAG_PUSH
  27. RAPIDJSON_DIAG_OFF(4702) // unreachable code
  28. RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
  29. #endif
  30. RAPIDJSON_NAMESPACE_BEGIN
  31. //! Cursor stream wrapper for counting line and column number if error exists.
  32. /*!
  33. \tparam InputStream Any stream that implements Stream Concept
  34. */
  35. template <typename InputStream, typename Encoding = UTF8<>>
  36. class CursorStreamWrapper : public GenericStreamWrapper<InputStream, Encoding> {
  37. public:
  38. typedef typename Encoding::Ch Ch;
  39. CursorStreamWrapper(InputStream &is)
  40. : GenericStreamWrapper<InputStream, Encoding>(is), line_(1), col_(0) {}
  41. // counting line and column number
  42. Ch Take() {
  43. Ch ch = this->is_.Take();
  44. if (ch == '\n') {
  45. line_++;
  46. col_ = 0;
  47. } else {
  48. col_++;
  49. }
  50. return ch;
  51. }
  52. //! Get the error line number, if error exists.
  53. size_t GetLine() const { return line_; }
  54. //! Get the error column number, if error exists.
  55. size_t GetColumn() const { return col_; }
  56. private:
  57. size_t line_; //!< Current Line
  58. size_t col_; //!< Current Column
  59. };
  60. #if defined(_MSC_VER) && _MSC_VER <= 1800
  61. RAPIDJSON_DIAG_POP
  62. #endif
  63. #if defined(__GNUC__)
  64. RAPIDJSON_DIAG_POP
  65. #endif
  66. RAPIDJSON_NAMESPACE_END
  67. #endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_