将字符串数组作为输入传递给外部 C 函数

Pass String array as input into external C function

我想将字符串向量传递给外部 C 函数。

在一个最小的例子中,我只想通过 C 函数传递字符串向量(或一维数组)。

我的 Modelica 函数如下所示:

function testreadstri
 input String instri[2];
 output String outstri[2];
 external "C" test_stri(instri,, size(instri, 1), outstri);
 annotation (Include="#include <ebcmysql.cpp>", Library="libmysql");
end testreadstri;

我的 C 函数看起来像:

void test_stri(const char* thestring, size_t nLines, const char **testresult)
{
    //bout = 12.3;

    size_t iLines;
    //size_t nLines;

    iLines = 0;
    //nLines = 1;

    while ( iLines <= nLines ) {

        <LINE_OF_INTEREST>
        iLines++;
    }
}

我为 <LINE_OF_INTEREST> 尝试了以下几行:

有效但当然不会将输入作为输出传递的是:

是否有可能在外部 C 函数中处理 Modelica 输入字符串向量?

提前致谢!

是的,这受 Modelica 规范支持,请参阅 https://specification.modelica.org/v3.4/Ch12.html#argument-type-mapping

这里有一个 short, self-contained and compilable example 演示了 Modelica 中纯外部函数的输入字符串和输出字符串处理

model Model
  function testreadstri
    input String instri[2];
    output String outstri[2];
    external "C" test_stri(instri, size(instri, 1), outstri, size(outstri, 1));
    annotation(Include="
#include \"ModelicaUtilities.h\"
#include <stdlib.h>
#include <string.h>

void test_stri(const char** thestring, size_t nLinesIn, const char** testresult, size_t nLinesOut)
{
  size_t iLines;

  // example for input string handling
  for (iLines = 0; iLines < nLinesIn; iLines++) {
    ModelicaFormatMessage(\"%s\n\", thestring[iLines]);
  }

  // example for output string handling
  for (iLines = 0; iLines < nLinesOut; iLines++) {
    char* line = ModelicaAllocateStringWithErrorReturn(6);
    if (line != NULL) {
      strcpy(line, \"result\");
      testresult[iLines] = line;
    }
  }
}");
  end testreadstri;
  String s[:] = testreadstri({"first", "second"});
end Model;