VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
AnyFile.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "CoreObject.h"
16#include "MediaFile.h"
17#include "PETName.h"
18
19#include <sstream>
20#include <fstream>
21
22namespace vc64 {
23
24/* All media files are organized in the class hierarchy displayed below. Two
25 * abstract classes are involed: AnyFile and AnyCollection. AnyFiles provides
26 * basic functionality for reading and writing files, streams, and buffers.
27 * AnyCollection provides an abstract interface for accessing single files.
28 * This interface is implemented for those formats that organize their contents
29 * as a collection of files (in contrast to those formats that organzie their
30 * contents as a collection of tracks or sectors, or do not store files at all).
31 *
32 * ---------
33 * | AnyFile |
34 * ---------
35 * |
36 * |--------------------------------------------------
37 * | | | | | | |
38 * | --------- | --------- | --------- |
39 * | | ROMFile | | |TAPFile | | | D64File | |
40 * | --------- | --------- | --------- |
41 * | ---------- ---------- ---------
42 * | | Snapshot | | CRTFile | | G64File |
43 * | ---------- ---------- ---------
44 * |
45 * ---------------
46 * | AnyCollection |
47 * ---------------
48 * |
49 * |-----------------------------------------------
50 * | | | |
51 * --------- --------- --------- ---------
52 * | T64File | | PRGFile | | P00File | | Folder |
53 * --------- --------- --------- ---------
54 */
55
56class AnyFile : public CoreObject, public MediaFile {
57
58public:
59
60 // Physical location of this file
61 fs::path path = "";
62
63 // The raw data of this file
64 u8 *data = nullptr;
65
66 // The size of this file in bytes
67 isize size = 0;
68
69
70 //
71 // Initializing
72 //
73
74public:
75
76 AnyFile() { };
77 AnyFile(isize capacity);
78 virtual ~AnyFile();
79
80 void init(isize capacity);
81 void init(const fs::path &path) throws;
82 void init(const fs::path &path, std::istream &stream) throws;
83 void init(std::istream &stream) throws;
84 void init(const u8 *buf, isize len) throws;
85 void init(FILE *file) throws;
86
87
88 //
89 // Methods from MediaFile
90 //
91
92 // Returns the size of this file
93 virtual isize getSize() const override { return size; }
94
95 // Returns a pointer to the file data
96 virtual u8 *getData() const override { return data; }
97
98
99 //
100 // Accessing
101 //
102
103public:
104
105 // Returns the logical name of this file
106 virtual string name() const override;
107 virtual PETName<16> getName() const;
108
109 // Returns a data byte
110 u8 getData(isize nr) { return (data && nr < size) ? data[nr] : 0; }
111
112 // Returns a fingerprint (hash value) for this file
113 u64 fnv() const override;
114
115 // Removes a certain number of bytes from the beginning of the file
116 void strip(isize count);
117
118
119 //
120 // Flashing
121 //
122
123 // Copies the file contents into a buffer starting at the provided offset
124 // DEPRECATED
125 void flash(u8 *buf, isize offset = 0) const override;
126
127
128 //
129 // Serializing
130 //
131
132protected:
133
134 virtual bool isCompatiblePath(const fs::path &path) = 0;
135 virtual bool isCompatibleStream(std::istream &stream) = 0;
136
137 void readFromStream(std::istream &stream) override;
138 void readFromFile(const fs::path &path) override;
139 void readFromBuffer(const u8 *buf, isize len) override;
140
141public:
142
143 void writeToStream(std::ostream &stream) override;
144 void writeToFile(const fs::path &path) override;
145 void writeToBuffer(u8 *buf) override;
146
147
148private:
149
150 // Delegation methods
151 virtual void finalizeRead() throws { };
152 virtual void finalizeWrite() throws { };
153};
154
155}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16