OpenASIP 2.2
Loading...
Searching...
No Matches
StringSection.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 StringSection.cc
26 *
27 * Definition of StringSection class.
28 *
29 * @author Ari Metsähalme 2003 (ari.metsahalme-no.spam-tut.fi)
30 * @author Mikael Lepistö 2004 (tmlepist-no.spam-cs.tut.fi)
31 * @note reviewed 17 October 2003 by am, pj, rm, kl
32 *
33 * @note rating: yellow
34 */
35
36#include "StringSection.hh"
37
38namespace TPEF {
39
40using std::string;
41
42// registers section prototype to the base class
43StringSection StringSection::proto_(true);
44
45/**
46 * Constructor.
47 *
48 * @param init True if registeration is wanted.
49 */
51 if (init) {
53 }
54
57}
58
59/**
60 * Destructor.
61 */
64
65/**
66 * Returns the string of the given chunk.
67 *
68 * @param chunk Chunk where string starts.
69 * @exception UnexpectedValue If no terminating zero is found.
70 */
71string
73 unsigned int offset = chunk->offset();
74 assert(offset <= length());
75
76 string result = "";
77 while (byte(offset) != 0) {
78 // if we are reading the last byte in section and it is not zero,
79 // we have an exception
80 if (offset == length() - 1) {
81 throw UnexpectedValue(
82 __FILE__, __LINE__, __func__,
83 "No terminating zero found!");
84 }
85 result.append(1, byte(offset));
86 offset++;
87 }
88
89 return result;
90}
91
92/**
93 * Checks if string is already found from section and returns chunk
94 * pointing to it.
95 *
96 * If string is not already found from section, it is added to end,
97 * before returning chunk.
98 *
99 * @param str String to find from section.
100 * @return Chunk pointing to requested string.
101 */
102Chunk*
103StringSection::string2Chunk(const std::string &str) {
104
105 // needed first byte
106 if (length() == 0) {
107 addByte(0);
108 }
109
110 for (int i = 0;
111 i < static_cast<int>(length()) -
112 static_cast<int>(str.length()); i++) {
113
114 Word charsToMatch = str.length() + 1;
115
116 while (charsToMatch) {
117 charsToMatch--;
118
119 if (byte(i + charsToMatch) != str.c_str()[charsToMatch]) {
120 charsToMatch = 1;
121 break;
122 }
123 }
124
125 // we found matching string
126 if (charsToMatch == 0) {
127 return chunk(i);
128 }
129 }
130
131 // didn't find matching string, add new one
132 SectionOffset returnOffset = length();
133
134 for (Word i = 0; i < str.size(); i++) {
135 addByte(str[i]);
136 }
137 addByte(0);
138
139 return chunk(returnOffset);
140}
141
142/**
143 * Returns the type of section.
144 *
145 * @return Type of section.
146 */
149 return ST_STRTAB;
150}
151
152/**
153 * Creates an instance of the class.
154 *
155 * @return Newly created section.
156 */
157Section*
159 return new StringSection(false);
160}
161
162}
#define __func__
#define assert(condition)
find Finds info of the inner loops in the false
SectionOffset offset() const
virtual Word length() const
virtual void addByte(Byte aByte)
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:212
static void registerSection(const Section *section)
Definition Section.cc:114
@ ST_STRTAB
String table.
Definition Section.hh:71
void unsetFlagNoBits()
void unsetFlagVLen()
virtual Section * clone() const
static StringSection proto_
Prototype of the section.
virtual SectionType type() const
std::string chunk2String(const Chunk *chunk) const
Chunk * string2Chunk(const std::string &str)
Word SectionOffset
Type for storing offsets relative to a given base offset value.