NCBI C++ ToolKit
rangelist.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: rangelist.cpp 74326 2016-08-25 03:53:47Z sadyrovr $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors:
27  * Dmitry Kazimirov
28  *
29  * File Description:
30  * Implementation of the CRangeList class.
31  */
32 
33 #include <ncbi_pch.hpp>
34 
35 #include <util/rangelist.hpp>
36 
38 
39 static const char* s_SkipSpaces(const char* input_string)
40 {
41  while (*input_string == ' ' || *input_string == '\t')
42  ++input_string;
43 
44  return input_string;
45 }
46 
47 void CRangeListImpl::Parse(const char* init_string,
48  const char* config_param_name,
49  TRangeVector* range_vector)
50 {
51  if (*init_string == '\0') {
53  "Configuration parameter '" << config_param_name <<
54  "' is not defined.");
55  }
56 
57  range_vector->clear();
58 
59  const char* pos = init_string;
60 
61  TIntegerRange new_range;
62 
63  int* current_bound_ptr = &new_range.first;
64  bool reading_range = false;
65 
66  for (;;) {
67  pos = s_SkipSpaces(pos);
68 
69  bool negative = *pos == '-' ? (++pos, true) : false;
70 
71  unsigned number = (unsigned) (*pos - '0');
72 
73  if (number > 9) {
74  NCBI_THROW_FMT(CInvalidParamException, eInvalidCharacter,
75  "'" << config_param_name <<
76  "': not a number at position " << (pos - init_string + 1));
77  }
78 
79  unsigned digit;
80 
81  while ((digit = (unsigned) (*++pos - '0')) <= 9)
82  number = number * 10 + digit;
83 
84  *current_bound_ptr = negative ? -int(number) : int(number);
85 
86  pos = s_SkipSpaces(pos);
87 
88  switch (*pos) {
89  case '\0':
90  case ',':
91  if (!reading_range)
92  new_range.second = new_range.first;
93  range_vector->push_back(new_range);
94  if (*pos == '\0')
95  return;
96  ++pos;
97  current_bound_ptr = &new_range.first;
98  new_range.second = 0;
99  break;
100 
101  case '-':
102  ++pos;
103  current_bound_ptr = &new_range.second;
104  break;
105 
106  default:
107  NCBI_THROW_FMT(CInvalidParamException, eInvalidCharacter,
108  "'" << config_param_name <<
109  "': invalid character at position " <<
110  (pos - init_string + 1));
111  }
112  }
113 }
114 
CInvalidParamException –.
Definition: ncbiexpt.hpp:1505
std::pair< int, int > TIntegerRange
Definition: rangelist.hpp:48
std::vector< TIntegerRange > TRangeVector
Definition: rangelist.hpp:49
const TRangeVector & Parse(const char *init_string, const char *config_param_name)
Definition: rangelist.hpp:51
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
Definition: ncbiexpt.hpp:719
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
T negative(T x_)
static BOOL number
Definition: pcregrep.c:193
static const char * s_SkipSpaces(const char *input_string)
Definition: rangelist.cpp:39
Modified on Sun Apr 14 05:25:44 2024 by modify_doxy.py rev. 669887