1 | /* CrO2 datassette emulator
|
---|
2 | * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
---|
3 | *
|
---|
4 | * Distributed under the terms of the MIT licence.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "Tape.h"
|
---|
8 |
|
---|
9 | #include "k5.h"
|
---|
10 | #include "zxtape.h"
|
---|
11 |
|
---|
12 | #include <string.h>
|
---|
13 | #include <string>
|
---|
14 | #include <stdexcept>
|
---|
15 |
|
---|
16 | Tape::~Tape()
|
---|
17 | {
|
---|
18 | for(Block* block: blocks) delete block;
|
---|
19 | }
|
---|
20 |
|
---|
21 | Tape* Tape::load(const char* filename) throw (const char*)
|
---|
22 | {
|
---|
23 | std::string fnam(filename);
|
---|
24 | try {
|
---|
25 | std::string fext(fnam.substr(fnam.length() - 3));
|
---|
26 |
|
---|
27 | // Load formats identified only by extension last
|
---|
28 | if (fext == "TAP" || fext == "tap")
|
---|
29 | return new ZXTAP(filename);
|
---|
30 | if (fext == ".K5" || fext == ".k5")
|
---|
31 | return new K5(filename);
|
---|
32 | } catch(std::out_of_range e) {}
|
---|
33 | throw "Unable to guess tape format";
|
---|
34 | }
|
---|
35 |
|
---|
36 | int Tape::getBlockCount() const
|
---|
37 | {
|
---|
38 | return blocks.size();
|
---|
39 | }
|
---|
40 |
|
---|
41 | Tape::Block& Tape::getBlock(int id)
|
---|
42 | {
|
---|
43 | return *blocks[id];
|
---|
44 | }
|
---|
45 |
|
---|
46 | const Tape::Block& Tape::getBlock(int id) const
|
---|
47 | {
|
---|
48 | return *blocks[id];
|
---|
49 | }
|
---|
50 |
|
---|
51 | // TAPE BLOCKS
|
---|
52 |
|
---|
53 | Tape::Block::Block(int length)
|
---|
54 | {
|
---|
55 | this->length = length;
|
---|
56 | data = new uint8_t[length];
|
---|
57 | }
|
---|
58 |
|
---|
59 | Tape::Block::Block(const Block& other)
|
---|
60 | {
|
---|
61 | length = other.length;
|
---|
62 | data = new uint8_t[length];
|
---|
63 | memcpy(data, other.data, length);
|
---|
64 | }
|
---|
65 |
|
---|
66 | const Tape::Block& Tape::Block::operator=(const Block& other)
|
---|
67 | {
|
---|
68 | delete[] data;
|
---|
69 | // May not be null ?
|
---|
70 |
|
---|
71 | length = other.length;
|
---|
72 | data = new uint8_t[length];
|
---|
73 | memcpy(data, other.data, length);
|
---|
74 |
|
---|
75 | return *this;
|
---|
76 | }
|
---|
77 |
|
---|
78 | Tape::Block::~Block()
|
---|
79 | {
|
---|
80 | delete[] data;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|