OpenASIP  2.0
TextGenerator.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file TextGenerator.cc
26  *
27  * Implementation of TextGenerator class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31  * @note reviewed 19 May 2004 by ml, jn, ao, am
32  * @note rating: green
33  */
34 
35 #include <string>
36 #include <map>
37 #include <cstdio>
38 #include <cstdlib>
39 #include <cstdarg>
40 
41 #include "MapTools.hh"
42 #include "Conversion.hh"
43 #include "Application.hh"
44 #include "TextGenerator.hh"
45 
47 using std::string;
48 using std::map;
49 using boost::format;
50 
51 
52 /**
53  * Constructor.
54  */
56  addText(Texts::TXT_HELLO_WORLD, "Hello %s world!");
57  addText(Texts::TXT_FILE_NOT_FOUND, "File not found.");
58  addText(Texts::TXT_FILE_X_NOT_FOUND, "File %s not found.");
59  addText(Texts::TXT_FILE_NOT_READABLE, "File not readable.");
60  addText(Texts::TXT_ILLEGAL_INPUT_FILE, "Illegal input file.");
61  addText(Texts::TXT_NO_FILENAME_DEFINED, "No filename defined.");
62  addText(
64  "Only one filename expected.");
65  addText(Texts::TXT_NO_SUCH_SETTING, "No such setting.");
66  addText(Texts::TXT_UNKNOWN_COMMAND, "Unknown command.");
67  addText(Texts::TXT_UNKNOWN_SUBCOMMAND, "Unknown subcommand.");
68  addText(Texts::TXT_ILLEGAL_ARGUMENT, "Illegal argument.");
69  addText(Texts::TXT_ILLEGAL_ARGUMENTS, "Illegal arguments.");
70 
71 }
72 
73 
74 /**
75  * Destructor.
76  *
77  * Frees the allocated memory.
78  */
80  for (MapIter mi = dataBase_.begin(); mi != dataBase_.end(); mi++) {
81  delete (*mi).second;
82  }
83 }
84 
85 
86 /**
87  * Returns a format object that contains the template string.
88  *
89  * @param textId Numeric code that identifies the template string.
90  * @return format object that contains the template string.
91  * @exception KeyNotFound If textId doesn't identify any template string.
92  */
93 format
95  if (!MapTools::containsKey(dataBase_, textId)) {
96  string method = "TextGenerator::text()";
97  string message("Requested message (code ");
98  message += Conversion::toString(textId) + ") does not exist.";
99  throw KeyNotFound(__FILE__, __LINE__, method, message);
100  }
101 
102  MapIter mt = dataBase_.find(textId);
103  const string& templateString = *((*mt).second);
104 
105  return format(templateString);
106 }
107 
108 /**
109  * Records a new template string that can be used to generate text messages.
110  *
111  * The template string is first copied to the dynamically allocated string,
112  * just to ensure that it will be not destroyed before TextGenerator.
113  *
114  * @param textId Numeric code that identifies the template string.
115  * @param templateString Template string to be recorded.
116  */
117 void
118 Texts::TextGenerator::addText(int textId, const std::string& templateString) {
119  string* toBeAdded = new string(templateString);
120  dataBase_.insert(ValType(textId, toBeAdded));
121 }
122 
123 
124 /**
125  * Replaces existing text with a new one.
126  *
127  * @param textID ID of the text to be replaced
128  * @param newString new string to be set
129  * @exception KeyNotFound Thrown if a text of given ID couldn't be found.
130  */
131 void
132 Texts::TextGenerator::replaceText(int textId, const std::string& newString) {
133  if (!MapTools::containsKey(dataBase_, textId)) {
134  string message("Requested message (code ");
135  message += Conversion::toString(textId) + ") does not exist.";
136  throw KeyNotFound(__FILE__, __LINE__, __FUNCTION__, message);
137  }
138 
139  delete dataBase_[textId];
140  string* toBeAdded = new string(newString);
141  dataBase_[textId] = toBeAdded;
142 }
143 
Texts::TXT_UNKNOWN_COMMAND
@ TXT_UNKNOWN_COMMAND
Definition: TextGenerator.hh:64
MapTools.hh
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
Texts::TXT_ONLY_ONE_FILENAME_EXPECTED
@ TXT_ONLY_ONE_FILENAME_EXPECTED
Definition: TextGenerator.hh:62
Texts::TextGenerator::MapIter
std::map< int, const std::string * >::iterator MapIter
Iterator for map.
Definition: TextGenerator.hh:93
Conversion::toString
static std::string toString(const T &source)
Texts::TXT_NO_SUCH_SETTING
@ TXT_NO_SUCH_SETTING
Definition: TextGenerator.hh:63
Texts::TextGenerator::addText
virtual void addText(int textId, const std::string &templateString)
Definition: TextGenerator.cc:118
Texts::TXT_ILLEGAL_ARGUMENT
@ TXT_ILLEGAL_ARGUMENT
Definition: TextGenerator.hh:66
Texts::TextGenerator::replaceText
virtual void replaceText(int textId, const std::string &newString)
Definition: TextGenerator.cc:132
Conversion.hh
Texts::TXT_UNKNOWN_SUBCOMMAND
@ TXT_UNKNOWN_SUBCOMMAND
Definition: TextGenerator.hh:65
Application.hh
Texts::TXT_ILLEGAL_ARGUMENTS
@ TXT_ILLEGAL_ARGUMENTS
Definition: TextGenerator.hh:67
Texts::TXT_ILLEGAL_INPUT_FILE
@ TXT_ILLEGAL_INPUT_FILE
Definition: TextGenerator.hh:60
Texts::TextGenerator
Definition: TextGenerator.hh:79
TextGenerator.hh
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
Texts::TXT_FILE_NOT_READABLE
@ TXT_FILE_NOT_READABLE
Definition: TextGenerator.hh:59
Texts::TXT_NO_FILENAME_DEFINED
@ TXT_NO_FILENAME_DEFINED
Definition: TextGenerator.hh:61
Texts::TXT_FILE_NOT_FOUND
@ TXT_FILE_NOT_FOUND
Definition: TextGenerator.hh:57
KeyNotFound
Definition: Exception.hh:285
Texts::TextGenerator::~TextGenerator
virtual ~TextGenerator()
Definition: TextGenerator.cc:79
Texts::TXT_FILE_X_NOT_FOUND
@ TXT_FILE_X_NOT_FOUND
Definition: TextGenerator.hh:58
Texts::TXT_HELLO_WORLD
@ TXT_HELLO_WORLD
For testing. Do not remove.
Definition: TextGenerator.hh:56
Texts::TextGenerator::TextGenerator
TextGenerator()
Definition: TextGenerator.cc:55
Texts::TextGenerator::ValType
std::map< int, const std::string * >::value_type ValType
value_type for map.
Definition: TextGenerator.hh:91