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

Subversion Repositories w11

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

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

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