vfs: check userland buffers before reading them.
[haiku.git] / src / apps / icon-o-matic / style / AddStylesCommand.cpp
bloba936bdd668e159283b274c9d67121df0e4e11f02
1 /*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
9 #include "AddStylesCommand.h"
11 #include <new>
12 #include <stdio.h>
13 #include <string.h>
15 #include <Catalog.h>
16 #include <Locale.h>
18 #include "StyleContainer.h"
19 #include "Style.h"
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-AddStylesCmd"
26 using std::nothrow;
28 // constructor
29 AddStylesCommand::AddStylesCommand(StyleContainer* container,
30 Style** const styles,
31 int32 count,
32 int32 index)
33 : Command(),
34 fContainer(container),
35 fStyles(styles && count > 0 ? new (nothrow) Style*[count] : NULL),
36 fCount(count),
37 fIndex(index),
38 fStylesAdded(false)
40 if (!fContainer || !fStyles)
41 return;
43 memcpy(fStyles, styles, sizeof(Style*) * fCount);
46 // destructor
47 AddStylesCommand::~AddStylesCommand()
49 if (!fStylesAdded && fStyles) {
50 for (int32 i = 0; i < fCount; i++)
51 fStyles[i]->ReleaseReference();
53 delete[] fStyles;
56 // InitCheck
57 status_t
58 AddStylesCommand::InitCheck()
60 return fContainer && fStyles ? B_OK : B_NO_INIT;
63 // Perform
64 status_t
65 AddStylesCommand::Perform()
67 status_t ret = B_OK;
69 // add shapes to container
70 int32 index = fIndex;
71 for (int32 i = 0; i < fCount; i++) {
72 if (fStyles[i] && !fContainer->AddStyle(fStyles[i], index)) {
73 ret = B_ERROR;
74 // roll back
75 for (int32 j = i - 1; j >= 0; j--)
76 fContainer->RemoveStyle(fStyles[j]);
77 break;
79 index++;
81 fStylesAdded = true;
83 return ret;
86 // Undo
87 status_t
88 AddStylesCommand::Undo()
90 // remove shapes from container
91 for (int32 i = 0; i < fCount; i++) {
92 fContainer->RemoveStyle(fStyles[i]);
94 fStylesAdded = false;
96 return B_OK;
99 // GetName
100 void
101 AddStylesCommand::GetName(BString& name)
103 if (fCount > 1)
104 name << B_TRANSLATE("Add Styles");
105 else
106 name << B_TRANSLATE("Add Style");