KiCad PCB EDA Suite
Loading...
Searching...
No Matches
file_utils.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <filesystem>
23
24#include <wx/string.h>
25
26#include <wx_filename.h>
27
28
29class PROJECT;
31
32namespace KI_TEST
33{
34/*
35 * A uniquely-named temporary directory removed on destruction.
36 */
38{
39public:
50 SCOPED_TEMP_DIR( const wxString& aPrefix );
51
53
54 // Non-copyable: the destructor removes the directory, so instances must not be copied.
55 SCOPED_TEMP_DIR( const SCOPED_TEMP_DIR& ) = delete;
57
59 const std::filesystem::path& Path() const { return m_path; }
60
62 wxString PathStr() const { return wxString::FromUTF8( m_path.string() ); }
63
78 wxString ChildPathStr( const wxString& aName ) const;
79
85 std::filesystem::path CreateChildDir( const wxString& aName ) const;
86
92 wxString CreateChildDirStr( const wxString& aName ) const;
93
99 std::filesystem::path CreateChildFile( const wxString& aName ) const;
100
106 wxString CreateChildFileStr( const wxString& aName ) const;
107
118 void Retain();
119
120 static bool AnyTempDirRetained() { return s_anyTempDirRetained; }
121
122private:
123 std::filesystem::path m_path;
124 bool m_keep = false;
125
126 // True if any temporary directory has been retained
128};
129
130
131/*
132 * A project loaded from its own temporary directory.
133 *
134 * The project is unloaded before the directory is removed so its lock file is released while the
135 * file still exists. Declare it ahead of any SCHEMATIC that refers to the project.
136 */
138{
139public:
140 SCOPED_TEMP_PROJECT( SETTINGS_MANAGER& aManager, const wxString& aPrefix, const wxString& aName );
141
143
146
147 PROJECT& Project() const { return *m_project; }
148
150 wxString DirStr() const { return m_dir.PathStr(); }
151
152private:
156};
157
179{
180public:
181 SCOPED_PROCESS_TEMP_DIR( const wxString& aPrefix );
183
184 // No copying: the destructor removes the directory
187
188private:
190};
191
192} // namespace KI_TEST
SCOPED_PROCESS_TEMP_DIR(const wxString &aPrefix)
SCOPED_PROCESS_TEMP_DIR(const SCOPED_PROCESS_TEMP_DIR &)=delete
SCOPED_PROCESS_TEMP_DIR & operator=(const SCOPED_PROCESS_TEMP_DIR &)=delete
std::filesystem::path m_path
Definition file_utils.h:123
wxString PathStr() const
Get the path to the temporary directory as a wxString.
Definition file_utils.h:62
SCOPED_TEMP_DIR & operator=(const SCOPED_TEMP_DIR &)=delete
static bool s_anyTempDirRetained
Definition file_utils.h:127
wxString CreateChildFileStr(const wxString &aName) const
Create and return the path to a direct child file as a wxString.
SCOPED_TEMP_DIR(const SCOPED_TEMP_DIR &)=delete
void Retain()
Keep this directory after all.
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:59
static bool AnyTempDirRetained()
Definition file_utils.h:120
wxString ChildPathStr(const wxString &aName) const
Get the path to a direct child of the temporary directory as a wxString, without creating the child.
wxString CreateChildDirStr(const wxString &aName) const
Create and return the path to a direct child directory as a wxString.
SCOPED_TEMP_DIR(const wxString &aPrefix)
Create a temporary directory with a unique name based on the given prefix.
std::filesystem::path CreateChildFile(const wxString &aName) const
Create and return the path to a direct child file.
std::filesystem::path CreateChildDir(const wxString &aName) const
Create and return the path to a direct child directory.
PROJECT & Project() const
Definition file_utils.h:147
SETTINGS_MANAGER & m_manager
Definition file_utils.h:154
SCOPED_TEMP_PROJECT(SETTINGS_MANAGER &aManager, const wxString &aPrefix, const wxString &aName)
SCOPED_TEMP_PROJECT(const SCOPED_TEMP_PROJECT &)=delete
SCOPED_TEMP_PROJECT & operator=(const SCOPED_TEMP_PROJECT &)=delete
wxString DirStr() const
Get the path to the temporary directory as a wxString.
Definition file_utils.h:150
Container for project specific data.
Definition project.h:63