source: thomson/elec/CrO2/software/k5.cpp@ 192e299

main
Last change on this file since 192e299 was 192e299, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

Cleanup.

git-svn-id: svn://localhost/thomson@15 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 1.8 KB
Line 
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
12K5::Block::Block(int length,uint8_t type)
13{
14 this->length = length;
15 data = new uint8_t[length];
16 this->type = type;
17}
18
19K5::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
27const 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
40K5::Block::~Block()
41{
42 delete[] data;
43}
44
45
46K5::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
85int K5::getBlockCount()
86{
87 return blocks.size();
88}
89
90K5::Block K5::getBlock(int id)
91{
92 return blocks[id];
93}
Note: See TracBrowser for help on using the repository browser.