1 /*
2  *
3  *  Copyright (C) 1998-2010, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module: dcmpstat
15  *
16  *  Author: Marco Eichelberg
17  *
18  *  Purpose:
19  *    classes: DVPSGraphicObject_PList
20  *
21  */
22 
23 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
24 #include "dcmtk/dcmpstat/dvpsgrl.h"
25 #include "dcmtk/dcmpstat/dvpsgr.h"      /* for DVPSGraphicObject */
26 
27 
DVPSGraphicObject_PList()28 DVPSGraphicObject_PList::DVPSGraphicObject_PList()
29 : list_()
30 {
31 }
32 
DVPSGraphicObject_PList(const DVPSGraphicObject_PList & arg)33 DVPSGraphicObject_PList::DVPSGraphicObject_PList(const DVPSGraphicObject_PList &arg)
34 : list_()
35 {
36   OFListConstIterator(DVPSGraphicObject *) first = arg.list_.begin();
37   OFListConstIterator(DVPSGraphicObject *) last = arg.list_.end();
38   while (first != last)
39   {
40     list_.push_back((*first)->clone());
41     ++first;
42   }
43 }
44 
~DVPSGraphicObject_PList()45 DVPSGraphicObject_PList::~DVPSGraphicObject_PList()
46 {
47   clear();
48 }
49 
clear()50 void DVPSGraphicObject_PList::clear()
51 {
52   OFListIterator(DVPSGraphicObject *) first = list_.begin();
53   OFListIterator(DVPSGraphicObject *) last = list_.end();
54   while (first != last)
55   {
56     delete (*first);
57     first = list_.erase(first);
58   }
59 }
60 
read(DcmItem & dset)61 OFCondition DVPSGraphicObject_PList::read(DcmItem &dset)
62 {
63   OFCondition result = EC_Normal;
64   DcmStack stack;
65   DVPSGraphicObject *newObject = NULL;
66   DcmSequenceOfItems *dseq=NULL;
67   DcmItem *ditem=NULL;
68 
69   if (EC_Normal == dset.search(DCM_GraphicObjectSequence, stack, ESM_fromHere, OFFalse))
70   {
71     dseq=(DcmSequenceOfItems *)stack.top();
72     if (dseq)
73     {
74       unsigned long numItems = dseq->card();
75       for (unsigned int i=0; i<numItems; i++)
76       {
77         ditem = dseq->getItem(i);
78         newObject = new DVPSGraphicObject();
79         if (newObject && ditem)
80         {
81           result = newObject->read(*ditem);
82           list_.push_back(newObject);
83         } else result = EC_MemoryExhausted;
84       }
85     }
86   }
87 
88   return result;
89 }
90 
write(DcmItem & dset)91 OFCondition DVPSGraphicObject_PList::write(DcmItem &dset)
92 {
93   if (size()==0) return EC_Normal; // don't write empty Sequence
94 
95   OFCondition result = EC_Normal;
96   DcmSequenceOfItems *dseq=NULL;
97   DcmItem *ditem=NULL;
98 
99   dseq = new DcmSequenceOfItems(DCM_GraphicObjectSequence);
100   if (dseq)
101   {
102     OFListIterator(DVPSGraphicObject *) first = list_.begin();
103     OFListIterator(DVPSGraphicObject *) last = list_.end();
104     while (first != last)
105     {
106       if (result==EC_Normal)
107       {
108         ditem = new DcmItem();
109         if (ditem)
110         {
111           result = (*first)->write(*ditem);
112           if (result==EC_Normal) dseq->insert(ditem); else delete ditem;
113         } else result = EC_MemoryExhausted;
114       }
115       ++first;
116     }
117     if (result==EC_Normal) dset.insert(dseq, OFTrue /*replaceOld*/); else delete dseq;
118   } else result = EC_MemoryExhausted;
119   return result;
120 }
121 
122 
getGraphicObject(size_t idx)123 DVPSGraphicObject *DVPSGraphicObject_PList::getGraphicObject(size_t idx)
124 {
125   OFListIterator(DVPSGraphicObject *) first = list_.begin();
126   OFListIterator(DVPSGraphicObject *) last = list_.end();
127   while (first != last)
128   {
129     if (idx==0) return *first;
130     idx--;
131     ++first;
132   }
133   return NULL;
134 }
135 
addGraphicObject(DVPSGraphicObject * graphic)136 void DVPSGraphicObject_PList::addGraphicObject(DVPSGraphicObject *graphic)
137 {
138   if (graphic) list_.push_back(graphic);
139 }
140 
removeGraphicObject(size_t idx)141 DVPSGraphicObject *DVPSGraphicObject_PList::removeGraphicObject(size_t idx)
142 {
143   OFListIterator(DVPSGraphicObject *) first = list_.begin();
144   OFListIterator(DVPSGraphicObject *) last = list_.end();
145   while (first != last)
146   {
147     if (idx==0)
148     {
149       DVPSGraphicObject *result = *first;
150       list_.erase(first);
151       return result;
152     }
153     idx--;
154     ++first;
155   }
156   return NULL;
157 }
158