ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/porting.C
(Generate patch)

Comparing deliantra/server/common/porting.C (file contents):
Revision 1.12 by root, Mon Jan 15 01:25:41 2007 UTC vs.
Revision 1.21 by root, Mon Sep 29 08:52:35 2008 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 3 *
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 7 *
8 This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version. 11 * (at your option) any later version.
12 12 *
13 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 16 * GNU General Public License for more details.
17 17 *
18 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 *
21 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 The authors can be reached via e-mail at <crossfire@schmorp.de>
23*/ 22 */
24 23
25/* This file contains various functions that are not really unique for 24/* This file contains various functions that are not really unique for
26 * crossfire, but rather provides what should be standard functions 25 * crossfire, but rather provides what should be standard functions
27 * for systems that do not have them. In this way, most of the 26 * for systems that do not have them. In this way, most of the
28 * nasty system dependent stuff is contained here, with the program 27 * nasty system dependent stuff is contained here, with the program
47 46
48#include <unistd.h> 47#include <unistd.h>
49 48
50/* Has to be after above includes so we don't redefine some values */ 49/* Has to be after above includes so we don't redefine some values */
51#include "global.h" 50#include "global.h"
52
53static unsigned int curtmp = 0;
54
55/* This function removes everything in the directory. */
56void
57remove_directory (const char *path)
58{
59 DIR *dirp;
60 char buf[MAX_BUF];
61 struct stat statbuf;
62 int status;
63
64 if ((dirp = opendir (path)) != NULL)
65 {
66 struct dirent *de;
67
68 for (de = readdir (dirp); de; de = readdir (dirp))
69 {
70 /* Don't remove '.' or '..' In theory we should do a better
71 * check for .., but the directories we are removing are fairly
72 * limited and should not have dot files in them.
73 */
74 if (de->d_name[0] == '.')
75 continue;
76
77 /* Linux actually has a type field in the dirent structure,
78 * but that is not portable - stat should be portable
79 */
80 status = stat (de->d_name, &statbuf);
81 if ((status != -1) && (S_ISDIR (statbuf.st_mode)))
82 {
83 sprintf (buf, "%s/%s", path, de->d_name);
84 remove_directory (buf);
85 continue;
86 }
87 sprintf (buf, "%s/%s", path, de->d_name);
88 if (unlink (buf))
89 {
90 LOG (llevError, "Unable to remove directory %s\n", path);
91 }
92 }
93 closedir (dirp);
94 }
95 if (rmdir (path))
96 {
97 LOG (llevError, "Unable to remove directory %s\n", path);
98 }
99}
100 51
101#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 52#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
102islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 53islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
103#define MBASE ('z' - 'a' + 1 + 10) 54#define MBASE ('z' - 'a' + 1 + 10)
104 55
125 s--; 76 s--;
126 } 77 }
127 return (char *) s; 78 return (char *) s;
128} 79}
129 80
130/*
131 * returns a char-pointer to a static array, in which a representation
132 * of the decimal number given will be stored.
133 */
134char *
135ltostr10 (signed long n)
136{
137 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
138 character for the trailing nul character */
139 snprintf (buf, sizeof (buf), "%ld", n);
140 return buf;
141}
142
143char *
144doubletostr10 (double v)
145{
146 static char tbuf[200];
147
148 sprintf (tbuf, "%f", v);
149 return tbuf;
150}
151
152/** 81/**
153 * open_and_uncompress() first searches for the original filename. If it exist, 82 * open_and_uncompress() first searches for the original filename. If it exist,
154 * then it opens it and returns the file-pointer. 83 * then it opens it and returns the file-pointer.
155 */ 84 */
156FILE * 85FILE *
168close_and_delete (FILE * fp, int compressed) 97close_and_delete (FILE * fp, int compressed)
169{ 98{
170 fclose (fp); 99 fclose (fp);
171} 100}
172 101
173/*
174 * If any directories in the given path doesn't exist, they are created.
175 */
176
177void
178make_path_to_file (char *filename)
179{
180 char buf[MAX_BUF], *cp = buf;
181 struct stat statbuf;
182
183 if (!filename || !*filename)
184 return;
185 strcpy (buf, filename);
186
187 while ((cp = strchr (cp + 1, (int) '/')))
188 {
189 *cp = '\0';
190 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))
191 {
192 if (mkdir (buf, SAVE_DIR_MODE))
193 {
194 LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno));
195 return;
196 }
197 }
198 *cp = '/';
199 }
200}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines