OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librtcltools/] [RtclNameSet.cpp] - Blame information for rev 33

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 wfjm
// $Id: RtclNameSet.cpp 584 2014-08-22 19:38:12Z mueller $
2 10 wfjm
//
3 27 wfjm
// Copyright 2011-2014 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 10 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 27 wfjm
// 2014-08-22   584   1.1.1  use nullptr
17 22 wfjm
// 2013-05-19   521   1.1    add CheckMatch()
18 19 wfjm
// 2013-02-03   481   1.0.1  use Rexception
19 10 wfjm
// 2011-02-20   363   1.0    Initial version
20
// ---------------------------------------------------------------------------
21
 
22
/*!
23
  \file
24 27 wfjm
  \version $Id: RtclNameSet.cpp 584 2014-08-22 19:38:12Z mueller $
25 10 wfjm
  \brief   Implemenation of RtclNameSet.
26
*/
27
 
28
// debug
29
#include <iostream>
30
 
31
#include "RtclNameSet.hpp"
32
 
33 19 wfjm
#include "librtools/Rexception.hpp"
34
 
35 10 wfjm
using namespace std;
36
 
37
/*!
38
  \class Retro::RtclNameSet
39
  \brief FIXME_docs
40
*/
41
 
42 19 wfjm
// all method definitions in namespace Retro
43
namespace Retro {
44 10 wfjm
 
45 19 wfjm
typedef std::pair<RtclNameSet::nset_it_t, bool>  nset_ins_t;
46
 
47 10 wfjm
//------------------------------------------+-----------------------------------
48
//! Default constructor
49
 
50
RtclNameSet::RtclNameSet()
51
  : fSet()
52
{}
53
 
54
//------------------------------------------+-----------------------------------
55
//! FIXME_docs
56
 
57
RtclNameSet::RtclNameSet(const std::string& nset)
58
  : fSet()
59
{
60
  size_t ibeg=0;
61
  while (true) {
62
    size_t iend = nset.find_first_of('|', ibeg);
63
    if (iend-ibeg > 0) {
64
      string name(nset, ibeg, iend-ibeg);
65
      nset_ins_t ret = fSet.insert(name);
66
        if (ret.second == false)                  // or use !(ret.second)
67 19 wfjm
          throw Rexception("RtclNameSet::<ctor>", "Bad args: " +
68
                           string("duplicate name '") + name +
69 21 wfjm
                           "' in set '" + nset + "'");
70 10 wfjm
    }
71
    if (iend == string::npos) break;
72
    ibeg = iend+1;
73
  }
74
}
75
 
76
//------------------------------------------+-----------------------------------
77
//! Destructor
78
 
79
RtclNameSet::~RtclNameSet()
80
{}
81
 
82
//------------------------------------------+-----------------------------------
83
//! FIXME_docs
84
 
85 22 wfjm
bool RtclNameSet::Check(Tcl_Interp* interp, std::string& rval,
86
                        const std::string& tval) const
87 10 wfjm
{
88 22 wfjm
  return CheckMatch(interp, rval, tval, true) > 0;
89
}
90
 
91
//------------------------------------------+-----------------------------------
92
//! FIXME_docs
93
//  irc = 1 -> match
94
//        0 -> ambiguous match  --> tcl err
95
//       -1 -> no match         --> tcl err if misserr
96
 
97
int RtclNameSet::CheckMatch(Tcl_Interp* interp, std::string& rval,
98
                            const std::string& tval, bool misserr) const
99
{
100 10 wfjm
  rval.clear();
101
  nset_cit_t it = fSet.lower_bound(tval);
102
 
103
  // no leading substring match
104
  if (it==fSet.end() || tval!=it->substr(0,tval.length())) {
105 22 wfjm
    if (misserr) {
106
      Tcl_AppendResult(interp, "-E: bad option '", tval.c_str(),
107 27 wfjm
                       "': must be ", nullptr);
108 22 wfjm
      const char* delim = "";
109
      for (nset_cit_t it1=fSet.begin(); it1!=fSet.end(); it1++) {
110 27 wfjm
        Tcl_AppendResult(interp, delim, it1->c_str(), nullptr);
111 22 wfjm
        delim = ",";
112
      }
113 10 wfjm
    }
114 22 wfjm
    return -1;
115 10 wfjm
  }
116
 
117
  // check for ambiguous substring match
118
  if (tval != *it) {
119
    nset_cit_t it1 = it;
120
    it1++;
121
    if (it1!=fSet.end() && tval==it1->substr(0,tval.length())) {
122 19 wfjm
      Tcl_AppendResult(interp, "-E: ambiguous option '", tval.c_str(),
123 27 wfjm
                       "': must be ", nullptr);
124 10 wfjm
      const char* delim = "";
125
      for (it1=it; it1!=fSet.end() &&
126
             tval==it1->substr(0,tval.length()); it1++) {
127 27 wfjm
        Tcl_AppendResult(interp, delim, it1->c_str(), nullptr);
128 10 wfjm
        delim = ",";
129
      }
130 22 wfjm
      return 0;
131 10 wfjm
    }
132
  }
133
 
134
  rval = *it;
135 22 wfjm
  return 1;
136 10 wfjm
}
137
 
138 19 wfjm
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.