]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Misc/TempFolder.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Misc / TempFolder.cpp
1 /************************************************************************\r
2 **\r
3 **  Copyright (C) 2010  Strahinja Markovic\r
4 **\r
5 **  This file is part of FlightCrew.\r
6 **\r
7 **  FlightCrew is free software: you can redistribute it and/or modify\r
8 **  it under the terms of the GNU Lesser General Public License as published\r
9 **  by the Free Software Foundation, either version 3 of the License, or\r
10 **  (at your option) any later version.\r
11 **\r
12 **  FlightCrew is distributed in the hope that it will be useful,\r
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 **  GNU Lesser General Public License for more details.\r
16 **\r
17 **  You should have received a copy of the GNU Lesser General Public License\r
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.\r
19 **\r
20 *************************************************************************/\r
21 \r
22 #include <stdafx.h>\r
23 #include "TempFolder.h"\r
24 #include <cstdio>\r
25 \r
26 namespace FlightCrew\r
27 {\r
28 \r
29 static const char *UNIQUE_PATH_MODEL = "%%%%-%%%%-%%%%-%%%%-%%%%-%%%%-%%%%-%%%%";\r
30 static const char *FC_TEMP_FOLDER    = "flightcrew";\r
31 \r
32 TempFolder::TempFolder()\r
33     : m_PathToFolder( GetNewTempFolderPath() )\r
34 {\r
35     fs::create_directories( m_PathToFolder );\r
36 }\r
37 \r
38 \r
39 TempFolder::~TempFolder()\r
40 {\r
41     fs::remove_all( m_PathToFolder );\r
42 }\r
43 \r
44 \r
45 fs::path TempFolder::GetPath()\r
46 {\r
47     return m_PathToFolder;\r
48 }\r
49 \r
50 \r
51 fs::path TempFolder::GetNewTempFolderPath()\r
52 {\r
53 #ifdef _WIN32\r
54     // The specified "c:\\temp" path will only be used\r
55     // when the TMP environment variable is undefined\r
56     // http://msdn.microsoft.com/en-us/library/hs3e7355.aspx\r
57     wchar_t *tmp_name = _wtempnam( L"c:\\temp", L"unused" );\r
58     fs::path main_temp_folder = fs::path( tmp_name ).parent_path();\r
59     free( tmp_name );\r
60 #else\r
61     // GCC bitches and moans if we use tempnam(), so\r
62     // we'll just use the P_tmpdir macro. We can't use\r
63     // that on Win because it points to the drive root there\r
64     // instead of the system temp folder\r
65     // http://www.delorie.com/gnu/docs/glibc/libc_295.html\r
66     fs::path main_temp_folder = fs::path( P_tmpdir );\r
67 #endif    \r
68 \r
69     return main_temp_folder / fs::path( FC_TEMP_FOLDER ) / fs::unique_path( UNIQUE_PATH_MODEL );\r
70 }\r
71 \r
72 \r
73 } // namespace FlightCrew\r