-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_functions.cpp
136 lines (105 loc) · 3 KB
/
file_functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
File loader/saver
Copyright (C) 2009 Patrice Mandin
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
static char *get_filename_ext(const char *src_filename, const char *new_ext)
{
int dst_namelength = strlen(src_filename)+1;
char *dst_filename;
char *posname, *posext;
dst_filename = (char *) malloc(dst_namelength);
if (!dst_filename) {
fprintf(stderr, "Can not allocate %d bytes\n", dst_namelength);
return NULL;
}
posname =(char*) strrchr(src_filename, '/');
if (posname) {
++posname; /* Go after / */
} else {
posname = (char*)strrchr(src_filename, '\\');
if (posname) {
++posname; /* Go after \\ */
} else {
/* No directory in source filename */
posname = (char *) src_filename;
}
}
sprintf(dst_filename, "%s", posname);
posext = strrchr(dst_filename, '.');
if (!posext) {
strcat(dst_filename, new_ext);
} else {
strcpy(posext, new_ext);
}
return dst_filename;
}
void save_file(const char *filename, void *buffer, int length)
{
SDL_RWops *dst;
dst = SDL_RWFromFile(filename, "wb");
if (!dst) {
fprintf(stderr, "Can not create %s for writing\n", filename);
return;
}
SDL_RWwrite(dst, buffer, length, 1);
SDL_RWclose(dst);
}
void save_bmp(const char *src_filename, SDL_Surface *image)
{
char *dst_filename;
dst_filename = get_filename_ext(src_filename, ".bmp");
if (!dst_filename) {
return;
}
printf("Saving to %s\n", dst_filename);
SDL_SaveBMP(image, dst_filename);
free(dst_filename);
}
void save_tim(const char *src_filename, Uint8 *buffer, int length)
{
char *dst_filename;
dst_filename = get_filename_ext(src_filename, ".tim");
if (!dst_filename) {
return;
}
printf("Saving to %s\n", dst_filename);
save_file(dst_filename, buffer, length);
free(dst_filename);
}
void save_pak(const char *src_filename, Uint8 *buffer, int length)
{
char *dst_filename;
dst_filename = get_filename_ext(src_filename, ".pak");
if (!dst_filename) {
return;
}
printf("Saving to %s\n", dst_filename);
save_file(dst_filename, buffer, length);
free(dst_filename);
}
void save_raw(const char *src_filename, Uint8 *buffer, int length)
{
char *dst_filename;
dst_filename = get_filename_ext(src_filename, ".raw");
if (!dst_filename) {
return;
}
printf("Saving to %s\n", dst_filename);
save_file(dst_filename, buffer, length);
free(dst_filename);
}