1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2005 Simon Howard
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 //-----------------------------------------------------------------------------
42 #include <sys/types.h>
62 void M_MakeDirectory(char *path
)
71 // Check if a file exists
73 boolean
M_FileExists(char *filename
)
77 fstream
= fopen(filename
, "r");
86 // If we can't open because the file is a directory, the
87 // "file" exists at least!
89 return errno
== EISDIR
;
94 // Determine the length of an open file.
97 long M_FileLength(FILE *handle
)
102 // save the current position in the file
103 savedpos
= ftell(handle
);
105 // jump to the end and find the length
106 fseek(handle
, 0, SEEK_END
);
107 length
= ftell(handle
);
109 // go back to the old location
110 fseek(handle
, savedpos
, SEEK_SET
);
119 boolean
M_WriteFile(char *name
, void *source
, int length
)
124 handle
= fopen(name
, "wb");
129 count
= fwrite(source
, 1, length
, handle
);
143 int M_ReadFile(char *name
, byte
**buffer
)
149 handle
= fopen(name
, "rb");
151 I_Error ("Couldn't read file %s", name
);
153 // find the size of the file by seeking to the end and
154 // reading the current position
156 length
= M_FileLength(handle
);
158 buf
= Z_Malloc (length
, PU_STATIC
, NULL
);
159 count
= fread(buf
, 1, length
, handle
);
163 I_Error ("Couldn't read file %s", name
);
169 // Returns the path to a temporary file of the given name, stored
170 // inside the system temporary directory.
172 // The returned value must be freed with Z_Free after use.
174 char *M_TempFile(char *s
)
181 // Check the TEMP environment variable to find the location.
183 tempdir
= getenv("TEMP");
190 // In Unix, just use /tmp.
195 result
= Z_Malloc(strlen(tempdir
) + strlen(s
) + 2, PU_STATIC
, 0);
196 sprintf(result
, "%s%c%s", tempdir
, DIR_SEPARATOR
, s
);