1 | #ifndef __GTK_PAPER_TAPE_MODEL_H__ |
---|
2 | #define __GTK_PAPER_TAPE_MODEL_H__ |
---|
3 | |
---|
4 | #include <string> |
---|
5 | |
---|
6 | namespace Gtk |
---|
7 | { class PaperTapeFile; } |
---|
8 | #include "lochstreifen.h" |
---|
9 | #include "gtkpapertape.h" |
---|
10 | |
---|
11 | namespace Gtk { |
---|
12 | |
---|
13 | /** |
---|
14 | * The PaperTapeFile class represents an opened Paper Tape File. This |
---|
15 | * is the "model" class in the GtkPaperTape MVC concept. |
---|
16 | * |
---|
17 | * The class holds the binary data from the file and performs modifications |
---|
18 | * to these data. It features a signal (sigc++) that is fired whenever the |
---|
19 | * data array is changed. |
---|
20 | * |
---|
21 | * |
---|
22 | **/ |
---|
23 | class PaperTapeFile { |
---|
24 | PaperTape *controller; |
---|
25 | |
---|
26 | byte_t *data; |
---|
27 | int data_len; |
---|
28 | |
---|
29 | public: |
---|
30 | /** |
---|
31 | * This signal is emitted whenever the data are changed. |
---|
32 | **/ |
---|
33 | sigc::signal<void, byte_t*, int> data_changed; |
---|
34 | |
---|
35 | PaperTapeFile(PaperTape* controller); |
---|
36 | |
---|
37 | // hier auch Funktionen zum LABEL |
---|
38 | // generieren, Daten abändern, etc. |
---|
39 | |
---|
40 | // File changing and getting |
---|
41 | /// get a copy of the byte at position "pos". |
---|
42 | inline byte_t get_byte(const row_t& pos); |
---|
43 | /// get the value of the bit at that position |
---|
44 | inline bool get_bit(const row_t& pos, const track_t& track); |
---|
45 | /// change byte at position "pos" to "value" |
---|
46 | inline bool change_byte(const row_t& pos, byte_t value); |
---|
47 | /// change the bit at position "pos", bit "track" to "value" |
---|
48 | inline bool change_bit(const row_t& pos, const track_t& track, bool value); |
---|
49 | /// toggle the bit at position "pos", bit "track" |
---|
50 | inline bool toggle_bit(const row_t& pos, const track_t& track); |
---|
51 | //void push_bytes |
---|
52 | //void pop_bytes |
---|
53 | // etc. |
---|
54 | |
---|
55 | // interne Low Level Funktionen. |
---|
56 | // (intern = Gtk::PaperTape) |
---|
57 | void set(byte_t* data, int data_len); |
---|
58 | void add_null_bytes(int start, int end); |
---|
59 | |
---|
60 | |
---|
61 | // File saving and opening |
---|
62 | /// GUI function: Show dialog for file opening |
---|
63 | void open(); |
---|
64 | |
---|
65 | /// open that file. |
---|
66 | /// @throws GError if an error occurred |
---|
67 | void open(const std::string& filename); |
---|
68 | |
---|
69 | /// if filename omitted, save under same name. |
---|
70 | bool save(const std::string& filename = std::string()); |
---|
71 | |
---|
72 | /// GUI function: SaveAs dialog incl. handling |
---|
73 | void saveAs(); |
---|
74 | |
---|
75 | /// GUI function: Close Model (with dialog) |
---|
76 | /// and destroy Object (notify controller/view). |
---|
77 | /// if you want no GUI, use the destructor |
---|
78 | /// directly (obj.~...Model()) |
---|
79 | void close(); |
---|
80 | |
---|
81 | /// GUI function: Reload file. Same as |
---|
82 | /// close(); new Model(same_file_name); |
---|
83 | void reload(); |
---|
84 | |
---|
85 | }; |
---|
86 | |
---|
87 | // inline functions: |
---|
88 | byte_t PaperTapeFile::get_byte(const row_t& pos) { |
---|
89 | if(pos > 0 && pos <= data_len) { |
---|
90 | return data[pos]; |
---|
91 | } else |
---|
92 | return 0; // muss ich mir noch ueberlegen |
---|
93 | } |
---|
94 | |
---|
95 | bool PaperTapeFile::get_bit(const row_t& pos, const track_t& track) { |
---|
96 | return ((get_byte(pos) >> track) & 0x01) == 0x01; |
---|
97 | } |
---|
98 | |
---|
99 | bool PaperTapeFile::change_byte(const row_t& pos, byte_t value) { |
---|
100 | if(pos > 0 && pos <= data_len) { |
---|
101 | data[pos] = value; |
---|
102 | data_changed.emit(data, data_len); |
---|
103 | return true; |
---|
104 | } else |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | bool PaperTapeFile::change_bit(const row_t& pos, const track_t& track, bool value) { |
---|
109 | if(pos > 0 && pos <= data_len && track != LOCHSTREIFEN_NO_TRACK) { |
---|
110 | data[pos] = value |
---|
111 | ? data[pos] | (0x01 << track) // turn on |
---|
112 | : data[pos] & (0xFF ^ (0x01 << track)); // turn off |
---|
113 | data_changed.emit(data, data_len); |
---|
114 | return true; |
---|
115 | } else |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | bool PaperTapeFile::toggle_bit(const row_t& pos, const track_t& track) { |
---|
120 | if(pos > 0 && pos <= data_len && track != LOCHSTREIFEN_NO_TRACK) { |
---|
121 | data[pos] ^= (0x01 << track); // XOR toggle. |
---|
122 | data_changed.emit(data, data_len); |
---|
123 | return true; |
---|
124 | } else |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | }; // Namespace Gtk |
---|
129 | #endif // __GTK_PAPER_TAPE_MODEL_H |
---|