cursorstreamwrapper.h 2.2 KB

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