mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-04 02:42:49 -04:00
raise errors properly
This commit is contained in:
parent
0e5cc07f83
commit
5e838f0406
@ -337,19 +337,31 @@ seekpos(streampos pos, ios_openmode which) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
int PandaFileStreamBuf::
|
int PandaFileStreamBuf::
|
||||||
overflow(int ch) {
|
overflow(int ch) {
|
||||||
|
bool okflag = true;
|
||||||
|
|
||||||
size_t n = pptr() - pbase();
|
size_t n = pptr() - pbase();
|
||||||
if (n != 0) {
|
if (n != 0) {
|
||||||
write_chars(pbase(), n);
|
size_t wrote = write_chars(pbase(), n);
|
||||||
pbump(-(int)n);
|
pbump(-(int)wrote);
|
||||||
|
if (wrote != n) {
|
||||||
|
okflag = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch != EOF) {
|
if (okflag && ch != EOF) {
|
||||||
// Store the extra character back in the buffer.
|
if (pptr() != epptr()) {
|
||||||
assert(pptr() != epptr());
|
// Store the extra character back in the buffer.
|
||||||
*(pptr()) = ch;
|
*(pptr()) = ch;
|
||||||
pbump(1);
|
pbump(1);
|
||||||
|
} else {
|
||||||
|
// No room to store ch.
|
||||||
|
okflag = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!okflag) {
|
||||||
|
return EOF;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,9 +375,12 @@ int PandaFileStreamBuf::
|
|||||||
sync() {
|
sync() {
|
||||||
size_t n = pptr() - pbase();
|
size_t n = pptr() - pbase();
|
||||||
|
|
||||||
write_chars(pbase(), n);
|
size_t wrote = write_chars(pbase(), n);
|
||||||
pbump(-(int)n);
|
pbump(-(int)wrote);
|
||||||
|
|
||||||
|
if (n != wrote) {
|
||||||
|
return EOF;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -485,11 +500,11 @@ read_chars(char *start, size_t length) {
|
|||||||
// Description: Outputs the indicated stream of characters to the
|
// Description: Outputs the indicated stream of characters to the
|
||||||
// current file position.
|
// current file position.
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
void PandaFileStreamBuf::
|
size_t PandaFileStreamBuf::
|
||||||
write_chars(const char *start, size_t length) {
|
write_chars(const char *start, size_t length) {
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
// Trivial no-op.
|
// Trivial no-op.
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the read buffer is flushed.
|
// Make sure the read buffer is flushed.
|
||||||
@ -518,7 +533,7 @@ write_chars(const char *start, size_t length) {
|
|||||||
<< "Error writing " << length
|
<< "Error writing " << length
|
||||||
<< " bytes to " << _filename << ", windows error code 0x" << hex
|
<< " bytes to " << _filename << ", windows error code 0x" << hex
|
||||||
<< error << dec << ". Disk full?\n";
|
<< error << dec << ". Disk full?\n";
|
||||||
return;
|
return bytes_written;
|
||||||
}
|
}
|
||||||
success = GetOverlappedResult(_handle, &overlapped, &bytes_written, false);
|
success = GetOverlappedResult(_handle, &overlapped, &bytes_written, false);
|
||||||
}
|
}
|
||||||
@ -531,26 +546,29 @@ write_chars(const char *start, size_t length) {
|
|||||||
if (lseek(_fd, _ppos, SEEK_SET) == -1) {
|
if (lseek(_fd, _ppos, SEEK_SET) == -1) {
|
||||||
cerr
|
cerr
|
||||||
<< "Error seeking to position " << _ppos << " in " << _filename << "\n";
|
<< "Error seeking to position " << _ppos << " in " << _filename << "\n";
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (length > 0) {
|
size_t remaining = length;
|
||||||
ssize_t result = ::write(_fd, start, length);
|
while (remaining > 0) {
|
||||||
|
ssize_t result = ::write(_fd, start, remaining);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
if (errno == EAGAIN) {
|
if (errno == EAGAIN) {
|
||||||
thread_yield();
|
thread_yield();
|
||||||
} else {
|
} else {
|
||||||
cerr
|
cerr
|
||||||
<< "Error writing " << length << " bytes to " << _filename << "\n";
|
<< "Error writing " << remaining << " bytes to " << _filename << "\n";
|
||||||
return;
|
return length - remaining;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
start += result;
|
start += result;
|
||||||
length -= result;
|
remaining -= result;
|
||||||
_ppos += result;
|
_ppos += result;
|
||||||
}
|
}
|
||||||
#endif // _WIN32
|
#endif // _WIN32
|
||||||
|
|
||||||
|
return length;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
size_t read_chars(char *start, size_t length);
|
size_t read_chars(char *start, size_t length);
|
||||||
void write_chars(const char *start, size_t length);
|
size_t write_chars(const char *start, size_t length);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string _filename;
|
string _filename;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user