vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / settings / TeamSettings.cpp
blob54a048070836be1e2b10869cb7056b47aa4cb15e
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2013-2015, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
5 */
8 #include "TeamSettings.h"
10 #include <new>
12 #include <Message.h>
14 #include <AutoLocker.h>
16 #include "ArchivingUtils.h"
17 #include "BreakpointSetting.h"
18 #include "Team.h"
19 #include "TeamFileManagerSettings.h"
20 #include "TeamSignalSettings.h"
21 #include "TeamUiSettings.h"
22 #include "TeamUiSettingsFactory.h"
23 #include "UserBreakpoint.h"
26 TeamSettings::TeamSettings()
28 fFileManagerSettings = new TeamFileManagerSettings();
29 fSignalSettings = new TeamSignalSettings();
33 TeamSettings::TeamSettings(const TeamSettings& other)
35 try {
36 *this = other;
37 } catch (...) {
38 _Unset();
39 throw;
44 TeamSettings::~TeamSettings()
46 _Unset();
50 status_t
51 TeamSettings::SetTo(Team* team)
53 _Unset();
55 AutoLocker<Team> locker(team);
57 fTeamName = team->Name();
59 // add breakpoints
60 for (UserBreakpointList::ConstIterator it
61 = team->UserBreakpoints().GetIterator();
62 UserBreakpoint* breakpoint = it.Next();) {
63 BreakpointSetting* breakpointSetting
64 = new(std::nothrow) BreakpointSetting;
65 if (breakpointSetting == NULL)
66 return B_NO_MEMORY;
68 status_t error = breakpointSetting->SetTo(breakpoint->Location(),
69 breakpoint->IsEnabled(), breakpoint->IsHidden(),
70 breakpoint->Condition());
71 if (error == B_OK && !fBreakpoints.AddItem(breakpointSetting))
72 error = B_NO_MEMORY;
73 if (error != B_OK) {
74 delete breakpointSetting;
75 return error;
79 // add signal configuration
81 fSignalSettings->SetDefaultSignalDisposition(
82 team->DefaultSignalDisposition());
84 const SignalDispositionMappings& mappings
85 = team->GetSignalDispositionMappings();
87 for (SignalDispositionMappings::const_iterator it = mappings.begin();
88 it != mappings.end(); ++it) {
89 status_t error = fSignalSettings->AddCustomSignalDisposition(
90 it->first, it->second);
91 if (error != B_OK)
92 return error;
95 return B_OK;
99 status_t
100 TeamSettings::SetTo(const BMessage& archive,
101 const TeamUiSettingsFactory& factory)
103 _Unset();
105 status_t error = archive.FindString("teamName", &fTeamName);
106 if (error != B_OK)
107 return error;
109 // add breakpoints
110 BMessage childArchive;
111 for (int32 i = 0; archive.FindMessage("breakpoints", i, &childArchive)
112 == B_OK; i++) {
113 BreakpointSetting* breakpointSetting
114 = new(std::nothrow) BreakpointSetting;
115 if (breakpointSetting == NULL)
116 return B_NO_MEMORY;
118 error = breakpointSetting->SetTo(childArchive);
119 if (error == B_OK && !fBreakpoints.AddItem(breakpointSetting))
120 error = B_NO_MEMORY;
121 if (error != B_OK) {
122 delete breakpointSetting;
123 return error;
127 // add UI settings
128 for (int32 i = 0; archive.FindMessage("uisettings", i, &childArchive)
129 == B_OK; i++) {
130 TeamUiSettings* setting = NULL;
131 error = factory.Create(childArchive, setting);
132 if (error == B_OK && !fUiSettings.AddItem(setting))
133 error = B_NO_MEMORY;
134 if (error != B_OK) {
135 delete setting;
136 return error;
140 if (archive.FindMessage("filemanagersettings", &childArchive) == B_OK) {
141 error = fFileManagerSettings->SetTo(childArchive);
142 if (error != B_OK)
143 return error;
146 if (archive.FindMessage("signalsettings", &childArchive) == B_OK) {
147 error = fSignalSettings->SetTo(childArchive);
148 if (error != B_OK)
149 return error;
152 return B_OK;
156 status_t
157 TeamSettings::WriteTo(BMessage& archive) const
159 status_t error = archive.AddString("teamName", fTeamName);
160 if (error != B_OK)
161 return error;
163 BMessage childArchive;
164 for (int32 i = 0; BreakpointSetting* breakpoint = fBreakpoints.ItemAt(i);
165 i++) {
166 error = breakpoint->WriteTo(childArchive);
167 if (error != B_OK)
168 return error;
170 error = archive.AddMessage("breakpoints", &childArchive);
171 if (error != B_OK)
172 return error;
175 for (int32 i = 0; TeamUiSettings* uiSetting = fUiSettings.ItemAt(i);
176 i++) {
177 error = uiSetting->WriteTo(childArchive);
178 if (error != B_OK)
179 return error;
181 error = archive.AddMessage("uisettings", &childArchive);
182 if (error != B_OK)
183 return error;
186 error = fFileManagerSettings->WriteTo(childArchive);
187 if (error != B_OK)
188 return error;
190 error = archive.AddMessage("filemanagersettings", &childArchive);
191 if (error != B_OK)
192 return error;
194 error = fSignalSettings->WriteTo(childArchive);
195 if (error != B_OK)
196 return error;
198 error = archive.AddMessage("signalsettings", &childArchive);
199 if (error != B_OK)
200 return error;
202 return B_OK;
206 int32
207 TeamSettings::CountBreakpoints() const
209 return fBreakpoints.CountItems();
213 const BreakpointSetting*
214 TeamSettings::BreakpointAt(int32 index) const
216 return fBreakpoints.ItemAt(index);
220 int32
221 TeamSettings::CountUiSettings() const
223 return fUiSettings.CountItems();
227 const TeamUiSettings*
228 TeamSettings::UiSettingAt(int32 index) const
230 return fUiSettings.ItemAt(index);
234 const TeamUiSettings*
235 TeamSettings::UiSettingFor(const char* id) const
237 for (int32 i = 0; i < fUiSettings.CountItems(); i++) {
238 TeamUiSettings* settings = fUiSettings.ItemAt(i);
239 if (strcmp(settings->ID(), id) == 0)
240 return settings;
243 return NULL;
247 status_t
248 TeamSettings::AddUiSettings(TeamUiSettings* settings)
250 if (!fUiSettings.AddItem(settings))
251 return B_NO_MEMORY;
253 return B_OK;
257 TeamSettings&
258 TeamSettings::operator=(const TeamSettings& other)
260 if (this == &other)
261 return *this;
263 _Unset();
265 fTeamName = other.fTeamName;
267 for (int32 i = 0; BreakpointSetting* breakpoint
268 = other.fBreakpoints.ItemAt(i); i++) {
269 BreakpointSetting* clonedBreakpoint
270 = new BreakpointSetting(*breakpoint);
271 if (!fBreakpoints.AddItem(clonedBreakpoint)) {
272 delete clonedBreakpoint;
273 throw std::bad_alloc();
277 for (int32 i = 0; TeamUiSettings* uiSetting
278 = other.fUiSettings.ItemAt(i); i++) {
279 TeamUiSettings* clonedSetting
280 = uiSetting->Clone();
281 if (!fUiSettings.AddItem(clonedSetting)) {
282 delete clonedSetting;
283 throw std::bad_alloc();
287 *fFileManagerSettings = *other.fFileManagerSettings;
289 *fSignalSettings = *other.fSignalSettings;
291 return *this;
295 TeamFileManagerSettings*
296 TeamSettings::FileManagerSettings() const
298 return fFileManagerSettings;
302 status_t
303 TeamSettings::SetFileManagerSettings(TeamFileManagerSettings* settings)
305 try {
306 *fFileManagerSettings = *settings;
307 } catch (...) {
308 return B_NO_MEMORY;
311 return B_OK;
315 TeamSignalSettings*
316 TeamSettings::SignalSettings() const
318 return fSignalSettings;
322 status_t
323 TeamSettings::SetSignalSettings(TeamSignalSettings* settings)
325 try {
326 *fSignalSettings = *settings;
327 } catch (...) {
328 return B_NO_MEMORY;
331 return B_OK;
335 void
336 TeamSettings::_Unset()
338 for (int32 i = 0; BreakpointSetting* breakpoint = fBreakpoints.ItemAt(i);
339 i++) {
340 delete breakpoint;
343 for (int32 i = 0; TeamUiSettings* uiSetting = fUiSettings.ItemAt(i); i++)
344 delete uiSetting;
346 fBreakpoints.MakeEmpty();
347 fUiSettings.MakeEmpty();
348 fSignalSettings->Unset();
350 fTeamName.Truncate(0);