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.17 by root, Mon May 28 21:21:40 2007 UTC vs.
Revision 1.21 by root, Mon Sep 29 08:52:35 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines