Coverage Report

Created: 2024-09-08 06:23

/src/aspell/common/itemize.cpp
Line
Count
Source (jump to first uncovered line)
1
// This file is part of The New Aspell
2
// Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license
3
// version 2.0 or 2.1.  You should have received a copy of the LGPL
4
// license along with this library if you did not you can find
5
// it at http://www.gnu.org/.
6
7
#include <string.h>
8
#include <stdlib.h>
9
10
#include "asc_ctype.hpp"
11
#include "itemize.hpp"
12
#include "mutable_container.hpp"
13
#include <stdio.h>
14
#include <cstdio>
15
16
//FIXME: it should be possible to escape ',' '+' '-' '!' so that they can
17
//       appear in values
18
//       If '\' is used, then what about when the option file is parsed
19
//         as it strips away all '\' escapes.
20
21
namespace acommon {
22
23
  struct ItemizeItem {
24
    char   action;
25
    const char * name;
26
664
    ItemizeItem() : action('\0'), name(0) {}
27
  };
28
29
  class ItemizeTokenizer {
30
  private:
31
    char * list;
32
    char * i;
33
  public:
34
    ItemizeTokenizer(const char * l);
35
    ~ItemizeTokenizer();
36
  private:
37
    ItemizeTokenizer(const ItemizeTokenizer & other) ;
38
    ItemizeTokenizer & operator=(const ItemizeTokenizer & other);
39
  public:
40
    ItemizeItem next();
41
  };
42
43
  ItemizeTokenizer::ItemizeTokenizer(const char * l) 
44
166
  {
45
166
    size_t size = strlen(l) + 1;
46
166
    list = new char[size];
47
166
    i = list;
48
166
    strncpy(list, l, size);
49
166
  }
50
51
  ItemizeTokenizer::~ItemizeTokenizer() 
52
166
  {
53
166
    delete[] list;
54
166
  }
55
56
57
  ItemizeItem ItemizeTokenizer::next() 
58
498
  {
59
498
    ItemizeItem li;
60
498
    while (*i != '\0' && (asc_isspace(*i) || *i == ',')) ++i;
61
498
    if (*i == '\0') return li;
62
332
    li.action = *i;
63
332
    if (*i == '+' || *i == '-') {
64
0
      ++i;
65
332
    } else if (*i == '!') {
66
0
      li.name = "";
67
0
      ++i;
68
0
      return li;
69
332
    } else {
70
332
      li.action = '+';
71
332
    }
72
332
    while (*i != '\0' && *i != ',' && asc_isspace(*i)) ++i;
73
332
    if (*i == '\0' || *i == ',') return next();
74
332
    li.name = i;
75
2.32k
    while (*i != '\0' && *i != ',') ++i;
76
332
    while (i != list && asc_isspace(*(i-1))) --i;
77
332
    if (*i != '\0') {
78
166
      *i = '\0';
79
166
      ++i;
80
166
    }
81
332
    return li;
82
332
  }
83
84
85
166
  PosibErr<void> itemize (ParmString s, MutableContainer & d) {
86
166
    ItemizeTokenizer els(s);
87
166
    ItemizeItem li;
88
498
    while (li = els.next(), li.name != 0) {
89
332
      switch (li.action) {
90
332
      case '+':
91
332
  RET_ON_ERR(d.add(li.name));
92
332
  break;
93
332
      case '-':
94
0
  RET_ON_ERR(d.remove(li.name));
95
0
  break;
96
0
      case '!':
97
0
  RET_ON_ERR(d.clear());
98
0
  break;
99
0
      default:
100
0
  abort();
101
332
      }
102
332
    }
103
166
    return no_err;
104
166
  }
105
106
}