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

Subversion Repositories w11

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 wfjm
// $Id: Rtcl.cpp 632 2015-01-11 12:30:03Z 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.0.5  use nullptr
17 19 wfjm
// 2013-01-06   473   1.0.4  add NewListIntObj(const uint(8|16)_t, ...)
18 10 wfjm
// 2011-03-13   369   1.0.2  add NewListIntObj(vector<uint8_t>)
19
// 2011-03-05   366   1.0.1  add AppendResultNewLines()
20
// 2011-02-26   364   1.0    Initial version
21
// 2011-02-13   361   0.1    First draft
22
// ---------------------------------------------------------------------------
23
 
24
/*!
25
  \file
26 29 wfjm
  \version $Id: Rtcl.cpp 632 2015-01-11 12:30:03Z mueller $
27 10 wfjm
  \brief   Implemenation of Rtcl.
28
*/
29
 
30
#include "Rtcl.hpp"
31
 
32
using namespace std;
33
 
34
/*!
35
  \class Retro::Rtcl
36
  \brief FIXME_docs
37
*/
38
 
39 19 wfjm
// all method definitions in namespace Retro
40
namespace Retro {
41
 
42 10 wfjm
//------------------------------------------+-----------------------------------
43
//! FIXME_docs
44
 
45
Tcl_Obj* Rtcl::NewLinesObj(const std::string& str)
46
{
47
  const char* data = str.data();
48
  int size         = str.length();
49
  if (size>0 && data[size-1]=='\n') size -= 1;
50
  return Tcl_NewStringObj(data, size);
51
}
52
 
53
//------------------------------------------+-----------------------------------
54
//! FIXME_docs
55
 
56 19 wfjm
Tcl_Obj* Rtcl::NewListIntObj(const uint8_t* data, size_t size)
57 10 wfjm
{
58 27 wfjm
  if (size == 0) return Tcl_NewListObj(0, nullptr);
59 10 wfjm
 
60
  vector<Tcl_Obj*> vobj;
61 19 wfjm
  vobj.reserve(size);
62 10 wfjm
 
63 19 wfjm
  for (size_t i=0; i<size; i++) {
64
    vobj.push_back(Tcl_NewIntObj((int)data[i]));
65 10 wfjm
  }
66
  return Tcl_NewListObj(vobj.size(), vobj.data());
67
}
68
 
69
//------------------------------------------+-----------------------------------
70
//! FIXME_docs
71
 
72 19 wfjm
Tcl_Obj* Rtcl::NewListIntObj(const uint16_t* data, size_t size)
73 10 wfjm
{
74 27 wfjm
  if (size == 0) return Tcl_NewListObj(0, nullptr);
75 10 wfjm
 
76
  vector<Tcl_Obj*> vobj;
77 19 wfjm
  vobj.reserve(size);
78 10 wfjm
 
79 19 wfjm
  for (size_t i=0; i<size; i++) {
80
    vobj.push_back(Tcl_NewIntObj((int)data[i]));
81 10 wfjm
  }
82
  return Tcl_NewListObj(vobj.size(), vobj.data());
83
}
84
 
85
//------------------------------------------+-----------------------------------
86
//! FIXME_docs
87
 
88 19 wfjm
Tcl_Obj* Rtcl::NewListIntObj(const std::vector<uint8_t>& vec)
89
{
90
  return NewListIntObj(vec.data(), vec.size());
91
}
92
 
93
//------------------------------------------+-----------------------------------
94
//! FIXME_docs
95
 
96
Tcl_Obj* Rtcl::NewListIntObj(const std::vector<uint16_t>& vec)
97
{
98
  return NewListIntObj(vec.data(), vec.size());
99
}
100
 
101
//------------------------------------------+-----------------------------------
102
//! FIXME_docs
103
 
104 10 wfjm
bool Rtcl::SetVar(Tcl_Interp* interp, const std::string& varname, Tcl_Obj* pobj)
105
{
106 29 wfjm
  Tcl_Obj* pret = nullptr;
107 10 wfjm
 
108
  size_t pos_pbeg = varname.find_first_of('(');
109
  size_t pos_pend = varname.find_first_of(')');
110
  if (pos_pbeg != string::npos || pos_pend != string::npos) {
111
    if (pos_pbeg == string::npos || pos_pbeg == 0 ||
112
        pos_pend == string::npos || pos_pend != varname.length()-1 ||
113
        pos_pend-pos_pbeg <= 1) {
114 19 wfjm
      Tcl_AppendResult(interp, "illformed array name '", varname.c_str(),
115 27 wfjm
                       "'", nullptr);
116 10 wfjm
      return false;
117
    }
118
    string arrname(varname.substr(0,pos_pbeg));
119
    string elename(varname.substr(pos_pbeg+1, pos_pend-pos_pbeg-1));
120
 
121
    pret = Tcl_SetVar2Ex(interp, arrname.c_str(), elename.c_str(), pobj,
122
                         TCL_LEAVE_ERR_MSG);
123
  } else {
124 27 wfjm
    pret = Tcl_SetVar2Ex(interp, varname.c_str(), nullptr, pobj,
125 10 wfjm
                         TCL_LEAVE_ERR_MSG);
126
  }
127
 
128
  return pret!=0;
129
}
130
 
131
//------------------------------------------+-----------------------------------
132
//! FIXME_docs
133
 
134
bool Rtcl::SetVarOrResult(Tcl_Interp* interp, const std::string& varname,
135
                          Tcl_Obj* pobj)
136
{
137
  if (varname != "-") {
138
    return SetVar(interp, varname, pobj);
139
  }
140
  Tcl_SetObjResult(interp, pobj);
141
  return true;
142
}
143
 
144
//------------------------------------------+-----------------------------------
145
//! FIXME_docs
146
 
147
void Rtcl::AppendResultNewLines(Tcl_Interp* interp)
148
{
149
  // check whether ObjResult is non-empty, in that case add an '\n'
150
  // that allows to append output from multiple AppendResultLines properly
151
  const char* res =  Tcl_GetStringResult(interp);
152
  if (res && res[0]) {
153 27 wfjm
    Tcl_AppendResult(interp, "\n", nullptr);
154 10 wfjm
  }
155
  return;
156
}
157
 
158
//------------------------------------------+-----------------------------------
159
//! FIXME_docs
160
 
161
void Rtcl::SetResult(Tcl_Interp* interp, const std::string& str)
162
{
163
  Tcl_SetObjResult(interp, NewLinesObj(str));
164
  return;
165
}
166
 
167 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.