ecere/com/dataTypes: Avoid infinite recursion on bit classes holding themselves
[sdk] / ecere / src / com / dataTypes.ec
index 644c40f..5a61add 100644 (file)
@@ -201,13 +201,13 @@ public:
    switch(_class.typeSize)
    {
       case 1:
-         i64Data = !strcmp(_class.dataTypeString, "byte") ? *(byte *)data : *(char *)data;
+         i64Data = !strcmp(_class.dataTypeString, "byte") ? (int64)*(byte *)data : (int64)*(char *)data;
          break;
       case 2:
-         i64Data = !strcmp(_class.dataTypeString, "uint16") ? *(uint16 *)data : *(short *)data;
+         i64Data = !strcmp(_class.dataTypeString, "uint16") ? (int64)*(uint16 *)data : (int64)*(short *)data;
          break;
       case 4:
-         i64Data = !strcmp(_class.dataTypeString, "uint") ? *(uint *)data : *(int *)data;
+         i64Data = !strcmp(_class.dataTypeString, "uint") ? (int64)*(uint *)data : (int64)*(int *)data;
          break;
       case 8:
          i64Data = !strcmp(_class.dataTypeString, "uint64") ? *(int64 *)data : *(int64 *)data;
@@ -222,11 +222,15 @@ public:
    }
    if(item)
    {
-      strcpy(tempString, item.name);
-      if(!needClass || !*needClass)
-         tempString[0] = (char)toupper(tempString[0]);
-      return tempString;
-      //return item.name;
+      if(tempString)
+      {
+         strcpy(tempString, item.name);
+         if(!needClass || !*needClass)
+            tempString[0] = (char)toupper(tempString[0]);
+         return tempString;
+      }
+      else
+         return item.name;
    }
    else
       return null;
@@ -701,7 +705,7 @@ static const char * OnGetString(Class _class, void * data, char * tempString, vo
 
                         //value.ui = (((uint)data & bitMember.mask) >> bitMember.pos);
                         value.ui64 = ((*(uint*)data & bitMember.mask) >> bitMember.pos);
-                        if(value.ui64)
+                        if(value.ui64 && (memberType != _class))  // Avoid infinite recursion on bit classes holding themselves
                         {
                            bool needClass = true;
                            char internalMemberString[1024];
@@ -1090,7 +1094,25 @@ static bool OnGetDataFromString(Class _class, void ** data, const char * string)
                   if(memberType.type == noHeadClass || memberType.type == normalClass || memberType.type == structClass)
                      ((void (*)(void *, void *))(void *)((Property)thisMember).Set)(data, value.p);
                   else
-                     ((void (*)(void *, int))(void *)((Property)thisMember).Set)(data, value.i);
+                  {
+                     // TODO: Complete and improve this type of stuff throughout
+                     if(!strcmp(memberType.dataTypeString, "float"))
+                     {
+                        ((void (*)(void *, float))(void *)((Property)thisMember).Set)(data, value.f);
+                     }
+                     else if(!strcmp(memberType.dataTypeString, "double"))
+                     {
+                        ((void (*)(void *, double))(void *)((Property)thisMember).Set)(data, value.d);
+                     }
+                     else if(!strcmp(memberType.dataTypeString, "int64"))
+                     {
+                        ((void (*)(void *, int64))(void *)((Property)thisMember).Set)(data, value.i64);
+                     }
+                     else
+                     {
+                        ((void (*)(void *, int))(void *)((Property)thisMember).Set)(data, value.i);
+                     }
+                  }
                }
             }
          }
@@ -1996,25 +2018,63 @@ static char * Float_OnGetString(Class _class, float * data, char * string, void
    {
       int c;
       int last = 0;
+      bool checkFor1 = true, checkFor9 = true;
       int numDigits = 7, num = 1;
+      int first9 = 0;
       char format[10];
+      char * dot;
+      int len;
       while(numDigits && num < f) numDigits--, num *= 10;
       sprintf(format, "%%.%df", numDigits);
 
       //sprintf(string, "%f", f);
       sprintf(string, format, f);
+      dot = strchr(string, '.');
 
-      c = strlen(string)-1;
+      len = strlen(string);
+      c = len-1;
       for( ; c >= 0; c--)
       {
-         if(string[c] != '0')
-            last = Max(last, c);
-         if(string[c] == '.')
+         char ch = string[c];
+         if(ch != '0' && dot)
+         {
+            if(ch == '1' && string + c - dot >= 6 && c == len - 1 && checkFor1)
+               checkFor1 = false;
+            else if(ch == '9' && string + c - dot >= 6 && c == len - 1 && checkFor9)
+               first9 = c;
+            else
+            {
+               last = Max(last, c);
+               checkFor9 = false;
+               checkFor1 = false;
+            }
+         }
+         if(ch == '.')
          {
             if(last == c)
                string[c] = 0;
             else
+            {
                string[last+1] = 0;
+               if(first9)
+               {
+                  while(--first9 > 0)
+                  {
+                     if(first9 != c)
+                        if(string[first9] < '9')
+                        {
+                           string[first9]++;
+                           break;
+                        }
+                  }
+                  if(first9 < c)
+                  {
+                     string[c-1] = '1';
+                     first9 = c;
+                  }
+                  string[first9] = 0;
+               }
+            }
             break;
          }
       }