1
1
import { bytesToStr , strToBytes } from './common' ;
2
+ import { addFileToIntelHex } from './fs-builder' ;
2
3
3
4
interface FsInterface {
4
- create ( filename : string , content ?: string ) : void ;
5
5
write ( filename : string , content : string ) : void ;
6
6
append ( filename : string , content : string ) : void ;
7
7
read ( filename : string ) : string ;
8
8
readBytes ( filename : string ) : Uint8Array ;
9
9
remove ( filename : string ) : void ;
10
+ exists ( filename : string ) : boolean ;
10
11
ls ( ) : string [ ] ;
11
12
}
12
13
13
14
class SimpleFile {
14
15
filename : string ;
15
- data : Uint8Array ;
16
+ private _dataBytes : Uint8Array ;
16
17
17
18
constructor ( filename : string , data : string | Uint8Array ) {
18
19
this . filename = filename ;
19
20
if ( typeof data === 'string' ) {
20
- this . data = strToBytes ( data ) ;
21
+ this . _dataBytes = strToBytes ( data ) ;
21
22
} else {
22
- this . data = data ;
23
+ this . _dataBytes = data ;
23
24
}
24
25
}
25
26
26
27
getText ( ) : string {
27
- return bytesToStr ( this . data ) ;
28
+ return bytesToStr ( this . _dataBytes ) ;
28
29
}
29
30
30
31
getBytes ( ) : Uint8Array {
31
- return this . data ;
32
+ return this . _dataBytes ;
32
33
}
33
34
}
34
35
36
+ // TODO: Max filename size
35
37
// tslint:disable-next-line:max-classes-per-file
36
38
class FileSystem implements FsInterface {
37
39
private _intelHex : string ;
38
40
private _files : { [ id : string ] : SimpleFile } = { } ;
39
41
40
42
constructor ( intelHex : string ) {
41
43
this . _intelHex = intelHex ;
42
- }
43
44
44
- create ( filename : string , content ?: string ) : void {
45
- // TODO: Create an empty file, with optional content
46
- // TODO: Throw error if file already exists
47
- // tslint:disable-next-line:no-console
48
- console . log ( 'create() method unimplemented.' ) ;
45
+ // TODO: Read present file system in Intel Hex and populate files here
49
46
}
50
47
51
48
write ( filename : string , content : string | Uint8Array ) : void {
@@ -54,7 +51,7 @@ class FileSystem implements FsInterface {
54
51
55
52
append ( filename : string , content : string ) : void {
56
53
// TODO: Append content to existing file
57
- // TODO: Throw error if file does not exists
54
+ // TODO: Do we throw error if file does not exists, or create it?
58
55
// tslint:disable-next-line:no-console
59
56
console . log ( 'append() method unimplemented.' ) ;
60
57
}
@@ -74,15 +71,22 @@ class FileSystem implements FsInterface {
74
71
delete this . _files [ filename ] ;
75
72
}
76
73
74
+ exists ( filename : string ) : boolean {
75
+ return this . _files . hasOwnProperty ( filename ) ;
76
+ }
77
+
77
78
ls ( ) : string [ ] {
78
79
const files : string [ ] = [ ] ;
79
80
Object . values ( this . _files ) . forEach ( ( value ) => files . push ( value . filename ) ) ;
80
81
return files ;
81
82
}
82
83
83
84
getIntelHex ( ) : string {
84
- // TODO: Generate filesystem and inject into Intel Hex string
85
- return this . _intelHex ;
85
+ let finalHex = this . _intelHex ;
86
+ Object . values ( this . _files ) . forEach ( ( file ) => {
87
+ finalHex = addFileToIntelHex ( finalHex , file . filename , file . getBytes ( ) ) ;
88
+ } ) ;
89
+ return finalHex ;
86
90
}
87
91
}
88
92
0 commit comments