Skip to content

Commit

Permalink
Refactor TemplateFileStream class using TemplateStream
Browse files Browse the repository at this point in the history
DataSourceStream
* [BC] Amend StreamType enumeration eSST_TemplateFile -> eSST_Template
* [BC] Make getStreamType() method const
* [BC] make id() method const
* Add virtual getName() method to provide generic way of getting filename from TemplateStream wrapper
* Add virtual isValid() method

TemplateFileStream
* Refactor as TemplateStream class to allow any other stream type to provide source template data
* Replace int with unsigned or size_t as appropriate
* Minor code improvements

HttpRequest
* setFile() method can use any ReadWriteStream-derived class, doesn't have to be FileStream.
  • Loading branch information
mikee47 committed Oct 23, 2018
1 parent d824789 commit ebc8e76
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 133 deletions.
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
34 changes: 34 additions & 0 deletions Sming/SmingCore/Data/Stream/TemplateFileStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/anakod/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
****/

#ifndef _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_
#define _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_

#include "FileStream.h"
#include "TemplateStream.h"

/**
* @brief Template File stream class
* @ingroup stream data
*
* @{
*/

class TemplateFileStream : public TemplateStream
{
public:
/** @brief Create a template file stream
* @param templateFileName Template filename
*/
TemplateFileStream(const String& fileName) : TemplateStream(new FileStream(fileName))
{
}
};

/** @} */

#endif /* _SMING_CORE_DATA_TEMPLATE_FILE_STREAM_H_ */
Loading

0 comments on commit ebc8e76

Please sign in to comment.