Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
filemanage Save Error
devilsclaw:
since i have wx with debugging I get certain errors which i have no problem getting since they should eventually get fixed.
in filemanager.cpp
--- Code: --- if(f.Write(mark, mark_length) != mark_length)
return false;
--- End code ---
this code causes an error.
I did a message box and the mark_length is 0
the error is caused by the f.write no biggy i can make a proper if statement to fix it but what im confused about is
!= mark_length
this means if it has nothing it return false. on my system that would mean i would not ever be able to save anything.
i changed it to this.
--- Code: --- if(mark_length)
{
f.Write(mark, mark_length);
}
--- End code ---
but im not sure if its supposed to return false or not and what would need to be change in order to have it write the mark header correctly
if it really should be returning if mark_length is 0
so you dont have to look at the code in the file here is whats above it
--- Code: --- char* mark = NULL;
size_t mark_length = 0;
if (bom)
{
switch (encoding)
{
case wxFONTENCODING_UTF8:
mark = "\xEF\xBB\xBF";
mark_length = 3;
break;
case wxFONTENCODING_UTF16BE:
mark = "\xFE\xFF";
mark_length = 2;
break;
case wxFONTENCODING_UTF16LE:
mark = "\xFF\xFE";
mark_length = 2;
break;
case wxFONTENCODING_UTF32BE:
mark = "\x00\x00\xFE\xFF";
mark_length = 4;
break;
case wxFONTENCODING_UTF32LE:
mark = "\xFF\xFE\x00\x00";
mark_length = 4;
break;
case wxFONTENCODING_SYSTEM:
default:
break;
}
}
if(f.Write(mark, mark_length) != mark_length)
return false;
--- End code ---
TDragon:
Looking at the context of the function in question, it's clear that mark_length will be 0 when none of the cases in the preceding switch statement are matched. It would seem appropriate, in that case, not to write a BOM -- putting the f.Write() call inside of "if (mark_length) {}" makes sense.
Without even looking up the documentation for the Write() function, it's apparent that it returns the number of bytes that were successfully written. Therefore, the original code is checking that the correct number of bytes were written and, if not, returning false to indicate that an error occurred.
So an appropriate solution in this case would probably be to change
--- Code: ---if(f.Write(mark, mark_length) != mark_length)
return false;
--- End code ---
to
--- Code: ---if (mark_length > 0)
{
if(f.Write(mark, mark_length) != mark_length)
return false;
}
--- End code ---
devilsclaw:
--- Code: ---wxFile::Write
size_t Write(const void* buffer, wxFileOffset count)
Writes the specified number of bytes from a buffer.
Parameters:
buffer - A buffer containing the data.
count - The number of bytes to write.
Return value:
the number of bytes actually written
--- End code ---
so that would mean that the mark_length does not get changed by the write and it is the return that tells you how much it wrote
so im guessing the code should look like this
--- Code: ---if (mark_length > 0)
{
if(!f.Write(mark, mark_length) )
return false;
}
--- End code ---
TDragon:
--- Quote from: devilsclaw on March 30, 2007, 11:47:22 pm ---so im guessing the code should look like this
--- Code: ---if (mark_length > 0)
{
if(!f.Write(mark, mark_length) )
return false;
}
--- End code ---
--- End quote ---
This incorrectly assumes that any nonzero value means the write was successful. wxFile::Write might return a number greater than 0 but less than mark_length. The original code would correctly detect, in that case, that the write wasn't fully successful, and return false.
devilsclaw:
yeah that is true.. i guess it could write less or more then it should depending..
Navigation
[0] Message Index
[#] Next page
Go to full version