Closed tom-seddon closed 6 years ago
Merging for now, but I think I should investigate further to understand what's happening. Why did you need this fix?
OK, I understand now what you mean.
You mean: I save "" and I load back "\n".
I'll see what I can do...
It should work now. This is my test code, that I leave here for future reference (I put it at the start of my main(...) method):
# define PERFORM_SERIALIZATION_TEST_AND_EXIT
# ifdef PERFORM_SERIALIZATION_TEST_AND_EXIT
{
const char* fileName = "serializationTest.txt";
struct Test {
char *s1,*s2,*s3;
int i;
static char* Strdup(char *& dst,const char *str) {
Strdel(dst);if (!str) return dst;
size_t len = strlen(str) + 1;
dst = (char*) ImGui::MemAlloc(len);
memcpy(dst, (const void*)str, len);
return dst;
}
static void Strdel(char*& str) {if (str) {ImGui::MemFree(str);str=NULL;}}
Test(const char* str1=NULL,const char* str2=NULL,const char* str3=NULL,int num=0) : s1(NULL),s2(NULL),s3(NULL),i(num) {resetVariables(str1,str2,str3,num);}
void resetVariables(const char* str1=NULL,const char* str2=NULL,const char* str3=NULL,int num=0) {Strdup(s1,str1);Strdup(s2,str2);Strdup(s3,str3);i=num;}
~Test() {Strdel(s1);Strdel(s2);Strdel(s3);}
bool serialize(const char* filename) const {
ImGuiHelper::Serializer s(filename);
if (!s.isValid()) return false;
bool rv = true;
rv&=s.save(s1,"s1");
rv&=s.save(s2,"s2");
rv&=s.save(s3,"s3"); // Please note that here if for example s3==NULL, rv = false and s3 is not serialized at all
rv&=s.save(&i,"i");
return rv;
}
static bool ParseCallback(ImGui::FieldType ft,int numArrayElements,void* pValue,const char* name,void* userPtr) {
Test& t = *((Test*)userPtr);
if (strcmp(name,"s1")==0) Strdup(t.s1,(const char*)pValue);
else if (strcmp(name,"s2")==0) Strdup(t.s2,(const char*)pValue);
else if (strcmp(name,"s3")==0) Strdup(t.s3,(const char*)pValue);
else if (strcmp(name,"i")==0) {t.i = *((const int*)pValue);return true;}
return false;
}
bool deserialize(const char* filename) {
resetVariables();
ImGuiHelper::Deserializer d(filename);
if (!d.isValid()) return false;
d.parse(&ParseCallback,this);
return true;
}
void display() const {
printf("s1=\"%s\" (len=%lu)\n",s1 ? s1 : "NULL",s1 ? strlen(s1) : 0);
printf("s2=\"%s\" (len=%lu)\n",s2 ? s2 : "NULL",s2 ? strlen(s2) : 0);
printf("s3=\"%s\" (len=%lu)\n",s3 ? s3 : "NULL",s3 ? strlen(s3) : 0);
printf("i =%d\n",i);
printf("\n");
}
};
Test test("","Test",NULL,3); // Basically now s3 is NULL and it won't be serialized.
test.display();
test.serialize(fileName);
test.deserialize(fileName);
test.display();
return 0; // This is just because I inject this code at the start of my main() function
}
# endif //PERFORM_SERIALIZATION_TEST_AND_EXIT
Basically now empty strings should work, but NULL strings don't work (but they return false). Hope it's good enough, and I've not broken anything...
I assume this is a bug in the serializer? - anyway, I don't have a good way of testing anything other than the dock code, so I'm afraid I decided to go for the quick and dirty dock-specific fix here.
Thanks,
--Tom