-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedFileWriter.cpp
More file actions
136 lines (122 loc) · 4.23 KB
/
Copy pathBufferedFileWriter.cpp
File metadata and controls
136 lines (122 loc) · 4.23 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/****************************************************************************
* FILENAME: BufferedFileWriter.cpp
* Copyright (c) 2020 EmbedHead Design; All Rights Reserved
* PURPOSE: Boost file write performance by adding buffering.
* DEPENDENCIES, LIMITATIONS, & DESIGN NOTES:
* See .h file.
****************************************************************************/
#include "BufferedFileWriter.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "debugIO.h"
#include "FS.h"
BufferedFileWriter::BufferedFileWriter(void)
{
bytesWrittenTotal = 0;
file = NULL;
writeEndPtr = buff + sizeof(buff);
clear();
}
BufferedFileWriter::~BufferedFileWriter(void)
{
if (NULL != file) {
flush();
file = NULL;
}
}
// Connect to (opened for write) file. Clear buffer.
// Must NOT reset bytes written count (see file header), because code may close and
// re-open (append to) the same file in order to force an update of the directory entry.
void BufferedFileWriter::setFile(FS_FILE *_file)
{
file = _file;
clear();
}
// Return number of bytes in the buffer.
size_t BufferedFileWriter::bufferCount(void)
{
return (size_t)(writePtr - buff);
}
void BufferedFileWriter::resetBytesWrittenTotal(void)
{
bytesWrittenTotal = 0;
}
// Return total bytes written (including bytes still residing in buffer, not yet flushed
// to file) since initialization or last clear().
size_t BufferedFileWriter::getBytesWrittenTotal(void)
{
return bytesWrittenTotal;
}
// Clear buffer before first use, or to reinitialize.
// Must NOT zero the total count of bytes written (see file header comments).
// May be called repeatedly.
void BufferedFileWriter::clear()
{
writePtr = buff;
}
// Flush write buffer to disk.
// After the last write, call flush().
// Returns FS_FWrite() return code, or WriteFail if setFile() was not called with a non-NULL file pointer.
uint32_t BufferedFileWriter::flush(void)
{
uint32_t retval = 0;
if (NULL == file) {
retval = WriteNoFile;
} else if (writePtr > buff) {
setDebug4(true);
retval = FS_FWrite(buff, (size_t)(writePtr - buff), 1, file);
setDebug4(false);
writePtr = buff;
}
return retval;
}
// Write data to disk buffer with specified length (handles binary).
// When the disk buffer is full, flush disk buffer to disk.
// After the last write, user must call flush().
// Returns FS_FWrite() return code if buffer is flushed, 0 otherwise.
uint32_t BufferedFileWriter::write(const char *source, // Buffer of data to write to disk buffer / disk
size_t nChars) // Count of bytes just written to buffer.
{
uint32_t retval = 0;
if (NULL == file) {
retval = WriteNoFile;
} else {
while (nChars > 0) {
*writePtr++ = *source++;
--nChars;
++bytesWrittenTotal;
// If full: flush to disk and set write pointer back to beginning of buff.
if (writePtr >= writeEndPtr) {
retval = flush();
}
}
}
return retval;
}
// Write string to disk buffer, length from strlen(). Writes 0 bytes for NULL string.
// When the disk buffer is full, flush disk buffer to disk.
// After the last write, user must call flush().
// If buffer is flushed, returns FS_FWrite() return code (or 0xffff if setFile() was not called with a
// non-NULL file pointer), 0 otherwise.
uint32_t BufferedFileWriter::writeStr(const char *string)
{
uint32_t retval = 0;
if (NULL != string) {
retval = write(string, strlen(string));
}
return retval;
}
// Logging-style printf formatting into disk buffer via fixed-length line buffer.
// Writes 0 bytes for NULL fmt.
// When the disk buffer is full, flush disk buffer to disk.
// After the last write, user must call flush().
// Returns true on success (includes file has been set), false if unable to write or file has not been set.
bool BufferedFileWriter::logPrintf(const char *fmt, ...)
{
va_list vl;
va_start(vl, fmt);
int nChars = vsnprintf(lineBuff, LineBuffSize, fmt, vl);
va_end(vl);
write(lineBuff, nChars);
}