* Move GUI in its own class
* Design C++ callbacksystem for IUP
* Load file from menu
git-svn-id: svn://localhost/thomson@13 85ae3b6b-dc8f-4344-a89d-598714f2e4e5
diff --git a/elec/CrO2/software/k5.cpp b/elec/CrO2/software/k5.cpp
new file mode 100644
index 0000000..d3c12bc
--- /dev/null
+++ b/elec/CrO2/software/k5.cpp
@@ -0,0 +1,80 @@
+/* CrO2 datassette emulator
+ * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
+ *
+ * Distributed under the terms of the MIT licence.
+ */
+
+#include "k5.h"
+
+#include <fstream>
+#include <string.h>
+
+K5::Block::Block(int length,uint8_t type)
+{
+ this->length = length;
+ data = new uint8_t[length];
+ this->type = type;
+}
+
+K5::Block::Block(const Block& other)
+{
+ length = other.length;
+ type = other.type;
+ data = new uint8_t[length];
+ memcpy(data, other.data, length);
+}
+
+K5::Block::~Block()
+{
+ delete[] data;
+}
+
+
+K5::K5(const char* name)
+{
+ std::ifstream stream(name, std::ifstream::in | std::ifstream::binary);
+
+ if (!stream.is_open()) throw "Could not open file.";
+ if (!stream.good()) throw "File error.";
+
+ uint8_t byte;
+
+ do {
+ do {
+ byte = stream.get();
+ if (stream.eof())
+ return;
+ } while (byte != 0x5A); // Skip pilot tone and sync bytes
+ // TODO make sure this is a standard block ?
+
+ uint8_t blktype = stream.get();
+ int blksize = stream.get(); // includes checksum
+ if (blksize == 0) blksize = 256;
+ blksize --;
+ Block block(blksize, blktype);
+ stream.read((char*)block.data, block.length);
+
+ blocks.push_back(block);
+/*
+ if (block.type == 0)
+ {
+ printf("FILE: %.11s\n",block.data);
+ }
+
+ printf("blk type %d size %d (%d)\n", block.type, block.length, (int)stream.tellg());
+*/
+ } while (stream.good());
+
+
+ stream.close();
+}
+
+int K5::getBlockCount()
+{
+ return blocks.size();
+}
+
+K5::Block K5::getBlock(int id)
+{
+ return blocks[id];
+}