FreeNOS
PseudoFile.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <Assert.h>
19#include <String.h>
20#include <MemoryBlock.h>
21#include "PseudoFile.h"
22
24 : File(inode, FileSystem::RegularFile)
25{
26 m_size = ZERO;
27 m_buffer = ZERO;
29}
30
32 const char *str)
33 : File(inode, FileSystem::RegularFile)
34{
37 m_buffer = new char[m_size + 1];
40}
41
43{
44 if (m_buffer)
45 {
46 delete[] m_buffer;
47 }
48}
49
51 Size & size,
52 const Size offset)
53{
54 // Bounds checking
55 if (offset >= m_size)
56 {
57 size = 0;
59 }
60
61 // How much bytes to copy?
62 const Size bytes = m_size - offset > size ? size : m_size - offset;
63 size = bytes;
64
65 // Copy the buffers
66 return buffer.write(m_buffer + offset, bytes);
67}
68
70 Size & size,
71 const Size offset)
72{
73 // Check for the buffer size
74 if (!m_buffer || m_size < (size + offset))
75 {
76 // Allocate a new buffer and copy the old data
77 char *new_buffer = new char[size+offset];
78 assert(new_buffer != NULL);
79 MemoryBlock::set(new_buffer, 0, sizeof(size+offset));
80
81 // Inherit from the old buffer, if needed
82 if (m_buffer)
83 {
84 MemoryBlock::copy(new_buffer, m_buffer, m_size);
85 delete[] m_buffer;
86 }
87
88 // Assign buffer
89 m_buffer = new_buffer;
90 m_size = size+offset;
91 }
92
93 // Copy the input data in the current buffer
94 return buffer.read(m_buffer + offset, size);
95}
Represents a file present on a FileSystem.
Definition File.h:40
FileSystem::FileModes m_access
Access permissions.
Definition File.h:145
Size m_size
Size of the file, in bytes.
Definition File.h:148
Abstract Input/Output buffer.
Definition IOBuffer.h:38
FileSystem::Result read(void *buffer, const Size size, const Size offset=ZERO)
Read bytes from the I/O buffer.
Definition IOBuffer.cpp:156
FileSystem::Result write(const void *buffer, const Size size, const Size offset=ZERO)
Write bytes to the I/O buffer.
Definition IOBuffer.cpp:180
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read bytes from the file.
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Write bytes to the file.
virtual ~PseudoFile()
Destructor.
PseudoFile(const u32 inode)
Default constructor.
char * m_buffer
Buffer from which we read.
Definition PseudoFile.h:92
Size length() const
Same as count().
Definition String.cpp:105
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
#define NULL
NULL means zero.
Definition Macros.h:39
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
Result
Result code for filesystem Actions.
Definition FileSystem.h:53