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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.6/] [tools/] [src/] [librtcltools/] [RtclCmdBase.cpp] - Blame information for rev 40

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 wfjm
// $Id: RtclCmdBase.cpp 516 2013-05-05 21:24:52Z mueller $
2 10 wfjm
//
3 19 wfjm
// Copyright 2011-2013 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 19 wfjm
// 2013-02-10   485   1.0.2  add static const defs
17
// 2013-02-05   483   1.0.1  remove 'unknown specified, full match only' logic
18
// 2013-02-02   480   1.0    Initial version (refactored out from ProxyBase)
19 10 wfjm
// ---------------------------------------------------------------------------
20
 
21
/*!
22
  \file
23 21 wfjm
  \version $Id: RtclCmdBase.cpp 516 2013-05-05 21:24:52Z mueller $
24 19 wfjm
  \brief   Implemenation of RtclCmdBase.
25 10 wfjm
*/
26
 
27 19 wfjm
#include "RtclCmdBase.hpp"
28 10 wfjm
 
29 19 wfjm
#include "librtools/Rexception.hpp"
30 10 wfjm
#include "Rtcl.hpp"
31
 
32
using namespace std;
33
 
34
/*!
35 19 wfjm
  \class Retro::RtclCmdBase
36 10 wfjm
  \brief FIXME_docs
37
*/
38
 
39 19 wfjm
// all method definitions in namespace Retro
40
namespace Retro {
41 10 wfjm
 
42 19 wfjm
typedef std::pair<RtclCmdBase::mmap_it_t, bool>  mmap_ins_t;
43
 
44 10 wfjm
//------------------------------------------+-----------------------------------
45 19 wfjm
// constants definitions
46 10 wfjm
 
47 19 wfjm
const int RtclCmdBase::kOK;
48
const int RtclCmdBase::kERR;
49 10 wfjm
 
50
//------------------------------------------+-----------------------------------
51 12 wfjm
//! FIXME_docs
52
 
53 19 wfjm
RtclCmdBase::RtclCmdBase()
54
  : fMapMeth()
55
{}
56 12 wfjm
 
57
//------------------------------------------+-----------------------------------
58 10 wfjm
//! Destructor
59
 
60 19 wfjm
RtclCmdBase::~RtclCmdBase()
61
{}
62 10 wfjm
 
63
//------------------------------------------+-----------------------------------
64
//! FIXME_docs
65
 
66 19 wfjm
int RtclCmdBase::DispatchCmd(RtclArgs& args)
67 10 wfjm
{
68 19 wfjm
  mmap_cit_t it_match;
69 10 wfjm
 
70 19 wfjm
  Tcl_Interp* interp = args.Interp();
71 10 wfjm
 
72 19 wfjm
  // no method name given
73
  if (args.Objc() <= 1) {                   // no args
74
    it_match = fMapMeth.find("$default");   // default method registered ?
75
    if (it_match != fMapMeth.end()) {
76
      return (it_match->second)(args);
77
    }
78
    Tcl_WrongNumArgs(interp, 1, args.Objv(), "option ?args?"); // or fail
79
    return kERR;
80
  }
81
 
82
  // here if at least method name given
83
  string name(Tcl_GetString(args[1]));
84 10 wfjm
 
85 19 wfjm
  it_match = fMapMeth.lower_bound(name);
86
 
87
  // no leading substring match
88
  if (it_match==fMapMeth.end() ||
89
      name!=it_match->first.substr(0,name.length())) {
90 10 wfjm
 
91 19 wfjm
    mmap_cit_t it_un = fMapMeth.find("$unknown"); // unknown method registered ?
92
    if (it_un!=fMapMeth.end()) {
93
      return (it_un->second)(args);
94 10 wfjm
    }
95
 
96 19 wfjm
    Tcl_AppendResult(interp, "-E: bad option '", name.c_str(),
97
                     "': must be ", NULL);
98
    const char* delim = "";
99
    for (mmap_cit_t it1=fMapMeth.begin(); it1!=fMapMeth.end(); it1++) {
100
      if (it1->first.c_str()[0] != '$') {
101
        Tcl_AppendResult(interp, delim, it1->first.c_str(), NULL);
102
        delim = ",";
103
      }
104 10 wfjm
    }
105 19 wfjm
    return kERR;
106
  }
107 10 wfjm
 
108 19 wfjm
  // check for ambiguous substring match
109
  if (name != it_match->first) {
110
    mmap_cit_t it1 = it_match;
111
    it1++;
112
    if (it1!=fMapMeth.end() && name==it1->first.substr(0,name.length())) {
113
      Tcl_AppendResult(interp, "-E: ambiguous option '",
114
                       name.c_str(), "': must be ", NULL);
115
      const char* delim = "";
116
      for (it1=it_match; it1!=fMapMeth.end() &&
117
             name==it1->first.substr(0,name.length()); it1++) {
118
        Tcl_AppendResult(interp, delim, it1->first.c_str(), NULL);
119
        delim = ",";
120 10 wfjm
      }
121 19 wfjm
      return kERR;
122 10 wfjm
    }
123
  }
124
 
125 12 wfjm
  return (it_match->second)(args);
126 10 wfjm
}
127
 
128
//------------------------------------------+-----------------------------------
129
//! FIXME_docs
130
 
131 19 wfjm
void RtclCmdBase::AddMeth(const std::string& name, const methfo_t& methfo)
132 10 wfjm
{
133 19 wfjm
  mmap_ins_t ret = fMapMeth.insert(mmap_val_t(name, methfo));
134
  if (ret.second == false)                  // or use !(ret.second)
135 21 wfjm
    throw Rexception("RtclCmdBase::AddMeth:",
136
                     string("Bad args: duplicate name: '") + name + "'");
137 10 wfjm
  return;
138
}
139
 
140 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.