Add key bindings for pause, message refresh.
[chocolate-doom.git] / src / m_misc.c
blobacab0a5b03605808d6bea0df967303ce098e210e
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2005 Simon Howard
6 //
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
20 // 02111-1307, USA.
22 // DESCRIPTION:
23 // Miscellaneous.
25 //-----------------------------------------------------------------------------
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <errno.h>
33 // for mkdir:
35 #ifdef _WIN32
36 #include <io.h>
37 #ifdef _MSC_VER
38 #include <direct.h>
39 #endif
40 #else
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #endif
45 #include "doomdef.h"
46 #include "doomstat.h"
48 #include "deh_main.h"
50 #include "i_swap.h"
51 #include "i_system.h"
52 #include "i_video.h"
53 #include "m_misc.h"
54 #include "v_video.h"
55 #include "w_wad.h"
56 #include "z_zone.h"
59 // Create a directory
62 void M_MakeDirectory(char *path)
64 #ifdef _WIN32
65 mkdir(path);
66 #else
67 mkdir(path, 0755);
68 #endif
71 // Check if a file exists
73 boolean M_FileExists(char *filename)
75 FILE *fstream;
77 fstream = fopen(filename, "r");
79 if (fstream != NULL)
81 fclose(fstream);
82 return true;
84 else
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)
99 long savedpos;
100 long length;
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);
112 return length;
116 // M_WriteFile
119 boolean M_WriteFile(char *name, void *source, int length)
121 FILE *handle;
122 int count;
124 handle = fopen(name, "wb");
126 if (handle == NULL)
127 return false;
129 count = fwrite(source, 1, length, handle);
130 fclose(handle);
132 if (count < length)
133 return false;
135 return true;
140 // M_ReadFile
143 int M_ReadFile(char *name, byte **buffer)
145 FILE *handle;
146 int count, length;
147 byte *buf;
149 handle = fopen(name, "rb");
150 if (handle == NULL)
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);
160 fclose (handle);
162 if (count < length)
163 I_Error ("Couldn't read file %s", name);
165 *buffer = buf;
166 return length;
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)
176 char *result;
177 char *tempdir;
179 #ifdef _WIN32
181 // Check the TEMP environment variable to find the location.
183 tempdir = getenv("TEMP");
185 if (tempdir == NULL)
187 tempdir = ".";
189 #else
190 // In Unix, just use /tmp.
192 tempdir = "/tmp";
193 #endif
195 result = Z_Malloc(strlen(tempdir) + strlen(s) + 2, PU_STATIC, 0);
196 sprintf(result, "%s%c%s", tempdir, DIR_SEPARATOR, s);
198 return result;