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