[Scummvm-cvs-logs] SF.net SVN: scummvm:[42291] tools/branches/gsoc2009-gui

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Thu Jul 9 17:22:38 CEST 2009


Revision: 42291
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42291&view=rev
Author:   fingolfin
Date:     2009-07-09 15:22:38 +0000 (Thu, 09 Jul 2009)

Log Message:
-----------
Make File::read and File::write more like fread/fwrite by returning the number of bytes read/written; added a File:pos() method; fixed doxygen comments (i.e., added lots of missing periods)

Modified Paths:
--------------
    tools/branches/gsoc2009-gui/util.cpp
    tools/branches/gsoc2009-gui/util.h

Modified: tools/branches/gsoc2009-gui/util.cpp
===================================================================
--- tools/branches/gsoc2009-gui/util.cpp	2009-07-09 15:17:45 UTC (rev 42290)
+++ tools/branches/gsoc2009-gui/util.cpp	2009-07-09 15:22:38 UTC (rev 42291)
@@ -400,7 +400,7 @@
 	return ret;
 }
 
-void File::read(void *data, size_t elementSize, size_t elementCount) {
+size_t File::read(void *data, size_t elementSize, size_t elementCount) {
 	if (!_file) 
 		throw FileException("File is not open");
 	if ((_mode & FILEMODE_READ) == 0)
@@ -409,6 +409,8 @@
 	size_t data_read = fread(data, elementSize, elementCount, _file);
 	if (data_read != elementCount)
 		throw FileException("Read beyond the end of file (" + _name.getFullPath() + ")");
+
+	return data_read;
 }
 
 void File::writeByte(uint8 b) {
@@ -447,7 +449,7 @@
 	writeByte((uint8)(value >> 24));
 }
 
-void File::write(const void *data, size_t elementSize, size_t elementCount) {
+size_t File::write(const void *data, size_t elementSize, size_t elementCount) {
 	if (!_file) 
 		throw FileException("File is not open");
 	if ((_mode & FILEMODE_WRITE) == 0)
@@ -456,6 +458,8 @@
 	size_t data_read = fwrite(data, elementSize, elementCount, _file);
 	if (data_read != elementCount)
 		throw FileException("Could not write to file (" + _name.getFullPath() + ")");
+
+	return data_read;
 }
 
 void File::seek(long offset, int origin) {
@@ -466,12 +470,16 @@
 		throw FileException("Could not seek in file (" + _name.getFullPath() + ")");
 }
 
+int File::pos() {
+	return ftell(_file);
+}
+
 uint32 File::size() {
 	uint32 sz;
-	uint32 pos = ftell(_file);
+	uint32 p = ftell(_file);
 	fseek(_file, 0, SEEK_END);
 	sz = ftell(_file);
-	fseek(_file, pos, SEEK_SET);
+	fseek(_file, p, SEEK_SET);
 	return sz;
 }
 

Modified: tools/branches/gsoc2009-gui/util.h
===================================================================
--- tools/branches/gsoc2009-gui/util.h	2009-07-09 15:17:45 UTC (rev 42290)
+++ tools/branches/gsoc2009-gui/util.h	2009-07-09 15:22:38 UTC (rev 42291)
@@ -328,14 +328,15 @@
 };
 
 /**
- * A basic wrapper around the FILE class
+ * A basic wrapper around the FILE class.
  * Offers functionality to write words easily, and deallocates the FILE
  * automatically on destruction.
  */
 class File {
 public:
 	/**
-	 * Opens the given file path as an in/out stream, depending on the second argument
+	 * Opens the given file path as an in/out stream, depending on the
+	 * second argument.
 	 * File is always opened in binary mode
 	 *
 	 * @param filename The file to open
@@ -344,13 +345,14 @@
 	File(const Filename &filename, FileMode mode);
 	File(const Filename &filename, const char *mode);
 	/**
-	 * Create an empty file, used for two-step construction
+	 * Create an empty file, used for two-step construction.
 	 */
 	File();
 	~File();
 
 	/**
-	 * Opens the given file path as an in/out stream, depending on the second argument
+	 * Opens the given file path as an in/out stream, depending on the
+	 * second argument.
 	 *
 	 * @param filename The file to open
 	 * @param mode The mode to open the file in
@@ -365,84 +367,88 @@
 
 	/**
 	 * Sets the xor mode of the file, bytes written / read to the file
-	 * will be XORed with this value. this value is *not* reset when opening a new file
-	 * Only works for write* and read* operation, not for the array "read" and "write" methods
+	 * will be XORed with this value. This value is *not* reset when
+	 * opening a new file.
+	 * Only works for write* and read* operation, not for the array
+	 * "read" and "write" methods
 	 */
 	void setXorMode(uint8 xormode);
 	
 	/**
-	 * Read a single unsigned byte
-	 * Throws FileException if file is not open / if read failed ended prematurely
+	 * Read a single unsigned byte.
+	 * Throws FileException if file is not open / if read failed.
 	 */
 	uint8 readByte();
 	/**
-	 * Read a single 16-bit word, big endian
-	 * Throws FileException if file is not open / if read failed
+	 * Read a single 16-bit word, big endian.
+	 * Throws FileException if file is not open / if read failed.
 	 */
 	uint16 readU16BE();
 	/**
-	 * Read a single 16-bit word, little endian
-	 * Throws FileException if file is not open / if read failed
+	 * Read a single 16-bit word, little endian.
+	 * Throws FileException if file is not open / if read failed.
 	 */
 	uint16 readU16LE();
 	/**
-	 * Read a single 32-bit word, big endian
-	 * Throws FileException if file is not open / if read failed
+	 * Read a single 32-bit word, big endian.
+	 * Throws FileException if file is not open / if read failed.
 	 */
 	uint32 readU32BE();
 	/**
-	 * Read a single 32-bit word, little endian
-	 * Throws FileException if file is not open / if read failed
+	 * Read a single 32-bit word, little endian.
+	 * Throws FileException if file is not open / if read failed.
 	 */
 	uint32 readU32LE();
 
 	/**
-	 * Works the same way as fread, but throws on error or if it could not read all elements
+	 * Works the same way as fread, but throws on error or if it could
+	 * not read all elements.
 	 *
 	 * @param data Where to put the read data
 	 * @param elementSize the size of one element (in bytes)
 	 * @param elementCount the number of elements to read
 	 */
-	void read(void *data, size_t elementSize, size_t elementCount);
+	size_t read(void *data, size_t elementSize, size_t elementCount);
 
 	
 	/**
-	 * Writes a single byte to the file
-	 * Throws FileException if file is not open / if write failed
+	 * Writes a single byte to the file.
+	 * Throws FileException if file is not open / if write failed.
 	 */
 	void writeByte(uint8 b);
 	/**
-	 * Writes a single 16-bit word to the file, big endian
-	 * Throws FileException if file is not open / if write failed
+	 * Writes a single 16-bit word to the file, big endian.
+	 * Throws FileException if file is not open / if write failed.
 	 */
 	void writeU16BE(uint16 value);
 	/**
-	 * Writes a single 16-bit word to the file, little endian
-	 * Throws FileException if file is not open / if write failed
+	 * Writes a single 16-bit word to the file, little endian.
+	 * Throws FileException if file is not open / if write failed.
 	 */
 	void writeU16LE(uint16 value);
 	/**
-	 * Writes a single 32-bit word to the file, big endian
-	 * Throws FileException if file is not open / if write failed
+	 * Writes a single 32-bit word to the file, big endian.
+	 * Throws FileException if file is not open / if write failed.
 	 */
 	void writeU32BE(uint32 value);
 	/**
-	 * Writes a single 32-bit word to the file, little endian
-	 * Throws FileException if file is not open / if write failed
+	 * Writes a single 32-bit word to the file, little endian.
+	 * Throws FileException if file is not open / if write failed.
 	 */
 	void writeU32LE(uint32 value);
 
 	/**
-	 * Works the same way as fwrite, but throws on error or if it could not write all data
+	 * Works the same way as fwrite, but throws on error or if
+	 * it could not write all data.
 	 *
 	 * @param data Where to read data from
 	 * @param elementSize the size of one element (in bytes)
 	 * @param elementCount the number of elements to read
 	 */
-	void write(const void *data, size_t elementSize, size_t elementCount);
+	size_t write(const void *data, size_t elementSize, size_t elementCount);
 
 	/**
-	 * Seek to the specified position in the stream
+	 * Seek to the specified position in the stream.
 	 *
 	 * @param offset how many bytes to jump
 	 * @param origin SEEK_SET, SEEK_CUR or SEEK_END
@@ -450,21 +456,29 @@
 	void seek(long offset, int origin);
 
 	/**
-	 * Returns the length of the file, in bytes, does not move the cursor
+	 * Returns the position in the stream.
 	 */
+	int pos();
+
+	/**
+	 * Returns the length of the file, in bytes, does not move the cursor.
+	 */
 	uint32 size();
 
-	/** We implicitly convert into a FILE, so we can use fread() etc. directly */
+	/**
+	 * We implicitly convert into a FILE, so we can use fread() etc. directly.
+	 * @todo get rid of this ASAP
+	 */
 	operator FILE *() {return _file;}
 
 protected:
-	/** The mode the file was opened in */
+	/** The mode the file was opened in. */
 	FileMode _mode;
-	/** Internal reference to the file */
+	/** Internal reference to the file. */
 	FILE *_file;
-	/** The name of the file, used for better error messages */
+	/** The name of the file, used for better error messages. */
 	Filename _name;
-	/** xor with this value while reading/writing (default 0), does not work for "read"/"write", only for byte operations */
+	/** xor with this value while reading/writing (default 0), does not work for "read"/"write", only for byte operations. */
 	uint8 _xormode;
 };
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list