GdbFileStream.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * GdbFileStream.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "ReadWriteStream.h"
14 #include "FileSystem.h"
15 
21 {
22 public:
24  {
25  }
26 
31  GdbFileStream(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly)
32  {
33  open(fileName, openFlags);
34  }
35 
37  {
38  close();
39  }
40 
47  bool open(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly);
48 
51  void close();
52 
53  size_t write(const uint8_t* buffer, size_t size) override;
54 
55  uint16_t readMemoryBlock(char* data, int bufSize) override;
56 
57  int seekFrom(int offset, unsigned origin) override;
58 
59  bool isFinished() override
60  {
61  return pos == size;
62  }
63 
68  {
69  return fileName;
70  }
71 
75  bool fileExist() const
76  {
77  return handle >= 0;
78  }
79 
80  String getName() const override
81  {
82  return fileName;
83  }
84 
85  bool isValid() const override
86  {
87  return fileExist();
88  }
89 
93  size_t getPos() const
94  {
95  return pos;
96  }
97 
101  int available() override
102  {
103  return size - pos;
104  }
105 
106  String id() const override;
107 
112  {
113  return lastError;
114  }
115 
116 private:
121  bool check(int res)
122  {
123  if(res >= 0) {
124  return true;
125  }
126 
127  if(lastError >= 0) {
128  lastError = res;
129  }
130  return false;
131  }
132 
133 private:
134  String fileName;
135  int handle = -1;
136  size_t pos = 0;
137  size_t size = 0;
138  int lastError = 0;
139 };
GdbFileStream(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly)
Create a file stream.
Definition: GdbFileStream.h:31
bool isFinished() override
Check if all data has been read.
Definition: GdbFileStream.h:59
bool isValid() const override
Determine if the stream object contains valid data.
Definition: GdbFileStream.h:85
GDB File stream class to provide access to host files whilst running under debugger.
Definition: GdbFileStream.h:20
bool fileExist() const
Determine if file exists.
Definition: GdbFileStream.h:75
void close()
Close file.
The String class.
Definition: WString.h:136
size_t write(const uint8_t *buffer, size_t size) override
Write chars to stream.
int seekFrom(int offset, unsigned origin) override
Change position in stream.
~GdbFileStream()
Definition: GdbFileStream.h:36
GdbFileStream()
Definition: GdbFileStream.h:23
Read only file.
Definition: FileSystem.h:26
int getLastError()
determine if an error occurred during operation
Definition: GdbFileStream.h:111
bool open(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly)
Open a file and attach this stream object to it.
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
int available() override
Return the total length of the stream.
Definition: GdbFileStream.h:101
String id() const override
Returns unique id of the resource.
size_t getPos() const
Get the offset of cursor from beginning of data.
Definition: GdbFileStream.h:93
String getFileName() const
Filename of file stream is attached to.
Definition: GdbFileStream.h:67
Base class for read/write stream.
Definition: ReadWriteStream.h:22
String getName() const override
Returns name of the resource.
Definition: GdbFileStream.h:80
FileOpenFlags
File open flags.
Definition: FileSystem.h:25