Salome HOME
Allow saving groups with non-ascii names.
[modules/smesh.git] / src / Driver / Driver_Mesh.cxx
index 024a0b836d4e0bffc94543e0f958f1f77d9ad709..d3fc4d9ebe301a683a1e22985d40606974a9d0b0 100644 (file)
@@ -20,7 +20,7 @@
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
-//  SMESH Driver : implementaion of driver for reading and writing      
+//  SMESH Driver : implementation of driver for reading and writing
 //  File   : Mesh_Reader.cxx
 //  Module : SMESH
 //
@@ -101,3 +101,41 @@ SMESH_ComputeErrorPtr Driver_Mesh::GetError()
   }
   return SMESH_ComputeError::New( myStatus == DRS_OK ? int(COMPERR_OK) : int(myStatus), msg );
 }
+
+//================================================================================
+/*!
+ * \brief Assure a string is UTF-8 valid by replacing invalid chars
+ */
+//================================================================================
+
+std::string Driver_Mesh::fixUTF8(const std::string & str )
+{
+  std::string fixed = str;
+  const unsigned char* s = reinterpret_cast<const unsigned char* >( fixed.data() );
+
+  for ( size_t i = 0; i < fixed.size(); ++i )
+  {
+    if ( s[i] < 128 )
+      continue; // latin
+
+    bool invalid = false;
+
+    // how many bytes follow?
+    int len = 0;
+    if      (s[i] >> 5 == 0b110  ) len = 1;
+    else if (s[i] >> 4 == 0b1110 ) len = 2;
+    else if (s[i] >> 3 == 0b11110) len = 3;
+    else
+      invalid = true;
+
+    // check the bytes
+    for ( int j = 0; j < len && !invalid; ++j )
+      invalid = ( s[i+j+1] >> 6 != 0b10 );
+
+    if ( invalid )
+      fixed[i] = '?';
+    else
+      i += len;
+  }
+  return fixed;
+}