1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2011 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
#define _FILE_OFFSET_BITS 64
|
20 |
#define _LARGEFILE_SOURCE
|
21 |
#define _LARGEFILE64_SOURCE
|
22 |
#include <unistd.h>
|
23 |
#include "maptool.h"
|
24 |
#include "debug.h"
|
25 |
|
26 |
char *
|
27 |
tempfile_name(char *suffix, char *name)
|
28 |
{
|
29 |
return g_strdup_printf("%s_%s.tmp", name, suffix);
|
30 |
}
|
31 |
|
32 |
FILE *
|
33 |
tempfile(char *suffix, char *name, int mode)
|
34 |
{
|
35 |
char *buffer = tempfile_name(suffix, name);
|
36 |
FILE *ret = NULL;
|
37 |
switch (mode)
|
38 |
{
|
39 |
case 0:
|
40 |
ret = fopen(buffer, "rb");
|
41 |
break;
|
42 |
case 1:
|
43 |
ret = fopen(buffer, "wb+");
|
44 |
break;
|
45 |
case 2:
|
46 |
ret = fopen(buffer, "ab");
|
47 |
break;
|
48 |
}
|
49 |
|
50 |
if (debug_itembin(6))
|
51 |
{
|
52 |
fprintf(stderr, "== tempfile == open == FILENAME: %s FILEPOINTER: %p ==\n", buffer, ret);
|
53 |
}
|
54 |
g_free(buffer);
|
55 |
|
56 |
return ret;
|
57 |
}
|
58 |
|
59 |
void tempfile_unlink(char *suffix, char *name)
|
60 |
{
|
61 |
char buffer[4096];
|
62 |
sprintf(buffer, "%s_%s.tmp", name, suffix);
|
63 |
unlink(buffer);
|
64 |
}
|
65 |
|
66 |
void tempfile_rename(char *suffix, char *from, char *to)
|
67 |
{
|
68 |
char buffer_from[4096], buffer_to[4096];
|
69 |
sprintf(buffer_from, "%s_%s.tmp", from, suffix);
|
70 |
sprintf(buffer_to, "%s_%s.tmp", to, suffix);
|
71 |
dbg_assert(rename(buffer_from, buffer_to) == 0);
|
72 |
}
|
73 |
|