ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/porting.C
Revision: 1.11
Committed: Sat Jan 6 14:42:29 2007 UTC (17 years, 4 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1 -0 lines
Log Message:
added some copyrights

File Contents

# User Rev Content
1 elmex 1.1 /*
2     CrossFire, A Multiplayer game for X-windows
3    
4 pippijn 1.11 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 elmex 1.1 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 root 1.7 The authors can be reached via e-mail at <crossfire@schmorp.de>
23 elmex 1.1 */
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 root 1.10 /* Need to pull in the HAVE_... values somehow */
34 elmex 1.1
35 root 1.10 #include <autoconf.h>
36 elmex 1.1
37 root 1.10 #include <cstdio>
38     #include <cstdlib>
39     #include <cstdarg>
40 root 1.5
41 root 1.10 #include <cctype>
42 elmex 1.1
43 root 1.10 #include <sys/stat.h>
44     #include <sys/wait.h>
45 elmex 1.1
46 root 1.10 #include <sys/param.h>
47 elmex 1.1
48 root 1.10 #include <unistd.h>
49 root 1.5
50 elmex 1.1 /* 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 root 1.4 void
57     remove_directory (const char *path)
58 elmex 1.1 {
59 root 1.4 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 root 1.5 {
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 root 1.4 closedir (dirp);
94     }
95     if (rmdir (path))
96     {
97     LOG (llevError, "Unable to remove directory %s\n", path);
98 elmex 1.1 }
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 root 1.4 char *
106     strcasestr_local (const char *s, const char *find)
107 elmex 1.1 {
108 root 1.4 char c, sc;
109     size_t len;
110 elmex 1.1
111 root 1.4 if ((c = *find++) != 0)
112     {
113     c = tolower (c);
114     len = strlen (find);
115     do
116 root 1.5 {
117     do
118     {
119     if ((sc = *s++) == 0)
120     return NULL;
121     }
122     while (tolower (sc) != c);
123     }
124 root 1.4 while (strncasecmp (s, find, len) != 0);
125     s--;
126     }
127     return (char *) s;
128 elmex 1.1 }
129    
130     /*
131     * Based on (n+1)^2 = n^2 + 2n + 1
132     * given that 1^2 = 1, then
133     * 2^2 = 1 + (2 + 1) = 1 + 3 = 4
134     * 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9
135     * 4^2 = 9 + (6 + 1) = 9 + 7 = 1 + 3 + 5 + 7 = 16
136     * ...
137     * In other words, a square number can be express as the sum of the
138     * series n^2 = 1 + 3 + ... + (2n-1)
139     */
140     int
141 root 1.4 isqrt (int n)
142 elmex 1.1 {
143 root 1.4 int result, sum, prev;
144 root 1.5
145 root 1.4 result = 0;
146     prev = sum = 1;
147     while (sum <= n)
148     {
149     prev += 2;
150     sum += prev;
151     ++result;
152     }
153     return result;
154 elmex 1.1 }
155    
156     /*
157     * returns a char-pointer to a static array, in which a representation
158     * of the decimal number given will be stored.
159     */
160 root 1.4 char *
161     ltostr10 (signed long n)
162     {
163 root 1.5 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
164     character for the trailing nul character */
165 root 1.4 snprintf (buf, sizeof (buf), "%ld", n);
166 elmex 1.1 return buf;
167     }
168 root 1.4
169     char *
170     doubletostr10 (double v)
171     {
172 elmex 1.1 static char tbuf[200];
173 root 1.5
174 root 1.4 sprintf (tbuf, "%f", v);
175 elmex 1.1 return tbuf;
176     }
177    
178     /**
179     * open_and_uncompress() first searches for the original filename. If it exist,
180     * then it opens it and returns the file-pointer.
181     */
182 root 1.4 FILE *
183     open_and_uncompress (const char *name, int flag, int *compressed)
184     {
185     *compressed = 0;
186     return fopen (name, "r");
187 elmex 1.1 }
188    
189     /*
190     * See open_and_uncompress().
191     */
192    
193 root 1.4 void
194 root 1.5 close_and_delete (FILE * fp, int compressed)
195 root 1.4 {
196     fclose (fp);
197 elmex 1.1 }
198    
199     /*
200     * If any directories in the given path doesn't exist, they are created.
201     */
202    
203 root 1.4 void
204     make_path_to_file (char *filename)
205 elmex 1.1 {
206 root 1.4 char buf[MAX_BUF], *cp = buf;
207     struct stat statbuf;
208 elmex 1.1
209 root 1.4 if (!filename || !*filename)
210     return;
211     strcpy (buf, filename);
212    
213     while ((cp = strchr (cp + 1, (int) '/')))
214     {
215     *cp = '\0';
216     if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))
217 root 1.5 {
218     if (mkdir (buf, SAVE_DIR_MODE))
219     {
220     LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno));
221     return;
222     }
223     }
224 root 1.4 *cp = '/';
225 elmex 1.1 }
226     }