Browse Source

cmGeneratedFileStream: Add support for a temporary alternate encoding

The Ninja generator needs to be able to write some file content in a
different encoding than the rest of the file.  Add a method to do this.
Brad King 3 years ago
parent
commit
328c15189d
2 changed files with 20 additions and 0 deletions
  1. 14 0
      Source/cmGeneratedFileStream.cxx
  2. 6 0
      Source/cmGeneratedFileStream.h

+ 14 - 0
Source/cmGeneratedFileStream.cxx

@@ -239,6 +239,20 @@ void cmGeneratedFileStream::SetTempExt(std::string const& ext)
   this->TempExt = ext;
 }
 
+void cmGeneratedFileStream::WriteAltEncoding(std::string const& data,
+                                             Encoding encoding)
+{
+#ifndef CMAKE_BOOTSTRAP
+  std::locale prevLocale =
+    this->imbue(std::locale(this->getloc(), new codecvt(encoding)));
+  this->write(data.data(), data.size());
+  this->imbue(prevLocale);
+#else
+  static_cast<void>(encoding);
+  this->write(data.data(), data.size());
+#endif
+}
+
 void cmGeneratedFileStream::WriteRaw(std::string const& data)
 {
 #ifndef CMAKE_BOOTSTRAP

+ 6 - 0
Source/cmGeneratedFileStream.h

@@ -147,6 +147,12 @@ public:
    */
   void SetTempExt(std::string const& ext);
 
+  /**
+   * Write a specific string using an alternate encoding.
+   * Afterward, the original encoding is restored.
+   */
+  void WriteAltEncoding(std::string const& data, Encoding encoding);
+
   /**
    * Writes the given string directly to the file without changing the
    * encoding.