MTemplateのこと

「$MOBA_HOME/src/xs/MTemplate」にあるMTemplate.xsを読んでみた。

ソースの頭は、XSとmmapと標準ヘッダファイルのインクルード。

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#include <sys/mman.h>
#include <stdlib.h>

次に定数を定義。

#define TPL_PLAIN   1
#define TPL_REPLACE 2
#define TPL_LOOP    3
#define TPL_IF      4
#define TPL_ELSE    5
#define TPL_QSA     6
#define TPL_LB      253
#define TPL_RB      254
#define TPL_END     255

ここは、テンプレートで記述されるMobaSiF独自のタグのタイプを示している。
MobaSiFでのテンプレート記述では、「$」で囲まれたタグを利用する。

次に、バイナリに変換されたテンプレートファイルを読み込む形式を、構造体で定義している。どうやらここで定義されている構造体の連続で、バイナリテンプレートファイルが書き込まれているらしい。

//書き込まれているタイプ、先読み用
typedef struct {
   uint type;
} TPL_DESC;

//$などがない単純なテキストタイプ?
typedef struct {
   uint type;
   uint ofsText;
} TPL_DESC_PLAIN;

//データの挿入箇所
typedef struct {
   uint type;
   uint ofsKey;
   uint opt;
} TPL_DESC_REPLACE;

//IF文
typedef struct {
   uint type;
   uint onTruePos;
   uint onFalsePos;
   uint ofsKey;
   uint ofsVal;
   uint condType;
} TPL_DESC_IF;

//ELSE文
typedef struct {
   uint type;
   uint onTruePos;
   uint onFalsePos;
} TPL_DESC_ELSE;

//LOOP文
typedef struct {
   uint type;
   uint ofsKey;
   uint onLoopEndPos;
} TPL_DESC_LOOP;

次から関数が記述されていく。
最初は、perlから呼び出される「template_insert()」を定義。
下の方ではXS記法で書かれている。

MODULE = MTemplate PACKAGE = MTemplate

SV*
insert(file,rHash,rHash2=NULL,rHash3=NULL)
   char* file
   SV*   rHash
   SV*   rHash2
   SV*   rHash3
   CODE:n   RETVAL = template_insert(file,rHash,rHash2,rHash3);
   OUTPUT:n   RETVAL