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.19 by root, Thu Nov 8 19:43:23 2007 UTC vs.
Revision 1.22 by root, Mon Oct 12 14:00:57 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra 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 * Deliantra 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 3 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, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24/* This file contains various functions that are not really unique for 25/* This file contains various functions that are not really unique for
47#include <unistd.h> 48#include <unistd.h>
48 49
49/* 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 */
50#include "global.h" 51#include "global.h"
51 52
52/* This function removes everything in the directory. */
53void
54remove_directory (const char *path)
55{
56 DIR *dirp;
57 char buf[MAX_BUF];
58 struct stat statbuf;
59 int status;
60
61 if ((dirp = opendir (path)) != NULL)
62 {
63 struct dirent *de;
64
65 for (de = readdir (dirp); de; de = readdir (dirp))
66 {
67 /* Don't remove '.' or '..' In theory we should do a better
68 * check for .., but the directories we are removing are fairly
69 * limited and should not have dot files in them.
70 */
71 if (de->d_name[0] == '.')
72 continue;
73
74 /* Linux actually has a type field in the dirent structure,
75 * but that is not portable - stat should be portable
76 */
77 status = stat (de->d_name, &statbuf);
78 if ((status != -1) && (S_ISDIR (statbuf.st_mode)))
79 {
80 sprintf (buf, "%s/%s", path, de->d_name);
81 remove_directory (buf);
82 continue;
83 }
84 sprintf (buf, "%s/%s", path, de->d_name);
85 if (unlink (buf))
86 {
87 LOG (llevError, "Unable to remove directory %s\n", path);
88 }
89 }
90 closedir (dirp);
91 }
92 if (rmdir (path))
93 {
94 LOG (llevError, "Unable to remove directory %s\n", path);
95 }
96}
97
98#define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 53#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
99islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 54islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
100#define MBASE ('z' - 'a' + 1 + 10) 55#define MBASE ('z' - 'a' + 1 + 10)
101 56
102char * 57char *
122 s--; 77 s--;
123 } 78 }
124 return (char *) s; 79 return (char *) s;
125} 80}
126 81
127/*
128 * returns a char-pointer to a static array, in which a representation
129 * of the decimal number given will be stored.
130 */
131char *
132ltostr10 (signed long n)
133{
134 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
135 character for the trailing nul character */
136 snprintf (buf, sizeof (buf), "%ld", n);
137 return buf;
138}
139
140char *
141doubletostr10 (double v)
142{
143 static char tbuf[200];
144
145 sprintf (tbuf, "%f", v);
146 return tbuf;
147}
148
149/** 82/**
150 * 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,
151 * then it opens it and returns the file-pointer. 84 * then it opens it and returns the file-pointer.
152 */ 85 */
153FILE * 86FILE *
165close_and_delete (FILE * fp, int compressed) 98close_and_delete (FILE * fp, int compressed)
166{ 99{
167 fclose (fp); 100 fclose (fp);
168} 101}
169 102
170/*
171 * If any directories in the given path doesn't exist, they are created.
172 */
173
174void
175make_path_to_file (char *filename)
176{
177 char buf[MAX_BUF], *cp = buf;
178 struct stat statbuf;
179
180 if (!filename || !*filename)
181 return;
182
183 assign (buf, filename);
184
185 while ((cp = strchr (cp + 1, (int) '/')))
186 {
187 *cp = '\0';
188 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))
189 {
190 if (mkdir (buf, SAVE_DIR_MODE))
191 {
192 LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno));
193 return;
194 }
195 }
196 *cp = '/';
197 }
198}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines