Skip to content

Commit

Permalink
config: add number_string_{plus,minus}_equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
jindraj authored and flatcap committed Jun 13, 2020
1 parent a5a4316 commit 4994102
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions config/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,84 @@ static intptr_t number_native_get(const struct ConfigSet *cs, void *var,
return *(short *) var;
}

/**
* number_string_plus_equals - Add to a Number by string - Implements ConfigSetType::string_plus_equals()
*/
static int number_string_plus_equals(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef,
const char *value, struct Buffer *err)
{
int num = 0;
if (!value || !value[0] || (mutt_str_atoi(value, &num) < 0))
{
mutt_buffer_printf(err, _("Invalid number: %s"), NONULL(value));
return CSR_ERR_INVALID | CSR_INV_TYPE;
}

int result = *((short *) var) + num;
if ((result < SHRT_MIN) || (result > SHRT_MAX))
{
mutt_buffer_printf(err, _("Number is too big: %s"), value);
return CSR_ERR_INVALID | CSR_INV_TYPE;
}

if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
{
mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
return CSR_ERR_INVALID | CSR_INV_VALIDATOR;
}

if (cdef->validator)
{
int rc = cdef->validator(cs, cdef, (intptr_t) result, err);

if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc | CSR_INV_VALIDATOR;
}

*(short *) var = result;
return CSR_SUCCESS;
}

/**
* number_string_minus_equals - Subtract from a Number by string - Implements ConfigSetType::string_plus_equals()
*/
static int number_string_minus_equals(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef,
const char *value, struct Buffer *err)
{
int num = 0;
if (!value || !value[0] || (mutt_str_atoi(value, &num) < 0))
{
mutt_buffer_printf(err, _("Invalid number: %s"), value);
return CSR_ERR_INVALID | CSR_INV_TYPE;
}

int result = *((short *) var) - num;
if ((result < SHRT_MIN) || (result > SHRT_MAX))
{
mutt_buffer_printf(err, _("Number is too big: %s"), value);
return CSR_ERR_INVALID | CSR_INV_TYPE;
}

if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
{
mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
return CSR_ERR_INVALID | CSR_INV_VALIDATOR;
}

if (cdef->validator)
{
int rc = cdef->validator(cs, cdef, (intptr_t) result, err);

if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc | CSR_INV_VALIDATOR;
}

*(short *) var = result;
return CSR_SUCCESS;
}

/**
* number_reset - Reset a Number to its initial value - Implements ConfigSetType::reset()
*/
Expand Down Expand Up @@ -182,8 +260,8 @@ void number_init(struct ConfigSet *cs)
number_string_get,
number_native_set,
number_native_get,
NULL, // string_plus_equals
NULL, // string_minus_equals
number_string_plus_equals,
number_string_minus_equals,
number_reset,
NULL, // destroy
};
Expand Down

0 comments on commit 4994102

Please sign in to comment.