HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
action_set_object_data.cpp
Go to the documentation of this file.
2 #include "gui/gui_globals.h"
4 
5 namespace hal
6 {
7 
9  {}
11 
13  {
14  return new ActionSetObjectData;
15  }
16 
18  : mAct(act), mDataEntry(dataEntry)
19  {;}
20 
21  /*
22  return DataTableEntry::fromContainer(container, mDataEntry.category.toStdString(), mDataEntry.key.toStdString());
23  return DataTableEntry::fromContainer(container, mOldCat.toStdString(), mOldKey.toStdString());
24  */
25 
27  {
28  if (mDataEntry.isNull()) return false;
29 
30  //for now, this action only supports setting the data of gates, modules and nets (DataContainer)
31  DataContainer* container = nullptr;
32  switch(mObject.type())
33  {
35  container = dynamic_cast<DataContainer*>(gNetlist->get_gate_by_id(mObject.id()));
36  break;
38  container = dynamic_cast<DataContainer*>(gNetlist->get_module_by_id(mObject.id()));
39  break;
41  container = dynamic_cast<DataContainer*>(gNetlist->get_net_by_id(mObject.id()));
42  break;
43  default:
44  return false;
45  }
46 
47  ActionSetObjectData* undoAction = nullptr; // undo action will allways be of same type as action
48 
49  std::string searchCategory = (mAct == MoveAction) ? mOldCat.toStdString() : mDataEntry.category.toStdString();
50  std::string searchKey = (mAct == MoveAction) ? mOldKey.toStdString() : mDataEntry.key.toStdString();
51  DataTableEntry currentEntry = DataTableEntry::fromContainer(container, searchCategory, searchKey);
52 
53  //construct appropriate undo action
54  switch (mAct)
55  {
56  case UnknownAction:
57  return false;
58  case DeleteAction:
59  if (currentEntry.isNull()) return false;
60  undoAction = new ActionSetObjectData(CreateAction, currentEntry);
61  break;
62  case CreateAction:
63  undoAction = new ActionSetObjectData(DeleteAction, mDataEntry);
64  break;
65  case ModifyAction:
66  if (currentEntry.isNull())
67  // does not exist yet, it is a create
68  undoAction = new ActionSetObjectData(DeleteAction, mDataEntry);
69  else
70  undoAction = new ActionSetObjectData(ModifyAction, currentEntry);
71  break;
72  case MoveAction:
73  if (currentEntry.isNull()) return false;
74  undoAction = new ActionSetObjectData(MoveAction, currentEntry);
75  undoAction->setChangeKeyAndOrCategory(mDataEntry.category, mDataEntry.key);
76  break;
77  }
78 
79  if (undoAction)
80  {
83  }
84 
85  // modify container
86  switch (mAct)
87  {
88  case DeleteAction:
89  container->delete_data(mDataEntry.category.toStdString(), mDataEntry.key.toStdString());
90  break;
91  case CreateAction:
92  case ModifyAction:
93  mDataEntry.toContainer(container);
94  break;
95  case MoveAction:
96  container->delete_data(mOldCat.toStdString(), mOldKey.toStdString());
97  mDataEntry.toContainer(container);
98  break;
99  default:
100  break;
101  }
102 
104  if (Gate* gate = gNetlist->get_gate_by_id(mObject.id()))
106 
107  return UserAction::exec();
108  }
109 
111  {
113  }
114 
116  {
117  const QStringList actionTypeToString = {"unknown", "delete", "create", "modify", "move"};
118  xmlOut.writeTextElement("action", actionTypeToString[mAct]);
119  xmlOut.writeTextElement("category", mDataEntry.category);
120  xmlOut.writeTextElement("key", mDataEntry.key);
121  xmlOut.writeTextElement("type", mDataEntry.dataType);
122  xmlOut.writeTextElement("value", mDataEntry.value);
123  if(mAct == MoveAction)
124  {
125  xmlOut.writeTextElement("oldCategory", mOldCat);
126  xmlOut.writeTextElement("oldKey", mOldKey);
127  }
128  }
129 
131  {
132  const QStringList actionTypeToString = {"unknown", "delete", "create", "modify", "move"};
133  while(xmlIn.readNextStartElement())
134  {
135  if(xmlIn.name() == QString("category"))
136  mDataEntry.category = xmlIn.readElementText();
137  else if(xmlIn.name() == QString("key"))
138  mDataEntry.key = xmlIn.readElementText();
139  else if(xmlIn.name() == QString("type"))
140  mDataEntry.dataType = xmlIn.readElementText();
141  else if(xmlIn.name() == QString("value"))
142  mDataEntry.value = xmlIn.readElementText();
143  else if(xmlIn.name() == QString("oldCategory"))
144  mOldCat = xmlIn.readElementText();
145  else if(xmlIn.name() == QString("oldKey"))
146  mOldKey = xmlIn.readElementText();
147  else if (xmlIn.name() == QString("action"))
148  {
149  QString tmpAct = xmlIn.readElementText();
150  int i = actionTypeToString.indexOf(tmpAct);
151  mAct = i < 0 ? UnknownAction : (ActionType) i;
152  }
153  }
154  }
155 
157  {
158  char ctype = (char) mAct;
159  cryptoHash.addData(&ctype, 1);
160  cryptoHash.addData("cat", 3);
161  cryptoHash.addData(mDataEntry.category.toUtf8());
162  cryptoHash.addData("key", 3);
163  cryptoHash.addData(mDataEntry.key.toUtf8());
164  cryptoHash.addData("type", 4);
165  cryptoHash.addData(mDataEntry.dataType.toUtf8());
166  cryptoHash.addData("val", 3);
167  cryptoHash.addData(mDataEntry.value.toUtf8());
168  if(mAct == MoveAction)
169  {
170  cryptoHash.addData("oldCat", 6);
171  cryptoHash.addData(mOldCat.toUtf8());
172  cryptoHash.addData("oldKey", 6);
173  cryptoHash.addData(mOldKey.toUtf8());
174  }
175  }
176 
178  {
179  mAct = MoveAction;
180  mOldKey = oldKey;
181  mOldCat = oldCategory;
182  }
183 
184 }
The ActionSetObjectDataFactory class.
static ActionSetObjectDataFactory * sFactory
UserAction * newAction() const override
QString tagname() const override
void writeToXml(QXmlStreamWriter &xmlOut) const override
void setChangeKeyAndOrCategory(QString oldCategory, QString oldKey)
void addToHash(QCryptographicHash &cryptoHash) const override
ActionSetObjectData(ActionType act=UnknownAction, const DataTableEntry &dataEntry=DataTableEntry())
void readFromXml(QXmlStreamReader &xmlIn) override
bool delete_data(const std::string &category, const std::string &key, const bool log_with_info_level=false)
static DataTableEntry fromContainer(DataContainer *container, std::string category, std::string key)
QString dataType
The data's key.
void toContainer(DataContainer *container) const
QString key
The data's category.
QString category
bool isNull() const
QString value
Definition: gate.h:59
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:195
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:615
Net * get_net_by_id(u32 net_id) const
Definition: netlist.cpp:355
void gateBooleanFunctionChanged(Gate *g) const
The UserActionFactory is the abstract base class for registration.
Definition: user_action.h:225
QString tagname() const
Definition: user_action.h:242
The UserAction class is the abstract base class for user interactions.
Definition: user_action.h:57
UserAction * mUndoAction
Definition: user_action.h:186
virtual bool exec()
Definition: user_action.cpp:23
virtual void setObject(const UserActionObject &obj)
Definition: user_action.cpp:32
UserActionObject mObject
Definition: user_action.h:183
UserAction * undoAction() const
Definition: user_action.h:152
UserActionObjectType::ObjectType type() const
Definition: defines.h:45
Netlist * gNetlist
Definition: gui_globals.h:69
NetlistRelay * gNetlistRelay
Definition: gui_globals.h:74
void addData(const char *data, int length)
std::string toStdString() const const
QByteArray toUtf8() const const
int indexOf(QStringView str, int from) const const
QStringRef name() const const
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void writeTextElement(const QString &qualifiedName, const QString &text)