Browse Source

cmGeneratedFileStream: Add optional encoding support

This allows to save file stream in different encoding than internal
encoding.
Dāvis Mosāns 9 years ago
parent
commit
f00214aa4f
2 changed files with 22 additions and 4 deletions
  1. 17 2
      Source/cmGeneratedFileStream.cxx
  2. 5 2
      Source/cmGeneratedFileStream.h

+ 17 - 2
Source/cmGeneratedFileStream.cxx

@@ -10,13 +10,21 @@
 #include <cm_zlib.h>
 #include <cm_zlib.h>
 #endif
 #endif
 
 
-cmGeneratedFileStream::cmGeneratedFileStream()
+cmGeneratedFileStream::cmGeneratedFileStream(Encoding encoding)
   : cmGeneratedFileStreamBase()
   : cmGeneratedFileStreamBase()
   , Stream()
   , Stream()
 {
 {
+#ifdef CMAKE_BUILD_WITH_CMAKE
+  if (encoding != codecvt::None) {
+    imbue(std::locale(getloc(), new codecvt(encoding)));
+  }
+#else
+  static_cast<void>(encoding);
+#endif
 }
 }
 
 
-cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet)
+cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet,
+                                             Encoding encoding)
   : cmGeneratedFileStreamBase(name)
   : cmGeneratedFileStreamBase(name)
   , Stream(TempName.c_str())
   , Stream(TempName.c_str())
 {
 {
@@ -26,6 +34,13 @@ cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet)
                          this->TempName.c_str());
                          this->TempName.c_str());
     cmSystemTools::ReportLastSystemError("");
     cmSystemTools::ReportLastSystemError("");
   }
   }
+#ifdef CMAKE_BUILD_WITH_CMAKE
+  if (encoding != codecvt::None) {
+    imbue(std::locale(getloc(), new codecvt(encoding)));
+  }
+#else
+  static_cast<void>(encoding);
+#endif
 }
 }
 
 
 cmGeneratedFileStream::~cmGeneratedFileStream()
 cmGeneratedFileStream::~cmGeneratedFileStream()

+ 5 - 2
Source/cmGeneratedFileStream.h

@@ -5,6 +5,7 @@
 
 
 #include <cmConfigure.h>
 #include <cmConfigure.h>
 
 
+#include <cm_codecvt.hxx>
 #include <cmsys/FStream.hxx>
 #include <cmsys/FStream.hxx>
 #include <string>
 #include <string>
 
 
@@ -71,12 +72,13 @@ class cmGeneratedFileStream : private cmGeneratedFileStreamBase,
 {
 {
 public:
 public:
   typedef cmsys::ofstream Stream;
   typedef cmsys::ofstream Stream;
+  typedef codecvt::Encoding Encoding;
 
 
   /**
   /**
    * This constructor prepares a default stream.  The open method must
    * This constructor prepares a default stream.  The open method must
    * be used before writing to the stream.
    * be used before writing to the stream.
    */
    */
-  cmGeneratedFileStream();
+  cmGeneratedFileStream(Encoding encoding = codecvt::None);
 
 
   /**
   /**
    * This constructor takes the name of the file to be generated.  It
    * This constructor takes the name of the file to be generated.  It
@@ -84,7 +86,8 @@ public:
    * file cannot be opened an error message is produced unless the
    * file cannot be opened an error message is produced unless the
    * second argument is set to true.
    * second argument is set to true.
    */
    */
-  cmGeneratedFileStream(const char* name, bool quiet = false);
+  cmGeneratedFileStream(const char* name, bool quiet = false,
+                        Encoding encoding = codecvt::None);
 
 
   /**
   /**
    * The destructor checks the stream status to be sure the temporary
    * The destructor checks the stream status to be sure the temporary