Commit fe3aebd2 authored by Yoni Fogel's avatar Yoni Fogel

Extracted strto* wrappers to common header.

git-svn-id: file:///svn/tokudb@381 c7de825b-a66e-492c-adef-691d508d4ae1
parent 1270f891
#if !defined(TOKUDB_COMMON_H)
#define TOKUDB_COMMON_H
#include <limits.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);
/*
* 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]+
* ERANGE: value out of range specified.
*
* *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) \
{ \
char* test; \
bigtype value; \
\
assert(str); \
assert(num); \
assert(min <= max); \
if (!str || *str == '\0' || !num) { \
errno = EINVAL; \
goto err; \
} \
errno = 0; \
\
value = strtofunc(str, &test, base); \
if (errno) goto err; \
if (*test != '\0') { \
errno = EINVAL; \
goto err; \
} \
if (value < min || value > max) { \
errno = ERANGE; \
goto err; \
} \
*num = value; \
return 0; \
\
err: \
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);
#endif /* #if !defined(TOKUDB_COMMON_H) */
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include "tokudb_common.h"
extern char* optarg; extern char* optarg;
extern int optind; extern int optind;
extern int optopt; extern int optopt;
...@@ -26,15 +28,19 @@ char dbt_delimiter = '\n'; ...@@ -26,15 +28,19 @@ char dbt_delimiter = '\n';
char sort_delimiter[2]; char sort_delimiter[2];
char* progname; char* progname;
bool plaintext = false; bool plaintext = false;
long lengthmin = -1; uint32_t lengthmin = 0;
long lengthlimit = -1; bool set_lengthmin = false;
int64_t numkeys = -1; uint32_t lengthlimit = 0;
bool set_lengthlimit= false;
uint64_t numkeys = 0;
bool set_numkeys = false;
bool header = true; bool header = true;
bool footer = true; bool footer = true;
bool justheader = false; bool justheader = false;
bool justfooter = false; bool justfooter = false;
bool outputkeys = true; bool outputkeys = true;
unsigned long seed = 1; uint32_t seed = 1;
bool set_seed = false;
bool printableonly = false; bool printableonly = false;
bool leadingspace = true; bool leadingspace = true;
...@@ -123,53 +129,39 @@ int main (int argc, char *argv[]) { ...@@ -123,53 +129,39 @@ int main (int argc, char *argv[]) {
break; break;
} }
case ('r'): { case ('r'): {
char* test; if (strtouint32(optarg, &seed, 0, UINT32_MAX, 10)) {
seed = strtol(optarg, &test, 10);
if (optarg[0] == '\0' || *test != '\0') {
fprintf(stderr, fprintf(stderr,
"%s: %s: (-r) Random seed invalid.", "%s: %s: (-r) Random seed invalid.",
progname, optarg); progname, optarg);
} }
set_seed = true;
break; break;
} }
case ('m'): { case ('m'): {
char* test; if (strtouint32(optarg, &lengthmin, 0, UINT32_MAX, 10)) {
if (optarg[0] == '\0' ||
(lengthmin = strtol(optarg, &test, 10)) < 0 ||
*test != '\0')
{
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);
} }
set_lengthmin = true;
break; break;
} }
case ('M'): { case ('M'): {
char* test; if (strtouint32(optarg, &lengthlimit, 1, UINT32_MAX, 10)) {
if (optarg[0] == '\0' ||
(lengthlimit = strtol(optarg, &test, 10)) < 0 ||
*test != '\0')
{
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);
} }
set_lengthlimit = true;
break; break;
} }
case ('n'): { case ('n'): {
char* test; if (strtouint64(optarg, &numkeys, 0, UINT64_MAX, 10)) {
if (optarg[0] == '\0' ||
(numkeys = strtoll(optarg, &test, 10)) <= 0 ||
*test != '\0')
{
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);
} }
set_numkeys = true;
break; break;
} }
case ('?'): case ('?'):
...@@ -226,26 +218,26 @@ int main (int argc, char *argv[]) { ...@@ -226,26 +218,26 @@ int main (int argc, char *argv[]) {
} }
} }
if (justfooter || justheader) outputkeys = false; if (justfooter || justheader) outputkeys = false;
else if (numkeys == -1) else if (!set_numkeys)
{ {
fprintf(stderr, fprintf(stderr,
"%s: The -n option is required.\n", "%s: The -n option is required.\n",
progname); progname);
return usage(); return usage();
} }
if (outputkeys && seed == 1) { if (outputkeys && !set_seed) {
fprintf(stderr, fprintf(stderr,
"%s: Using default seed. (-r 1).\n", "%s: Using default seed. (-r 1).\n",
progname); progname);
seed = 1; seed = 1;
} }
if (outputkeys && lengthmin == -1) { if (outputkeys && !set_lengthmin) {
fprintf(stderr, fprintf(stderr,
"%s: Using default lengthmin. (-m 0).\n", "%s: Using default lengthmin. (-m 0).\n",
progname); progname);
lengthmin = 0; lengthmin = 0;
} }
if (outputkeys && lengthlimit == -1) { if (outputkeys && !set_lengthlimit) {
fprintf(stderr, fprintf(stderr,
"%s: Using default lengthlimit. (-M 1024).\n", "%s: Using default lengthlimit. (-M 1024).\n",
progname); progname);
...@@ -301,11 +293,11 @@ uint8_t randbyte() ...@@ -301,11 +293,11 @@ uint8_t randbyte()
return retval; return retval;
} }
/* Almost-uniformly random int from [0,max) */ /* Almost-uniformly random int from [0,limit) */
int32_t random_below(int32_t max) int32_t random_below(int32_t limit)
{ {
assert(max > 0); assert(limit > 0);
return random() % max; return random() % limit;
} }
void outputbyte(uint8_t ch) void outputbyte(uint8_t ch)
...@@ -330,8 +322,8 @@ void outputstring(char* str) ...@@ -330,8 +322,8 @@ void outputstring(char* str)
void generate_keys() void generate_keys()
{ {
bool usedemptykey = false; bool usedemptykey = false;
int64_t numgenerated = 0; uint64_t numgenerated = 0;
int64_t totalsize = 0; uint64_t totalsize = 0;
char identifier[24]; /* 8 bytes * 2 = 16; 16+1=17; 17+null terminator = 18. Extra padding. */ char identifier[24]; /* 8 bytes * 2 = 16; 16+1=17; 17+null terminator = 18. Extra padding. */
int length; int length;
int i; int i;
......
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