mirror of
https://github.com/fltk/fltk.git
synced 2026-05-24 00:06:20 +08:00
Fluid: Fix unique id reader.
This commit is contained in:
+10
-4
@@ -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;
|
||||
|
||||
@@ -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_; }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user