Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.1.0 - 日本語

Scilab 6.1.0
Change language to:
English - Français - Português - Русский

Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
However, this page did not exist in the previous stable version.

Scilabヘルプ >> API Scilab > legacy > Low level functions > 文字列の読み込み (Scilab ゲートウェイ)

文字列の読み込み (Scilab ゲートウェイ)

ゲートウェイで文字列の行列を読み込む方法.

呼び出し手順

入力引数プロファイル:

SciErr getMatrixOfString(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piLength, char** _pstStrings)
SciErr getMatrixOfWideString(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piLength, wchar_t** _pwstStrings)

名前指定変数プロファイル:

SciErr createNamedMatrixOfString(void* _pvCtx, const char* _pstName, int _iRows, int _iCols, char** _pstStrings)
SciErr createNamedMatrixOfWideString(void* _pvCtx, const char* _pstName, int _iRows, int _iCols, wchar_t** _pwstStrings)

引数

_pvCtx

Scilab環境ポインタ, api_scilab.h により定義された "pvApiCtx"で指定.

_piAddress

Scilab変数のアドレス.

_pstName

"名前指定"関数の場合の変数名.

_piRows

返される行数.

_piCols

返される列数.

_piLength

文字列長の配列のアドレス (要メモリ確保, 大きさ: _piRows * _piCols)

_pstStrings

char*の配列のアドレス (要メモリ確保, 大きさ: _piRows * _piCols)

_pwstStrings

wchar_t* の配列のアドレス(要メモリ確保, 大きさ: _piRows * _piCols)

SciErr

エラー構造体で,エラーメッセージ履歴と最初のエラー番号を格納します.

説明

このヘルプはScilab APIにより文字列の行列を処理する方法を示します.

ゲートウェイのソース

#include "api_scilab.h"
int read_string(char *fname,void* pvApiCtx)
{
	SciErr sciErr;
	int i,j;
	int iLen		= 0;
	//変数の情報
	int iRows		= 0;
	int iCols		= 0;
	int* piAddr		= NULL;
	int* piLen		= NULL;
	char** pstData	= NULL;
	//output variable
	int iRowsOut	= 1;
	int iColsOut	= 1;
	char* pstOut	= NULL;
	//入力/出力引数の確認
    CheckInputArgument(pvApiCtx, 1, 1);
    CheckOutputArgument(pvApiCtx, 0, 1);
	//変数アドレスを取得
	sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//次元を取得するための最初のコール
	sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	piLen = (int*)malloc(sizeof(int) * iRows * iCols);
	//各文字列の長さを取得するための2回目のコール
	sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	pstData = (char**)malloc(sizeof(char*) * iRows * iCols);
	for(i = 0 ; i < iRows * iCols ; i++)
	{
		pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
	}
	//データ取得用の3回目のコール
	sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//全文字列の長さを計算
	for(i = 0 ; i < iRows * iCols ; i++)
	{
		iLen += piLen[i];
	}
	//出力変数用のメモリを確保
	pstOut = (char*)malloc(sizeof(char) * (iLen + iRows * iCols));
	//文字列を0x00に初期化
	memset(pstOut, 0x00, sizeof(char) * (iLen + iRows * iCols));
	//入力文字列を出力文字列に結合
	for(i = 0 ; i < iRows ; i++)
	{
		for(j = 0 ; j < iCols ; j++)
		{
			int iCurLen = strlen(pstOut);
			if(iCurLen)
			{
				strcat(pstOut, " ");
			}
			strcpy(pstOut + strlen(pstOut), pstData[j * iRows + i]);
		}
	}
	//新規変数を作成
	sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iRowsOut, iColsOut, &pstOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//メモリを解放
	free(piLen);
	for(i = 0 ; i < iRows * iCols ; i++)
	{
		free(pstData[i]);
	}
	free(pstData);
	free(pstOut);
	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	return 0;
}

Scilabテストスクリプト

a_ref = ["may the puffin be with you"];
a = ["may", "the", "puffin"; "be","with","you"];
b = read_string(a);
if a_ref <> b then error("failed"), end
Report an issue
<< 疎行列の書き込み (Scilab ゲートウェイ) Low level functions 文字列の書き込み (Scilab ゲートウェイ) >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue Feb 25 08:53:30 CET 2020