Commit faff947d authored by Fabio D'Urso's avatar Fabio D'Urso Committed by Albert Astals Cid

Added goo/grandom.[cc|h] with POSIX implementation

parent af8d05d1
......@@ -243,6 +243,7 @@ set(poppler_SRCS
goo/JpegWriter.cc
goo/ImgWriter.cc
goo/gstrtod.cc
goo/grandom.cc
fofi/FoFiBase.cc
fofi/FoFiEncodings.cc
fofi/FoFiTrueType.cc
......@@ -485,6 +486,7 @@ if(ENABLE_XPDF_HEADERS)
goo/ImgWriter.h
goo/GooLikely.h
goo/gstrtod.h
goo/grandom.h
DESTINATION include/poppler/goo)
if(PNG_FOUND)
install(FILES
......
......@@ -33,6 +33,7 @@ check_function_exists(localtime_r HAVE_LOCALTIME_R)
check_function_exists(popen HAVE_POPEN)
check_function_exists(mkstemp HAVE_MKSTEMP)
check_function_exists(mkstemps HAVE_MKSTEMPS)
check_function_exists(rand_r HAVE_RAND_R)
macro(CHECK_FOR_DIR include var)
check_c_source_compiles(
......
......@@ -76,6 +76,9 @@
/* Define to 1 if you have the `mkstemps' function. */
#cmakedefine HAVE_MKSTEMPS 1
/* Define to 1 if you have the `rand_r' function. */
#cmakedefine HAVE_RAND_R 1
/* Define to 1 if you have the <ndir.h> header file, and it defines `DIR'. */
#cmakedefine HAVE_NDIR_H 1
......
......@@ -133,6 +133,7 @@ AC_LANG_CPLUSPLUS
AC_CHECK_DECL(gettimeofday, [AC_CHECK_FUNC(gettimeofday, AC_DEFINE(HAVE_GETTIMEOFDAY, 1, [Defines if gettimeofday is available on your system]))],[],[#include <sys/time.h>])
AC_CHECK_FUNC(localtime_r, AC_DEFINE(HAVE_LOCALTIME_R, 1, [Defines if localtime_r is available on your system]))
AC_CHECK_FUNC(gmtime_r, AC_DEFINE(HAVE_GMTIME_R, 1, [Defines if gmtime_r is available on your system]))
AC_CHECK_FUNC(rand_r, AC_DEFINE(HAVE_RAND_R, 1, [Defines if rand_r is available on your system]))
dnl ##### Check for extra libraries needed by X. (LynxOS needs this.)
AC_CHECK_FUNC(gethostbyname)
......
......@@ -18,7 +18,8 @@ poppler_goo_include_HEADERS = \
TiffWriter.h \
ImgWriter.h \
GooLikely.h \
gstrtod.h
gstrtod.h \
grandom.h
endif
......@@ -59,4 +60,5 @@ libgoo_la_SOURCES = \
TiffWriter.cc \
ImgWriter.cc \
gtypes_p.h \
gstrtod.cc
gstrtod.cc \
grandom.cc
/*
* grandom.cc
*
* Pseudo-random number generation
*
* Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
*/
#include <config.h>
#include "grandom.h"
#include "gtypes.h"
#ifdef HAVE_RAND_R // rand_r backend (POSIX)
static GBool initialized = gFalse;
#include <stdlib.h>
#include <time.h>
static unsigned int seed;
static void initialize() {
if (!initialized) {
seed = time(NULL);
initialized = gTrue;
}
}
void grandom_fill(Guchar *buff, int size)
{
initialize();
while (size--)
*buff++ = rand_r(&seed) % 256;
}
double grandom_double()
{
initialize();
return rand_r(&seed) / (1 + (double)RAND_MAX);
}
#else // srand+rand backend (unsafe, because it may interfere with the application)
static GBool initialized = gFalse;
#include <stdlib.h>
#include <time.h>
static void initialize() {
if (!initialized) {
srand(time(NULL));
initialized = gTrue;
}
}
void grandom_fill(Guchar *buff, int size)
{
initialize();
while (size--)
*buff++ = rand() % 256;
}
double grandom_double()
{
initialize();
return rand() / (1 + (double)RAND_MAX);
}
#endif
/*
* grandom.h
*
* Pseudo-random number generation
*
* Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
*/
#ifndef GRANDOM_H
#define GRANDOM_H
#include "gtypes.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Fills the given buffer with random bytes
*/
extern void grandom_fill(Guchar *buff, int size);
/*
* Returns a random number in [0,1)
*/
extern double grandom_double();
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment