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:
52 SCOPED_TEMP_DIR( const wxString& aPrefix, const wxString& aSuffix = wxEmptyString );
53
55
56 // Non-copyable: the destructor removes the directory, so instances must not be copied.
57 SCOPED_TEMP_DIR( const SCOPED_TEMP_DIR& ) = delete;
59
61 const std::filesystem::path& Path() const { return m_path; }
62
64 wxString PathStr() const { return wxString::FromUTF8( m_path.string() ); }
65
80 wxString ChildPathStr( const wxString& aName ) const;
81
87 std::filesystem::path CreateChildDir( const wxString& aName ) const;
88
94 wxString CreateChildDirStr( const wxString& aName ) const;
95
101 std::filesystem::path CreateChildFile( const wxString& aName ) const;
102
108 wxString CreateChildFileStr( const wxString& aName ) const;
109
120 void Retain();
121
122 static bool AnyTempDirRetained() { return s_anyTempDirRetained; }
123
124private:
125 std::filesystem::path m_path;
126 bool m_keep = false;
127
128 // True if any temporary directory has been retained
130};
131
132
133/*
134 * A project loaded from its own temporary directory.
135 *
136 * The project is unloaded before the directory is removed so its lock file is released while the
137 * file still exists. Declare it ahead of any SCHEMATIC that refers to the project.
138 */
140{
141public:
142 SCOPED_TEMP_PROJECT( SETTINGS_MANAGER& aManager, const wxString& aPrefix, const wxString& aName );
143
145
148
149 PROJECT& Project() const { return *m_project; }
150
152 wxString DirStr() const { return m_dir.PathStr(); }
153
154private:
158};
159
181{
182public:
183 SCOPED_PROCESS_TEMP_DIR( const wxString& aPrefix );
185
186 // No copying: the destructor removes the directory
189
190private:
192};
193
194} // 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:125
wxString PathStr() const
Get the path to the temporary directory as a wxString.
Definition file_utils.h:64
SCOPED_TEMP_DIR & operator=(const SCOPED_TEMP_DIR &)=delete
static bool s_anyTempDirRetained
Definition file_utils.h:129
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:61
static bool AnyTempDirRetained()
Definition file_utils.h:122
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.
std::filesystem::path CreateChildFile(const wxString &aName) const
Create and return the path to a direct child file.
SCOPED_TEMP_DIR(const wxString &aPrefix, const wxString &aSuffix=wxEmptyString)
Create a temporary directory with a unique name based on the given prefix and optional suffix.
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:149
SETTINGS_MANAGER & m_manager
Definition file_utils.h:156
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:152
Container for project specific data.
Definition project.h:63
Test utilities for timestamps.