# HG changeset patch # User Matti Hamalainen # Date 1317009655 -10800 # Node ID d554e443475d3cf7e3e0bb92f24410cdb2120123 # Parent 334cf83fd5762bcdb92f89116e3b9588d85f69bb Un-inline some member functions for easier debugging. Other smaller cleanups. diff -r 334cf83fd576 -r d554e443475d src/wadfile.cc --- a/src/wadfile.cc Mon Sep 26 07:00:05 2011 +0300 +++ b/src/wadfile.cc Mon Sep 26 07:00:55 2011 +0300 @@ -36,21 +36,15 @@ */ Wad_file::~Wad_file() { - if (directory != NULL) - { - FreeMemory(directory); - directory = NULL; // Catch bugs - } + FreeMemory(directory); + directory = NULL; + FreeMemory(filename); + filename = NULL; // Catch bugs if (fp != NULL) { fclose(fp); fp = NULL; // Catch bugs } - if (filename != NULL) - { - FreeMemory(filename); - filename = NULL; // Catch bugs - } } @@ -118,3 +112,186 @@ } return bytes_read_total; } + +/* + * Wad_file::error - tell whether any errors occurred + * + * Reset the error indicator and call clearerr() on the + * underlying stdio stream. Thus calling Wad_file::error() + * again immediately after always returns false. Calling + * this function is the only way to clear the error flag of + * a Wad_file. + * + * So short that it's a good candidate for inlining. + * + * Return true if an error occurred, false otherwise. + */ +bool Wad_file::error() const +{ + if (!error_) + return false; + + clearerr(fp); + error_ = false; + return true; +} + + +/* + * Wad_file::seek - move the file pointer + * + * If an error occurs, set the error flag. + */ +void Wad_file::seek(long offset) const +{ + if (fseek(fp, offset, 0) != 0) + { + if (!error_) + err("%s: can't seek to %lXh", filename, offset); + error_ = true; + } +} + + +/* + * Wad_file::read_u8 - read a byte + * + * If an error occurs, set the error flag and the return + * value is undefined. + */ +u8 Wad_file::read_u8() const +{ + u8 v = u8(getc(fp)); + + if (feof(fp) || ferror(fp)) + { + if (!error_) + err("%s: read error", where()); + error_ = true; + } + return v; +} + + +/* + * Wad_file::read_u8 - read a byte + * + * If an error occurs, set the error flag and the contents + * of buf is undefined. + */ +void Wad_file::read_u8(u8 & buf) const +{ + buf = getc(fp); + + if (feof(fp) || ferror(fp)) + { + if (!error_) + err("%s: read error", where()); + error_ = true; + } +} + + +/* + * Wad_file::read_i16 - read a little-endian 16-bit signed integer + * + * If an error occurs, set the error flag and the return + * value is undefined. + */ +i16 Wad_file::read_i16() const +{ + const size_t nbytes = 2; + u8 buf[nbytes]; + + if (fread(buf, 1, nbytes, fp) != nbytes) + { + if (!error_) + err("%s: read error", where()); + error_ = true; + return EOF; // Whatever + } + return buf[0] | buf[1] << 8; +} + + +/* + * Wad_file::read_i16 - read a little-endian 16-bit signed integer + * + * The value read is stored in *buf. If an error occurs, + * set the error flag and the contents of *buf is undefined. + */ +void Wad_file::read_i16(i16 * buf) const +{ + *buf = getc(fp) | (getc(fp) << 8); + + if (feof(fp) || ferror(fp)) + { + if (!error_) + err("%s: read error", where()); + error_ = true; + } +} + + +/* + * Wad_file::read_i32 - read little-endian 32-bit signed integers + * + * Read little-endian 32-bit signed integers from + * wad file into *buf. If an error occurs, set + * error_ and the contents of *buf is undefined. + */ +void Wad_file::read_i32(i32 * buf, long count) const +{ + while (count-- > 0) + { + *buf++ = getc(fp) + | (getc(fp) << 8) + | ((i32) getc(fp) << 16) | ((i32) getc(fp) << 24); + } + + if (feof(fp) || ferror(fp)) + { + if (!error_) + err("%s: read error", where()); + error_ = true; + } +} + + +/* + * Wad_file::read_bytes - read bytes from a wad file + * + * Read bytes and store them into buffer . + * is _not_ limited to size_t. If an I/O error + * occurs or EOF is reached before the requested number of + * bytes is read, set the error flag. + */ +void Wad_file::read_bytes(void *buf, long count) const +{ + long bytes_read; + + bytes_read = read_vbytes(buf, count); + if (bytes_read != count) + { + if (!error_) + err("%s: read error", where()); + error_ = true; + } +} + + +/* + * Wad_file::what - what a wad contains + * + * Written for the sake of the "w" command. Return the + * name of the first lump in the wad, which gives an idea + * of what it contains. The string is *not* NUL-terminated. + */ +const char *Wad_file::what() const +{ + if (directory == 0) + return "(nodir)"; + if (dirsize < 1) + return "(empty)"; + return directory[0].name; +} diff -r 334cf83fd576 -r d554e443475d src/wadfile.h --- a/src/wadfile.h Mon Sep 26 07:00:05 2011 +0300 +++ b/src/wadfile.h Mon Sep 26 07:00:55 2011 +0300 @@ -99,17 +99,31 @@ friend void SaveEntryToRawFile(FILE *, const char *); friend void ListFileDirectory(FILE *, const Wad_file *); - public:Wad_file():filename(0), pic_format_(YGPF_NORMAL), - fp(0), dirsize(0), dirstart(0), - directory(0), error_(false) + public:Wad_file() { + filename = NULL; + fp = NULL; + pic_format_ = YGPF_NORMAL; + dirsize = 0; + dirstart = 0; + directory = NULL; strcpy(type, "BUG"); strcpy(where_, "DEADBEEF"); + error_ = false; } ~Wad_file(); - const char *pathname() const; - ygpf_t pic_format() const; + + inline const char *pathname() const + { + return filename; + } + + inline ygpf_t pic_format() const + { + return pic_format_; + } + bool error() const; const char *where() const; void seek(long offset) const; @@ -137,207 +151,4 @@ Wad_file & operator=(const Wad_file & rhs); // Deliberately not implemented }; - -/* - * Wad_file::pathname - return the pathname of the file - */ -inline const char *Wad_file::pathname() const -{ - return filename; -} - - -/* - * Wad_file::pic_format - return the pic_format of the wad - */ -inline ygpf_t Wad_file::pic_format() const -{ - return pic_format_; -} - - -/* - * Wad_file::error - tell whether any errors occurred - * - * Reset the error indicator and call clearerr() on the - * underlying stdio stream. Thus calling Wad_file::error() - * again immediately after always returns false. Calling - * this function is the only way to clear the error flag of - * a Wad_file. - * - * So short that it's a good candidate for inlining. - * - * Return true if an error occurred, false otherwise. - */ -inline bool Wad_file::error() const -{ - if (!error_) - return false; - - clearerr(fp); - error_ = false; - return true; -} - - -/* - * Wad_file::seek - move the file pointer - * - * If an error occurs, set the error flag. - */ -inline void Wad_file::seek(long offset) const -{ - if (fseek(fp, offset, 0) != 0) - { - if (!error_) - err("%s: can't seek to %lXh", filename, offset); - error_ = true; - } -} - - -/* - * Wad_file::read_u8 - read a byte - * - * If an error occurs, set the error flag and the return - * value is undefined. - */ -inline u8 Wad_file::read_u8() const -{ - u8 v = u8(getc(fp)); - - if (feof(fp) || ferror(fp)) - { - if (!error_) - err("%s: read error", where()); - error_ = true; - } - return v; -} - - -/* - * Wad_file::read_u8 - read a byte - * - * If an error occurs, set the error flag and the contents - * of buf is undefined. - */ -inline void Wad_file::read_u8(u8 & buf) const -{ - buf = getc(fp); - - if (feof(fp) || ferror(fp)) - { - if (!error_) - err("%s: read error", where()); - error_ = true; - } -} - - -/* - * Wad_file::read_i16 - read a little-endian 16-bit signed integer - * - * If an error occurs, set the error flag and the return - * value is undefined. - */ -inline i16 Wad_file::read_i16() const -{ - const size_t nbytes = 2; - u8 buf[nbytes]; - - if (fread(buf, 1, nbytes, fp) != nbytes) - { - if (!error_) - err("%s: read error", where()); - error_ = true; - return EOF; // Whatever - } - return buf[0] | buf[1] << 8; -} - - -/* - * Wad_file::read_i16 - read a little-endian 16-bit signed integer - * - * The value read is stored in *buf. If an error occurs, - * set the error flag and the contents of *buf is undefined. - */ -inline void Wad_file::read_i16(i16 * buf) const -{ - *buf = getc(fp) | (getc(fp) << 8); - - if (feof(fp) || ferror(fp)) - { - if (!error_) - err("%s: read error", where()); - error_ = true; - } -} - - -/* - * Wad_file::read_i32 - read little-endian 32-bit signed integers - * - * Read little-endian 32-bit signed integers from - * wad file into *buf. If an error occurs, set - * error_ and the contents of *buf is undefined. - */ -inline void Wad_file::read_i32(i32 * buf, long count) const -{ - while (count-- > 0) - { - *buf++ = getc(fp) - | (getc(fp) << 8) - | ((i32) getc(fp) << 16) | ((i32) getc(fp) << 24); - } - - if (feof(fp) || ferror(fp)) - { - if (!error_) - err("%s: read error", where()); - error_ = true; - } -} - - -/* - * Wad_file::read_bytes - read bytes from a wad file - * - * Read bytes and store them into buffer . - * is _not_ limited to size_t. If an I/O error - * occurs or EOF is reached before the requested number of - * bytes is read, set the error flag. - */ -inline void Wad_file::read_bytes(void *buf, long count) const -{ - long bytes_read; - - bytes_read = read_vbytes(buf, count); - if (bytes_read != count) - { - if (!error_) - err("%s: read error", where()); - error_ = true; - } -} - - -/* - * Wad_file::what - what a wad contains - * - * Written for the sake of the "w" command. Return the - * name of the first lump in the wad, which gives an idea - * of what it contains. The string is *not* NUL-terminated. - */ -inline const char *Wad_file::what() const -{ - if (directory == 0) - return "(nodir)"; - if (dirsize < 1) - return "(empty)"; - return directory[0].name; -} - - #endif /* DO NOT ADD ANYTHING AFTER THIS LINE */