Common lisp 将字符串从ECL传递到C++; 我试图进入C++中嵌入的普通LISP的迷人世界。我的问题是,我无法读取和打印C++中由ECL .中定义的LISP函数返回的字符串。 在C++中,我有这个函数运行任意的LISP表达式: cl_object lisp(const std::string & call) { return cl_safe_eval(c_string_to_object(call.c_str()), Cnil, Cnil); }

Common lisp 将字符串从ECL传递到C++; 我试图进入C++中嵌入的普通LISP的迷人世界。我的问题是,我无法读取和打印C++中由ECL .中定义的LISP函数返回的字符串。 在C++中,我有这个函数运行任意的LISP表达式: cl_object lisp(const std::string & call) { return cl_safe_eval(c_string_to_object(call.c_str()), Cnil, Cnil); },common-lisp,ecl,Common Lisp,Ecl,我可以通过以下方式使用一个数字来完成它: ECL: 用C++阅读和打印: auto x = ecl_to_float(lisp("(return-a-number)")); std::cout << "The number is " << x << std::endl; C++: cl_对象y=lisp(“(返回字符串)”); std::cout根据的部分,我认为实际的字符数组是通过访问返回对象的string.self字段找到的。你能试试下面的吗 std::

可以通过以下方式使用一个数字来完成它

ECL:

用C++阅读和打印:

auto x = ecl_to_float(lisp("(return-a-number)"));
std::cout << "The number is " << x << std::endl;
C++:

cl_对象y=lisp(“(返回字符串)”);
std::cout根据的部分,我认为实际的字符数组是通过访问返回对象的
string.self
字段找到的。你能试试下面的吗

std::cout << y->string.self << std::endl;
std::cout string.self(从@coredump的答案开始,
string.self
字段提供结果。)

string.self
字段被定义为type
ecl\u character*
(ecl/object.h),它在ecl/config.h中似乎作为type
int
(尽管我怀疑这有点依赖于平台)。因此,您不能像打印字符数组一样打印它

我发现对我有效的方法是将其重新解释为
wchar\u t
(即unicode字符)。不幸的是,我很确定这不是可移植的,这既取决于ECL的配置方式,也取决于C++编译器。
// basic check that this should work
static_assert(sizeof(ecl_character)==sizeof(wchar_t),"sizes must be the same");

std::wcout << "A string: " << reinterpret_cast<wchar_t*>(y->string.self) << std::endl;
// prints hello, as required
// note the use of wcout
(可能有更优雅的方法可以转换为
基本字符串,但我不知道)

用C语言打印++

cl_object y2 = lisp("(return-a-base-string)");
std::cout << "Another: " << y2->base_string.self << std::endl;
cl_object y2=lisp(“(return-a-base-string)”);
std::cout
std::string str{”“};
cl_object y2=lisp(“(return-a-base-string)”);
//获取维度
int j=y2->string.dim;
//获取指针
ecl_character*selv=y2->string.self;
//做简单的指针加法

对于(inti=0;i在我的系统上,不需要返回-a-base-string,但我认为为了兼容性可以添加它。我使用(ecl)嵌入的CLISP 16.1.2版本。 下面的代码从LISP读取字符串并转换为C++字符串类型——STD::string和c-字符串并将它们存储在C++变量:

// strings initializations: string and c-string
std::string str2 {""};     
char str_c[99] = " ";
// text read from clisp, whatever clisp function that returns string type
cl_object cl_text = lisp("(coerce (text-from-lisp X) 'base-string)");
//cl_object cl_text = lisp("(text-from-lisp X)"); // no base string conversions
// catch dimension
int cl_text_dim = cl_text->string.dim;
// complete c-string char by char
for(int ind=0;i<cl_text_dim;i++){
str_c[i] = ecl_char(cl_text,i); // ecl function to get char from cl_object
   }
str_c[cl_text_dim] ='\0'; // end of the c-string
str2 = str_c; // get the string on the other string type
std::cout <<  "Dim: " << cl_ text_dim << " C-String var: " << str_c() << " String var << str2 << std::endl;   
//字符串初始化:字符串和c字符串
std::字符串str2{“};
char str_c[99]=“”;
//从clisp读取的文本,返回字符串类型的任何clisp函数
cl_对象cl_text=lisp((强制(来自lisp X的文本)‘基本字符串’);
//cl_object cl_text=lisp(“(lisp X中的文本)”;//无基本字符串转换
//捕获维度
int cl_text_dim=cl_text->string.dim;
//逐字符完成c字符串

对于(In IND=0;Iys:我也尝试过了,内存地址是打印的。@ USER 6034 704我现在不能测试,但是我稍后再检查。我想您可以使用<代码> CLUPINS/<代码>来打印LISP对象,如果需要的话,但我猜这是一个更大的解决方案。CordEMP C++知道“代码> y->字符串。有两种方法:1)将其强制为
char*
或2)从中构造
std::string
。是的,我可以使用cl_print打印它,但我希望它在变量中,因为我需要在GUI中而不是控制台中显示它。
// basic check that this should work
static_assert(sizeof(ecl_character)==sizeof(wchar_t),"sizes must be the same");

std::wcout << "A string: " << reinterpret_cast<wchar_t*>(y->string.self) << std::endl;
// prints hello, as required
// note the use of wcout
(defun return-a-base-string ()
    (coerce "Hello" 'base-string))
cl_object y2 = lisp("(return-a-base-string)");
std::cout << "Another: " << y2->base_string.self << std::endl;
std::string str {""};    
cl_object y2 = lisp("(return-a-base-string)");
//get dimension
int j = y2->string.dim;
//get pointer
ecl_character* selv = y2->string.self;
//do simple pointer addition
for(int i=0;i<j;i++){
    str += (*(selv+i));
}
//do whatever you want to str
// strings initializations: string and c-string
std::string str2 {""};     
char str_c[99] = " ";
// text read from clisp, whatever clisp function that returns string type
cl_object cl_text = lisp("(coerce (text-from-lisp X) 'base-string)");
//cl_object cl_text = lisp("(text-from-lisp X)"); // no base string conversions
// catch dimension
int cl_text_dim = cl_text->string.dim;
// complete c-string char by char
for(int ind=0;i<cl_text_dim;i++){
str_c[i] = ecl_char(cl_text,i); // ecl function to get char from cl_object
   }
str_c[cl_text_dim] ='\0'; // end of the c-string
str2 = str_c; // get the string on the other string type
std::cout <<  "Dim: " << cl_ text_dim << " C-String var: " << str_c() << " String var << str2 << std::endl;