vfs: check userland buffers before reading them.
[haiku.git] / src / apps / icon-o-matic / shape / commands / RemoveTransformersCommand.cpp
blobb301ca72a3537b3c6c035ee21eeecf450918358f
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 "RemoveTransformersCommand.h"
11 #include <new>
12 #include <stdio.h>
13 #include <string.h>
15 #include <Catalog.h>
16 #include <Locale.h>
18 #include "ShapeContainer.h"
19 #include "Shape.h"
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-RemoveTransformersCmd"
26 using std::nothrow;
28 // constructor
29 RemoveTransformersCommand::RemoveTransformersCommand(Shape* container,
30 const int32* indices,
31 int32 count)
32 : Command(),
33 fContainer(container),
34 fTransformers(count > 0 ? new (nothrow) Transformer*[count] : NULL),
35 fIndices(count > 0 ? new (nothrow) int32[count] : NULL),
36 fCount(count),
37 fTransformersRemoved(false)
39 if (!fContainer || !fTransformers || !fIndices)
40 return;
42 memcpy(fIndices, indices, sizeof(int32) * fCount);
43 for (int32 i = 0; i < fCount; i++)
44 fTransformers[i] = fContainer->TransformerAt(fIndices[i]);
47 // destructor
48 RemoveTransformersCommand::~RemoveTransformersCommand()
50 if (fTransformersRemoved && fTransformers) {
51 for (int32 i = 0; i < fCount; i++)
52 delete fTransformers[i];
54 delete[] fTransformers;
55 delete[] fIndices;
58 // InitCheck
59 status_t
60 RemoveTransformersCommand::InitCheck()
62 return fContainer && fTransformers && fIndices ? B_OK : B_NO_INIT;
65 // Perform
66 status_t
67 RemoveTransformersCommand::Perform()
69 status_t ret = B_OK;
71 // remove shapes from container
72 for (int32 i = 0; i < fCount; i++) {
73 if (fTransformers[i]
74 && !fContainer->RemoveTransformer(fTransformers[i])) {
75 ret = B_ERROR;
76 break;
79 fTransformersRemoved = true;
81 return ret;
84 // Undo
85 status_t
86 RemoveTransformersCommand::Undo()
88 status_t ret = B_OK;
90 // add shapes to container at remembered indices
91 for (int32 i = 0; i < fCount; i++) {
92 if (fTransformers[i]
93 && !fContainer->AddTransformer(fTransformers[i], fIndices[i])) {
94 ret = B_ERROR;
95 break;
98 fTransformersRemoved = false;
100 return ret;
103 // GetName
104 void
105 RemoveTransformersCommand::GetName(BString& name)
107 // if (fCount > 1)
108 // name << _GetString(MOVE_TRANSFORMERS, "Move Transformers");
109 // else
110 // name << _GetString(MOVE_TRANSFORMER, "Move Transformer");
111 if (fCount > 1)
112 name << B_TRANSLATE("Remove Transformers");
113 else
114 name << B_TRANSLATE("Remove Transformer");