1 module styx2000.extrautil.dirstat;
2 
3 private {
4 	import std.conv : to;
5 	import std..string : format;
6 	
7 	import styx2000.lowlevel.endianness;
8 	import styx2000.lowlevel.vls;
9 	import styx2000.protobj.styxobject;
10 }
11 
12 public {
13 	import styx2000.extrautil.dir;
14 	
15 	import styx2000.protobj.perm;
16 	import styx2000.protobj.qid;
17 }
18 
19 
20 // data structure to represent directory with Stat data
21 class DirStat : StyxObject
22 {
23 	protected {
24 		Dir[] _dirs;
25 		ubyte[] _representation;
26 	}
27 	
28 	// construct
29 	this(Dir[] dirs...)
30 	{
31 		_dirs = dirs;
32 		
33 		foreach (dir; dirs)
34 		{
35 			_representation ~= dir.pack;
36 		}
37 	}
38 	
39 	// get directory entries
40 	Dir[] getDirs()
41 	{
42 		return _dirs;
43 	}
44 	
45 	// set directory entries
46 	void setDirs(Dir[] dirs...)
47 	{
48 		_dirs = dirs;
49 		_representation = [];
50 		
51 		foreach (dir; dirs)
52 		{
53 			_representation ~= dir.pack;
54 		}
55 	}
56 	
57 	// save to byte array
58 	ubyte[] pack()
59 	{
60 		return _representation;
61 	}
62 	
63 	// restore from bytes
64 	void unpack(ubyte[] bytes...)
65 	{
66 		_representation = bytes;
67 		_dirs = [];
68 		auto _contents = bytes;
69 		
70 		while (_contents.length != 0)
71 		{
72 			auto dirSize = fromLEBytes!ushort(_contents[0..2]) + 2;
73 			auto dir = _contents[0..dirSize];
74 			Dir d = new Dir;
75 			d.unpack(dir);
76 			_dirs ~= d;
77 			_contents = _contents[dirSize..$];
78 		}
79 	}
80 	
81 	// string representation
82 	override string toString()
83 	{
84 		return format(
85 			`DirStat(dirs=%s)`,
86 			_dirs.to!string
87 		);
88 	}
89 	
90 	alias pack this;	
91 }