summaryrefslogtreecommitdiff
path: root/ucoo/base/fs/romfs/romfs.hh
blob: 203e596c82d494a346f9c35bd1653bd7fa753ba4 (plain)
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
#ifndef ucoo_base_fs_romfs_romfs_hh
#define ucoo_base_fs_romfs_romfs_hh
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/intf/file_system.hh"
#include "ucoo/utils/pool.hh"
#include "ucoo/common.hh"

namespace ucoo {

/// Read only file system, read data from a buffer.
///
/// Buffer structure, little endian:
///  u32: 'RomF'
///  u32: number of files
///  u32[n]: indexes in data buffer of file names, sorted by file names
///  u32[n]: indexes in data buffer of file content
///  u32: index in data buffer to first unused byte
///  char[][n]: file names, not zero terminated
///  char[][n]: file contents
class RomFS : public FileSystem
{
    /// Stream from a romfs.
    class RomFSStream : public Stream
    {
      public:
        /// Constructor from a memory chunk.
        RomFSStream (const char *begin, const char *end);
        /// See Stream::read.
        int read (char *buf, int count) override;
        /// See Stream::write.
        int write (const char *buf, int count) override;
        /// See Stream::poll.
        int poll () override;
      private:
        const char *begin_, *end_;
    };
  public:
    /// Constructor, takes romfs address and size.
    RomFS (const uint32_t *addr, int size);
    /// See FileSystem::open.
    Stream *open (const char *filename, Mode mode, Error &error) override;
    Stream *open (const char *filename, Mode mode = Mode::READ)
    {
        return FileSystem::open (filename, mode);
    }
    /// See FileSystem::close.
    void close (Stream *file) override;
    /// See FileSystem::unlink, this is a no-op.
    void unlink (const char *filename) override;
  private:
    /// Number of files.
    int files_count_;
    /// File names indexes.
    const uint32_t *filenames_;
    /// File contents indexes.
    const uint32_t *filecontents_;
    /// Byte array with file names and contents.
    const char *data_;
    /// Pool of stream.
    Pool<RomFSStream, 5> pool_;
};

} // namespace ucoo

#endif // ucoo_base_fs_romfs_romfs_hh