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.49 by root, Sat Apr 21 22:57:15 2007 UTC vs.
Revision 1.52 by root, Fri Apr 27 14:41:37 2007 UTC

63 63
64 return state [0] ^ state [1] ^ state [2] ^ state [3]; 64 return state [0] ^ state [1] ^ state [2] ^ state [3];
65} 65}
66 66
67uint32_t 67uint32_t
68tausworthe_random_generator::get_range (uint32_t r_max) 68tausworthe_random_generator::get_range (uint32_t num)
69{ 69{
70 return (next () * (uint64_t)r_max) >> 32U; 70 return (next () * (uint64_t)num) >> 32U;
71} 71}
72 72
73// return a number within (min .. max) 73// return a number within (min .. max)
74int 74int
75tausworthe_random_generator::get_range (int r_min, int r_max) 75tausworthe_random_generator::get_range (int r_min, int r_max)
76{ 76{
77 return r_min + get_range (max (r_max - r_min + 1, 1)); 77 return r_min + get_range (max (r_max - r_min + 1, 0));
78} 78}
79 79
80/* 80/*
81 * The random functions here take luck into account when rolling random 81 * The random functions here take luck into account when rolling random
82 * dice or numbers. This function has less of an impact the larger the 82 * dice or numbers. This function has less of an impact the larger the
553fork_abort (const char *msg) 553fork_abort (const char *msg)
554{ 554{
555 if (!fork ()) 555 if (!fork ())
556 { 556 {
557 signal (SIGABRT, SIG_DFL); 557 signal (SIGABRT, SIG_DFL);
558 // try to put corefiles into a subdirectory, if existing, to allow
559 // an administrator to reduce the I/O load.
560 chdir ("cores");
558 abort (); 561 abort ();
559 } 562 }
560 563
561 LOG (llevError, "fork abort: %s\n", msg); 564 LOG (llevError, "fork abort: %s\n", msg);
562} 565}
607 memcpy (dst + maxlen - 4, "...", 4); 610 memcpy (dst + maxlen - 4, "...", 4);
608 } 611 }
609 } 612 }
610 else 613 else
611 memcpy (dst, src, len + 1); 614 memcpy (dst, src, len + 1);
615}
616
617const std::string
618format (const char *format, ...)
619{
620 int len;
621
622 {
623 char buf[128];
624
625 va_list ap;
626 va_start (ap, format);
627 len = vsnprintf (buf, sizeof (buf), format, ap);
628 va_end (ap);
629
630 assert (len >= 0); // shield againstz broken vsnprintf's
631
632 // was our static buffer short enough?
633 if (len < sizeof (buf))
634 return std::string (buf, len);
635 }
636
637 {
638 // longer, try harder
639 char *buf = salloc<char> (len + 1);
640
641 va_list ap;
642 va_start (ap, format);
643 vsnprintf (buf, len + 1, format, ap);
644 va_end (ap);
645
646 const std::string s (buf, len);
647 sfree<char> (buf, len + 1);
648
649 return buf;
650 }
612} 651}
613 652
614tstamp now () 653tstamp now ()
615{ 654{
616 struct timeval tv; 655 struct timeval tv;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines