OpenASIP  2.0
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 
38 namespace TPEF {
39 
40 using std::string;
41 
42 // registers section prototype to the base class
43 StringSection StringSection::proto_(true);
44 
45 /**
46  * Constructor.
47  *
48  * @param init True if registeration is wanted.
49  */
51  if (init) {
53  }
54 
55  unsetFlagVLen();
57 }
58 
59 /**
60  * Destructor.
61  */
63 }
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  */
71 string
72 StringSection::chunk2String(const Chunk* chunk) const {
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  */
102 Chunk*
103 StringSection::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  */
157 Section*
159  return new StringSection(false);
160 }
161 
162 }
TPEF::StringSection::proto_
static StringSection proto_
Prototype of the section.
Definition: StringSection.hh:64
TPEF::Section::unsetFlagVLen
void unsetFlagVLen()
TPEF::DataSection
Definition: DataSection.hh:52
TPEF::StringSection::chunk2String
std::string chunk2String(const Chunk *chunk) const
Definition: StringSection.cc:72
StringSection.hh
TPEF::DataSection::addByte
virtual void addByte(Byte aByte)
Definition: DataSection.cc:200
TPEF::Section
Definition: Section.hh:64
TPEF::StringSection::~StringSection
virtual ~StringSection()
Definition: StringSection.cc:62
assert
#define assert(condition)
Definition: Application.hh:86
UnexpectedValue
Definition: Exception.hh:455
TPEF::StringSection::string2Chunk
Chunk * string2Chunk(const std::string &str)
Definition: StringSection.cc:103
__func__
#define __func__
Definition: Application.hh:67
TPEF::StringSection::StringSection
StringSection(bool init)
Definition: StringSection.cc:50
TPEF::Section::unsetFlagNoBits
void unsetFlagNoBits()
TPEF::StringSection::clone
virtual Section * clone() const
Definition: StringSection.cc:158
TPEF::Section::registerSection
static void registerSection(const Section *section)
Definition: Section.cc:114
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::Chunk::offset
SectionOffset offset() const
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
TPEF::Section::ST_STRTAB
@ ST_STRTAB
String table.
Definition: Section.hh:71
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::DataSection::length
virtual Word length() const
Definition: DataSection.cc:210
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
TPEF::StringSection::type
virtual SectionType type() const
Definition: StringSection.cc:148
TPEF::Chunk
Definition: Chunk.hh:45
TPEF
Definition: Assembler.hh:43