1 // Written in the D programming language.
2 
3 /**
4 This module provides a DirStat structure, which is a folder view containing the Strat structures for a folder. The description of the structure is absent in the Styx protocol, but it can be useful in the formation of some types of messages.
5 
6 Copyright: LightHouse Software, 2021
7 License:   $(HTTP https://github.com/aquaratixc/ESL-License, Experimental Software License 1.0).
8 Authors:   Oleg Bakharev,
9 		   Ilya Pertsev   
10 */
11 module styx2000.extrautil.dirstat;
12 
13 private {
14 	import std.conv : to;
15 	import std.string : format;
16 	
17 	import styx2000.lowlevel.endianness;
18 	import styx2000.lowlevel.vls;
19 	import styx2000.protobj.styxobject;
20 }
21 
22 public {
23 	import styx2000.extrautil.dir;
24 	
25 	import styx2000.protobj.perm;
26 	import styx2000.protobj.qid;
27 }
28 
29 
30 /// Data structure to represent directory with Stat data
31 class DirStat : StyxObject
32 {
33 	protected {
34 		Dir[] _dirs;
35 		ubyte[] _representation;
36 	}
37 	
38 	/// Construct DirStat
39 	this(Dir[] dirs...)
40 	{
41 		_dirs = dirs;
42 		
43 		foreach (dir; dirs)
44 		{
45 			_representation ~= dir.pack;
46 		}
47 	}
48 	
49 	/// Get directory entries
50 	Dir[] getDirs()
51 	{
52 		return _dirs;
53 	}
54 	
55 	/// Set directory entries
56 	void setDirs(Dir[] dirs...)
57 	{
58 		_dirs = dirs;
59 		_representation = [];
60 		
61 		foreach (dir; dirs)
62 		{
63 			_representation ~= dir.pack;
64 		}
65 	}
66 	
67 	/// Save to byte array
68 	ubyte[] pack()
69 	{
70 		return _representation;
71 	}
72 	
73 	/// Restore from bytes
74 	void unpack(ubyte[] bytes...)
75 	{
76 		_representation = bytes;
77 		_dirs = [];
78 		auto _contents = bytes;
79 		
80 		while (_contents.length != 0)
81 		{
82 			auto dirSize = fromLEBytes!ushort(_contents[0..2]) + 2;
83 			auto dir = _contents[0..dirSize];
84 			Dir d = new Dir;
85 			d.unpack(dir);
86 			_dirs ~= d;
87 			_contents = _contents[dirSize..$];
88 		}
89 	}
90 	
91 	/// String representation of DirStat
92 	override string toString()
93 	{
94 		return format(
95 			`DirStat(dirs=%s)`,
96 			_dirs.to!string
97 		);
98 	}
99 	
100 	/// Pack to binary data
101 	alias pack this;	
102 }