-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQFile.cpp
More file actions
executable file
·106 lines (92 loc) · 3.02 KB
/
QFile.cpp
File metadata and controls
executable file
·106 lines (92 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
///////////////////////////////////////////////////////////////////////////////
// :mode=c:
/* QFile.cpp */
///////////////////////////////////////////////////////////////////////////////
#include "QFile.h"
//#include "Arduino.h"
#include "QTrace.h"
/**************************************************************************************/
// QFile
/**************************************************************************************/
QFile * QFile::_pMasterObject= NULL;
/**************************************************************************************/
QFile::QFile(const char * pFileName)
{
Init();
_pFileName= pFileName;
} // QFile
/**************************************************************************************/
void QFile::Init()
{
if (QFile::_pMasterObject == NULL)
{ // First instance initialization of static items
_pMasterObject= this;
// Mount File System
#ifdef ENB_SPIFFS
bool Result= SPIFFS.begin(); // returns true if successful
#else
bool Result= LittleFS.begin();
#endif
if (!Result)
_Trace.printf(TS_SERVICES, TLT_Error, "Spiffs: could not mount file system");
}
} // Init
/**************************************************************************************/
bool QFile::Exists()
{
#ifdef ENB_SPIFFS
return (SPIFFS.exists(_pFileName));
#else
return (LittleFS.exists(_pFileName));
#endif
} // Exists
/**************************************************************************************/
int QFile::ReadStr(char * pStrBfr, int BfrSize)
{
int Result= 0;
if (this->Exists())
{
#ifdef ENB_SPIFFS
File f= SPIFFS.open(_pFileName, "r");
#else
File f= LittleFS.open(_pFileName, "r");
#endif
//_Trace.printf(TS_SERVICES, TLT_Verbose, "Spiffs File Open for Read:%s", (f != NULL)?("Ok"):("Error"));
if (f != NULL)
{
int i;
for (i= 0 ; (i < f.size()) && (i < (BfrSize-1)) ; i++)
{
pStrBfr[i]= (char) f.read();
}
pStrBfr[i]= 0;
Result= i;
f.close();
#ifdef _QFILE_DEBUG
_Trace.printf(TS_SERVICES, TLT_Verbose, "QFile::ReadStr():%d, %s", Result, pStrBfr);
#endif
}
else
_Trace.printf(TS_SERVICES, TLT_Error, "QFile::ReadStr(): file read error");
}
return Result;
} // ReadStr
/**************************************************************************************/
int QFile::WriteStr(char * pStrBfr)
{
int Result= 0;
#ifdef ENB_SPIFFS
File f= SPIFFS.open(_pFileName, "w");
#else
File f= LittleFS.open(_pFileName, "w");
#endif
//_Trace.printf(TS_SERVICES, TLT_Verbose, "QFile::WriteStr:%s", (f != NULL)?("Ok"):("Error"));
if (f != NULL)
{
f.print(pStrBfr);
f.close();
}
else
_Trace.printf(TS_SERVICES, TLT_Error, "QFile::WriteStr(): file write error");
return Result;
} // WriteStr