ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.4 by root, Mon Sep 4 11:07:59 2006 UTC vs.
Revision 1.37 by root, Mon Jan 15 02:39:40 2007 UTC

1/*
2 * static char *rcsid_utils_c =
3 * "$Id: utils.C,v 1.4 2006/09/04 11:07:59 root Exp $";
4 */
5
6/* 1/*
7 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
8 3
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
9 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10 Copyright (C) 1992 Frank Tore Johansen 6 Copyright (C) 1992 Frank Tore Johansen
11 7
12 This program is free software; you can redistribute it and/or modify 8 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by 9 it under the terms of the GNU General Public License as published by
21 17
22 You should have received a copy of the GNU General Public License 18 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software 19 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 21
26 The authors can be reached via e-mail at crossfire-devel@real-time.com 22 The authors can be reached via e-mail at <crossfire@schmorp.de>
27*/ 23*/
28 24
29/* 25/*
30 * General convenience functions for crossfire. 26 * General convenience functions for crossfire.
31 */ 27 */
28
29#include <cstdlib>
30#include <sys/types.h>
31#include <unistd.h>
32#include <sys/time.h>
33#include <time.h>
34#include <signal.h>
32 35
33#include <global.h> 36#include <global.h>
34#include <funcpoint.h> 37#include <funcpoint.h>
35#include <material.h> 38#include <material.h>
36 39
50 * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. 53 * and if goodbad is non-zero, luck increases the roll, if zero, it decreases.
51 * Generally, op should be the player/caster/hitter requesting the roll, 54 * Generally, op should be the player/caster/hitter requesting the roll,
52 * not the recipient (ie, the poor slob getting hit). [garbled 20010916] 55 * not the recipient (ie, the poor slob getting hit). [garbled 20010916]
53 */ 56 */
54 57
58int
55int random_roll(int min, int max, const object *op, int goodbad) { 59random_roll (int min, int max, const object *op, int goodbad)
60{
56 int omin, diff, luck, base, ran; 61 int omin, diff, luck, base, ran;
57 62
58 omin = min; 63 omin = min;
59 diff = max - min + 1; 64 diff = max - min + 1;
60 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ 65 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */
61 66
62 if (max < 1 || diff < 1) { 67 if (max < 1 || diff < 1)
68 {
63 LOG(llevError, "Calling random_roll with min=%d max=%d\n", min, max); 69 LOG (llevError, "Calling random_roll with min=%d max=%d\n", min, max);
64 return(min); /* avoids a float exception */ 70 return (min); /* avoids a float exception */
65 } 71 }
66 72
67 ran = RANDOM(); 73 ran = RANDOM ();
68 74
69 if (op->type != PLAYER) 75 if (op->type != PLAYER)
70 return((ran%diff)+min); 76 return ((ran % diff) + min);
71 77
72 luck = op->stats.luck; 78 luck = op->stats.luck;
73 if (RANDOM()%base < MIN(10, abs(luck))) { 79 if (RANDOM () % base < MIN (10, abs (luck)))
80 {
74 /* we have a winner */ 81 /* we have a winner */
75 ((luck > 0) ? (luck = 1) : (luck = -1)); 82 ((luck > 0) ? (luck = 1) : (luck = -1));
76 diff -= luck; 83 diff -= luck;
77 if (diff < 1) 84 if (diff < 1)
78 return(omin); /*check again*/ 85 return (omin); /*check again */
79 ((goodbad) ? (min += luck) : (diff)); 86 ((goodbad) ? (min += luck) : (diff));
80 87
81 return(MAX(omin, MIN(max, (ran%diff)+min))); 88 return (MAX (omin, MIN (max, (ran % diff) + min)));
82 } 89 }
83 return((ran%diff)+min); 90 return ((ran % diff) + min);
84} 91}
85 92
86/* 93/*
87 * This is a 64 bit version of random_roll above. This is needed 94 * This is a 64 bit version of random_roll above. This is needed
88 * for exp loss calculations for players changing religions. 95 * for exp loss calculations for players changing religions.
89 */ 96 */
90 97
98sint64
91sint64 random_roll64(sint64 min, sint64 max, const object *op, int goodbad) { 99random_roll64 (sint64 min, sint64 max, const object *op, int goodbad)
100{
92 sint64 omin, diff, luck, ran; 101 sint64 omin, diff, luck, ran;
93 int base; 102 int base;
94 103
95 omin = min; 104 omin = min;
96 diff = max - min + 1; 105 diff = max - min + 1;
97 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ 106 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */
98 107
99 if (max < 1 || diff < 1) { 108 if (max < 1 || diff < 1)
100#ifndef WIN32 109 {
101 LOG(llevError, "Calling random_roll with min=%lld max=%lld\n", min, max);
102#else
103 LOG(llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); 110 LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max);
104#endif
105 return(min); /* avoids a float exception */ 111 return (min); /* avoids a float exception */
106 } 112 }
107 113
108 /* Don't know of a portable call to get 64 bit random values. 114 /* Don't know of a portable call to get 64 bit random values.
109 * So make a call to get two 32 bit random numbers, and just to 115 * So make a call to get two 32 bit random numbers, and just to
110 * a little byteshifting. Do make sure the first one is only 116 * a little byteshifting. Do make sure the first one is only
111 * 32 bit, so we don't get skewed results 117 * 32 bit, so we don't get skewed results
112 */ 118 */
113 ran = (RANDOM() & 0xffffffff) | ((sint64)RANDOM() << 32); 119 ran = (RANDOM () & 0xffffffff) | ((sint64) RANDOM () << 32);
114 120
115 if (op->type != PLAYER) 121 if (op->type != PLAYER)
116 return((ran%diff)+min); 122 return ((ran % diff) + min);
117 123
118 luck = op->stats.luck; 124 luck = op->stats.luck;
119 if (RANDOM()%base < MIN(10, abs(luck))) { 125 if (RANDOM () % base < MIN (10, abs (luck)))
126 {
120 /* we have a winner */ 127 /* we have a winner */
121 ((luck > 0) ? (luck = 1) : (luck = -1)); 128 ((luck > 0) ? (luck = 1) : (luck = -1));
122 diff -= luck; 129 diff -= luck;
123 if (diff < 1) 130 if (diff < 1)
124 return(omin); /*check again*/ 131 return (omin); /*check again */
125 ((goodbad) ? (min += luck) : (diff)); 132 ((goodbad) ? (min += luck) : (diff));
126 133
127 return(MAX(omin, MIN(max, (ran%diff)+min))); 134 return (MAX (omin, MIN (max, (ran % diff) + min)));
128 } 135 }
129 return((ran%diff)+min); 136 return ((ran % diff) + min);
130} 137}
131 138
132/* 139/*
133 * Roll a number of dice (2d3, 4d6). Uses op to determine luck, 140 * Roll a number of dice (2d3, 4d6). Uses op to determine luck,
134 * If goodbad is non-zero, luck increases the roll, if zero, it decreases. 141 * If goodbad is non-zero, luck increases the roll, if zero, it decreases.
135 * Generally, op should be the player/caster/hitter requesting the roll, 142 * Generally, op should be the player/caster/hitter requesting the roll,
136 * not the recipient (ie, the poor slob getting hit). 143 * not the recipient (ie, the poor slob getting hit).
137 * The args are num D size (ie 4d6) [garbled 20010916] 144 * The args are num D size (ie 4d6) [garbled 20010916]
138 */ 145 */
139 146
147int
140int die_roll(int num, int size, const object *op, int goodbad) { 148die_roll (int num, int size, const object *op, int goodbad)
149{
141 int min, diff, luck, total, i, gotlucky, base, ran; 150 int min, diff, luck, total, i, gotlucky, base, ran;
142 151
143 diff = size; 152 diff = size;
144 min = 1; 153 min = 1;
145 luck = total = gotlucky = 0; 154 luck = total = gotlucky = 0;
146 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ 155 ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */
147 if (size < 2 || diff < 1) { 156 if (size < 2 || diff < 1)
157 {
148 LOG(llevError, "Calling die_roll with num=%d size=%d\n", num, size); 158 LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size);
149 return(num); /* avoids a float exception */ 159 return (num); /* avoids a float exception */
150 } 160 }
151 161
152 if (op->type == PLAYER) 162 if (op->type == PLAYER)
153 luck = op->stats.luck; 163 luck = op->stats.luck;
154 164
155 for (i = 0; i < num; i++) { 165 for (i = 0; i < num; i++)
166 {
156 if (RANDOM()%base < MIN(10, abs(luck)) && !gotlucky) { 167 if (RANDOM () % base < MIN (10, abs (luck)) && !gotlucky)
168 {
157 /* we have a winner */ 169 /* we have a winner */
158 gotlucky++; 170 gotlucky++;
159 ((luck > 0) ? (luck = 1) : (luck = -1)); 171 ((luck > 0) ? (luck = 1) : (luck = -1));
160 diff -= luck; 172 diff -= luck;
161 if (diff < 1) 173 if (diff < 1)
162 return(num); /*check again*/ 174 return (num); /*check again */
163 ((goodbad) ? (min += luck) : (diff)); 175 ((goodbad) ? (min += luck) : (diff));
164 ran = RANDOM(); 176 ran = RANDOM ();
165 total += MAX(1, MIN(size, (ran%diff)+min)); 177 total += MAX (1, MIN (size, (ran % diff) + min));
178 }
166 } else { 179 else
180 {
167 total += RANDOM()%size+1; 181 total += RANDOM () % size + 1;
168 } 182 }
169 } 183 }
170 return(total); 184 return (total);
171} 185}
172 186
173/* 187/*
174 * Another convenience function. Returns a number between min and max. 188 * Another convenience function. Returns a number between min and max.
175 * It is suggested one use these functions rather than RANDOM()%, as it 189 * It is suggested one use these functions rather than RANDOM()%, as it
176 * would appear that a number of off-by-one-errors exist due to improper 190 * would appear that a number of off-by-one-errors exist due to improper
177 * use of %. This should also prevent SIGFPE. 191 * use of %. This should also prevent SIGFPE.
178 */ 192 */
179 193
194int
180int rndm(int min, int max) 195rndm (int min, int max)
181{ 196{
182 int diff; 197 int diff;
183 198
184 diff = max - min + 1; 199 diff = max - min + 1;
185 if (max < 1 || diff < 1) 200 if (max < 1 || diff < 1)
186 return(min); 201 return (min);
187 202
188 return(RANDOM()%diff+min); 203 return (RANDOM () % diff + min);
189} 204}
190 205
191/* decay and destroy persihable items in a map */ 206/* decay and destroy perishable items in a map */
192 207void
193void decay_objects(mapstruct *m) 208maptile::decay_objects ()
194{ 209{
195 int x, y, destroy; 210 if (!spaces)
196 object *op, *otmp;
197
198 if (m->unique)
199 return; 211 return;
200 212
201 for (x=0; x < MAP_WIDTH(m); x++) 213 for (mapspace *ms = spaces + size (); ms-- > spaces; )
202 for (y=0; y < MAP_HEIGHT(m); y++) 214 for (object *above, *op = ms->bot; op; op = above)
203 for (op = get_map_ob(m, x, y); op; op = otmp) { 215 {
216 above = op->above;
217
204 destroy = 0; 218 bool destroy = 0;
205 otmp = op->above; 219
220 // do not decay anything above unique floor tiles (yet :)
206 if (QUERY_FLAG(op,FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) 221 if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE))
207 break; 222 break;
223
208 if (QUERY_FLAG(op, FLAG_IS_FLOOR) || 224 if (QUERY_FLAG (op, FLAG_IS_FLOOR)
209 QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) || 225 || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL)
210 QUERY_FLAG(op, FLAG_OBJ_SAVE_ON_OVL) || 226 || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL)
211 QUERY_FLAG(op, FLAG_UNIQUE) || 227 || QUERY_FLAG (op, FLAG_UNIQUE)
212 QUERY_FLAG(op, FLAG_OVERLAY_FLOOR) || 228 || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)
213 QUERY_FLAG(op, FLAG_UNPAID) || IS_LIVE(op)) 229 || QUERY_FLAG (op, FLAG_UNPAID)
214 continue; 230 || op->is_alive ())
215 /* otherwise, we decay and destroy */ 231 ; // do not decay
216 if (IS_WEAPON(op)) { 232 else if (op->is_weapon ())
233 {
217 op->stats.dam--; 234 op->stats.dam--;
218 if (op->stats.dam < 0) 235 if (op->stats.dam < 0)
219 destroy = 1; 236 destroy = 1;
220 } else if (IS_ARMOR(op)) {
221 op->stats.ac--;
222 if (op->stats.ac < 0)
223 destroy = 1;
224 } else if (op->type == FOOD) {
225 op->stats.food -= rndm(5,20);
226 if (op->stats.food < 0)
227 destroy = 1;
228 } else {
229 if (op->material & M_PAPER || op->material & M_LEATHER ||
230 op->material & M_WOOD || op->material & M_ORGANIC ||
231 op->material & M_CLOTH || op->material & M_LIQUID)
232 destroy = 1;
233 if (op->material & M_IRON && rndm(1,5) == 1)
234 destroy = 1;
235 if (op->material & M_GLASS && rndm(1,2) == 1)
236 destroy = 1;
237 if ((op->material & M_STONE || op->material & M_ADAMANT) &&
238 rndm(1,10) == 1)
239 destroy = 1;
240 if ((op->material & M_SOFT_METAL || op->material & M_BONE) &&
241 rndm(1,3) == 1)
242 destroy = 1;
243 if (op->material & M_ICE && MAP_TEMP(m) > 32)
244 destroy = 1;
245 }
246 /* adjust overall chance below */
247 if (destroy && rndm(0, 1)) {
248 remove_ob(op);
249 free_object(op);
250 }
251 } 237 }
238 else if (op->is_armor ())
239 {
240 op->stats.ac--;
241 if (op->stats.ac < 0)
242 destroy = 1;
243 }
244 else if (op->type == FOOD)
245 {
246 op->stats.food -= rndm (5, 20);
247 if (op->stats.food < 0)
248 destroy = 1;
249 }
250 else
251 {
252 int mat = op->material;
253
254 if (mat & M_PAPER
255 || mat & M_LEATHER
256 || mat & M_WOOD
257 || mat & M_ORGANIC
258 || mat & M_CLOTH
259 || mat & M_LIQUID
260 || (mat & M_IRON && rndm (1, 5) == 1)
261 || (mat & M_GLASS && rndm (1, 2) == 1)
262 || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1)
263 || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1)
264 || (mat & M_ICE && temp > 32))
265 destroy = 1;
266 }
267
268 /* adjust overall chance below */
269 if (destroy && rndm (0, 1))
270 op->destroy ();
271 }
252} 272}
253 273
254/* convert materialname to materialtype_t */ 274/* convert materialname to materialtype_t */
255 275
276materialtype_t *
256materialtype_t *name_to_material(const char *name) 277name_to_material (const char *name)
257{ 278{
258 materialtype_t *mt, *nmt; 279 materialtype_t *mt, *nmt;
259 280
260 mt = NULL; 281 mt = NULL;
261 for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt=nmt->next) { 282 for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt = nmt->next)
283 {
262 if (strcmp(name, nmt->name) == 0) { 284 if (strcmp (name, nmt->name) == 0)
285 {
263 mt = nmt; 286 mt = nmt;
264 break; 287 break;
265 } 288 }
266 } 289 }
267 return mt; 290 return mt;
268} 291}
269 292
270/* when doing transmutation of objects, we have to recheck the resistances, 293/* when doing transmutation of objects, we have to recheck the resistances,
271 * as some that did not apply previously, may apply now. 294 * as some that did not apply previously, may apply now.
272 */ 295 */
273 296
297void
274void transmute_materialname(object *op, const object *change) 298transmute_materialname (object *op, const object *change)
275{ 299{
276 materialtype_t *mt; 300 materialtype_t *mt;
277 int j; 301 int j;
278 302
279 if (op->materialname == NULL) 303 if (op->materialname == NULL)
280 return; 304 return;
281 305
282 if (change->materialname != NULL && 306 if (change->materialname != NULL && strcmp (op->materialname, change->materialname))
283 strcmp(op->materialname, change->materialname))
284 return; 307 return;
285 308
286 if (!IS_ARMOR(op)) 309 if (!op->is_armor ())
287 return; 310 return;
288 311
289 mt = name_to_material(op->materialname); 312 mt = name_to_material (op->materialname);
290 if (!mt) { 313 if (!mt)
314 {
291 LOG(llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->name, &op->name, &op->materialname); 315 LOG (llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->name, &op->name, &op->materialname);
292 return; 316 return;
293 } 317 }
294 318
295 for (j=0; j < NROFATTACKS; j++) 319 for (j = 0; j < NROFATTACKS; j++)
296 if (op->resist[j] == 0 && change->resist[j] != 0) { 320 if (op->resist[j] == 0 && change->resist[j] != 0)
321 {
297 op->resist[j] += mt->mod[j]; 322 op->resist[j] += mt->mod[j];
298 if (op->resist[j] > 100) 323 if (op->resist[j] > 100)
299 op->resist[j] = 100; 324 op->resist[j] = 100;
300 if (op->resist[j] < -100) 325 if (op->resist[j] < -100)
301 op->resist[j] = -100; 326 op->resist[j] = -100;
302 } 327 }
303} 328}
304 329
305/* set the materialname and type for an item */ 330/* set the materialname and type for an item */
331void
306void set_materialname(object *op, int difficulty, materialtype_t *nmt) 332set_materialname (object *op, int difficulty, materialtype_t *nmt)
307{ 333{
308 materialtype_t *mt, *lmt; 334 materialtype_t *mt, *lmt;
335
336#ifdef NEW_MATERIAL_CODE
309 int j; 337 int j;
338#endif
310 339
311 if (op->materialname != NULL) 340 if (op->materialname != NULL)
312 return; 341 return;
313 342
314 343
315 344
316 if (nmt == NULL) { 345 if (nmt == NULL)
346 {
317 lmt = NULL; 347 lmt = NULL;
318#ifndef NEW_MATERIAL_CODE 348#ifndef NEW_MATERIAL_CODE
319 for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { 349 for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next)
350 {
320 if (op->material & mt->material) { 351 if (op->material & mt->material)
352 {
321 lmt = mt; 353 lmt = mt;
354 break;
355 }
356 }
357
358#else
359 for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next)
360 {
361 if (op->material & mt->material && rndm (1, 100) <= mt->chance &&
362 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
363 {
364 lmt = mt;
365 if (!(op->is_weapon () || op->is_armor ()))
322 break; 366 break;
323 } 367 }
324 } 368 }
369#endif
370 }
371 else
372 {
373 lmt = nmt;
374 }
325 375
376 if (lmt != NULL)
377 {
378#ifndef NEW_MATERIAL_CODE
379 op->materialname = lmt->name;
380 return;
326#else 381#else
327 for (mt = materialt; mt != NULL && mt->next != NULL; mt=mt->next) { 382
328 if (op->material & mt->material && rndm(1, 100) <= mt->chance && 383 if (op->stats.dam && op->is_weapon ())
329 difficulty >= mt->difficulty &&
330 (op->magic >= mt->magic || mt->magic == 0)) {
331 lmt = mt;
332 if (!(IS_WEAPON(op) || IS_ARMOR(op)))
333 break;
334 }
335 } 384 {
336#endif
337 } else {
338 lmt = nmt;
339 }
340
341 if (lmt != NULL) {
342#ifndef NEW_MATERIAL_CODE
343 op->materialname = lmt->name;
344 return;
345#else
346
347 if (op->stats.dam && IS_WEAPON(op)) {
348 op->stats.dam += lmt->damage; 385 op->stats.dam += lmt->damage;
349 if (op->stats.dam < 1) 386 if (op->stats.dam < 1)
350 op->stats.dam = 1; 387 op->stats.dam = 1;
351 } 388 }
352 if (op->stats.sp && op->type == BOW) 389 if (op->stats.sp && op->type == BOW)
353 op->stats.sp += lmt->sp; 390 op->stats.sp += lmt->sp;
354 if (op->stats.wc && IS_WEAPON(op)) 391 if (op->stats.wc && op->is_weapon ())
355 op->stats.wc += lmt->wc; 392 op->stats.wc += lmt->wc;
356 if (IS_ARMOR(op)) { 393 if (op->is_armor ())
394 {
357 if (op->stats.ac) 395 if (op->stats.ac)
358 op->stats.ac += lmt->ac; 396 op->stats.ac += lmt->ac;
359 for (j=0; j < NROFATTACKS; j++) 397 for (j = 0; j < NROFATTACKS; j++)
360 if (op->resist[j] != 0) { 398 if (op->resist[j] != 0)
399 {
361 op->resist[j] += lmt->mod[j]; 400 op->resist[j] += lmt->mod[j];
362 if (op->resist[j] > 100) 401 if (op->resist[j] > 100)
363 op->resist[j] = 100; 402 op->resist[j] = 100;
364 if (op->resist[j] < -100) 403 if (op->resist[j] < -100)
365 op->resist[j] = -100; 404 op->resist[j] = -100;
366 } 405 }
367 } 406 }
368 op->materialname = add_string(lmt->name); 407 op->materialname = add_string (lmt->name);
369 /* dont make it unstackable if it doesn't need to be */ 408 /* dont make it unstackable if it doesn't need to be */
370 if (IS_WEAPON(op) || IS_ARMOR(op)) { 409 if (op->is_weapon () || op->is_armor ())
410 {
371 op->weight = (op->weight * lmt->weight)/100; 411 op->weight = (op->weight * lmt->weight) / 100;
372 op->value = (op->value * lmt->value)/100; 412 op->value = (op->value * lmt->value) / 100;
373 } 413 }
374#endif 414#endif
375 } 415 }
376} 416}
377 417
378/* 418/*
379 * Strip out the media tags from a String. 419 * Strip out the media tags from a String.
380 * Warning the input string will contain the result string 420 * Warning the input string will contain the result string
381 */ 421 */
422void
382void strip_media_tag(char *message){ 423strip_media_tag (char *message)
424{
383 int in_tag=0; 425 int in_tag = 0;
384 char* dest; 426 char *dest;
385 char* src; 427 char *src;
428
386 src=dest=message; 429 src = dest = message;
387 while (*src!='\0'){ 430 while (*src != '\0')
431 {
388 if (*src=='['){ 432 if (*src == '[')
433 {
389 in_tag=1; 434 in_tag = 1;
435 }
390 } else if (in_tag && (*src==']')) 436 else if (in_tag && (*src == ']'))
391 in_tag=0; 437 in_tag = 0;
392 else if (!in_tag){ 438 else if (!in_tag)
439 {
393 *dest=*src; 440 *dest = *src;
394 dest++; 441 dest++;
395 } 442 }
396 src++; 443 src++;
397 } 444 }
398 *dest='\0'; 445 *dest = '\0';
399} 446}
400 447
448const char *
401const char* strrstr(const char* haystack, const char* needle){ 449strrstr (const char *haystack, const char *needle)
450{
402 const char* lastneedle; 451 const char *lastneedle;
452
403 lastneedle=NULL; 453 lastneedle = NULL;
404 while((haystack=strstr(haystack,needle))!=NULL){ 454 while ((haystack = strstr (haystack, needle)) != NULL)
455 {
405 lastneedle=haystack; 456 lastneedle = haystack;
406 haystack++; 457 haystack++;
407 } 458 }
408 return lastneedle; 459 return lastneedle;
409 460
410} 461}
462
411#define EOL_SIZE (sizeof("\n")-1) 463#define EOL_SIZE (sizeof("\n")-1)
464void
412void strip_endline(char* buf){ 465strip_endline (char *buf)
466{
413 if (strlen(buf)<sizeof("\n")){ 467 if (strlen (buf) < sizeof ("\n"))
468 {
414 return; 469 return;
415 } 470 }
416 if (!strcmp(buf+strlen(buf)-EOL_SIZE,"\n")) 471 if (!strcmp (buf + strlen (buf) - EOL_SIZE, "\n"))
417 buf[strlen(buf)-EOL_SIZE]='\0'; 472 buf[strlen (buf) - EOL_SIZE] = '\0';
418} 473}
419 474
420/** 475/**
421 * Replace in string src all occurrences of key by replacement. The resulting 476 * Replace in string src all occurrences of key by replacement. The resulting
422 * string is put into result; at most resultsize characters (including the 477 * string is put into result; at most resultsize characters (including the
423 * terminating null character) will be written to result. 478 * terminating null character) will be written to result.
424 */ 479 */
480void
425void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize) 481replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
426{ 482{
427 size_t resultlen; 483 size_t resultlen;
428 size_t keylen; 484 size_t keylen;
429 485
430 /* special case to prevent infinite loop if key==replacement=="" */ 486 /* special case to prevent infinite loop if key==replacement=="" */
431 if(strcmp(key, replacement) == 0) 487 if (strcmp (key, replacement) == 0)
432 { 488 {
433 snprintf(result, resultsize, "%s", src); 489 snprintf (result, resultsize, "%s", src);
434 return; 490 return;
435 } 491 }
436 492
437 keylen = strlen(key); 493 keylen = strlen (key);
438 494
439 resultlen = 0; 495 resultlen = 0;
440 while(*src != '\0' && resultlen+1 < resultsize) 496 while (*src != '\0' && resultlen + 1 < resultsize)
441 { 497 {
442 if(strncmp(src, key, keylen) == 0) 498 if (strncmp (src, key, keylen) == 0)
443 { 499 {
444 snprintf(result+resultlen, resultsize-resultlen, "%s", replacement); 500 snprintf (result + resultlen, resultsize - resultlen, "%s", replacement);
445 resultlen += strlen(result+resultlen); 501 resultlen += strlen (result + resultlen);
446 src += keylen; 502 src += keylen;
447 } 503 }
448 else 504 else
449 {
450 result[resultlen++] = *src++;
451 }
452 } 505 {
506 result[resultlen++] = *src++;
507 }
508 }
453 result[resultlen] = '\0'; 509 result[resultlen] = '\0';
454} 510}
455 511
456/** 512/**
457 * Taking a string as an argument, mutate it into a string that looks like a list. 513 * Taking a string as an argument, mutate it into a string that looks like a list.
458 * a 'list' for the purposes here, is a string of items, seperated by commas, except 514 * a 'list' for the purposes here, is a string of items, seperated by commas, except
459 * for the last entry, which has an 'and' before it, and a full stop (period) after it. 515 * for the last entry, which has an 'and' before it, and a full stop (period) after it.
460 * This function will also strip all trailing non alphanumeric characters. 516 * This function will also strip all trailing non alphanumeric characters.
461 * It does not insert an oxford comma. 517 * It does not insert an oxford comma.
462 */ 518 */
463 519
520void
464void make_list_like(char *input) { 521make_list_like (char *input)
522{
465 char *p, tmp[MAX_BUF]; 523 char *p, tmp[MAX_BUF];
466 int i; 524 int i;
525
467 if (!input || strlen(input) > MAX_BUF-5) return; 526 if (!input || strlen (input) > MAX_BUF - 5)
468 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */
469
470 strncpy(tmp, input, MAX_BUF-5);
471 /*trim all trailing commas, spaces etc.*/
472 for (i=strlen(tmp); !isalnum(tmp[i]) && i >= 0; i--)
473 tmp[i]='\0';
474 strcat(tmp, ".");
475
476 p=strrchr(tmp, ',');
477 if (p) {
478 *p='\0';
479 strcpy(input, tmp);
480 p++;
481 strcat(input, " and");
482 strcat(input, p);
483 }
484 else strcpy(input, tmp);
485 return; 527 return;
486} 528 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */
487 529
488//TODO: overhead due to specifying size twice 530 strncpy (tmp, input, MAX_BUF - 5);
531 /*trim all trailing commas, spaces etc. */
532 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--)
533 tmp[i] = '\0';
534
535 strcat (tmp, ".");
536
537 p = strrchr (tmp, ',');
538 if (p)
539 {
540 *p = '\0';
541 strcpy (input, tmp);
542 p++;
543 strcat (input, " and");
544 strcat (input, p);
545 }
546 else
547 strcpy (input, tmp);
548
549 return;
550}
551
552/////////////////////////////////////////////////////////////////////////////
553
489void * 554void
490zero_initialised::operator new (size_t s) 555fork_abort (const char *msg)
491{
492 return g_slice_alloc0 (s);
493}
494
495void *
496zero_initialised::operator new [] (size_t s)
497{
498 return g_slice_alloc0 (s);
499}
500
501void
502zero_initialised::operator delete (void *p, size_t s)
503{ 556{
504 g_slice_free1 (s, p); 557 LOG (llevError, "fork abort: %s\n", msg);
505} 558 if (!fork ())
559 {
560 signal (SIGABRT, SIG_DFL);
561 abort ();
562 }
506 563
507void
508zero_initialised::operator delete [] (void *p, size_t s)
509{
510 g_slice_free1 (s, p);
511} 564}
565void *salloc_ (int n) throw (std::bad_alloc)
566{
567 void *ptr = g_slice_alloc (n);
512 568
569 if (!ptr)
570 throw std::bad_alloc ();
571
572 return ptr;
573}
574
575void *salloc_ (int n, void *src) throw (std::bad_alloc)
576{
577 void *ptr = salloc_ (n);
578
579 if (src)
580 memcpy (ptr, src, n);
581 else
582 memset (ptr, 0, n);
583
584 return ptr;
585}
586
587void assign (char *dst, const char *src, int maxlen)
588{
589 if (!src)
590 src = "";
591
592 int len = strlen (src);
593
594 if (len >= maxlen - 1)
595 {
596 if (maxlen <= 4)
597 {
598 memset (dst, '.', maxlen - 1);
599 dst [maxlen - 1] = 0;
600 }
601 else
602 {
603 memcpy (dst, src, maxlen - 4);
604 memcpy (dst + maxlen - 4, "...", 4);
605 }
606 }
607 else
608 memcpy (dst, src, len + 1);
609}
610
611tstamp now ()
612{
613 struct timeval tv;
614
615 gettimeofday (&tv, 0);
616 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
617}
618
619int
620similar_direction (int a, int b)
621{
622 if (!a || !b)
623 return 0;
624
625 int diff = (b - a) & 7;
626 return diff <= 1 || diff >= 7;
627}
628

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines