You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
  5. * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * @file streamwrapper.h
  22. */
  23. #ifndef STREAMWRAPPER_H
  24. #define STREAMWRAPPER_H
  25. #include <iostream>
  26. #if defined( _WIN32 ) && defined( __GNUC__ )
  27. #include <ext/stdio_filebuf.h>
  28. #define OSTREAM std::ostream
  29. #define OPEN_OSTREAM( var, name ) \
  30. kicad::stream var##_BUF_; \
  31. std::ostream& var = *var##_BUF_.Open( name, std::ios_base::out | std::ios_base::trunc \
  32. | std::ios_base::binary )
  33. #define OPEN_ISTREAM( var, name ) \
  34. kicad::stream var##_BUF_; \
  35. std::istream& var = *var##_BUF_.Open( name, std::ios_base::in | std::ios_base::binary )
  36. #define OPEN_IOSTREAM( var, name ) \
  37. kicad::stream var##_BUF_; \
  38. std::iostream& var = *var##_BUF_.Open( name, std::ios_base::out | std::ios_base::in \
  39. | std::ios_base::binary )
  40. #define CLOSE_STREAM( var ) var##_BUF_.Close()
  41. /**
  42. * \namespace kicad
  43. */
  44. namespace kicad
  45. {
  46. /**
  47. * This is equivalent to std::stream but accepts UTF8 chars in filenames.
  48. */
  49. class stream
  50. {
  51. private:
  52. __gnu_cxx::stdio_filebuf<char>* m_buf;
  53. std::iostream* m_stream;
  54. public:
  55. stream();
  56. virtual ~stream();
  57. std::iostream* Open( const char* aFileName, std::ios_base::openmode aMode );
  58. void Close( void );
  59. std::iostream* GetStream( void );
  60. };
  61. } // namespace kicad
  62. #elif defined( _MSC_VER ) // defined( _WIN32 ) && defined( __GNUC__ )
  63. #define OSTREAM std::ofstream
  64. #define OPEN_OSTREAM( var, name ) \
  65. std::ofstream var; \
  66. var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
  67. std::ios_base::out | std::ios_base::trunc | std::ios_base::binary )
  68. #define OPEN_ISTREAM( var, name ) \
  69. std::ifstream var; \
  70. var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
  71. std::ios_base::in | std::ios_base::binary )
  72. #define OPEN_IOSTREAM( var, name ) \
  73. std::fstream var; \
  74. var.open( wxString::FromUTF8Unchecked( name ).wc_str(), \
  75. std::ios_base::out | std::ios_base::in | std::ios_base::binary )
  76. #define CLOSE_STREAM( var ) var.close()
  77. #else // defined( _WIN32 ) && defined( __GNUC__ )
  78. #define OSTREAM std::ofstream
  79. #define OPEN_OSTREAM( var, name ) \
  80. std::ofstream var; \
  81. var.open( name, std::ios_base::out | std::ios_base::trunc )
  82. #define OPEN_ISTREAM( var, name ) \
  83. std::ifstream var; \
  84. var.open( name, std::ios_base::in )
  85. #define OPEN_IOSTREAM( var, name ) \
  86. std::fstream var; \
  87. var.open( name, std::ios_base::out | std::ios_base::in )
  88. #define CLOSE_STREAM( var ) var.close()
  89. #endif // defined( _WIN32 ) && defined( __GNUC__ )
  90. #endif // STREAMWRAPPER_H