OpenASIP 2.2
Loading...
Searching...
No Matches
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
47using std::string;
48using std::map;
49using 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 */
93format
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 */
117void
118Texts::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 */
131void
132Texts::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
static std::string toString(const T &source)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
virtual boost::format text(int textId)
virtual void replaceText(int textId, const std::string &newString)
virtual void addText(int textId, const std::string &templateString)
std::map< int, conststd::string * >::iterator MapIter
Iterator for map.
std::map< int, conststd::string * >::value_type ValType
value_type for map.
@ TXT_HELLO_WORLD
For testing. Do not remove.
@ TXT_ONLY_ONE_FILENAME_EXPECTED
@ TXT_ILLEGAL_INPUT_FILE
@ TXT_ILLEGAL_ARGUMENT
@ TXT_NO_SUCH_SETTING
@ TXT_FILE_X_NOT_FOUND
@ TXT_NO_FILENAME_DEFINED
@ TXT_FILE_NOT_READABLE
@ TXT_UNKNOWN_COMMAND
@ TXT_FILE_NOT_FOUND
@ TXT_UNKNOWN_SUBCOMMAND
@ TXT_ILLEGAL_ARGUMENTS