-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstorage.js
165 lines (135 loc) · 3.7 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Persistent storage for JS/Linux.
* Copyright (C) 2011 Kevin van der Vlist
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Kevin van der Vlist - [email protected]
*/
/**
* JavaScript storage backend for block devices
*/
Storage.prototype.debug = false;
// Storage types
Storage.prototype.type = {
HTML5STORAGE: 0x00,
HTML5STORAGEWORDS: 0x01,
MEMORY: 0xff
};
/**
* Storage class providing specific subclasses
* Count: Number of sectors
* Size: Size of a sector
*/
function Storage (count, size) {
this.sectorCount = count;
this.sectorSize = size;
if(this.debug) {
console.log("Initialising storage backend. Geometry: %d sectors of %d bytes makes %d bytes.", count, size, count * size);
}
}
/**
* Initialize the memory backend.
* Count: Number of sectors
* Size: Size of a sector
* Type: The type of storage backend
*/
Storage.prototype.getStorageBackend = function(count, size, type) {
var stor;
// Check for localstorage support.
function localStorage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
switch (type) {
case this.type.HTML5STORAGE:
if(localStorage()) {
stor = new HTML5Storage(count, size);
} else {
console.error("Can't initialize local storage.");
return null;
}
break;
case this.type.HTML5STORAGEWORDS:
if(localStorage()) {
stor = new HTML5StorageWords(count, size);
} else {
console.error("Can't initialize local storage with words.");
return null;
}
break;
case this.type.MEMORY:
// Default choice: fall-through
default:
stor = new MemStorage(count, size);
break;
}
// Check if we are set up correctly
if(stor == null) {
console.error("Falling back on memory storage.");
stor = new MemStorage(count, size);
throw new Error("Non-persistent memory backend implicitly chosen.");
}
return stor;
}
/**
* Read a certain byte from the storage backend.
* Sector: The sector location of the requested byte.
* Byte: The requested byte location on a certain sector.
*/
Storage.prototype.getByte = function(sector, byte) {
return null;
}
/**
* Write a certain byte to the storage backend.
* Sector: The sector location of the requested byte.
* Byte: The requested byte location on a certain sector.
* Value: The value to store.
*/
Storage.prototype.setByte = function(sector, byte, value) {
return;
}
/**
* Return a download link to the current filesystem image.
*/
Storage.prototype.getDownloadLink = function() {
return;
}
/**
* Load a saved disk to memory.
* File: The File object received from <input>
*/
Storage.prototype.loadData = function(file) {
}
/**
* Clear disk data
*/
Storage.prototype.clearDisk = function() {
}
/**
* Print a human readable (i.e. consistent padding) hexadecimal digit.
* d: The digit to print.
* padding: The amount of padding.
*/
Storage.prototype.dec2hex = function (d, padding) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
}