Fix all include location, and drop versions of the library that were never shipped.
[awesomized/libmemcached] / clients / generator.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12
13 #include <config.h>
14
15 #include <stdint.h>
16
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 #include <unistd.h>
22
23 #include "generator.h"
24
25 /* Use this for string generation */
26 static const char ALPHANUMERICS[]=
27 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
28
29 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
30
31 static size_t get_alpha_num(void)
32 {
33 return (size_t)random() % ALPHANUMERICS_SIZE;
34 }
35
36 static void get_random_string(char *buffer, size_t size)
37 {
38 char *buffer_ptr= buffer;
39
40 while (--size)
41 *buffer_ptr++= ALPHANUMERICS[get_alpha_num()];
42 *buffer_ptr++= ALPHANUMERICS[get_alpha_num()];
43 }
44
45 void pairs_free(pairs_st *pairs)
46 {
47 if (pairs == NULL)
48 {
49 return;
50 }
51
52 /* We free until we hit the null pair we stores during creation */
53 for (uint32_t x= 0; pairs[x].key; x++)
54 {
55 free(pairs[x].key);
56 if (pairs[x].value)
57 {
58 free(pairs[x].value);
59 }
60 }
61
62 free(pairs);
63 }
64
65 pairs_st *pairs_generate(uint64_t number_of, size_t value_length)
66 {
67 pairs_st *pairs= (pairs_st*)calloc((size_t)number_of + 1, sizeof(pairs_st));
68
69 if (pairs == NULL)
70 {
71 goto error;
72 }
73
74 for (uint64_t x= 0; x < number_of; x++)
75 {
76 pairs[x].key= (char *)calloc(100, sizeof(char));
77
78 if (pairs[x].key == NULL)
79 goto error;
80
81 get_random_string(pairs[x].key, 100);
82 pairs[x].key_length= 100;
83
84 if (value_length)
85 {
86 pairs[x].value= (char *)calloc(value_length, sizeof(char));
87
88 if (pairs[x].value == NULL)
89 goto error;
90
91 get_random_string(pairs[x].value, value_length);
92 pairs[x].value_length= value_length;
93 }
94 else
95 {
96 pairs[x].value= NULL;
97 pairs[x].value_length= 0;
98 }
99 }
100
101 return pairs;
102 error:
103 std::cerr << "Memory Allocation failure in pairs_generate." << std::endl;
104 exit(EXIT_SUCCESS);
105 }