]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
ENH: Commiting 1/2 patch from Jean Michel Rouet, for better DICOM writting support
[gdcm.git] / src / gdcmUtil.cxx
index 889ece2064d22206fc35856c25f818fdfc714f56..d0359f8be4ad844395cb613504a8597ebcc34c0b 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/10/28 23:10:25 $
-  Version:   $Revision: 1.58 $
+  Date:      $Date: 2004/11/10 18:27:24 $
+  Version:   $Revision: 1.61 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include "gdcmUtil.h"
 #include "gdcmDebug.h"
 
+// For GetCurrentDate, GetCurrentTime
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #include <stdarg.h>  //only included in implementation file
 #include <stdio.h>   //only included in implementation file
 
@@ -168,7 +173,7 @@ std::string Util::GetPath(std::string const & fullName)
 }
 
 /**
- * \ingroup Globals
+ * \ingroup Util
  * \brief   Get the (last) name of a full path file name
  * @param   fullName file/directory name to extract end name from
  */
@@ -189,6 +194,67 @@ std::string Util::GetName(std::string const & fullName)
     }
 } 
 
+/**
+ * \ingroup Util
+ * \brief Create a /DICOM/ string:
+ * It should a of even lenght (no odd length ever)
+ * It can contains as many \0 as you want.
+ * This function is similar to DicomString(const char*), 
+ * except it doesn't take a lenght. 
+ * It only pad with a null character if length is odd
+ */
+std::string Util::DicomString(const char* s)
+{
+   size_t l = strlen(s);
+   if( l%2 )
+   {
+      l++;
+   }
+   std::string r(s, s+l);
+   assert( !(r.size() % 2) );
+   return r;
+}
+
+/**
+ * \ingroup Util
+ * \brief   Get the current date of the system in a dicom string
+ */
+std::string Util::GetCurrentDate()
+{
+    char tmp[512];
+    time_t tloc;
+    time (&tloc);    
+    strftime(tmp,512,"%Y%m%d", localtime(&tloc) );
+    return tmp;
+}
+
+/**
+ * \ingroup Util
+ * \brief   Get the current time of the system in a dicom string
+ */
+std::string Util::GetCurrentTime()
+{
+    char tmp[512];
+    time_t tloc;
+    time (&tloc);
+    strftime(tmp,512,"%H%M%S", localtime(&tloc) );
+    return tmp;  
+}
+
+
+
+/**
+ * \ingroup Util
+ * \brief Create a /DICOM/ string:
+ * It should a of even length (no odd length ever)
+ * It can contains as many \0 as you want.
+ */
+std::string Util::DicomString(const char* s, size_t l)
+{
+   std::string r(s, s+l);
+   assert( !(r.size() % 2) );
+   return r;
+}
 
 template <class T>
 std::ostream& binary_write(std::ostream& os, const T& val)
@@ -225,6 +291,11 @@ 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