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.13 by pippijn, Mon Jan 15 21:06:18 2007 UTC vs.
Revision 1.22 by root, Mon Oct 12 14:00:57 2009 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 it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * 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 Affero GNU General Public License
19 * along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25/* This file contains various functions that are not really unique for 25/* This file contains various functions that are not really unique for
26 * crossfire, but rather provides what should be standard functions 26 * crossfire, but rather provides what should be standard functions
27 * for systems that do not have them. In this way, most of the 27 * for systems that do not have them. In this way, most of the
48#include <unistd.h> 48#include <unistd.h>
49 49
50/* Has to be after above includes so we don't redefine some values */ 50/* Has to be after above includes so we don't redefine some values */
51#include "global.h" 51#include "global.h"
52 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
101#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 53#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
102islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 54islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
103#define MBASE ('z' - 'a' + 1 + 10) 55#define MBASE ('z' - 'a' + 1 + 10)
104 56
105char * 57char *
125 s--; 77 s--;
126 } 78 }
127 return (char *) s; 79 return (char *) s;
128} 80}
129 81
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/** 82/**
153 * open_and_uncompress() first searches for the original filename. If it exist, 83 * open_and_uncompress() first searches for the original filename. If it exist,
154 * then it opens it and returns the file-pointer. 84 * then it opens it and returns the file-pointer.
155 */ 85 */
156FILE * 86FILE *
168close_and_delete (FILE * fp, int compressed) 98close_and_delete (FILE * fp, int compressed)
169{ 99{
170 fclose (fp); 100 fclose (fp);
171} 101}
172 102
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