Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::filesystem::FileResource Class Referencefinal

A wrapper for a File which exposes File operations as Resource operations. More...

#include <vfsresource.h>

Inheritance diagram for MaxOS::filesystem::FileResource:
MaxOS::processes::Resource

Public Member Functions

 FileResource (const string &name, size_t flags, processes::resource_type_t type)
 Construct a new File Resource object.
 
int read (void *buffer, size_t size, size_t flags) final
 read from a file resource
 
int write (const void *buffer, size_t size, size_t flags) final
 write to a file resource
 
- Public Member Functions inherited from MaxOS::processes::Resource
 Resource (const string &name, size_t flags, resource_type_t type)
 Constructs a new Resource object.
 
string name ()
 Gets the name of this resource.
 
resource_type_t type ()
 Gets the type of this resource.
 
virtual void open (size_t flags)
 Opens the resource.
 
virtual void close (size_t flags)
 Closes the resource.
 

Public Attributes

Filefile
 The file that this resource handles & exposes.
 

Detailed Description

A wrapper for a File which exposes File operations as Resource operations.

See also
File
Resource
Todo:
Should this free the memory of the file when the resource is done??

Definition at line 32 of file vfsresource.h.

Constructor & Destructor Documentation

◆ FileResource()

FileResource::FileResource ( const string name,
size_t  flags,
processes::resource_type_t  type 
)

Construct a new File Resource object.

Parameters
nameThe name of the resource
flagsThe flags for the resource when opened
typeThe type of the resource

Definition at line 24 of file vfsresource.cpp.

25: Resource(name, flags, type),
26 file(nullptr) // Initialised by the registry
27{
28
29}
File * file
The file that this resource handles & exposes.
Definition vfsresource.h:38
Represents a generic resource that can be opened, closed, read from and written to.
Definition resource.h:29
string name()
Gets the name of this resource.
Definition resource.cpp:79
resource_type_t type()
Gets the type of this resource.
Definition resource.cpp:88

Member Function Documentation

◆ read()

int FileResource::read ( void buffer,
size_t  size,
size_t  flags 
)
finalvirtual

read from a file resource

Parameters
bufferThe buffer to read into
sizeThe number of bytes to read
flagsThe flags to pass to the reading
Returns
The number of bytes successfully read or -1 on error

Reimplemented from MaxOS::processes::Resource.

Definition at line 41 of file vfsresource.cpp.

41 {
42
43 // File not found
44 if(!file)
45 return -1;
46
47 // Handle the operation
48 switch ((FileFlags)flags) {
49
50 case FileFlags::DEFAULT:{
51
52 buffer_t file_buffer(buffer, size);
53 file->read(&file_buffer, size);
54 break;
55 }
56
57 case FileFlags::READ_SIZE:{
58 return file->size();
59 }
60
61 case FileFlags::READ_OFFSET:{
62
63 return file->position();
64 }
65
66 default:
67 return -1;
68 }
69 return size;
70}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
virtual void read(common::buffer_t *data, size_t size)
read data from the file
size_t size() const
Get the size of the file.
uint32_t position() const
Get where the file is currently at (amount read/write/seeked)

References file, MaxOS::filesystem::File::position(), MaxOS::filesystem::File::read(), and MaxOS::filesystem::File::size().

◆ write()

int FileResource::write ( const void buffer,
size_t  size,
size_t  flags 
)
finalvirtual

write to a file resource

Parameters
bufferThe buffer to write from
sizeThe number of bytes to write
flagsThe flags to pass to the writing
Returns
The number of bytes successfully written or -1 on error

Reimplemented from MaxOS::processes::Resource.

Definition at line 80 of file vfsresource.cpp.

80 {
81
82 // File not found
83 if(!file)
84 return -1;
85
86 // Handle the operation
87 switch ((FileFlags)flags) {
88 case FileFlags::DEFAULT:{
89
90 buffer_t file_buffer((void*)buffer, size);
91 file->write(&file_buffer, size);
92 break;
93 }
94
95 case FileFlags::WRITE_SEEK_SET:
96 file->seek(SeekType::SET, size);
97 break;
98
99 case FileFlags::WRITE_SEEK_CUR:
100 file->seek(SeekType::CURRENT, size);
101 break;
102
103 case FileFlags::WRITE_SEEK_END:
104 file->seek(SeekType::END, size);
105 break;
106
107 case FileFlags::WRITE_NAME:{
108
109 // Open the parent
110 Resource* parent = GlobalResourceRegistry::get_registry(resource_type_t::FILESYSTEM)->get_resource(Path::parent_directory(name()));
111 auto parent_directory = ((DirectoryResource*)parent)->directory;
112
113 // Rename
114 string new_name = string((uint8_t*)buffer, size);
115 parent_directory->rename_file(file, new_name);
116 break;
117 }
118
119 default:
120 return -1;
121
122 }
123
124 return size;
125}
A wrapper for a Directory which exposes Directory operations as Resource operations.
Definition vfsresource.h:49
void seek(SeekType seek_type, size_t offset)
Seek to a position in the file.
virtual void write(common::buffer_t *data, size_t size)
write data to the file
static string parent_directory(const string &path)
Get the parent directory of the path.
Definition path.cpp:148
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:217
class MaxOS::String string
Typedef for String.

References file, MaxOS::processes::GlobalResourceRegistry::get_registry(), MaxOS::processes::Resource::name(), MaxOS::filesystem::Path::parent_directory(), MaxOS::filesystem::File::seek(), and MaxOS::filesystem::File::write().

Member Data Documentation

◆ file

File* MaxOS::filesystem::FileResource::file

The file that this resource handles & exposes.

Definition at line 38 of file vfsresource.h.

Referenced by read(), and write().


The documentation for this class was generated from the following files: