diff options
Diffstat (limited to 'gdb/cli')
-rw-r--r-- | gdb/cli/cli-utils.c | 76 | ||||
-rw-r--r-- | gdb/cli/cli-utils.h | 114 |
2 files changed, 112 insertions, 78 deletions
diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index 0946db06c49..0fb68f2e1fe 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -121,39 +121,49 @@ get_number (char **pp) /* See documentation in cli-utils.h. */ +number_or_range_parser::number_or_range_parser (const char *string) +{ + init (string); +} + +/* See documentation in cli-utils.h. */ + void -init_number_or_range (struct get_number_or_range_state *state, - const char *string) +number_or_range_parser::init (const char *string) { - memset (state, 0, sizeof (*state)); - state->string = string; + m_finished = false; + m_cur_tok = string; + m_last_retval = 0; + m_end_value = 0; + m_end_ptr = NULL; + m_in_range = false; } /* See documentation in cli-utils.h. */ int -get_number_or_range (struct get_number_or_range_state *state) +number_or_range_parser::get_number () { - if (state->in_range) + if (m_in_range) { /* All number-parsing has already been done. Return the next integer value (one greater than the saved previous value). Do not advance the token pointer until the end of range is reached. */ - if (++state->last_retval == state->end_value) + if (++m_last_retval == m_end_value) { /* End of range reached; advance token pointer. */ - state->string = state->end_ptr; - state->in_range = 0; + m_cur_tok = m_end_ptr; + m_in_range = false; } } - else if (*state->string != '-') + else if (*m_cur_tok != '-') { - /* Default case: state->string is pointing either to a solo + /* Default case: state->m_cur_tok is pointing either to a solo number, or to the first number of a range. */ - state->last_retval = get_number_trailer (&state->string, '-'); - if (*state->string == '-') + m_last_retval = get_number_trailer (&m_cur_tok, '-'); + if (*m_cur_tok == '-') { const char **temp; @@ -161,42 +171,42 @@ get_number_or_range (struct get_number_or_range_state *state) Skip the '-', parse and remember the second number, and also remember the end of the final token. */ - temp = &state->end_ptr; - state->end_ptr = skip_spaces_const (state->string + 1); - state->end_value = get_number_const (temp); - if (state->end_value < state->last_retval) + temp = &m_end_ptr; + m_end_ptr = skip_spaces_const (m_cur_tok + 1); + m_end_value = get_number_const (temp); + if (m_end_value < m_last_retval) { error (_("inverted range")); } - else if (state->end_value == state->last_retval) + else if (m_end_value == m_last_retval) { /* Degenerate range (number1 == number2). Advance the token pointer so that the range will be treated as a - single number. */ - state->string = state->end_ptr; + single number. */ + m_cur_tok = m_end_ptr; } else - state->in_range = 1; + m_in_range = true; } } else error (_("negative value")); - state->finished = *state->string == '\0'; - return state->last_retval; + m_finished = *m_cur_tok == '\0'; + return m_last_retval; } /* See documentation in cli-utils.h. */ void -number_range_setup_range (struct get_number_or_range_state *state, - int start_value, int end_value, const char *end_ptr) +number_or_range_parser::setup_range (int start_value, int end_value, + const char *end_ptr) { gdb_assert (start_value > 0); - state->in_range = 1; - state->end_ptr = end_ptr; - state->last_retval = start_value - 1; - state->end_value = end_value; + m_in_range = true; + m_end_ptr = end_ptr; + m_last_retval = start_value - 1; + m_end_value = end_value; } /* Accept a number and a string-form list of numbers such as is @@ -210,15 +220,13 @@ number_range_setup_range (struct get_number_or_range_state *state, int number_is_in_list (const char *list, int number) { - struct get_number_or_range_state state; - if (list == NULL || *list == '\0') return 1; - init_number_or_range (&state, list); - while (!state.finished) + number_or_range_parser parser (list); + while (!parser.finished ()) { - int gotnum = get_number_or_range (&state); + int gotnum = parser.get_number (); if (gotnum == 0) error (_("Args must be numbers or '$' variables.")); diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index a31fff53969..3188bb78df0 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -39,65 +39,91 @@ extern int get_number_const (const char **); extern int get_number (char **); -/* An object of this type is passed to get_number_or_range. It must - be initialized by calling init_number_or_range. This type is - defined here so that it can be stack-allocated, but all members - other than `finished' and `string' should be treated as opaque. */ +/* Parse a number or a range. + A number will be of the form handled by get_number. + A range will be of the form <number1> - <number2>, and + will represent all the integers between number1 and number2, + inclusive. */ -struct get_number_or_range_state +class number_or_range_parser { - /* Non-zero if parsing has completed. */ - int finished; +public: + /* Default construction. Must call init before calling + get_next. */ + number_or_range_parser () {} + + /* Calls init automatically. */ + number_or_range_parser (const char *string); + + /* STRING is the string to be parsed. */ + void init (const char *string); + + /* While processing a range, this fuction is called iteratively; At + each call it will return the next value in the range. + + At the beginning of parsing a range, the char pointer + STATE->m_cur_tok will be advanced past <number1> and left + pointing at the '-' token. Subsequent calls will not advance the + pointer until the range is completed. The call that completes + the range will advance the pointer past <number2>. */ + int get_number (); + + /* Setup internal state such that get_next() returns numbers in the + START_VALUE to END_VALUE range. END_PTR is where the string is + advanced to when get_next() returns END_VALUE. */ + void setup_range (int start_value, int end_value, + const char *end_ptr); + + /* Returns true if parsing has completed. */ + bool finished () const + { return m_finished; } + + /* Return the string being parsed. When parsing has finished, this + points past the last parsed token. */ + const char *cur_tok () const + { return m_cur_tok; } + + /* True when parsing a range. */ + bool in_range () const + { return m_in_range; } + + /* When parsing a range, the final value in the range. */ + int end_value () const + { return m_end_value; } + + /* When parsing a range, skip past the final token in the range. */ + void skip_range () + { + gdb_assert (m_in_range); + m_cur_tok = m_end_ptr; + } + +private: + /* No need for these. They are intentionally not defined anywhere. */ + number_or_range_parser (const number_or_range_parser &); + number_or_range_parser &operator= (const number_or_range_parser &); + + /* True if parsing has completed. */ + bool m_finished; /* The string being parsed. When parsing has finished, this points past the last parsed token. */ - const char *string; + const char *m_cur_tok; /* Last value returned. */ - int last_retval; + int m_last_retval; /* When parsing a range, the final value in the range. */ - int end_value; + int m_end_value; /* When parsing a range, a pointer past the final token in the range. */ - const char *end_ptr; + const char *m_end_ptr; - /* Non-zero when parsing a range. */ - int in_range; + /* True when parsing a range. */ + bool m_in_range; }; -/* Initialize a get_number_or_range_state for use with - get_number_or_range_state. STRING is the string to be parsed. */ - -extern void init_number_or_range (struct get_number_or_range_state *state, - const char *string); - -/* Parse a number or a range. - A number will be of the form handled by get_number. - A range will be of the form <number1> - <number2>, and - will represent all the integers between number1 and number2, - inclusive. - - While processing a range, this fuction is called iteratively; - At each call it will return the next value in the range. - - At the beginning of parsing a range, the char pointer STATE->string will - be advanced past <number1> and left pointing at the '-' token. - Subsequent calls will not advance the pointer until the range - is completed. The call that completes the range will advance - the pointer past <number2>. */ - -extern int get_number_or_range (struct get_number_or_range_state *state); - -/* Setups STATE such that get_number_or_range returns numbers in range - START_VALUE to END_VALUE. When get_number_or_range returns - END_VALUE, the STATE string is advanced to END_PTR. */ - -extern void number_range_setup_range (struct get_number_or_range_state *state, - int start_value, int end_value, - const char *end_ptr); - /* Accept a number and a string-form list of numbers such as is accepted by get_number_or_range. Return TRUE if the number is in the list. |