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 "k5.h"
|
---|
8 |
|
---|
9 | #include <fstream>
|
---|
10 | #include <string.h>
|
---|
11 |
|
---|
12 | K5::Block::Block(int length,uint8_t type)
|
---|
13 | {
|
---|
14 | this->length = length;
|
---|
15 | data = new uint8_t[length];
|
---|
16 | this->type = type;
|
---|
17 | }
|
---|
18 |
|
---|
19 | K5::Block::Block(const Block& other)
|
---|
20 | {
|
---|
21 | length = other.length;
|
---|
22 | type = other.type;
|
---|
23 | data = new uint8_t[length];
|
---|
24 | memcpy(data, other.data, length);
|
---|
25 | }
|
---|
26 |
|
---|
27 | const K5::Block& K5::Block::operator=(const Block& other)
|
---|
28 | {
|
---|
29 | delete[] data;
|
---|
30 | // May not be null ?
|
---|
31 |
|
---|
32 | length = other.length;
|
---|
33 | type = other.type;
|
---|
34 | data = new uint8_t[length];
|
---|
35 | memcpy(data, other.data, length);
|
---|
36 |
|
---|
37 | return *this;
|
---|
38 | }
|
---|
39 |
|
---|
40 | K5::Block::~Block()
|
---|
41 | {
|
---|
42 | delete[] data;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | K5::K5(const char* name)
|
---|
47 | {
|
---|
48 | std::ifstream stream(name, std::ifstream::in | std::ifstream::binary);
|
---|
49 |
|
---|
50 | if (!stream.is_open()) throw "Could not open file.";
|
---|
51 | if (!stream.good()) throw "File error.";
|
---|
52 |
|
---|
53 | uint8_t byte;
|
---|
54 |
|
---|
55 | do {
|
---|
56 | do {
|
---|
57 | byte = stream.get();
|
---|
58 | if (stream.eof())
|
---|
59 | return;
|
---|
60 | } while (byte != 0x5A); // Skip pilot tone and sync bytes
|
---|
61 | // TODO make sure this is a standard block ?
|
---|
62 |
|
---|
63 | uint8_t blktype = stream.get();
|
---|
64 | int blksize = stream.get(); // includes checksum
|
---|
65 | if (blksize == 0) blksize = 256;
|
---|
66 | blksize --;
|
---|
67 | Block block(blksize, blktype);
|
---|
68 | stream.read((char*)block.data, block.length);
|
---|
69 |
|
---|
70 | blocks.push_back(block);
|
---|
71 | /*
|
---|
72 | if (block.type == 0)
|
---|
73 | {
|
---|
74 | printf("FILE: %.11s\n",block.data);
|
---|
75 | }
|
---|
76 |
|
---|
77 | printf("blk type %d size %d (%d)\n", block.type, block.length, (int)stream.tellg());
|
---|
78 | */
|
---|
79 | } while (stream.good());
|
---|
80 |
|
---|
81 |
|
---|
82 | stream.close();
|
---|
83 | }
|
---|
84 |
|
---|
85 | int K5::getBlockCount()
|
---|
86 | {
|
---|
87 | return blocks.size();
|
---|
88 | }
|
---|
89 |
|
---|
90 | K5::Block K5::getBlock(int id)
|
---|
91 | {
|
---|
92 | return blocks[id];
|
---|
93 | }
|
---|