"Fossies" - the Fresh Open Source Software Archive

Member "bonnie++-2.00a/rand.cpp" (3 Jul 2009, 1055 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "rand.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "rand.h"
    2 #include <unistd.h>
    3 #include <netinet/in.h>
    4 #include <sys/types.h>
    5 #include <sys/stat.h>
    6 #include <fcntl.h>
    7 
    8 bool Rand::seedFile(CPCCHAR name)
    9 {
   10   int fd = file_open(name, O_RDONLY);
   11   struct stat buf;
   12   if(fd == -1 || fstat(fd, &buf) == -1)
   13   {
   14     fprintf(stderr, "Can't open random file \"%s\".\n", name);
   15     if(fd != -1)
   16       close(fd);
   17     return true;
   18   }
   19   int size = buf.st_size / sizeof(int);
   20   delete(m_arr);
   21   m_arr = new int[size];
   22   m_size = size;
   23   if(size_t(read(fd, m_arr, size * sizeof(int))) != size * sizeof(int))
   24   {
   25     fprintf(stderr, "Can't read random data from \"%s\".\n", name);
   26     return true;
   27   }
   28   for(int i = 0; i < size; i++)
   29   {
   30     m_arr[i] = abs(int(ntohl(m_arr[i])));
   31   }
   32   close(fd);
   33   m_ind = -1;
   34   m_name = string(name);
   35   return false;
   36 }
   37  
   38 void Rand::seedNum(UINT num)
   39 {
   40   delete(m_arr);
   41   m_arr = NULL;
   42   m_size = 0;
   43   srand(num);
   44   m_init = num;
   45   char buf[12];
   46   sprintf(buf, "%u", num);
   47   m_name = string(buf);
   48 }
   49 
   50 void Rand::reset()
   51 {
   52   if(m_arr)
   53     m_ind = -1;
   54   else
   55     srand(m_init);
   56 }