1 /*******************************************************************************
2 
3     Registry of open files, plus methods to reopen one or all.
4 
5     Copyright:
6         Copyright (c) 2018 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.application.components.OpenFiles;
17 
18 import ocean.meta.types.Qualifiers;
19 
20 /// ditto
21 public class OpenFiles
22 {
23     import ocean.core.Buffer;
24     import ocean.core.Verify;
25     import ocean.io.device.File;
26     import ocean.sys.Environment;
27     import ocean.text.convert.Formatter;
28 
29     /***************************************************************************
30 
31         List of open files to be reopened when reopenAll() is called.
32 
33     ***************************************************************************/
34 
35     private File[] open_files;
36 
37 
38     /***************************************************************************
39 
40         Current working directory. Used for building absolute paths for the
41         registered files.
42 
43     ***************************************************************************/
44 
45     private istring cwd;
46 
47 
48     /***************************************************************************
49 
50         Buffer for rendering the absolute paths.
51 
52     ***************************************************************************/
53 
54     private Buffer!(char) path_buffer;
55 
56 
57     /***************************************************************************
58 
59         Constructor.
60 
61     ***************************************************************************/
62 
63     public this ( )
64     {
65         this.cwd = Environment.directory();
66         verify(this.cwd[$-1] == '/');
67     }
68 
69 
70     /***************************************************************************
71 
72         Registers the specified file, to be reopened when reopenAll() is called.
73 
74         Params:
75             file = file to add to the set of reopenable files
76 
77     ***************************************************************************/
78 
79     public void register ( File file )
80     {
81         // TODO check for presence to avoid duplicates?
82         this.open_files ~= file;
83     }
84 
85 
86     /***************************************************************************
87 
88         Reopens all registered files.
89 
90     ***************************************************************************/
91 
92     public void reopenAll ( )
93     {
94         foreach ( file; this.open_files )
95         {
96             file.close();
97             file.open(file.path(), file.style);
98         }
99     }
100 
101 
102     /***************************************************************************
103 
104         Reopens all registered files with the given path
105 
106         Params:
107             file_path = path of the file to open
108 
109         Returns:
110             true if the file was registered and reopened, false otherwise.
111 
112     ***************************************************************************/
113 
114     public bool reopenFile ( cstring file_path )
115     {
116         // It might be the case that we have several files registered with the
117         // same path. We need to reopen all of them.
118         bool reopened;
119 
120         foreach (file; this.open_files)
121         {
122             this.path_buffer.reset();
123             sformat(this.path_buffer, "{}{}", this.cwd, file.path());
124 
125             // Check both relative and absolute paths
126             if (file.path() == file_path || this.path_buffer[] == file_path)
127             {
128                 file.close();
129                 file.open(file.path(), file.style);
130                 reopened = true;
131             }
132         }
133 
134         return reopened;
135     }
136 }