Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 // std includes
18 #include <algorithm>
19 #include <string>
20 
21 // CTK includes
22 #include "mitkCommandLineParser.h"
23 
24 // MITK includes
26 #include <mitkIOUtil.h>
27 
40 int main(int argc, char *argv[])
42 {
44 
46  mitkCommandLineParser parser;
47 
48  // set general information about your MiniApp
49  parser.setCategory("MITK-Examples");
50  parser.setTitle("To Upper Case");
51  parser.setDescription("An example MiniApp that converts the contents of a test file to upper case.");
52  parser.setContributor("MBI");
54 
56  // how should arguments be prefixed
57  parser.setArgumentPrefix("--", "-");
58  // add each argument, unless specified otherwise each argument is optional
59  // see mitkCommandLineParser::addArgument for more information
60  parser.beginGroup("Required I/O parameters");
61  parser.addArgument(
62  "input", "i", mitkCommandLineParser::InputFile, "Input file", "input file (.txt/.example)", us::Any(), false);
63  parser.addArgument("output",
64  "o",
66  "Output file",
67  "where to save the output (.txt/.example)",
68  us::Any(),
69  false);
70  parser.endGroup();
71 
72  parser.beginGroup("Optional parameters");
73  parser.addArgument(
74  "verbose", "v", mitkCommandLineParser::Bool, "Verbose Output", "Whether to produce verbose output");
75  parser.endGroup();
77 
79  // parse arguments, this method returns a mapping of long argument names and their values
80  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
81 
82  if (parsedArgs.size() == 0)
83  return EXIT_FAILURE;
84 
85  // parse, cast and set required arguments
86  std::string inFilename = us::any_cast<std::string>(parsedArgs["input"]);
87  std::string outFileName = us::any_cast<std::string>(parsedArgs["output"]);
88 
89  // default values for optional arguments
90  bool verbose(false);
91 
92  // parse, cast and set optional arguments if given
93  if (parsedArgs.count("verbose"))
94  {
95  verbose = us::any_cast<bool>(parsedArgs["verbose"]);
96  }
98 
100  try
101  {
102  // verbosity in this example is slightly over the top
103  if (verbose)
104  {
105  MITK_INFO << "Trying to read file.";
106  }
107 
108  std::vector<mitk::BaseData::Pointer> inVector = mitk::IOUtil::Load(inFilename);
109  if (inVector.empty())
110  {
111  std::string errorMessage = "File at " + inFilename + " could not be read. Aborting.";
112  MITK_ERROR << errorMessage;
113  return EXIT_FAILURE;
114  }
115  mitk::BaseData *inBaseData = inVector.at(0);
116  mitk::ExampleDataStructure *inExample = dynamic_cast<mitk::ExampleDataStructure *>(inBaseData);
117 
118  if (verbose)
119  {
120  MITK_INFO << "Converting string.";
121  }
123  std::string data = inExample->GetData();
124  if (verbose)
125  {
126  MITK_INFO << "String before conversion: " << data;
127  }
128  std::transform(data.begin(), data.end(), data.begin(), ::toupper);
129  if (verbose)
130  {
131  MITK_INFO << "String after conversion: " << data;
132  }
133  outExample->SetData(data);
134 
135  if (verbose)
136  {
137  MITK_INFO << "Trying to write to file.";
138  }
139 
140  mitk::IOUtil::SaveBaseData(outExample.GetPointer(), outFileName);
141 
142  return EXIT_SUCCESS;
143  }
145 
146  catch (itk::ExceptionObject e)
147  {
148  MITK_ERROR << e;
149  return EXIT_FAILURE;
150  }
151  catch (std::exception e)
152  {
153  MITK_ERROR << e.what();
154  return EXIT_FAILURE;
155  }
156  catch (...)
157  {
158  MITK_ERROR << "Unexpected error encountered.";
159  return EXIT_FAILURE;
160  }
161 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
Base of all data objects.
Definition: mitkBaseData.h:39
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual std::string GetData()
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
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.
static bool SaveBaseData(mitk::BaseData *data, const std::string &path)
SaveBaseData Convenience method to save arbitrary baseData.
Definition: mitkIOUtil.cpp:888
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)
Definition: usAny.h:163
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
void setTitle(std::string title)
void setDescription(std::string description)
void beginGroup(const std::string &description)