]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
ENH: /binary_write/ gdcm source. Now even on big endian we are writting little endian...
[gdcm.git] / src / gdcmUtil.cxx
index d09fe4534e49e0d7e4f9461928f00952e59d3d71..f8dbe313c41eecaebda4f29267566010d037037e 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/10/27 22:58:06 $
-  Version:   $Revision: 1.56 $
+  Date:      $Date: 2004/11/03 20:52:13 $
+  Version:   $Revision: 1.59 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -189,5 +189,47 @@ std::string Util::GetName(std::string const & fullName)
     }
 } 
 
+
+template <class T>
+std::ostream& binary_write(std::ostream& os, const T& val)
+{
+    return os.write(reinterpret_cast<const char*>(&val), sizeof val);
+}
+
+std::ostream& binary_write(std::ostream& os, const uint16_t& val)
+{
+#ifdef GDCM_WORDS_BIGENDIAN
+    uint16_t swap;
+    swap = ((( val << 8 ) & 0x0ff00 ) | (( val >> 8 ) & 0x00ff ) );
+    return os.write(reinterpret_cast<const char*>(&swap), 2);
+#else
+    return os.write(reinterpret_cast<const char*>(&val), 2);
+#endif //GDCM_WORDS_BIGENDIAN
+}
+
+std::ostream& binary_write(std::ostream& os, const uint32_t& val)
+{
+#ifdef GDCM_WORDS_BIGENDIAN
+    uint32_t swap;
+    swap = ( ((val<<24) & 0xff000000) | ((val<<8)  & 0x00ff0000) | 
+             ((val>>8)  & 0x0000ff00) | ((val>>24) & 0x000000ff) );
+    return os.write(reinterpret_cast<const char*>(&swap), 4);
+#else
+    return os.write(reinterpret_cast<const char*>(&val), 4);
+#endif //GDCM_WORDS_BIGENDIAN
+}
+
+//template <>
+std::ostream& binary_write(std::ostream& os, const char* val)
+{
+    return os.write(val, strlen(val));
+}
+
+std::ostream& binary_write(std::ostream& os, std::string const & val)
+{
+    return os.write(val.c_str(), val.size());
+}
+
+
 } // end namespace gdcm