fw4spl
helper/DicomCodedAttribute.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGdcmIO/helper/DicomCodedAttribute.hpp"
8 
9 #include <regex>
10 
11 namespace fwGdcmIO
12 {
13 namespace helper
14 {
15 
16 //------------------------------------------------------------------------------
17 
20 {
22 
23  if(!entry.empty())
24  {
25  const std::regex codeRegex("\\(([^;]+);([^;]+);([^;)]+)\\)");
26  std::smatch match;
27 
28  auto textIt = entry.begin();
29 
30  // Loop through matches
31  // - match[0] = Single entry '(AAA;BBB;CCC)'
32  // - match[1] = Code Value 'AAA'
33  // - match[2] = Coding Scheme Designator 'BBB'
34  // - match[3] = Code Meaning 'CCC'
35  while(std::regex_search(textIt, entry.end(), match, codeRegex))
36  {
37  result.push_back(::fwGdcmIO::container::DicomCodedAttribute(match[1], match[2], match[3]));
38  textIt = match[0].second;
39  }
40  }
41 
42  return result;
43 }
44 
45 //------------------------------------------------------------------------------
46 
47 ::gdcm::Segment::BasicCodedEntryVector DicomCodedAttribute::convertEntryToGDCMCodedAttribute(const std::string& entry)
48 {
49  ::gdcm::Segment::BasicCodedEntryVector result;
50 
52  for(const auto& attribute : codedAttributes)
53  {
54  result.push_back(attribute.toGDCMFormat());
55  }
56 
57  return result;
58 }
59 
60 //------------------------------------------------------------------------------
61 
62 bool DicomCodedAttribute::checkAndFormatEntry(std::string& entry, bool multipleValue)
63 {
64  const std::string input = entry;
65 
66  std::vector<std::string> attributeContainer;
67 
68  const std::regex codeRegex("(\\([^;]+;[^;]+;[^;)]+\\))");
69  std::smatch match;
70 
71  auto textIt = input.begin();
72 
73  // Loop through matches
74  while(std::regex_search(textIt, input.end(), match, codeRegex))
75  {
76  attributeContainer.push_back(match[1]);
77  textIt = match[0].second;
78  }
79 
80  // If the entry matches the criterion, we format the entry by concatenating
81  // all the matches
82  if(attributeContainer.size() == 1 || (attributeContainer.size() > 1 && multipleValue))
83  {
84  entry = "";
85  for(const auto& attribute : attributeContainer)
86  {
87  entry += attribute;
88  }
89  return true;
90  }
91 
92  return false;
93 }
94 
95 //------------------------------------------------------------------------------
96 
97 } //namespace helper
98 } //namespace fwGdcmIO
99 
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
This class represents a DICOM basic coded entry. It stores the four mains attributes : Code Value...
static FWGDCMIO_API bool checkAndFormatEntry(std::string &entry, bool multipleValue=false)
Retrieve &#39;(AAA;BBB;CCC)&#39; patterns inside of the entry string. Remove spaces or other characters that ...
static FWGDCMIO_API::gdcm::Segment::BasicCodedEntryVector convertEntryToGDCMCodedAttribute(const std::string &entry)
Convert an entry of the form &#39;(AAA;BBB;CCC)&#39; into GDCM&#39;s BasicCodedEntryVector. Several DicomCodedAtt...
static FWGDCMIO_API DicomCodedAttributeVectorType convertEntryToCodedAttribute(const std::string &entry)
Convert an entry of the form &#39;(AAA;BBB;CCC)&#39; into a list of DicomCodedAttributes. Several DicomCodedA...
std::vector< ::fwGdcmIO::container::DicomCodedAttribute > DicomCodedAttributeVectorType
DicomCodedAttribute container type.