File: gdcmPrepGroupName.cxx

package info (click to toggle)
gdcm 2.4.4-3%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 32,912 kB
  • ctags: 52,166
  • sloc: cpp: 188,527; ansic: 124,526; xml: 41,799; sh: 7,162; python: 3,667; cs: 2,128; java: 1,344; lex: 1,290; tcl: 677; php: 128; makefile: 116
file content (120 lines) | stat: -rw-r--r-- 3,290 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*
 * Executable meant to preprocess a GroupName dictionary, entry should look like this:
 * 0000 CMD Command
 * ...
 */

#include <iostream>
#include <fstream>
#include <sstream>  // for std::getline
#include <assert.h>
#include <stdio.h>
#include <iomanip> // std::setw

void write_header(std::ofstream &of)
{
  of << "#include \"gdcmTypes.h\"\n"
    "#include \"gdcmGroupDict.h\"\n\n"
    "namespace gdcm {\n\n"
    "typedef struct\n{\n"
    "  uint16_t group;\n"
    "  const char *abbreviation;\n"
    "  const char *name;\n"
    "} GROUP_ENTRY;\n\n"
    "static GROUP_ENTRY groupname[] = {\n";
}

void write_footer(std::ofstream &of)
{
  of << "\t{0,0,0} // will not be added to the dict \n"
    "};\n\n"
    "void GroupDict::FillDefaultGroupName()\n"
    "{\n"
    "  unsigned int i = 0;\n"
    "  GROUP_ENTRY n = groupname[i];\n"
    "  while( n.name != 0 )\n"
    "  {\n"
    "    Insert( n.group, n.abbreviation, n.name );\n"
    "    n = groupname[++i];\n"
    "  }\n"
    "}\n\n"
    "} // namespace gdcm\n";
}

bool check_abbr(std::string &abbr)
{
  std::string::const_iterator it = abbr.begin();
  for(; it != abbr.end(); ++it)
    {
    if ( *it < 'A' || *it > 'Z' ) return false;
    }
  return true;
}

int main(int argc, char *argv[])
{
  if( argc < 3 )
    return 1;

  const char *filename = argv[1]; // Full path to the dict
  const char *outfilename = argv[2]; // Full path to output the dict
  //std::cerr << "open: " << filename << std::endl;
  std::ifstream from(filename, std::ios::binary);
  std::ofstream into(outfilename,std::ios::binary);
  if(!from)
    {
    std::cerr << "Problem opening the from file" << std::endl;
    return 1;
    }
  if(!into)
    {
    std::cerr << "Problem opening the into file" << std::endl;
    return 1;
    }

  write_header(into);
  int error = 0;
  std::string line;
  while(std::getline(from, line))
    {
     if( !line.empty() )
       {
       std::string::iterator e(line.end()-1);
       if( *e == '\r' ) line.erase(e);
       }
    unsigned int group; // Group Number
    std::string abbr; // NHI Abbreviation (when known) - not part of DICOM standard -
    std::string meaning; // Meaning          (when known) - not part of DICOM standard -
    std::istringstream is(line);
    is >> std::hex >> group;
    if ( group > 0xffff)
      return 1;
    is >> abbr;
    if( !check_abbr(abbr) )
      return 1;
    // skip any whitespace before calling getline
    is >> std::ws;
    // get all the remaining characters
    std::getline(is,meaning);
    into << "\t{0x" << std::hex << std::setw(4) << std::setfill('0') << group << ",\"" << abbr << "\",\"" << meaning << "\"},\n";
    }
  write_footer(into);

  from.close();
  into.close();

  return error;
}