Executable/
Standard.rs

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
166
167
168
169
170
171
172
173
174
175
176
177
use File_system::{
    File_identifier_type, Mode_type, Path_type, Size_type, Unique_file_identifier_type,
};
use Task::Task_identifier_type;
use Virtual_file_system::Virtual_file_system_type;

use crate::Result_type;

pub struct Standard_type {
    Standard_in: Unique_file_identifier_type,
    Standard_out: Unique_file_identifier_type,
    Standard_error: Unique_file_identifier_type,
    Task: Task_identifier_type,
    Virtual_file_system: &'static Virtual_file_system_type<'static>,
}

impl Drop for Standard_type {
    fn drop(&mut self) {
        let _ = self.Virtual_file_system.Close(self.Standard_in, self.Task);

        let _ = self.Virtual_file_system.Close(self.Standard_out, self.Task);

        let _ = self
            .Virtual_file_system
            .Close(self.Standard_error, self.Task);
    }
}

impl Standard_type {
    pub fn Open(
        Standard_in: &impl AsRef<Path_type>,
        Standard_out: &impl AsRef<Path_type>,
        Standard_error: &impl AsRef<Path_type>,
        Task: Task_identifier_type,
        Virtual_file_system: &'static Virtual_file_system_type,
    ) -> Result_type<Self> {
        let Standard_in =
            Virtual_file_system.Open(Standard_in, Mode_type::Read_only.into(), Task)?;

        let Standard_out =
            Virtual_file_system.Open(Standard_out, Mode_type::Write_only.into(), Task)?;

        let Standard_error =
            Virtual_file_system.Open(Standard_error, Mode_type::Write_only.into(), Task)?;

        Ok(Self::New(
            Standard_in,
            Standard_out,
            Standard_error,
            Task,
            Virtual_file_system,
        ))
    }

    pub fn New(
        Standard_in: Unique_file_identifier_type,
        Standard_out: Unique_file_identifier_type,
        Standard_error: Unique_file_identifier_type,
        Task: Task_identifier_type,
        Virtual_file_system: &'static Virtual_file_system_type,
    ) -> Self {
        Self {
            Standard_in,
            Standard_out,
            Standard_error,
            Task,
            Virtual_file_system,
        }
    }

    pub fn Print(&self, Arguments: &str) {
        let _ = self
            .Virtual_file_system
            .Write(self.Standard_out, Arguments.as_bytes(), self.Task);
    }

    pub fn Out_flush(&self) {
        self.Virtual_file_system
            .Flush(self.Standard_out, self.Task)
            .unwrap();
    }

    pub fn Write(&self, Data: &[u8]) -> Size_type {
        match self
            .Virtual_file_system
            .Write(self.Standard_out, Data, self.Task)
        {
            Ok(Size) => Size,
            Err(_) => 0_usize.into(),
        }
    }

    pub fn Print_line(&self, Arguments: &str) {
        self.Print(Arguments);
        self.Print("\n");
    }

    pub fn Print_error(&self, Arguments: &str) {
        let _ =
            self.Virtual_file_system
                .Write(self.Standard_error, Arguments.as_bytes(), self.Task);
    }

    pub fn Print_error_line(&self, Arguments: &str) {
        self.Print_error(Arguments);
        self.Print_error("\n");
    }

    pub fn Read_line(&self, Buffer: &mut String) {
        Buffer.clear();

        let _ = Virtual_file_system::Get_instance().Read_line(self.Standard_in, self.Task, Buffer);
    }

    pub fn Get_task(&self) -> Task_identifier_type {
        self.Task
    }

    pub fn Duplicate(&self) -> Result_type<Self> {
        let Standard_in = self
            .Virtual_file_system
            .Duplicate_file_identifier(self.Standard_in, self.Task)?;

        let Standard_out = self
            .Virtual_file_system
            .Duplicate_file_identifier(self.Standard_out, self.Task)?;

        let Standard_error = self
            .Virtual_file_system
            .Duplicate_file_identifier(self.Standard_error, self.Task)?;

        Ok(Self::New(
            Standard_in,
            Standard_out,
            Standard_error,
            self.Task,
            self.Virtual_file_system,
        ))
    }

    pub fn Split(
        &self,
    ) -> (
        Unique_file_identifier_type,
        Unique_file_identifier_type,
        Unique_file_identifier_type,
    ) {
        (self.Standard_in, self.Standard_out, self.Standard_error)
    }

    pub(crate) fn Transfert(mut self, Task: Task_identifier_type) -> Result_type<Self> {
        self.Standard_in = self.Virtual_file_system.Transfert(
            self.Standard_in,
            self.Task,
            Task,
            Some(File_identifier_type::Standard_in),
        )?;

        self.Standard_out = self.Virtual_file_system.Transfert(
            self.Standard_out,
            self.Task,
            Task,
            Some(File_identifier_type::Standard_out),
        )?;

        self.Standard_error = self.Virtual_file_system.Transfert(
            self.Standard_error,
            self.Task,
            Task,
            Some(File_identifier_type::Standard_error),
        )?;

        self.Task = Task;

        Ok(self)
    }
}