4 #include "FTPBasicAPI.h"
21 bool autoClose =
true) {
22 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile", name);
23 auto_close = autoClose;
24 if (name !=
nullptr) file_name = name;
26 this->api_ptr = api_ptr;
30 if (auto_close) close();
33 size_t write(uint8_t data) {
34 if (!is_open)
return 0;
35 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"write");
36 if (mode == READ_MODE) {
37 FTPLogger::writeLog(LOG_ERROR,
"FTPFile",
"Cannot write in READ_MODE");
40 Stream *result_ptr = api_ptr->write(file_name.c_str(), mode);
41 return result_ptr->write(data);
44 size_t write(
const uint8_t *data,
size_t len)
override {
45 if (!is_open)
return 0;
46 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"write");
47 if (mode == READ_MODE) {
48 FTPLogger::writeLog(LOG_ERROR,
"FTPFile",
"Cannot write in READ_MODE");
51 Stream *result_ptr = api_ptr->write(file_name.c_str(), mode);
52 return result_ptr->write(data, len);
55 size_t write(
const char *data,
int len) {
56 return write((
const uint8_t *)data, len);
60 if (!is_open)
return -1;
61 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"read");
62 Stream *result_ptr = api_ptr->read(file_name.c_str());
63 return result_ptr->read();
66 size_t readBytes(
char *buf,
size_t nbyte) {
67 return readBytes((uint8_t *)buf, nbyte);
70 size_t readBytes(uint8_t *buf,
size_t nbyte) {
71 if (!is_open)
return 0;
72 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"readBytes");
73 memset(buf, 0, nbyte);
74 Stream *result_ptr = api_ptr->read(file_name.c_str());
75 return result_ptr->readBytes((
char *)buf, nbyte);
78 size_t readln(
char *buf,
size_t nbyte) {
79 if (!is_open)
return 0;
80 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"readln");
81 memset(buf, 0, nbyte);
82 Stream *result_ptr = api_ptr->read(file_name.c_str());
83 return result_ptr->readBytesUntil(eol[0], (
char *)buf, nbyte);
87 if (!is_open)
return -1;
88 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"peek");
89 Stream *result_ptr = api_ptr->read(file_name.c_str());
90 return result_ptr->peek();
94 if (api_ptr->currentOperation() == IS_EOF)
return 0;
95 if (!is_open)
return 0;
98 Stream *result_ptr = api_ptr->read(file_name.c_str());
99 int len = result_ptr->available();
100 sprintf(msg,
"available: %d", len);
101 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile", msg);
106 if (!is_open)
return;
107 if (api_ptr->currentOperation() == WRITE_OP) {
108 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"flush");
113 void reopen() { is_open =
true; }
117 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"close");
118 if (api_ptr->currentOperation() == WRITE_OP) {
120 api_ptr->data_ptr->stop();
121 const char *ok[] = {
"226",
"250",
nullptr};
122 api_ptr->checkResult(ok,
"close-write",
true);
123 }
else if (api_ptr->currentOperation() == READ_OP) {
125 api_ptr->data_ptr->stop();
126 const char *ok[] = {
"226",
"250",
nullptr};
127 api_ptr->checkResult(ok,
"close-read",
true);
129 api_ptr->setCurrentOperation(NOP);
136 FTPLogger::writeLog(LOG_INFO,
"FTPFile",
"cancel");
137 bool result = api_ptr->abort();
144 const char *name()
const {
return file_name.c_str(); }
146 size_t size()
const {
147 if (!is_open)
return 0;
149 size_t size = api_ptr->size(file_name.c_str());
150 sprintf(msg,
"size: %ld", size);
151 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile", msg);
155 void setEOL(
char *eol) {
156 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"setEOL");
160 bool isDirectory()
const {
161 if (!is_open)
return false;
162 FTPLogger::writeLog(LOG_DEBUG,
"FTPFile",
"isDirectory");
163 return api_ptr->objectType(file_name.c_str()) == TypeDirectory;
166 operator bool() {
return is_open && file_name.length() > 0; }
170 const char *eol =
"\n";
173 ObjectType object_type = TypeUndefined;
175 bool auto_close =
false;
FTPBasicAPI Implementation of Low Level FTP protocol. In order to simplify the logic we always use Pa...
Definition: FTPBasicAPI.h:17
FTPFile A single file which supports read and write operations. This class is implemented as an Ardui...
Definition: FTPFile.h:16