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::FWFS
26 {
31 {
32 public:
33  void write(const void* data, size_t size)
34  {
35  auto n = mem.write(static_cast<const uint8_t*>(data), size);
36  (void)n;
37  assert(n == size);
38  }
39 
40  void write(const String& value)
41  {
42  write(value.c_str(), value.length());
43  }
44 
45  void write(const NameBuffer& value)
46  {
47  write(value.c_str(), value.length);
48  }
49 
50  void write(uint32_t value)
51  {
52  return write(&value, sizeof(value));
53  }
54 
55  void write(const Object& hdr, size_t extra, size_t bodySize)
56  {
57  size_t headerSize = hdr.contentOffset() + extra;
58  if(bodySize != 0) {
59  mem.ensureCapacity(mem.getSize() + headerSize + bodySize);
60  }
61  write(&hdr, headerSize);
62  }
63 
64  void writeRef(Object::Type type, Object::ID objId)
65  {
66  Object hdr;
67  hdr.setType(type, true);
68  hdr.data8.ref.packedOffset = objId;
69  size_t idSize = (objId <= 0xff) ? 1 : (objId <= 0xffff) ? 2 : (objId <= 0xffffff) ? 3 : 4;
70  hdr.data8.setContentSize(idSize);
71  write(hdr, idSize, 0);
72  }
73 
75  {
76  Object hdr;
77  if(size <= 0xff) {
78  hdr.setType(Object::Type::Data8);
79  hdr.data8.setContentSize(size);
80  } else if(size <= 0xffff) {
81  hdr.setType(Object::Type::Data16);
82  hdr.data16.setContentSize(size);
83  } else {
84  hdr.setType(Object::Type::Data24);
85  hdr.data24.setContentSize(size);
86  }
87  write(hdr, 0, 0);
88  return hdr.type();
89  }
90 
91  void writeNamed(Object::Type type, const char* name, uint8_t namelen, TimeStamp mtime)
92  {
93  Object hdr;
94  hdr.setType(type);
95  hdr.data16.named.namelen = namelen;
96  hdr.data16.named.mtime = mtime;
97  write(hdr, hdr.data16.named.nameOffset(), namelen);
98  write(name, namelen);
99  }
100 
101  void fixupSize()
102  {
103  auto objptr = mem.getStreamPointer();
104  Object hdr;
105  memcpy(&hdr, objptr, 4);
106  hdr.setContentSize(mem.available() - hdr.contentOffset());
107  memcpy(const_cast<char*>(objptr), &hdr, 4);
108  }
109 
110  void clear()
111  {
112  mem.clear();
113  }
114 
115  operator IDataSourceStream*()
116  {
117  return &mem;
118  }
119 
120 private:
121  MemoryDataStream mem;
122 };
123 
124 } // namespace IFS::FWFS
Base class for read-only stream.
Definition: DataSourceStream.h:46
Class to manage writing object data into a stream.
Definition: ObjectBuffer.h:31
Object::Type writeDataHeader(size_t size)
Definition: ObjectBuffer.h:74
void write(uint32_t value)
Definition: ObjectBuffer.h:50
void fixupSize()
Definition: ObjectBuffer.h:101
void write(const String &value)
Definition: ObjectBuffer.h:40
void write(const Object &hdr, size_t extra, size_t bodySize)
Definition: ObjectBuffer.h:55
void write(const NameBuffer &value)
Definition: ObjectBuffer.h:45
void clear()
Definition: ObjectBuffer.h:110
void writeRef(Object::Type type, Object::ID objId)
Definition: ObjectBuffer.h:64
void writeNamed(Object::Type type, const char *name, uint8_t namelen, TimeStamp mtime)
Definition: ObjectBuffer.h:91
void write(const void *data, size_t size)
Definition: ObjectBuffer.h:33
Read/write stream using expandable memory buffer.
Definition: MemoryDataStream.h:27
size_t getSize() const
Definition: MemoryDataStream.h:118
int available() override
Return the total length of the stream.
Definition: MemoryDataStream.h:60
void clear()
Clear data from stream and reset to start, but keep buffer allocated.
Definition: MemoryDataStream.h:101
bool ensureCapacity(size_t minCapacity)
Pre-allocate stream to given size.
const char * getStreamPointer() const
Get a pointer to the current position.
Definition: MemoryDataStream.h:51
size_t write(const uint8_t *buffer, size_t size) override
Write chars to end of stream.
The String class.
Definition: WString.h:133
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:609
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:238
Definition: Components/IFS/src/include/IFS/FWFS/ArchiveStream.h:28
Object structure.
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:142
void setContentSize(size_t size)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:406
Type type() const
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:185
struct IFS::FWFS::Object::@26::@28 data8
struct IFS::FWFS::Object::@26::@29 data16
struct IFS::FWFS::Object::@26::@30 data24
Type
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:150
void setType(Type type, bool isRef=false)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:190
uint32_t ID
Object identifier (offset from start of image)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:148
size_t contentOffset() const
return offset to start of object content
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:380
defines a 'safe' name buffer
Definition: NameBuffer.h:44
uint16_t length
OUT: length of name.
Definition: NameBuffer.h:47
const char * c_str() const
Definition: NameBuffer.h:72
Manage IFS timestamps stored as an unsigned 32-bit value.
Definition: TimeStamp.h:37