Fluid: Fix unique id reader.

This commit is contained in:
Matthias Melcher
2025-11-29 12:38:55 +01:00
parent 92a1558a0e
commit 725be0116f
2 changed files with 11 additions and 4 deletions
+10 -4
View File
@@ -699,7 +699,7 @@ void Node::add(Node *anchor, Strategy strategy) {
{ // make sure that we have no duplicate uid's
Node *tp = this;
do {
tp->set_uid(tp->uid_);
tp->ensure_unique_uid();
tp = tp->next;
} while (tp!=end && tp!=nullptr);
}
@@ -752,7 +752,7 @@ void Node::insert(Node *g) {
{ // make sure that we have no duplicate uid's
Node *tp = this;
do {
tp->set_uid(tp->uid_);
tp->ensure_unique_uid();
tp = tp->next;
} while (tp!=end && tp!=nullptr);
}
@@ -951,7 +951,7 @@ void Node::read_property(fld::io::Project_Reader &f, const char *c) {
const char *hex = f.read_word();
int x = 0;
if (hex)
x = sscanf(hex, "%04x", &x);
sscanf(hex, "%04x", &x); // defaults x to 0 if format fails
set_uid(x);
} else if (!strcmp(c,"label"))
label(f.read_word());
@@ -1265,21 +1265,27 @@ void Node::write_code2(fld::io::Code_Writer&) {
until we find one that is unique.
\param[in] suggested_uid the preferred uid for this node
\return the actualt uid that was given to the node
\return the actual uid that was given to the node
*/
unsigned short Node::set_uid(unsigned short suggested_uid) {
// if there is no suggestion, come up with a random number
if (suggested_uid==0)
suggested_uid = (unsigned short)rand();
// loop until we find a unique number
for (;;) {
// loop through every node in the project
Node *tp = Fluid.proj.tree.first;
for ( ; tp; tp = tp->next) {
// abort if the suggested id is already taken
if (tp!=this && tp->uid_==suggested_uid) {
break;
}
}
// we are done if we have not fund the suggested id anywhere else
if (tp==nullptr) {
break;
}
// try again with another random number
suggested_uid = (unsigned short)rand();
}
uid_ = suggested_uid;
+1
View File
@@ -307,6 +307,7 @@ public:
int has_function(const char*, const char*) const;
unsigned short set_uid(unsigned short suggested_uid=0);
unsigned short ensure_unique_uid() { return set_uid(uid_); }
unsigned short get_uid() { return uid_; }
};