Bumped copyright dates for 2013
[barry.git] / desktop / src / tempdir.cc
blob428214896a79e10527d3b49584987d7a5f317b66
1 ///
2 /// \file tempdir.cc
3 /// Temp directory & file wrapper class
4 ///
6 /*
7 Copyright (C) 2009-2013, Chris Frey <cdfrey@foursquare.net>
8 The idea to use glib's g_get_tmp_dir() came from opensync's osynctool.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "tempdir.h"
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <sstream>
30 #include <stdexcept>
31 #include "i18n.h"
33 TempDir::TempDir(const char *basename)
34 : m_template(0)
35 , m_files(0)
37 m_template = g_strdup_printf("%s/%s-XXXXXX", g_get_tmp_dir(), basename);
38 if( mkdtemp(m_template) == NULL ) {
39 g_free(m_template);
40 throw std::runtime_error(std::string(_C("Cannot create temp directory: ")) + strerror(errno));
44 TempDir::~TempDir()
46 // delete all files
47 for( int i = 0; i < m_files; i++ ) {
48 unlink(MakeFilename(i).c_str());
51 // delete directory
52 rmdir(m_template);
54 // cleanup memory
55 g_free(m_template);
58 std::string TempDir::MakeFilename(int file_id) const
60 std::ostringstream oss;
61 oss << m_template << "/" << file_id;
62 return oss.str();
65 std::string TempDir::GetNewFilename()
67 return MakeFilename(m_files++);