# HG changeset patch
# User Matthew Clemence <matthew.clemence@philips.com>
# Date 1471535614 -3600
#      Thu Aug 18 16:53:34 2016 +0100
# Node ID 07cf56b6a5e95b558ee19011a02cb7ad5a2bcf84
# Parent  7b73c5d20252cd79715d7f49dc9f4d40d6b2718a
Bug - item values not appearing as separet entries (requires cast of to octave value)
Bug - multiframe dicom with sequences of undefined length

diff -r 7b73c5d20252 -r 07cf56b6a5e9 src/dicominfo.cpp
--- a/src/dicominfo.cpp	Tue Aug 09 20:53:37 2016 +0100
+++ b/src/dicominfo.cpp	Thu Aug 18 16:53:34 2016 +0100
@@ -117,7 +117,7 @@
 		") {
 	octave_value_list retval;  // create object to store return values
 	if ( 0 == args.length()) {
-		error(QUOTED(OCT_FN_NAME)": one arg required: dicom filename");
+		error(QUOTED(OCT_FNitem_no_NAME)": one arg required: dicom filename");
 		return retval; 
 	}
 	int chatty = !nargout; // dump output to stdout if not assigning to var
@@ -450,27 +450,85 @@
 }
 
 void dumpSequence(octave_value *ov, gdcm::SequenceOfItems *seq, int chatty, int sequenceDepth) {
-	const octave_idx_type nDataSet=seq->GetNumberOfItems(); // objects in sequence
-	if (chatty) octave_stdout << nDataSet << " object" << ((nDataSet==1)?"":"s") << std::endl;
-	char item_name_buf[16];
-	octave_map om;
-	for (octave_idx_type j=1; j<=nDataSet; j++ ) {
-		const gdcm::DataSet::DataElementSet des=seq->GetItem(j).GetNestedDataSet().GetDES() ;
-		octave_map subom;
-		for (gdcm::DataSet::Iterator it=des.begin(); it != des.end(); it++) {
-			std::string key("");
-			octave_value subov;
-			if( DICOM_OK==element2value(key, &subov, &(*it), chatty, sequenceDepth)) {
-				subom.assign(key.c_str(), subov);
-			} else {
-				if (0==key.length()) continue ;
-				subom.assign(key.c_str(), octave_value("not assigned"));
+	// MXC - there are two types of sequences - defined and undefined
+	// it appears that a squence of undefined length returns 1 object - need
+	// additional checking
+	if (seq->GetLength() == 0xffffffff) {
+
+		octave_map om;
+		// Need to deal with a sequence of undefined length
+		if (chatty) octave_stdout << " MXC Sequence of undefined length" << std::endl;
+		// In which case we keep track of our own numbers for the item numbering
+		// and iterate over the sequence
+		octave_idx_type item_no = 0;
+		// MXC ? gdcm bug for getting the lengths of ITEMS and DATAELEMENTS - template failures
+		// so can't work out how to check for undefined length - for now iterate
+		// over the nested data set
+		for (gdcm::SequenceOfItems::Iterator it=seq->Begin(); it != seq->End(); it ++) {
+			std::ostringstream buff;
+			item_no++;
+			octave_map subom;
+
+			gdcm::Item mi = *it;
+			// MXC - this should be the item tag fffe,e000 and max havea  defined length or
+			// undefined length - for now going to assume we can iterate over both
+			if (chatty) octave_stdout << "MXC got item " << item_no << " " << mi.GetTag() << std::endl;
+
+			gdcm::DataSet ds = it->GetNestedDataSet();
+
+			for (gdcm::DataSet::Iterator dsit=ds.Begin(); dsit != ds.End(); dsit++) {
+				std::string key("");
+				octave_value subov;
+				if( DICOM_OK==element2value(key, &subov, &(*dsit), chatty, sequenceDepth)) {
+					subom.assign(key.c_str(), subov);
+				} else {
+					if (0==key.length()) continue ;
+					subom.assign(key.c_str(), octave_value("not assigned"));
+				}
+				if (chatty) octave_stdout << "MXC sub map" << subom.nfields()<< std::endl;
+
 			}
+
+			buff << "Item_" << item_no;
+			if (chatty) octave_stdout << "MXC storing " << buff.str() << std::endl;
+			// MXC octave odd behaviour  - when the assign is a map, it ads the map items
+			// not a single item of type sub map
+
+			if (chatty) octave_stdout << "MXC before insert" << om.nfields()<< std::endl;
+			om.assign(buff.str().c_str() , octave_value(subom));
+			if (chatty) octave_stdout << "MXC " << om.nfields()<< std::endl;
+
 		}
-		snprintf(item_name_buf,15,"Item_%i",j);
-		om.assign(item_name_buf, subom);
+		*ov=om;
+	} else {
+		// proceed as before for defined sequences
+
+		const octave_idx_type nDataSet=seq->GetNumberOfItems(); // objects in sequence
+		if (chatty) octave_stdout << nDataSet << " object" << ((nDataSet==1)?"":"s") << std::endl;
+		std::ostringstream buff;
+		octave_map om;
+		for (octave_idx_type j=1; j<=nDataSet; j++ ) {
+			const gdcm::DataSet::DataElementSet des=seq->GetItem(j).GetNestedDataSet().GetDES() ;
+			octave_map subom;
+			for (gdcm::DataSet::Iterator it=des.begin(); it != des.end(); it++) {
+				std::string key("");
+				octave_value subov;
+				if( DICOM_OK==element2value(key, &subov, &(*it), chatty, sequenceDepth)) {
+					subom.assign(key.c_str(), subov);
+				} else {
+					if (0==key.length()) continue ;
+					subom.assign(key.c_str(), octave_value("not assigned"));
+				}
+			}
+			// MXC get odd compile wrning
+			//dicominfo.cpp:516:34: warning: the address of ‘item_name_buf’ will always evaluate as ‘true’ [-Waddress]
+			//    om.assign(item_name_buf, subom);
+
+			buff << "Item_" << j;
+			om.assign(buff.str().c_str() , octave_value(subom));
+		}
+		*ov=om;
 	}
-	*ov=om;
 }
 
 void getFileModTime(char *timeStr, const char *filename) {
