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