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
89 std::filesystem::path CreateChildDir( const wxString& aName ) const;
90
98 wxString CreateChildDirStr( const wxString& aName ) const;
99
107 std::filesystem::path CreateChildFile( const wxString& aName ) const;
108
116 wxString CreateChildFileStr( const wxString& aName ) const;
117
128 void Retain();
129
130 static bool AnyTempDirRetained() { return s_anyTempDirRetained; }
131
132private:
133 std::filesystem::path m_path;
134 bool m_keep = false;
135
136 // True if any temporary directory has been retained
138};
139
140
141/*
142 * A project loaded from its own temporary directory.
143 *
144 * The project is unloaded before the directory is removed so its lock file is released while the
145 * file still exists. Declare it ahead of any SCHEMATIC that refers to the project.
146 */
148{
149public:
150 SCOPED_TEMP_PROJECT( SETTINGS_MANAGER& aManager, const wxString& aPrefix, const wxString& aName );
151
153
156
157 PROJECT& Project() const { return *m_project; }
158
160 wxString DirStr() const { return m_dir.PathStr(); }
161
162private:
166};
167
189{
190public:
191 SCOPED_PROCESS_TEMP_DIR( const wxString& aPrefix );
193
194 // No copying: the destructor removes the directory
197
198private:
200};
201
202} // 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:133
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:137
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:130
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:157
SETTINGS_MANAGER & m_manager
Definition file_utils.h:164
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:160
Container for project specific data.
Definition project.h:63
Test utilities for timestamps.