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.

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