Quote plugin caused crash if the file was empty. Fixed.

This commit is contained in:
Kimmo Pekkola 2009-07-27 20:06:07 +00:00
parent 1dbfc85b01
commit b5618bfb92

View File

@ -245,154 +245,157 @@ double Update2(UINT id)
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
int size = ftell(file); int size = ftell(file);
// Go to a random place if (size > 0)
int pos = rand() % size;
fseek(file, (pos / 2) * 2, SEEK_SET);
qData.value.erase();
if (0xFEFF == *(WCHAR*)buffer)
{ {
// It's unicode // Go to a random place
WCHAR* wBuffer = (WCHAR*)buffer; int pos = rand() % size;
fseek(file, (pos / 2) * 2, SEEK_SET);
// Read until we find the first separator qData.value.erase();
WCHAR* sepPos1 = NULL;
WCHAR* sepPos2 = NULL; if (0xFEFF == *(WCHAR*)buffer)
do
{ {
size_t len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file); // It's unicode
buffer[len] = 0; WCHAR* wBuffer = (WCHAR*)buffer;
buffer[len + 1] = 0;
sepPos1 = wcsstr(wBuffer, qData.separator.c_str()); // Read until we find the first separator
if (sepPos1 == NULL) WCHAR* sepPos1 = NULL;
WCHAR* sepPos2 = NULL;
do
{ {
// The separator wasn't found size_t len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
if (feof(file)) buffer[len] = 0;
{ buffer[len + 1] = 0;
// End of file reached -> read from start
fseek(file, 2, SEEK_SET);
len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
buffer[len] = 0;
buffer[len + 1] = 0;
sepPos1 = wBuffer;
}
// else continue reading
}
else
{
sepPos1 += qData.separator.size();
}
}
while (sepPos1 == NULL);
// Find the second separator sepPos1 = wcsstr(wBuffer, qData.separator.c_str());
do if (sepPos1 == NULL)
{
sepPos2 = wcsstr(sepPos1, qData.separator.c_str());
if (sepPos2 == NULL)
{
// The separator wasn't found
if (feof(file))
{ {
// End of file reached -> read the rest // The separator wasn't found
qData.value += sepPos1; if (feof(file))
break; {
// End of file reached -> read from start
fseek(file, 2, SEEK_SET);
len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
buffer[len] = 0;
buffer[len + 1] = 0;
sepPos1 = wBuffer;
}
// else continue reading
} }
else else
{ {
qData.value += sepPos1; sepPos1 += qData.separator.size();
// else continue reading
size_t len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
buffer[len] = 0;
buffer[len + 1] = 0;
sepPos1 = wBuffer;
} }
} }
else while (sepPos1 == NULL);
// Find the second separator
do
{ {
if (sepPos2) sepPos2 = wcsstr(sepPos1, qData.separator.c_str());
if (sepPos2 == NULL)
{ {
*sepPos2 = 0; // The separator wasn't found
} if (feof(file))
{
// End of file reached -> read the rest
qData.value += sepPos1;
break;
}
else
{
qData.value += sepPos1;
// Read until we find the second separator // else continue reading
qData.value += sepPos1; size_t len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
} buffer[len] = 0;
} buffer[len + 1] = 0;
while (sepPos2 == NULL); sepPos1 = wBuffer;
} }
else
{
// It's ascii
char* aBuffer = (char*)buffer;
// Read until we find the first separator
char* sepPos1 = NULL;
char* sepPos2 = NULL;
do
{
size_t len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
aBuffer[len] = 0;
sepPos1 = strstr(aBuffer, ConvertToAscii(qData.separator.c_str()).c_str());
if (sepPos1 == NULL)
{
// The separator wasn't found
if (feof(file))
{
// End of file reached -> read from start
fseek(file, 0, SEEK_SET);
len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
aBuffer[len] = 0;
sepPos1 = aBuffer;
}
// else continue reading
}
else
{
sepPos1 += qData.separator.size();
}
}
while (sepPos1 == NULL);
// Find the second separator
do
{
sepPos2 = strstr(sepPos1, ConvertToAscii(qData.separator.c_str()).c_str());
if (sepPos2 == NULL)
{
// The separator wasn't found
if (feof(file))
{
// End of file reached -> read the rest
qData.value += ConvertToWide(sepPos1);
break;
} }
else else
{ {
qData.value += ConvertToWide(sepPos1); if (sepPos2)
{
*sepPos2 = 0;
}
// else continue reading // Read until we find the second separator
size_t len = fread(buffer, sizeof(char), BUFFER_SIZE, file); qData.value += sepPos1;
aBuffer[len] = 0;
sepPos1 = aBuffer;
} }
} }
else while (sepPos2 == NULL);
{ }
if (sepPos2) else
{ {
*sepPos2 = 0; // It's ascii
} char* aBuffer = (char*)buffer;
// Read until we find the second separator // Read until we find the first separator
qData.value += ConvertToWide(sepPos1); char* sepPos1 = NULL;
} char* sepPos2 = NULL;
do
{
size_t len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
aBuffer[len] = 0;
sepPos1 = strstr(aBuffer, ConvertToAscii(qData.separator.c_str()).c_str());
if (sepPos1 == NULL)
{
// The separator wasn't found
if (feof(file))
{
// End of file reached -> read from start
fseek(file, 0, SEEK_SET);
len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
aBuffer[len] = 0;
sepPos1 = aBuffer;
}
// else continue reading
}
else
{
sepPos1 += qData.separator.size();
}
}
while (sepPos1 == NULL);
// Find the second separator
do
{
sepPos2 = strstr(sepPos1, ConvertToAscii(qData.separator.c_str()).c_str());
if (sepPos2 == NULL)
{
// The separator wasn't found
if (feof(file))
{
// End of file reached -> read the rest
qData.value += ConvertToWide(sepPos1);
break;
}
else
{
qData.value += ConvertToWide(sepPos1);
// else continue reading
size_t len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
aBuffer[len] = 0;
sepPos1 = aBuffer;
}
}
else
{
if (sepPos2)
{
*sepPos2 = 0;
}
// Read until we find the second separator
qData.value += ConvertToWide(sepPos1);
}
}
while (sepPos2 == NULL);
} }
while (sepPos2 == NULL);
} }
fclose(file); fclose(file);