Commit 10530b2f authored by Yoni Fogel's avatar Yoni Fogel

Fixed some errors in the header

git-svn-id: file:///svn/tokudb@393 c7de825b-a66e-492c-adef-691d508d4ae1
parent 80bb78fd
#if !defined(TOKUDB_COMMON_H) #if !defined(TOKUDB_COMMON_H)
#define TOKUDB_COMMON_H #define TOKUDB_COMMON_H
#include <stdlib.h>
#include <stdint.h>
#include <limits.h> #include <limits.h>
#include <db.h>
int strtoint32(char* str, int32_t* num, int32_t min, int32_t max, int base); typedef uint8_t bool;
int strtouint32(char* str, uint32_t* num, uint32_t min, uint32_t max, int base);
int strtoint64(char* str, int64_t* num, int64_t min, int64_t max, int base); #define true ((bool)1)
int strtouint64(char* str, uint64_t* num, uint64_t min, uint64_t max, int base); #define false ((bool)0)
#define SET_BITS(bitvector, bits) ((bitvector) |= (bits))
#define REMOVE_BITS(bitvector, bits) ((bitvector) &= ~(bits))
#define IS_SET_ANY(bitvector, bits) ((bitvector) & (bits))
#define IS_SET_ALL(bitvector, bits) (((bitvector) & (bits)) == (bits))
#define IS_POWER_OF_2(num) ((num) > 0 && ((num) & (num) - 1))
int strtoint32 (DB* db, char* progname, char* str, int32_t* num, int32_t min, int32_t max, int base);
int strtouint32 (DB* db, char* progname, char* str, uint32_t* num, uint32_t min, uint32_t max, int base);
int strtoint64 (DB* db, char* progname, char* str, int64_t* num, int64_t min, int64_t max, int base);
int strtouint64 (DB* db, char* progname, char* str, uint64_t* num, uint64_t min, uint64_t max, int base);
/* /*
* Convert a string to an "type". Uses base 10. * Convert a string to an "type". Uses base 10.
* Allow range of [min, max]. * Allow range of [min, max].
* *
* *
* Sets errno and returns: * Sets errno and returns:
* EINVAL: str == NULL, num == NULL, or string not of the form [ ]+[+-]?[0-9]+ * EINVAL: str == NULL, num == NULL, or string not of the form [ ]+[+-]?[0-9]+
...@@ -19,8 +33,8 @@ int strtouint64(char* str, uint64_t* num, uint64_t min, uint64_t max, int base ...@@ -19,8 +33,8 @@ int strtouint64(char* str, uint64_t* num, uint64_t min, uint64_t max, int base
* *
* *num is unchanged on error. * *num is unchanged on error.
*/ */
#define DEF_STR_TO(name, type, bigtype, strtofunc) \ #define DEF_STR_TO(name, type, bigtype, strtofunc, frmt) \
int name(char* str, type* num, type min, type max, int base) \ int name(DB* db, char* progname, char* str, type* num, type min, type max, int base) \
{ \ { \
char* test; \ char* test; \
bigtype value; \ bigtype value; \
...@@ -28,32 +42,41 @@ int name(char* str, type* num, type min, type max, int base) \ ...@@ -28,32 +42,41 @@ int name(char* str, type* num, type min, type max, int base) \
assert(str); \ assert(str); \
assert(num); \ assert(num); \
assert(min <= max); \ assert(min <= max); \
if (!str || *str == '\0' || !num) { \ assert(db || progname); \
errno = EINVAL; \ assert(base == 0 || (base >= 2 && base <= 36)); \
goto err; \
} \
errno = 0; \
\ \
errno = 0; \
value = strtofunc(str, &test, base); \ value = strtofunc(str, &test, base); \
if (errno) goto err; \ if ((*test != '\0' && *test != '\n') || test == str) { \
if (*test != '\0') { \ if (db == NULL) fprintf(stderr, "%s: %s: Invalid numeric argument\n", progname, str); \
else db->errx(db, "%s: Invalid numeric argument", str); \
errno = EINVAL; \ errno = EINVAL; \
goto err; \ goto error; \
} \
if (errno == ERANGE) { \
if (db == NULL) fprintf(stderr, "%s: %s: %s\n", progname, str, strerror(errno)); \
else db->err(db, errno, "%s", str); \
goto error; \
} \ } \
if (value < min || value > max) { \ if (value < min) { \
errno = ERANGE; \ if (db == NULL) fprintf(stderr, "%s: %s: Less than minimum value (%" frmt ")\n", progname, str, min); \
goto err; \ else db->errx(db, "%s: Less than minimum value (%" frmt ")", str, min); \
goto error; \
} \
if (value > max) { \
if (db == NULL) fprintf(stderr, "%s: %s: Greater than maximum value (%" frmt ")\n", progname, str, max); \
else db->errx(db, "%s: Greater than maximum value (%" frmt ")", str, max); \
goto error; \
} \ } \
*num = value; \ *num = value; \
return 0; \ return 0; \
\ error: \
err: \
return errno; \ return errno; \
} }
DEF_STR_TO(strtoint32, int32_t, int64_t, strtoll); DEF_STR_TO(strtoint32, int32_t, int64_t, strtoll, "d");
DEF_STR_TO(strtouint32, uint32_t, uint64_t, strtoull); DEF_STR_TO(strtouint32, uint32_t, uint64_t, strtoull, "u");
DEF_STR_TO(strtoint64, int64_t, int64_t, strtoll); DEF_STR_TO(strtoint64, int64_t, int64_t, strtoll, "lld");
DEF_STR_TO(strtouint64, uint64_t, uint64_t, strtoull); DEF_STR_TO(strtouint64, uint64_t, uint64_t, strtoull, "llu");
#endif /* #if !defined(TOKUDB_COMMON_H) */ #endif /* #if !defined(TOKUDB_COMMON_H) */
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
...@@ -16,10 +14,6 @@ extern int optopt; ...@@ -16,10 +14,6 @@ extern int optopt;
extern int opterr; extern int opterr;
extern int optreset; extern int optreset;
typedef uint8_t bool;
#define true ((bool)1)
#define false ((bool)0)
int usage(void); int usage(void);
void generate_keys(void); void generate_keys(void);
int get_delimiter(char* str); int get_delimiter(char* str);
...@@ -49,7 +43,7 @@ int main (int argc, char *argv[]) { ...@@ -49,7 +43,7 @@ int main (int argc, char *argv[]) {
progname = argv[0]; progname = argv[0];
strcpy(sort_delimiter, ""); strcpy(sort_delimiter, "");
while ((ch = getopt(argc, argv, "PfFhHTpr:s:d:p:m:M:n:o:")) != EOF) { while ((ch = getopt(argc, argv, "PfFhHTpr:s:d:p:m:M:n:o:")) != EOF) {
switch (ch) { switch (ch) {
case ('P'): { case ('P'): {
...@@ -89,7 +83,7 @@ int main (int argc, char *argv[]) { ...@@ -89,7 +83,7 @@ int main (int argc, char *argv[]) {
fprintf(stderr, fprintf(stderr,
"%s: %s: reopen: %s\n", "%s: %s: reopen: %s\n",
progname, optarg, strerror(errno)); progname, optarg, strerror(errno));
return (EXIT_FAILURE); goto error;
} }
break; break;
} }
...@@ -99,13 +93,13 @@ int main (int argc, char *argv[]) { ...@@ -99,13 +93,13 @@ int main (int argc, char *argv[]) {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-d) Key (or value) delimiter must be one character.", "%s: %s: (-d) Key (or value) delimiter must be one character.",
progname, optarg); progname, optarg);
return (EXIT_FAILURE); goto error;
} }
if (isxdigit(temp)) { if (isxdigit(temp)) {
fprintf(stderr, fprintf(stderr,
"%s: %c: (-d) Key (or value) delimiter cannot be a hex digit.", "%s: %c: (-d) Key (or value) delimiter cannot be a hex digit.",
progname, temp); progname, temp);
return (EXIT_FAILURE); goto error;
} }
dbt_delimiter = (char)temp; dbt_delimiter = (char)temp;
break; break;
...@@ -116,50 +110,54 @@ int main (int argc, char *argv[]) { ...@@ -116,50 +110,54 @@ int main (int argc, char *argv[]) {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-s) Sorting (Between key/value pairs) delimiter must be one character.", "%s: %s: (-s) Sorting (Between key/value pairs) delimiter must be one character.",
progname, optarg); progname, optarg);
return (EXIT_FAILURE); goto error;
} }
if (isxdigit(temp)) { if (isxdigit(temp)) {
fprintf(stderr, fprintf(stderr,
"%s: %c: (-s) Sorting (Between key/value pairs) delimiter cannot be a hex digit.", "%s: %c: (-s) Sorting (Between key/value pairs) delimiter cannot be a hex digit.",
progname, temp); progname, temp);
return (EXIT_FAILURE); goto error;
} }
sort_delimiter[0] = (char)temp; sort_delimiter[0] = (char)temp;
sort_delimiter[1] = '\0'; sort_delimiter[1] = '\0';
break; break;
} }
case ('r'): { case ('r'): {
if (strtouint32(optarg, &seed, 0, UINT32_MAX, 10)) { if (strtouint32(NULL, progname, optarg, &seed, 0, UINT32_MAX, 10)) {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-r) Random seed invalid.", "%s: %s: (-r) Random seed invalid.",
progname, optarg); progname, optarg);
goto error;
} }
set_seed = true; set_seed = true;
break; break;
} }
case ('m'): { case ('m'): {
if (strtouint32(optarg, &lengthmin, 0, UINT32_MAX, 10)) { if (strtouint32(NULL, progname, optarg, &lengthmin, 0, UINT32_MAX, 10)) {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-m) Min length of keys/values invalid.", "%s: %s: (-m) Min length of keys/values invalid.",
progname, optarg); progname, optarg);
goto error;
} }
set_lengthmin = true; set_lengthmin = true;
break; break;
} }
case ('M'): { case ('M'): {
if (strtouint32(optarg, &lengthlimit, 1, UINT32_MAX, 10)) { if (strtouint32(NULL, progname, optarg, &lengthlimit, 1, UINT32_MAX, 10)) {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-M) Limit of key/value length invalid.", "%s: %s: (-M) Limit of key/value length invalid.",
progname, optarg); progname, optarg);
goto error;
} }
set_lengthlimit = true; set_lengthlimit = true;
break; break;
} }
case ('n'): { case ('n'): {
if (strtouint64(optarg, &numkeys, 0, UINT64_MAX, 10)) { if (strtouint64(NULL, progname, optarg, &numkeys, 0, UINT64_MAX, 10)) {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-n) Number of keys to generate invalid.", "%s: %s: (-n) Number of keys to generate invalid.",
progname, optarg); progname, optarg);
goto error;
} }
set_numkeys = true; set_numkeys = true;
break; break;
...@@ -172,85 +170,67 @@ int main (int argc, char *argv[]) { ...@@ -172,85 +170,67 @@ int main (int argc, char *argv[]) {
} }
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (justheader && !header) { if (justheader && !header) {
fprintf(stderr, fprintf(stderr,
"%s: The -h and -H options may not both be specified.\n", "%s: The -h and -H options may not both be specified.\n",
progname); progname);
return usage(); goto error;
} }
if (justfooter && !footer) { if (justfooter && !footer) {
fprintf(stderr, fprintf(stderr,
"%s: The -f and -F options may not both be specified.\n", "%s: The -f and -F options may not both be specified.\n",
progname); progname);
return usage(); goto error;
} }
if (justfooter && justheader) { if (justfooter && justheader) {
fprintf(stderr, fprintf(stderr,
"%s: The -H and -F options may not both be specified.\n", "%s: The -H and -F options may not both be specified.\n",
progname); progname);
return usage(); goto error;
} }
if (justfooter && header) { if (justfooter && header) {
fprintf(stderr, fprintf(stderr, "%s: -F implies -h\n", progname);
"%s: -F implies -h\n",
progname);
header = false; header = false;
} }
if (justheader && footer) { if (justheader && footer) {
fprintf(stderr, fprintf(stderr, "%s: -H implies -f\n", progname);
"%s: -H implies -f\n",
progname);
footer = false; footer = false;
} }
if (!leadingspace) { if (!leadingspace) {
if (footer) { if (footer) {
fprintf(stderr, fprintf(stderr, "%s: -p implies -f\n", progname);
"%s: -p implies -f\n",
progname);
footer = false; footer = false;
} }
if (header) { if (header) {
fprintf(stderr, fprintf(stderr, "%s: -p implies -h\n", progname);
"%s: -p implies -h\n",
progname);
header = false; header = false;
} }
} }
if (justfooter || justheader) outputkeys = false; if (justfooter || justheader) outputkeys = false;
else if (!set_numkeys) else if (!set_numkeys)
{ {
fprintf(stderr, fprintf(stderr, "%s: The -n option is required.\n", progname);
"%s: The -n option is required.\n", goto error;
progname);
return usage();
} }
if (outputkeys && !set_seed) { if (outputkeys && !set_seed) {
fprintf(stderr, fprintf(stderr, "%s: Using default seed. (-r 1).\n", progname);
"%s: Using default seed. (-r 1).\n",
progname);
seed = 1; seed = 1;
} }
if (outputkeys && !set_lengthmin) { if (outputkeys && !set_lengthmin) {
fprintf(stderr, fprintf(stderr, "%s: Using default lengthmin. (-m 0).\n", progname);
"%s: Using default lengthmin. (-m 0).\n",
progname);
lengthmin = 0; lengthmin = 0;
} }
if (outputkeys && !set_lengthlimit) { if (outputkeys && !set_lengthlimit) {
fprintf(stderr, fprintf(stderr, "%s: Using default lengthlimit. (-M 1024).\n", progname);
"%s: Using default lengthlimit. (-M 1024).\n",
progname);
lengthlimit = 1024; lengthlimit = 1024;
} }
if (outputkeys && lengthmin >= lengthlimit) { if (outputkeys && lengthmin >= lengthlimit) {
fprintf(stderr, fprintf(stderr, "%s: Max key size must be greater than min key size.\n", progname);
"%s: Max key size must be greater than min key size.\n", goto error;
progname);
return usage();
} }
if (argc != 0) { if (argc != 0) {
return usage(); return usage();
} }
if (header) { if (header) {
...@@ -261,10 +241,13 @@ int main (int argc, char *argv[]) { ...@@ -261,10 +241,13 @@ int main (int argc, char *argv[]) {
"HEADER=END\n", "HEADER=END\n",
plaintext ? "print" : "bytevalue"); plaintext ? "print" : "bytevalue");
} }
if (justheader) return 0;
if (outputkeys) generate_keys(); if (outputkeys) generate_keys();
if (footer) printf("DATA=END\n"); if (footer) printf("DATA=END\n");
return 0; return 0;
error:
fprintf(stderr, "Quitting out due to errors.\n");
return EXIT_FAILURE;
} }
int usage() int usage()
...@@ -282,7 +265,7 @@ uint8_t randbyte() ...@@ -282,7 +265,7 @@ uint8_t randbyte()
static uint32_t numsavedbits = 0; static uint32_t numsavedbits = 0;
static uint64_t savedbits = 0; static uint64_t savedbits = 0;
uint8_t retval; uint8_t retval;
if (numsavedbits < 8) { if (numsavedbits < 8) {
savedbits |= ((uint64_t)random()) << numsavedbits; savedbits |= ((uint64_t)random()) << numsavedbits;
numsavedbits += 31; /* Random generates 31 random bits. */ numsavedbits += 31; /* Random generates 31 random bits. */
...@@ -332,15 +315,15 @@ void generate_keys() ...@@ -332,15 +315,15 @@ void generate_keys()
srandom(seed); srandom(seed);
while (numgenerated < numkeys) { while (numgenerated < numkeys) {
numgenerated++; numgenerated++;
/* Each key is preceded by a space (unless using -T). */ /* Each key is preceded by a space (unless using -T). */
if (leadingspace) printf(" "); if (leadingspace) printf(" ");
/* Generate a key. */ /* Generate a key. */
{ {
/* Pick a key length. */ /* Pick a key length. */
length = random_below(lengthlimit - lengthmin) + lengthmin; length = random_below(lengthlimit - lengthmin) + lengthmin;
/* Output 'length' random bytes. */ /* Output 'length' random bytes. */
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
do {ch = randbyte();} do {ch = randbyte();}
...@@ -359,13 +342,13 @@ void generate_keys() ...@@ -359,13 +342,13 @@ void generate_keys()
printf("%c", dbt_delimiter); printf("%c", dbt_delimiter);
/* Each value is preceded by a space (unless using -T). */ /* Each value is preceded by a space (unless using -T). */
if (leadingspace) printf(" "); if (leadingspace) printf(" ");
/* Generate a value. */ /* Generate a value. */
{ {
/* Pick a key length. */ /* Pick a key length. */
length = random_below(lengthlimit - lengthmin) + lengthmin; length = random_below(lengthlimit - lengthmin) + lengthmin;
/* Output 'length' random bytes. */ /* Output 'length' random bytes. */
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
do {ch = randbyte();} do {ch = randbyte();}
......
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