Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TemplateFileStream class using TemplateStream #1500

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sming/Libraries/ArduCAM/ArduCAMStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ArduCAMStream: public ReadWriteStream {
ArduCAMStream(ArduCAM *cam);
virtual ~ArduCAMStream();

virtual StreamType getStreamType() { return eSST_User; }
virtual StreamType getStreamType() const { return eSST_User; }

virtual uint16_t readMemoryBlock(char* data, int bufSize);
virtual bool seek(int len);
Expand Down
4 changes: 2 additions & 2 deletions Sming/SmingCore/Data/CircularBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CircularBuffer : public ReadWriteStream
* @retval StreamType The stream type.
* @todo Return value of IDataSourceStream:getStreamType base class function should be of type StreamType, e.g. eSST_User
*/
virtual StreamType getStreamType()
virtual StreamType getStreamType() const
{
return StreamType::eSST_Memory;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ class CircularBuffer : public ReadWriteStream
* @brief Returns unique id of the resource.
* @retval String the unique id of the stream.
*/
virtual String id()
virtual String id() const
{
return String(reinterpret_cast<uint32_t>(&buffer), HEX);
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/MailMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ MailMessage& MailMessage::addAttachment(FileStream* stream)
return *this;
}

String filename = stream->fileName();
String filename = stream->getName();
String mime = ContentType::fromFullFileName(filename);

return addAttachment(stream, mime, filename);
Expand Down
39 changes: 30 additions & 9 deletions Sming/SmingCore/Data/Stream/DataSourceStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
* @{
*/
enum StreamType {
eSST_Memory, ///< Memory data stream
eSST_File, ///< File data stream
eSST_TemplateFile, ///< Template file data stream
eSST_JsonObject, ///< JSON object data stream
eSST_User, ///< User defined data stream
eSST_Unknown ///< Unknown data stream type
eSST_Invalid, ///< Stream content not valid
eSST_Memory, ///< Memory data stream
eSST_File, ///< File data stream
eSST_Template, ///< Template data stream
eSST_JsonObject, ///< JSON object data stream
eSST_User, ///< User defined data stream
eSST_Unknown ///< Unknown data stream type
};
/** @} */

Expand All @@ -43,7 +44,17 @@ class IDataSourceStream : public Stream
* @retval StreamType The stream type.
* @todo Return value of IDataSourceStream:getStreamType base class function should be of type StreamType, e.g. eSST_User
*/
virtual StreamType getStreamType() = 0;
virtual StreamType getStreamType() const = 0;

/** @brief Determine if the stream object contains valid data
* @retval bool true if valid, false if invalid
* @note Where inherited classes are initialised by constructor this method indicates
* whether that was successful or not (e.g. FileStream)
*/
virtual bool isValid() const
{
return getStreamType() != eSST_Invalid;
}

/** @brief Read a block of memory
* @param data Pointer to the data to be read
Expand All @@ -61,7 +72,7 @@ class IDataSourceStream : public Stream

/**
* @brief Read a character without advancing the stream pointer
* @retval The character that was read or -1 if none is available
* @retval int The character that was read or -1 if none is available
*/
virtual int peek();

Expand Down Expand Up @@ -116,7 +127,17 @@ class IDataSourceStream : public Stream
* @brief Returns unique id of the resource.
* @retval String the unique id of the stream.
*/
virtual String id()
virtual String id() const
{
return nullptr;
}

/**
* @brief Returns name of the resource.
* @retval String
* @note Commonly used to obtain name of file
*/
virtual String getName() const
{
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/EndlessMemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EndlessMemoryStream : public ReadWriteStream
delete stream;
}

virtual StreamType getStreamType()
virtual StreamType getStreamType() const
{
return eSST_Memory;
}
Expand Down
12 changes: 7 additions & 5 deletions Sming/SmingCore/Data/Stream/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ bool FileStream::isFinished()
return fileIsEOF(handle);
}

String FileStream::fileName()
String FileStream::fileName() const
{
spiffs_stat stat;
fileStats(handle, &stat);
return String((char*)stat.name);
if(fileStats(handle, &stat) < 0)
return nullptr;
else
return String((char*)stat.name);
}

bool FileStream::fileExist()
bool FileStream::fileExist() const
{
return size != -1;
}

String FileStream::id()
String FileStream::id() const
{
spiffs_stat stat;
fileStats(handle, &stat);
Expand Down
17 changes: 13 additions & 4 deletions Sming/SmingCore/Data/Stream/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FileStream : public ReadWriteStream

virtual bool attach(const String& fileName, FileOpenFlags openFlags);
//Use base class documentation
virtual StreamType getStreamType()
virtual StreamType getStreamType() const
{
return eSST_File;
}
Expand All @@ -47,8 +47,17 @@ class FileStream : public ReadWriteStream
//Use base class documentation
virtual bool isFinished();

String fileName(); ///< Filename of file stream is attached to
bool fileExist(); ///< True if file exists
String fileName() const; ///< Filename of file stream is attached to
bool fileExist() const; ///< True if file exists
virtual String getName() const
{
return fileName();
}

virtual bool isValid() const
{
return fileExist();
}

/** @brief Get the offset of cursor from beginning of data
* @retval int Cursor offset
Expand All @@ -67,7 +76,7 @@ class FileStream : public ReadWriteStream
return size;
}

virtual String id();
virtual String id() const;

private:
file_t handle;
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/JsonObjectStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class JsonObjectStream : public MemoryDataStream
}

//Use base class documentation
virtual StreamType getStreamType()
virtual StreamType getStreamType() const
{
return eSST_JsonObject;
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/LimitedMemoryStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LimitedMemoryStream::~LimitedMemoryStream()
delete[] buffer;
}

StreamType LimitedMemoryStream::getStreamType()
StreamType LimitedMemoryStream::getStreamType() const
{
return eSST_Memory;
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/LimitedMemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LimitedMemoryStream : public ReadWriteStream
virtual ~LimitedMemoryStream();

//Use base class documentation
virtual StreamType getStreamType();
virtual StreamType getStreamType() const;

/**
* @brief Return the total length of the stream
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/MemoryDataStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MemoryDataStream : public ReadWriteStream
}

//Use base class documentation
virtual StreamType getStreamType()
virtual StreamType getStreamType() const
{
return eSST_Memory;
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Data/Stream/MultipartStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MultipartStream : public ReadWriteStream
virtual ~MultipartStream();

//Use base class documentation
virtual StreamType getStreamType()
virtual StreamType getStreamType() const
{
// TODO: fix this...
return stream->getStreamType();
Expand Down
124 changes: 0 additions & 124 deletions Sming/SmingCore/Data/Stream/TemplateFileStream.cpp

This file was deleted.

Loading