ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/porting.C
Revision: 1.12
Committed: Mon Jan 15 01:25:41 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +0 -26 lines
Log Message:
- faster implementation for isqrt
- add fast idistance approximation

now find_dir_2 has moved to the top of the profiling output for
mlab/cwdccastleofmarquis3, followed by get_rangevector, hit_map
and ok_to_put_more.

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
6 Copyright (C) 1992 Frank Tore Johansen
7
8 This program is free software; you can redistribute it and/or modify
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
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
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
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 The authors can be reached via e-mail at <crossfire@schmorp.de>
23 */
24
25 /* This file contains various functions that are not really unique for
26 * crossfire, but rather provides what should be standard functions
27 * for systems that do not have them. In this way, most of the
28 * nasty system dependent stuff is contained here, with the program
29 * calling these functions.
30 */
31
32
33 /* Need to pull in the HAVE_... values somehow */
34
35 #include <autoconf.h>
36
37 #include <cstdio>
38 #include <cstdlib>
39 #include <cstdarg>
40
41 #include <cctype>
42
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45
46 #include <sys/param.h>
47
48 #include <unistd.h>
49
50 /* Has to be after above includes so we don't redefine some values */
51 #include "global.h"
52
53 static unsigned int curtmp = 0;
54
55 /* This function removes everything in the directory. */
56 void
57 remove_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' : \
102 islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
103 #define MBASE ('z' - 'a' + 1 + 10)
104
105 char *
106 strcasestr_local (const char *s, const char *find)
107 {
108 char c, sc;
109 size_t len;
110
111 if ((c = *find++) != 0)
112 {
113 c = tolower (c);
114 len = strlen (find);
115 do
116 {
117 do
118 {
119 if ((sc = *s++) == 0)
120 return NULL;
121 }
122 while (tolower (sc) != c);
123 }
124 while (strncasecmp (s, find, len) != 0);
125 s--;
126 }
127 return (char *) s;
128 }
129
130 /*
131 * returns a char-pointer to a static array, in which a representation
132 * of the decimal number given will be stored.
133 */
134 char *
135 ltostr10 (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
143 char *
144 doubletostr10 (double v)
145 {
146 static char tbuf[200];
147
148 sprintf (tbuf, "%f", v);
149 return tbuf;
150 }
151
152 /**
153 * open_and_uncompress() first searches for the original filename. If it exist,
154 * then it opens it and returns the file-pointer.
155 */
156 FILE *
157 open_and_uncompress (const char *name, int flag, int *compressed)
158 {
159 *compressed = 0;
160 return fopen (name, "r");
161 }
162
163 /*
164 * See open_and_uncompress().
165 */
166
167 void
168 close_and_delete (FILE * fp, int compressed)
169 {
170 fclose (fp);
171 }
172
173 /*
174 * If any directories in the given path doesn't exist, they are created.
175 */
176
177 void
178 make_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 }