w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RtclNameSet.cpp
Go to the documentation of this file.
1// $Id: RtclNameSet.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-11-16 1070 1.1.2 use auto; use range loop
8// 2014-08-22 584 1.1.1 use nullptr
9// 2013-05-19 521 1.1 add CheckMatch()
10// 2013-02-03 481 1.0.1 use Rexception
11// 2011-02-20 363 1.0 Initial version
12// ---------------------------------------------------------------------------
13
18// debug
19#include <iostream>
20
21#include "RtclNameSet.hpp"
22
24
25using namespace std;
26
32// all method definitions in namespace Retro
33namespace Retro {
34
35//------------------------------------------+-----------------------------------
37
39 : fSet()
40{}
41
42//------------------------------------------+-----------------------------------
44
45RtclNameSet::RtclNameSet(const std::string& nset)
46 : fSet()
47{
48 size_t ibeg=0;
49 while (true) {
50 size_t iend = nset.find_first_of('|', ibeg);
51 if (iend-ibeg > 0) {
52 string name(nset, ibeg, iend-ibeg);
53 auto ret = fSet.insert(name);
54 if (ret.second == false) // or use !(ret.second)
55 throw Rexception("RtclNameSet::<ctor>", "Bad args: " +
56 string("duplicate name '") + name +
57 "' in set '" + nset + "'");
58 }
59 if (iend == string::npos) break;
60 ibeg = iend+1;
61 }
62}
63
64//------------------------------------------+-----------------------------------
66
68{}
69
70//------------------------------------------+-----------------------------------
72
73bool RtclNameSet::Check(Tcl_Interp* interp, std::string& rval,
74 const std::string& tval) const
75{
76 return CheckMatch(interp, rval, tval, true) > 0;
77}
78
79//------------------------------------------+-----------------------------------
81// irc = 1 -> match
82// 0 -> ambiguous match --> tcl err
83// -1 -> no match --> tcl err if misserr
84
85int RtclNameSet::CheckMatch(Tcl_Interp* interp, std::string& rval,
86 const std::string& tval, bool misserr) const
87{
88 rval.clear();
89 auto it = fSet.lower_bound(tval);
90
91 // no leading substring match
92 if (it==fSet.end() || tval!=it->substr(0,tval.length())) {
93 if (misserr) {
94 Tcl_AppendResult(interp, "-E: bad option '", tval.c_str(),
95 "': must be ", nullptr);
96 const char* delim = "";
97 for (auto& o: fSet) {
98 Tcl_AppendResult(interp, delim, o.c_str(), nullptr);
99 delim = ",";
100 }
101 }
102 return -1;
103 }
104
105 // check for ambiguous substring match
106 if (tval != *it) {
107 auto it1 = it;
108 it1++;
109 if (it1!=fSet.end() && tval==it1->substr(0,tval.length())) {
110 Tcl_AppendResult(interp, "-E: ambiguous option '", tval.c_str(),
111 "': must be ", nullptr);
112 const char* delim = "";
113 for (it1=it; it1!=fSet.end() &&
114 tval==it1->substr(0,tval.length()); it1++) {
115 Tcl_AppendResult(interp, delim, it1->c_str(), nullptr);
116 delim = ",";
117 }
118 return 0;
119 }
120 }
121
122 rval = *it;
123 return 1;
124}
125
126} // end namespace Retro
FIXME_docs.
Definition: Rexception.hpp:29
~RtclNameSet()
Destructor.
Definition: RtclNameSet.cpp:67
int CheckMatch(Tcl_Interp *interp, std::string &rval, const std::string &tval, bool misserr) const
FIXME_docs.
Definition: RtclNameSet.cpp:85
bool Check(Tcl_Interp *interp, std::string &rval, const std::string &tval) const
FIXME_docs.
Definition: RtclNameSet.cpp:73
RtclNameSet()
Default constructor.
Definition: RtclNameSet.cpp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47