1 /*
2   Pacman Arena
3   Copyright (C) 2003 Nuno Subtil
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   as published by the Free Software Foundation; either version 2
8   of the License, or (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19 
20 static const char cvsid[] =
21   "$Id: file.c,v 1.5 2003/11/23 00:06:39 nsubtil Exp $";
22 
23 #include <SDL.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 
28 #include "include/file.h"
29 
file_open(char * file)30 FILE *file_open(char *file)
31 {
32 	char fname[FILE_NAME_LEN];
33 
34 	if(strlen(file) + strlen(FILE_BASE_PATH) + 2 > FILE_NAME_LEN)
35 	{
36 		printf("file_open: %s: that's too much for me\n", file);
37 		exit(1);
38 	}
39 
40 	sprintf(fname, "%s/%s", FILE_BASE_PATH, file);
41 	return fopen(fname, "rb");
42 }
43 
44 /*
45   all data on files is in little-endian format
46 */
file_read_values32(void * ret,int n,FILE * fp)47 void file_read_values32(void *ret, int n, FILE *fp)
48 {
49 #ifdef SDL_LIL_ENDIAN
50 	fread(ret, 4, n, fp);
51 #else
52 	int c, *i;
53 
54 	fread(ret, 4, n, fp);
55 	i = (int *)ret;
56 
57 	for(c = 0; c < n; c++)
58 		i[c] = SDL_Swap32(i[c]);
59 #endif /* SDL_LIL_ENDIAN */
60 }
61 
file_write_values32(void * src,int n,FILE * fp)62 void file_write_values32(void *src, int n, FILE *fp)
63 {
64 #ifdef SDL_LIL_ENDIAN
65 	fwrite(src, 4, n, fp);
66 #else
67 	int c, tmp, *i;
68 
69 	i = (int *)src;
70 	for(c = 0; c < n; c++)
71 	{
72 		tmp = SDL_Swap32(i[c]);
73 		fwrite(&tmp, 4, 1, fp);
74 	}
75 #endif /* SDL_LIL_ENDIAN */
76 }
77 
file_make_fname(char * file)78 char *file_make_fname(char *file)
79 {
80 	static char fname[FILE_NAME_LEN];
81 
82 	if(strlen(FILE_BASE_PATH) + strlen(file) + 2 > FILE_NAME_LEN)
83 	{
84 		printf("file_make_fname: too much!\n");
85 		exit(1);
86 	}
87 
88 	sprintf(fname, "%s/%s", FILE_BASE_PATH, file);
89 	return fname;
90 }
91