2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
22 #include "generator.h"
24 /* Use this for string generation */
25 static const char ALPHANUMERICS
[]=
26 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
28 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
30 static size_t get_alpha_num(void)
32 return (size_t)random() % ALPHANUMERICS_SIZE
;
35 static void get_random_string(char *buffer
, size_t size
)
37 char *buffer_ptr
= buffer
;
40 *buffer_ptr
++= ALPHANUMERICS
[get_alpha_num()];
41 *buffer_ptr
++= ALPHANUMERICS
[get_alpha_num()];
44 void pairs_free(pairs_st
*pairs
)
49 /* We free until we hit the null pair we stores during creation */
50 for (uint32_t x
= 0; pairs
[x
].key
; x
++)
60 pairs_st
*pairs_generate(uint64_t number_of
, size_t value_length
)
62 pairs_st
*pairs
= (pairs_st
*)calloc((size_t)number_of
+ 1, sizeof(pairs_st
));
69 for (uint64_t x
= 0; x
< number_of
; x
++)
71 pairs
[x
].key
= (char *)calloc(100, sizeof(char));
73 if (pairs
[x
].key
== NULL
)
76 get_random_string(pairs
[x
].key
, 100);
77 pairs
[x
].key_length
= 100;
81 pairs
[x
].value
= (char *)calloc(value_length
, sizeof(char));
83 if (pairs
[x
].value
== NULL
)
86 get_random_string(pairs
[x
].value
, value_length
);
87 pairs
[x
].value_length
= value_length
;
92 pairs
[x
].value_length
= 0;
98 std::cerr
<< "Memory Allocation failure in pairs_generate." << std::endl
;