From: Michael Andres Date: Mon, 18 Jun 2007 20:55:26 +0000 (+0000) Subject: enhanced base/Random.h; duplicate code removed X-Git-Tag: BASE-SuSE-Linux-10_3-Branch~603 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f785dc64a1d87278199186d5014f3324ccf81bdc;p=platform%2Fupstream%2Flibzypp.git enhanced base/Random.h; duplicate code removed --- diff --git a/zypp/base/Random.cc b/zypp/base/Random.cc index e6abfe5..123da94 100644 --- a/zypp/base/Random.cc +++ b/zypp/base/Random.cc @@ -39,7 +39,7 @@ std::string random_string(int length) int i = 0; while (length--) { - int r=random() % 62; + int r=::random() % 62; r+=48; if (r>57) r+=7; if (r>90) r+=6; @@ -48,7 +48,7 @@ std::string random_string(int length) } return str; } - - + + } } diff --git a/zypp/base/Random.h b/zypp/base/Random.h index 6daf788..9c988f9 100644 --- a/zypp/base/Random.h +++ b/zypp/base/Random.h @@ -23,6 +23,24 @@ namespace zypp // Taken from KApplication std::string random_string(int length); + + /** Return a random number from [0,RAND_MAX[. */ + inline unsigned random() + { + return random_int(); + } + /** Return a random number from [0,size_r[. */ + inline unsigned random( unsigned size_r ) + { + return random_int() % size_r; + } + /** Return a random number from [min_r,min_r+size_r[. */ + inline unsigned random( unsigned min_r, unsigned size_r ) + { + return min_r + random( size_r ); + } + + } //ns base } // ns zypp diff --git a/zypp/source/SourceImpl.cc b/zypp/source/SourceImpl.cc index 3a24538..350ee35 100644 --- a/zypp/source/SourceImpl.cc +++ b/zypp/source/SourceImpl.cc @@ -13,6 +13,7 @@ #include #include "zypp/base/Logger.h" #include "zypp/base/Gettext.h" +#include "zypp/base/Random.h" #include "zypp/Digest.h" #include "zypp/SourceFactory.h" #include "zypp/source/SourceImpl.h" @@ -32,48 +33,6 @@ namespace source IMPL_PTR_TYPE(SourceImpl); -// Taken from KApplication -static int random() -{ - static bool init = false; - if (!init) - { - unsigned int seed; - init = true; - int fd = open("/dev/urandom", O_RDONLY); - if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed)) - { - // No /dev/urandom... try something else. - srand(getpid()); - seed = rand()+time(0); - } - if (fd >= 0) close(fd); - srand(seed); - } - return rand(); -} - -// Taken from KApplication -static std::string randomString(int length) -{ - if (length <=0 ) return std::string(); - - std::string str; - str.resize( length ); - int i = 0; - while (length--) - { - int r=random() % 62; - r+=48; - if (r>57) r+=7; - if (r>90) r+=6; - str[i++] = char(r); - // so what if I work backwards? - } - return str; -} - - class DownloadProgressPackageReceiver : public callback::ReceiveReport { callback::SendReport & _report;