vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / model / TypeComponentPath.cpp
blob0377e2976298a51db2bfd77a8039187829d2868c
1 /*
2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "TypeComponentPath.h"
9 #include <stdio.h>
11 #include <new>
13 #include "StringUtils.h"
16 // #pragma mark - TypeComponent
19 bool
20 TypeComponent::HasPrefix(const TypeComponent& other) const
22 if (*this == other)
23 return true;
25 return componentKind == TYPE_COMPONENT_ARRAY_ELEMENT
26 && other.componentKind == TYPE_COMPONENT_ARRAY_ELEMENT
27 && name.Compare(other.name, other.name.Length()) == 0;
31 uint32
32 TypeComponent::HashValue() const
34 uint32 hash = ((uint32)index << 8) | (componentKind << 4) | typeKind;
35 return StringUtils::HashValue(name) * 13 + hash;
39 void
40 TypeComponent::Dump() const
42 switch (typeKind) {
43 case TYPE_PRIMITIVE:
44 printf("primitive");
45 break;
46 case TYPE_COMPOUND:
47 printf("compound");
48 break;
49 case TYPE_MODIFIED:
50 printf("modified");
51 break;
52 case TYPE_TYPEDEF:
53 printf("typedef");
54 break;
55 case TYPE_ADDRESS:
56 printf("address");
57 break;
58 case TYPE_ENUMERATION:
59 printf("enum");
60 break;
61 case TYPE_SUBRANGE:
62 printf("subrange");
63 break;
64 case TYPE_ARRAY:
65 printf("array");
66 break;
67 case TYPE_UNSPECIFIED:
68 printf("unspecified");
69 break;
70 case TYPE_FUNCTION:
71 printf("function");
72 break;
73 case TYPE_POINTER_TO_MEMBER:
74 printf("pointer to member");
75 break;
78 printf(" ");
80 switch (componentKind) {
81 case TYPE_COMPONENT_UNDEFINED:
82 printf("undefined");
83 break;
84 case TYPE_COMPONENT_BASE_TYPE:
85 printf("base %" B_PRIu64 " \"%s\"", index, name.String());
86 break;
87 case TYPE_COMPONENT_DATA_MEMBER:
88 printf("member %" B_PRIu64 " \"%s\"", index, name.String());
89 break;
90 case TYPE_COMPONENT_ARRAY_ELEMENT:
91 printf("element %" B_PRIu64 " \"%s\"", index, name.String());
92 break;
97 bool
98 TypeComponent::operator==(const TypeComponent& other) const
100 return componentKind == other.componentKind
101 && typeKind == other.typeKind
102 && index == other.index
103 && name == other.name;
107 // #pragma mark - TypeComponentPath
110 TypeComponentPath::TypeComponentPath()
112 fComponents(10, true)
117 TypeComponentPath::TypeComponentPath(const TypeComponentPath& other)
119 fComponents(10, true)
121 *this = other;
125 TypeComponentPath::~TypeComponentPath()
130 int32
131 TypeComponentPath::CountComponents() const
133 return fComponents.CountItems();
137 TypeComponent
138 TypeComponentPath::ComponentAt(int32 index) const
140 TypeComponent* component = fComponents.ItemAt(index);
141 return component != NULL ? *component : TypeComponent();
145 bool
146 TypeComponentPath::AddComponent(const TypeComponent& component)
148 TypeComponent* myComponent = new(std::nothrow) TypeComponent(component);
149 if (myComponent == NULL || !fComponents.AddItem(myComponent)) {
150 delete myComponent;
151 return false;
154 return true;
158 void
159 TypeComponentPath::Clear()
161 fComponents.MakeEmpty();
165 TypeComponentPath*
166 TypeComponentPath::CreateSubPath(int32 componentCount) const
168 if (componentCount < 0 || componentCount > fComponents.CountItems())
169 componentCount = fComponents.CountItems();
171 TypeComponentPath* path = new(std::nothrow) TypeComponentPath;
172 if (path == NULL)
173 return NULL;
174 BReference<TypeComponentPath> pathReference(path, true);
176 for (int32 i = 0; i < componentCount; i++) {
177 if (!path->AddComponent(*fComponents.ItemAt(i)))
178 return NULL;
181 return pathReference.Detach();
185 uint32
186 TypeComponentPath::HashValue() const
188 int32 count = fComponents.CountItems();
189 if (count == 0)
190 return 0;
192 uint32 hash = fComponents.ItemAt(0)->HashValue();
194 for (int32 i = 1; i < count; i++)
195 hash = hash * 17 + fComponents.ItemAt(i)->HashValue();
197 return hash;
201 void
202 TypeComponentPath::Dump() const
204 int32 count = fComponents.CountItems();
205 for (int32 i = 0; i < count; i++) {
206 if (i == 0)
207 printf("[");
208 else
209 printf(" -> [");
210 fComponents.ItemAt(i)->Dump();
211 printf("]");
216 TypeComponentPath&
217 TypeComponentPath::operator=(const TypeComponentPath& other)
219 if (this != &other) {
220 fComponents.MakeEmpty();
222 for (int32 i = 0;
223 TypeComponent* component = other.fComponents.ItemAt(i); i++) {
224 if (!AddComponent(*component))
225 break;
229 return *this;
233 bool
234 TypeComponentPath::operator==(const TypeComponentPath& other) const
236 int32 count = fComponents.CountItems();
237 if (count != other.fComponents.CountItems())
238 return false;
240 for (int32 i = 0; i < count; i++) {
241 if (*fComponents.ItemAt(i) != *other.fComponents.ItemAt(i))
242 return false;
245 return true;