ObjectBuffer.h
Go to the documentation of this file.
1 /****
2  * ObjectBuffer.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the IFS Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #pragma once
21 
23 #include "../include/IFS/FWFS/Object.h"
24 
25 namespace IFS
26 {
27 namespace FWFS
28 {
32 class ObjectBuffer
33 {
34 public:
35  void write(const void* data, size_t size)
36  {
37  auto n = mem.write(static_cast<const uint8_t*>(data), size);
38  (void)n;
39  assert(n == size);
40  }
41 
42  void write(const String& value)
43  {
44  write(value.c_str(), value.length());
45  }
46 
47  void write(const NameBuffer& value)
48  {
49  write(value.c_str(), value.length);
50  }
51 
52  void write(uint32_t value)
53  {
54  return write(&value, sizeof(value));
55  }
56 
57  void write(const Object& hdr, size_t extra, size_t bodySize)
58  {
59  size_t headerSize = hdr.contentOffset() + extra;
60  if(bodySize != 0) {
61  mem.ensureCapacity(mem.getSize() + headerSize + bodySize);
62  }
63  write(&hdr, headerSize);
64  }
65 
66  void writeRef(Object::Type type, Object::ID objId)
67  {
68  Object hdr;
69  hdr.setType(type, true);
70  hdr.data8.ref.packedOffset = objId;
71  size_t idSize = (objId <= 0xff) ? 1 : (objId <= 0xffff) ? 2 : (objId <= 0xffffff) ? 3 : 4;
72  hdr.data8.setContentSize(idSize);
73  write(hdr, idSize, 0);
74  }
75 
76  Object::Type writeDataHeader(size_t size)
77  {
78  Object hdr;
79  if(size <= 0xff) {
80  hdr.setType(Object::Type::Data8);
81  hdr.data8.setContentSize(size);
82  } else if(size <= 0xffff) {
83  hdr.setType(Object::Type::Data16);
84  hdr.data16.setContentSize(size);
85  } else {
86  hdr.setType(Object::Type::Data24);
87  hdr.data24.setContentSize(size);
88  }
89  write(hdr, 0, 0);
90  return hdr.type();
91  }
92 
93  void writeNamed(Object::Type type, const char* name, uint8_t namelen, TimeStamp mtime)
94  {
95  Object hdr;
96  hdr.setType(type);
97  hdr.data16.named.namelen = namelen;
98  hdr.data16.named.mtime = mtime;
99  write(hdr, hdr.data16.named.nameOffset(), namelen);
100  write(name, namelen);
101  }
102 
103  void fixupSize()
104  {
105  auto objptr = mem.getStreamPointer();
106  Object hdr;
107  memcpy(&hdr, objptr, 4);
109  memcpy(const_cast<char*>(objptr), &hdr, 4);
110  }
111 
112  void clear()
113  {
114  mem.clear();
115  }
116 
117  operator IDataSourceStream*()
118  {
119  return &mem;
120  }
121 
122 private:
123  MemoryDataStream mem;
124 };
125 
126 } // namespace FWFS
127 } // namespace IFS
Base class for read-only stream.
Definition: DataSourceStream.h:45
Read/write stream using expandable memory buffer.
Definition: MemoryDataStream.h:26
size_t write(const uint8_t *buffer, size_t size) override
Write chars to end of stream.
Manage IFS timestamps stored as an unsigned 32-bit value.
Definition: TimeStamp.h:53
The String class.
Definition: WString.h:136
size_t getSize() const
Definition: MemoryDataStream.h:126
size_t contentOffset() const
return offset to start of object content
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:522
uint32_t ID
Object identifier (offset from start of image)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:290
struct IFS::FWFS::Object::@26::@28 data8
Definition: DirectoryTemplate.h:36
void clear()
Clear data from stream and reset to start, but keep buffer allocated.
Definition: MemoryDataStream.h:109
void writeRef(Object::Type type, Object::ID objId)
Definition: ObjectBuffer.h:117
struct IFS::FWFS::Object::@26::@29 data16
void write(const void *data, size_t size)
Definition: ObjectBuffer.h:86
struct IFS::FWFS::Object::@26::@30 data24
Type
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:292
Type type() const
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:327
Object::Type writeDataHeader(size_t size)
Definition: ObjectBuffer.h:127
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
void setContentSize(size_t size)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:548
void clear()
Definition: ObjectBuffer.h:163
const char * getStreamPointer() const
Get a pointer to the current position.
Definition: MemoryDataStream.h:59
void fixupSize()
Definition: ObjectBuffer.h:154
int available() override
Return the total length of the stream.
Definition: MemoryDataStream.h:68
void writeNamed(Object::Type type, const char *name, uint8_t namelen, TimeStamp mtime)
Definition: ObjectBuffer.h:144
void setType(Type type, bool isRef=false)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:332
Object structure.
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:284
bool ensureCapacity(size_t minCapacity)
Pre-allocate stream to given size.
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243