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