Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
ExampleToUpperCaseMiniApp.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // std includes
14 #include <algorithm>
15 #include <string>
16 
17 // CTK includes
18 #include "mitkCommandLineParser.h"
19 
20 // MITK includes
22 #include <mitkIOUtil.h>
23 
36 int main(int argc, char *argv[])
38 {
40 
42  mitkCommandLineParser parser;
43 
44  // set general information about your MiniApp
45  parser.setCategory("MITK-Examples");
46  parser.setTitle("To Upper Case");
47  parser.setDescription("An example MiniApp that converts the contents of a test file to upper case.");
48  parser.setContributor("German Cancer Research Center (DKFZ)");
50 
52  // how should arguments be prefixed
53  parser.setArgumentPrefix("--", "-");
54  // add each argument, unless specified otherwise each argument is optional
55  // see mitkCommandLineParser::addArgument for more information
56  parser.beginGroup("Required I/O parameters");
57  parser.addArgument(
58  "input", "i", mitkCommandLineParser::File, "Input file", "input file (.txt/.example)", us::Any(), false, false, false, mitkCommandLineParser::Input);
59  parser.addArgument("output",
60  "o",
62  "Output file",
63  "where to save the output (.txt/.example)",
64  us::Any(),
65  false, false, false, mitkCommandLineParser::Output);
66  parser.endGroup();
67 
68  parser.beginGroup("Optional parameters");
69  parser.addArgument(
70  "verbose", "v", mitkCommandLineParser::Bool, "Verbose Output", "Whether to produce verbose output");
71  parser.endGroup();
73 
75  // parse arguments, this method returns a mapping of long argument names and their values
76  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
77 
78  if (parsedArgs.size() == 0)
79  return EXIT_FAILURE;
80 
81  // parse, cast and set required arguments
82  std::string inFilename = us::any_cast<std::string>(parsedArgs["input"]);
83  std::string outFileName = us::any_cast<std::string>(parsedArgs["output"]);
84 
85  // default values for optional arguments
86  bool verbose(false);
87 
88  // parse, cast and set optional arguments if given
89  if (parsedArgs.count("verbose"))
90  {
91  verbose = us::any_cast<bool>(parsedArgs["verbose"]);
92  }
94 
96  try
97  {
98  // verbosity in this example is slightly over the top
99  if (verbose)
100  {
101  MITK_INFO << "Trying to read file.";
102  }
103 
104  std::vector<mitk::BaseData::Pointer> inVector = mitk::IOUtil::Load(inFilename);
105  if (inVector.empty())
106  {
107  std::string errorMessage = "File at " + inFilename + " could not be read. Aborting.";
108  MITK_ERROR << errorMessage;
109  return EXIT_FAILURE;
110  }
111  mitk::BaseData *inBaseData = inVector.at(0);
112  mitk::ExampleDataStructure *inExample = dynamic_cast<mitk::ExampleDataStructure *>(inBaseData);
113 
114  if (verbose)
115  {
116  MITK_INFO << "Converting string.";
117  }
119  std::string data = inExample->GetData();
120  if (verbose)
121  {
122  MITK_INFO << "String before conversion: " << data;
123  }
124  std::transform(data.begin(), data.end(), data.begin(), ::toupper);
125  if (verbose)
126  {
127  MITK_INFO << "String after conversion: " << data;
128  }
129  outExample->SetData(data);
130 
131  if (verbose)
132  {
133  MITK_INFO << "Trying to write to file.";
134  }
135 
136  mitk::IOUtil::Save(outExample.GetPointer(), outFileName);
137 
138  return EXIT_SUCCESS;
139  }
141 
142  catch (itk::ExceptionObject& e)
143  {
144  MITK_ERROR << e;
145  return EXIT_FAILURE;
146  }
147  catch (std::exception& e)
148  {
149  MITK_ERROR << e.what();
150  return EXIT_FAILURE;
151  }
152  catch (...)
153  {
154  MITK_ERROR << "Unexpected error encountered.";
155  return EXIT_FAILURE;
156  }
157 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
Base of all data objects.
Definition: mitkBaseData.h:37
#define MITK_ERROR
Definition: mitkLogMacros.h:20
virtual std::string GetData()
void setContributor(std::string contributor)
std::string inFilename
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::string outFileName
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
int main(int argc, char *argv[])
Example MiniApp that converts a text file content to upper case.
bool verbose(false)
Definition: usAny.h:163
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
void setTitle(std::string title)
void setDescription(std::string description)
void beginGroup(const std::string &description)
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489