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

Subversion Repositories w11

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

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

Line No. Rev Author Line
1 29 wfjm
// $Id: RtclGetList.cpp 631 2015-01-09 21:36:51Z mueller $
2 19 wfjm
//
3 29 wfjm
// Copyright 2013-2015 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 19 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 29 wfjm
// 2015-01-08   631   1.1    add Clear(), add '?' (key list) and '*' (kv list)
17 27 wfjm
// 2014-08-22   584   1.0.1  use nullptr
18 19 wfjm
// 2013-02-12   487   1.0    Initial version
19
// ---------------------------------------------------------------------------
20
 
21
/*!
22
  \file
23 29 wfjm
  \version $Id: RtclGetList.cpp 631 2015-01-09 21:36:51Z mueller $
24 19 wfjm
  \brief   Implemenation of class RtclGetList.
25
*/
26
 
27
#include <iostream>
28
 
29
#include "librtools/Rexception.hpp"
30
 
31
#include "RtclGet.hpp"
32
#include "RtclGetList.hpp"
33 29 wfjm
#include "RtclOPtr.hpp"
34 19 wfjm
 
35
using namespace std;
36
 
37
/*!
38
  \class Retro::RtclGetList
39
  \brief FIXME_docs
40
*/
41
 
42
// all method definitions in namespace Retro
43
namespace Retro {
44
 
45
//------------------------------------------+-----------------------------------
46
//! FIXME_docs
47
 
48
RtclGetList::RtclGetList()
49
  : fMap()
50
{}
51
 
52
//------------------------------------------+-----------------------------------
53
//! FIXME_docs
54
 
55
RtclGetList::~RtclGetList()
56
{
57
  for (map_cit_t it=fMap.begin(); it != fMap.end(); it++) {
58
    delete (it->second);
59
  }
60
}
61
 
62
//------------------------------------------+-----------------------------------
63
//! FIXME_docs
64
 
65
void RtclGetList::Add(const std::string& name, RtclGetBase* pget)
66
{
67
  typedef std::pair<Retro::RtclGetList::map_it_t, bool>  map_ins_t;
68
  map_ins_t ret = fMap.insert(map_val_t(name, pget));
69
  if (ret.second == false)
70 21 wfjm
     throw Rexception("RtclGetList::Add:",
71
                      string("Bad args: duplicate name: '") + name + "'");
72 19 wfjm
  return;
73
}
74
 
75
//------------------------------------------+-----------------------------------
76
//! FIXME_docs
77
 
78 29 wfjm
void RtclGetList::Clear()
79
{
80
  fMap.clear();
81
  return;
82
}
83
 
84
//------------------------------------------+-----------------------------------
85
//! FIXME_docs
86
 
87 19 wfjm
int RtclGetList::M_get(RtclArgs& args)
88
{
89 29 wfjm
  Tcl_Interp* interp = args.Interp();
90
  string pname("*");
91
  if (!args.GetArg("??pname", pname)) return TCL_ERROR;
92
  if (!args.AllDone()) return TCL_ERROR;
93 19 wfjm
 
94 29 wfjm
  if (pname == "?") {
95
    RtclOPtr rlist(Tcl_NewListObj(0,nullptr));
96
    for (const auto& kv : fMap) {
97
      RtclOPtr pele(Tcl_NewStringObj(kv.first.c_str(), -1));
98
      Tcl_ListObjAppendElement(nullptr, rlist, pele);
99
    }
100
    Tcl_SetObjResult(interp, rlist);
101
    return TCL_OK;
102
 
103
  } else if (pname == "*") {
104
    RtclOPtr rlist(Tcl_NewListObj(0,nullptr));
105
    for (const auto& kv : fMap) {
106
      RtclOPtr pele(Tcl_NewStringObj(kv.first.c_str(), -1));
107
      Tcl_ListObjAppendElement(nullptr, rlist, pele);
108
      Tcl_ListObjAppendElement(nullptr, rlist, kv.second->operator()());
109
    }
110
    Tcl_SetObjResult(interp, rlist);
111
    return TCL_OK;
112
  }
113
 
114 19 wfjm
  map_cit_t it = fMap.lower_bound(pname);
115
 
116
  // complain if not found
117
  if (it == fMap.end() || pname != it->first.substr(0,pname.length())) {
118
    Tcl_AppendResult(interp, "-E: unknown property '", pname.c_str(),
119 27 wfjm
                     "': must be ", nullptr);
120 19 wfjm
    const char* delim = "";
121
    for (map_cit_t it1=fMap.begin(); it1!=fMap.end(); it1++) {
122 27 wfjm
      Tcl_AppendResult(interp, delim, it1->first.c_str(), nullptr);
123 19 wfjm
      delim = ",";
124
    }
125
    return TCL_ERROR;
126
  }
127
 
128
  // check for ambiguous substring match
129
  map_cit_t it1 = it;
130
  it1++;
131
  if (it1!=fMap.end() && pname==it1->first.substr(0,pname.length())) {
132
    Tcl_AppendResult(interp, "-E: ambiguous property name '", pname.c_str(),
133 27 wfjm
                     "': must be ", nullptr);
134 19 wfjm
    const char* delim = "";
135
    for (it1=it; it1!=fMap.end() &&
136
           pname==it1->first.substr(0,pname.length()); it1++) {
137 27 wfjm
      Tcl_AppendResult(interp, delim, it1->first.c_str(), nullptr);
138 19 wfjm
      delim = ",";
139
    }
140
 
141
    return TCL_ERROR;
142
  }
143
 
144
  args.SetResult((it->second)->operator()());
145
  return TCL_OK;
146
}
147
 
148
} // end namespace Retro

powered by: WebSVN 2.1.0

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