Sfoglia il codice sorgente

Remove stupid read/write_byte() from streams and fix posix file implementation

Daniele Bartolini 12 anni fa
parent
commit
bb8b26d5f3

+ 4 - 4
src/JSONParser.cpp

@@ -71,7 +71,7 @@ JSONParser::parse()
 	{
 	{
 		JSONType type;
 		JSONType type;
 
 
-		c = (char)m_stream->read_byte();
+		m_stream->read(&c, 1);
 		m_pos = m_stream->position();
 		m_pos = m_stream->position();
 
 
 		switch(c)
 		switch(c)
@@ -200,7 +200,7 @@ JSONParser::parse_string()
 
 
 	while(!m_stream->end_of_stream())
 	while(!m_stream->end_of_stream())
 	{	
 	{	
-		c = (char) m_stream->read_byte();
+		m_stream->read(&c, 1);
 		m_pos = m_stream->position();
 		m_pos = m_stream->position();
 
 
 		if (c == '\"' || c == '\'')
 		if (c == '\"' || c == '\'')
@@ -221,7 +221,7 @@ JSONParser::parse_string()
 
 
 		if (c == '\\')
 		if (c == '\\')
 		{
 		{
-			c = (char)m_stream->read_byte();
+			m_stream->read(&c, 1);
 			m_pos = m_stream->position();
 			m_pos = m_stream->position();
 
 
 			switch(c)
 			switch(c)
@@ -262,7 +262,7 @@ JSONParser::parse_primitive()
 
 
 	while (!m_stream->end_of_stream())
 	while (!m_stream->end_of_stream())
 	{
 	{
-		c = (char)m_stream->read_byte();
+		m_stream->read(&c, 1);
 		m_pos = m_stream->position();
 		m_pos = m_stream->position();
 
 
 		switch (c)
 		switch (c)

+ 0 - 18
src/core/streams/BinaryWriter.cpp

@@ -82,23 +82,5 @@ void BinaryWriter::write_float(float buffer)
 	m_stream.write(&buffer, sizeof(float));
 	m_stream.write(&buffer, sizeof(float));
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void BinaryWriter::insert_byte(int8_t val, size_t offset)
-{
-	size_t tmpSize = m_stream.size() - offset;
-	int8_t* tmp = new int8_t[tmpSize];
-
-	m_stream.seek(offset);
-	m_stream.read(tmp, tmpSize);
-
-	m_stream.seek(offset);
-
-	m_stream.write_byte(val);
-
-	m_stream.write(tmp, tmpSize);
-
-	delete[] tmp;
-}
-
 } // namespace crown
 } // namespace crown
 
 

+ 0 - 2
src/core/streams/BinaryWriter.h

@@ -46,8 +46,6 @@ public:
 	void				write_double(double);
 	void				write_double(double);
 	void				write_float(float);
 	void				write_float(float);
 
 
-	void				insert_byte(int8_t val, size_t offset);
-
 private:
 private:
 
 
 	Stream&				m_stream;
 	Stream&				m_stream;

+ 6 - 38
src/core/streams/FileStream.cpp

@@ -70,7 +70,7 @@ void FileStream::skip(size_t bytes)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-uint8_t FileStream::read_byte()
+void FileStream::read(void* buffer, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 
 
@@ -80,25 +80,21 @@ uint8_t FileStream::read_byte()
 		m_file.seek(0);
 		m_file.seek(0);
 	}
 	}
 
 
-	uint8_t buffer;
-
-	ce_assert(m_file.read(&buffer, 1) == 1, "Failed to read from file");
-
-	return buffer;
+	ce_assert(m_file.read(buffer, size) == size, "Failed to read from file");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void FileStream::read(void* buffer, size_t size)
+void FileStream::write(const void* buffer, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 
 
-	if (!m_last_was_read)
+	if (m_last_was_read)
 	{
 	{
-		m_last_was_read = true;
+		m_last_was_read = false;
 		m_file.seek(0);
 		m_file.seek(0);
 	}
 	}
 
 
-	ce_assert(m_file.read(buffer, size) == size, "Failed to read from file");
+	ce_assert(m_file.write(buffer, size) == size, "Failed to write to file");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -154,34 +150,6 @@ bool FileStream::is_valid() const
 	return m_file.is_open();
 	return m_file.is_open();
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void FileStream::write_byte(uint8_t val)
-{
-	check_valid();
-
-	if (m_last_was_read)
-	{
-		m_last_was_read = false;
-		m_file.seek(0);
-	}
-
-	ce_assert(m_file.write(&val, 1) == 1, "Failed to write to file");
-}
-
-//-----------------------------------------------------------------------------
-void FileStream::write(const void* buffer, size_t size)
-{
-	check_valid();
-
-	if (m_last_was_read)
-	{
-		m_last_was_read = false;
-		m_file.seek(0);
-	}
-
-	ce_assert(m_file.write(buffer, size) == size, "Failed to write to file");
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void FileStream::flush()
 void FileStream::flush()
 {
 {

+ 0 - 6
src/core/streams/FileStream.h

@@ -53,15 +53,9 @@ public:
 	/// @copydoc Stream::skip() 
 	/// @copydoc Stream::skip() 
 	void			skip(size_t bytes);
 	void			skip(size_t bytes);
 
 
-	/// @copydoc Stream::read_byte() 
-	uint8_t			read_byte();
-
 	/// @copydoc Stream::read() 
 	/// @copydoc Stream::read() 
 	void			read(void* buffer, size_t size);
 	void			read(void* buffer, size_t size);
 
 
-	/// @copydoc Stream::write_byte() 
-	void			write_byte(uint8_t val);
-
 	/// @copydoc Stream::write() 
 	/// @copydoc Stream::write() 
 	void			write(const void* buffer, size_t size);
 	void			write(const void* buffer, size_t size);
 
 

+ 0 - 21
src/core/streams/MemoryStream.cpp

@@ -144,19 +144,6 @@ void MemoryStream::skip(size_t bytes)
 	ce_assert(m_memory_offset <= m_memory->size(), "Trying to skip beyond end of stream");
 	ce_assert(m_memory_offset <= m_memory->size(), "Trying to skip beyond end of stream");
 }
 }
 
 
-//-----------------------------------------------------------------------------
-uint8_t MemoryStream::read_byte()
-{
-	check_valid();
-
-	if (m_memory_offset >= m_memory->size())
-	{
-		Log::e("Trying to read beyond the end of stream.");
-	}
-
-	return m_memory->data()[m_memory_offset++];
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void MemoryStream::read(void* buffer, size_t size)
 void MemoryStream::read(void* buffer, size_t size)
 {
 {
@@ -187,14 +174,6 @@ bool MemoryStream::copy_to(Stream& stream, size_t size)
 	return true;
 	return true;
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void MemoryStream::write_byte(uint8_t val)
-{
-	check_valid();
-	m_memory->write(&val, m_memory_offset, 1);
-	m_memory_offset++;
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void MemoryStream::write(const void* buffer, size_t size)
 void MemoryStream::write(const void* buffer, size_t size)
 {
 {

+ 0 - 6
src/core/streams/MemoryStream.h

@@ -99,15 +99,9 @@ public:
 	/// @copydoc Stream::skip()
 	/// @copydoc Stream::skip()
 	void				skip(size_t bytes);
 	void				skip(size_t bytes);
 
 
-	/// @copydoc Stream::read_byte()
-	uint8_t				read_byte();
-
 	/// @copydoc Stream::read()
 	/// @copydoc Stream::read()
 	void				read(void* buffer, size_t size);
 	void				read(void* buffer, size_t size);
 
 
-	/// @copydoc Stream::write_byte()
-	void				write_byte(uint8_t val);
-
 	/// @copydoc Stream::write()
 	/// @copydoc Stream::write()
 	void				write(const void* buffer, size_t size);
 	void				write(const void* buffer, size_t size);
 
 

+ 2 - 13
src/core/streams/NullStream.h

@@ -51,11 +51,6 @@ public:
 
 
 	/// @copydoc Stream::skip()
 	/// @copydoc Stream::skip()
 	void		skip(size_t bytes) { (void)bytes; }
 	void		skip(size_t bytes) { (void)bytes; }
-
-	/// @copydoc Stream::read_byte()
-	/// @note
-	///	Returns always zero
-	uint8_t		read_byte() { return 0; }
 				
 				
 	/// @copydoc Stream::read()
 	/// @copydoc Stream::read()
 	/// @note
 	/// @note
@@ -68,9 +63,6 @@ public:
 		}
 		}
 	}
 	}
 
 
-	/// @copydoc Stream::write_byte()
-	void		write_byte(uint8_t val) { (void)val; }
-
 	/// @copydoc Stream::write()
 	/// @copydoc Stream::write()
 	void		write(const void* buffer, size_t size) { (void)buffer; (void)size; }
 	void		write(const void* buffer, size_t size) { (void)buffer; (void)size; }
 
 
@@ -79,11 +71,8 @@ public:
 	///	Returns always true
 	///	Returns always true
 	bool		copy_to(Stream& stream, size_t size = 0)
 	bool		copy_to(Stream& stream, size_t size = 0)
 	{
 	{
-		for (size_t i = 0; i < size; i++)
-		{
-			stream.write_byte(0);
-		}
-		
+		char zero = 0;
+		stream.write(&zero, size);		
 		return true;
 		return true;
 	}
 	}
 
 

+ 0 - 6
src/core/streams/Stream.h

@@ -64,15 +64,9 @@ public:
 	/// Sets the position indicator to bytes after current position
 	/// Sets the position indicator to bytes after current position
 	virtual void		skip(size_t bytes) = 0;
 	virtual void		skip(size_t bytes) = 0;
 
 
-	/// Reads a byte from the stream starting at current position.
-	virtual uint8_t		read_byte() = 0;
-
 	/// Reads a block of data from the stream.
 	/// Reads a block of data from the stream.
 	virtual void		read(void* buffer, size_t size) = 0;
 	virtual void		read(void* buffer, size_t size) = 0;
 
 
-	/// Writes a byte to the stream starting at current position.
-	virtual void		write_byte(uint8_t val) = 0;
-
 	/// Writes a block of data to the stream.
 	/// Writes a block of data to the stream.
 	virtual void		write(const void* buffer, size_t size) = 0;
 	virtual void		write(const void* buffer, size_t size) = 0;
 
 

+ 10 - 21
src/core/streams/TextReader.cpp

@@ -36,38 +36,27 @@ TextReader::TextReader(Stream& stream) : m_stream(stream)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-char TextReader::read_char()
+size_t TextReader::read_string(char* string, size_t size)
 {
 {
-	return m_stream.read_byte();
-}
-
-//-----------------------------------------------------------------------------
-char* TextReader::read_string(char* string, uint32_t count)
-{
-	char currentChar;
-	uint32_t i = 0;
+	char current_char;
+	size_t bytes_read = 0;
 
 
-	while(!m_stream.end_of_stream() && i < count - 1)
+	while(!m_stream.end_of_stream() && bytes_read < size - 1)
 	{
 	{
-		currentChar = m_stream.read_byte();
-		string[i] = currentChar;
+		m_stream.read(&current_char, 1);
+		string[bytes_read] = current_char;
 
 
-		i++;
+		bytes_read++;
 
 
-		if (currentChar == '\n')
+		if (current_char == '\n')
 		{
 		{
 			break;
 			break;
 		}
 		}
 	}
 	}
 
 
-	if (i == 0)
-	{
-		return NULL;
-	}
-
-	string[i] = '\0';
+	string[bytes_read] = '\0';
 
 
-	return string;
+	return bytes_read;
 }
 }
 
 
 } // namespace crown
 } // namespace crown

+ 2 - 5
src/core/streams/TextReader.h

@@ -37,18 +37,15 @@ public:
 
 
 						TextReader(Stream& s);
 						TextReader(Stream& s);
 
 
-	/// Read a single character from the stream
-	char				read_char();
-
 	/// Reads characters from stream and stores them as a C string
 	/// Reads characters from stream and stores them as a C string
-	/// into string until (count-1) characters have been read or
+	/// into string until (size-1) characters have been read or
 	/// either a newline or the End-of-Stream is reached, whichever
 	/// either a newline or the End-of-Stream is reached, whichever
 	/// comes first.
 	/// comes first.
 	/// A newline character makes fgets stop reading, but it is considered
 	/// A newline character makes fgets stop reading, but it is considered
 	/// a valid character and therefore it is included in the string copied to string.
 	/// a valid character and therefore it is included in the string copied to string.
 	/// A null character is automatically appended in str after the characters read to
 	/// A null character is automatically appended in str after the characters read to
 	/// signal the end of the C string.
 	/// signal the end of the C string.
-	char*				read_string(char* string, uint32_t count);
+	size_t				read_string(char* string, size_t size);
 
 
 private:
 private:
 
 

+ 2 - 13
src/core/streams/TextWriter.cpp

@@ -25,6 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #include "TextWriter.h"
 #include "TextWriter.h"
 #include "Stream.h"
 #include "Stream.h"
+#include "String.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -34,22 +35,10 @@ TextWriter::TextWriter(Stream& stream) : m_stream(stream)
 {
 {
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void TextWriter::write_char(char c)
-{
-	m_stream.write_byte(c);
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void TextWriter::write_string(const char* string)
 void TextWriter::write_string(const char* string)
 {
 {
-	size_t count = 0;
-
-	while(string[count] != '\0')
-	{
-		m_stream.write_byte(string[count]);
-		count++;
-	}
+	m_stream.write(string, string::strlen(string));
 }
 }
 
 
 } // namespace crown
 } // namespace crown

+ 1 - 4
src/core/streams/TextWriter.h

@@ -34,14 +34,11 @@ class TextWriter
 public:
 public:
 
 
 						TextWriter(Stream& s);
 						TextWriter(Stream& s);
-
-	/// Writes a single character to the stream.
-	void				write_char(char c);
 	
 	
 	/// Writes the string pointed by string to the stream.
 	/// Writes the string pointed by string to the stream.
 	/// The function begins copying from the address specified (string)
 	/// The function begins copying from the address specified (string)
 	/// until it reaches the terminating null character ('\0').
 	/// until it reaches the terminating null character ('\0').
-	/// This final null character is not copied to the stream.
+	/// The final null character is not copied to the stream.
 	void				write_string(const char* string);
 	void				write_string(const char* string);
 
 
 private:
 private:

+ 1 - 1
src/os/posix/File.cpp

@@ -36,7 +36,7 @@ namespace crown
 File::File(const char* path, StreamOpenMode mode) :
 File::File(const char* path, StreamOpenMode mode) :
 	m_file_handle(NULL)
 	m_file_handle(NULL)
 {
 {
-	m_file_handle = fopen(path, SOM_READ ? "rb" : "wb");
+	m_file_handle = fopen(path, (mode == SOM_READ) ? "rb" : "wb");
 	ce_assert(m_file_handle != NULL, "Unable to open file: %s", path);
 	ce_assert(m_file_handle != NULL, "Unable to open file: %s", path);
 
 
 	m_mode = mode;
 	m_mode = mode;