2015年10月19日月曜日

test

#include "scriptparts.h"
#include "voice.h"

wstring g_LastString;
map g_Val;

int ReadBox(FILE *fp,const wchar_t *boxpath,int nst,int nIfFlg)
{
    long RtnLine=0;
    map l_Val;/* ローカル変数 */
    int nRet = 0;
    int nPos = 0;
//    wchar_t wszLine[LINE_SIZE] = {0};
    wchar_t wcsc = 0;
    wstring strLine;
    wstring strLast=L"";
    wstring func;
    map localVal;
    nst++;

//    printf("%d\n",nst);

//    while(fgetws(wszLine,sizeof(wszLine)-1,fp) != NULL)
    while((wcsc = fgetwc(fp)) != WEOF)
    {
        int nPos = 0;
        //strLine = wszLine;
        if(wcsc == L'\\')
        {
            fgetwc(fp);
            continue;
        }
        else
        {
            strLine = strLine + wcsc;
            if(wcsc != L'\n')
            {
                continue;
            }
        }
        //wcout << strLine << endl;
        nPos = strLine.find(L"!",0);
        if(nPos == 0)
        {
            /* 無変換で出力する */
            wstring substr = strLine.substr(nPos+1);
            wcout << substr;
            strLine = L"";
            continue;
        }

        nPos = strLine.find(L"#",0);
        if(nPos != wstring::npos)
        {
            /* コメントアウト */
            strLine.replace(nPos,strLine.size(),L"");
        }

        //strLine = wszLine;
        //改行削除
        nPos = strLine.find(L"\n",0);
        if(nPos != wstring::npos)
        {
            strLine.replace(nPos,1,L"");
        }

        if(strLine.size() < 1)
        {
            strLine = L"";
            continue;
        }
        //タブ削除
        while(1)
        {    int nPos = 0;
            nPos = strLine.find(L"\t",0);
            if(nPos == wstring::npos)
            {
                break;
            }
            strLine.replace(nPos,1,L"");
        }

        //スペース削除
        while(1)
        {    int nPos = 0;
            nPos = strLine.find(L" ",0);
            if(nPos == wstring::npos)
            {
                break;
            }
            strLine.replace(nPos,1,L"");
        }
        /* 変数の展開 */
        strLine = ExpandValue(l_Val,strLine);

        /* ローカル変数の宣言 */
        nPos = strLine.find(L"=",0);
        if(nPos != wstring::npos)
        {
        //    wprintf(L"きた\n");
            wstring c = L"=";
            c = strLine.substr(nPos+1,1);
    //        wprintf(L"%ls\n",c.c_str());
            if(c != L"=")
            {
                /* == ではないことをチェック */
                /* 変数の宣言 */
                if(nst == 0)
                {
                    SetVal(strLine);
                }
                else
                {
                    SetVal(strLine);
                    //SetLocalVal(l_Val,strLine);
                }
                strLine = L"";
                continue;
            }
        }

        /* 変数の展開 */
        strLine = ExpandValue(l_Val,strLine);

    //    wcout << strLine << endl;
        nPos = strLine.find(BOX_DEF_START,0);
        if(nPos != wstring::npos)
        {
            /* BOX定義開始だった */
            BoxSkip(fp);
            strLine = L"";
            continue;
        }

        nPos = strLine.find(BOX_DEF_END,0);
        if(nPos != wstring::npos)
        {
            /* BOX定義終了だった */
            break;
        }

        /* if_endが来た */
        nPos = strLine.find(IF_END,0);
        if(nPos != wstring::npos)
        {
            break;
        }

        /* else */
        nPos = strLine.find(ELSE_START,0);
        if(nPos != wstring::npos)
        {
            if(nIfFlg == 0)
            {
                //wprintf(L"elseでRead\n");
                strLine = L"";
                continue;
            //    ReadBox(fp,boxpath,nst,nIfFlg);
            }
            else
            {
            //    wprintf(L"else不一致\n");
                SearchElseIf(fp);
            }
            strLine = L"";
            continue;
        }

        /* elseif */
        nPos = strLine.find(ELSEIF_START,0);
        if(nPos != wstring::npos)
        {
            //wprintf(L"きてはいるんだよな\n");
            if(isMatchelseIf(strLine))
            {
                /* 一致した */
                nIfFlg = 1;
                strLine = L"";
                continue;
            //    ReadBox(fp,boxpath,nst,nIfFlg);
            }
            else
            {
                SearchElseIf(fp);
            }
            strLine = L"";
            continue;
        }

        /* ifの開始 */
        nPos = strLine.find(IF_START,0);
        if(nPos != wstring::npos)
        {
            //wprintf(L"ifきました\n");
            if(isMatchIf(strLine))
            {
                /* 一致した */
            //    wcout << strLine << endl;
                nIfFlg = 1;
                ReadBox(fp,boxpath,nst,nIfFlg);
                strLine = L"";
                continue;
            }
            else
            {
                /* 一致しなかった */
                //wprintf(L"一致してません\n");
                SearchElseIf(fp);
                //nIfFlg = 0;
                ReadBox(fp,boxpath,nst,0);
                strLine = L"";
                continue;
            }
        }

        nPos = strLine.find(IFLAST_START,0);
        if(nPos != wstring::npos)
        {
            /* iflastの開始だった */
            if(isMatchIfLast(strLine,strLast))
            {
                //一致した
                //wprintf(L"一致しました\n");
                ReadBox(fp,boxpath,nst,nIfFlg);
                strLine = L"";
                continue;
            }
            else
            {
                //一致しなかった
            //    wprintf(L"一致しなかった\n");
                iflastSkip(fp);
                strLine = L"";
                continue;
            }
        }
        nPos = strLine.find(IFLAST_END,0);
        if(nPos != wstring::npos)
        {
            /* iflast終了だった */
            break;
        }

        nPos = strLine.find(GOTOFIN,0);
        if(nPos != wstring::npos)
        {
            /* FINだった */
            GotoFIN(fp);
            strLine = L"";
            continue;
        }



        nPos = strLine.find(L"|",0);
        if(nPos != wstring::npos)
        {
            strLine = Which(strLine);
            //選んだ結果がboxだった
        }

        nPos = strLine.find(CALL_BOX,0);
        if(nPos != wstring::npos)
        {
            /* BOXをコールされた */
            nPos = nPos + wcslen(CALL_BOX);
            wstring title=strLine.substr(nPos);
            RtnLine = SearchBoxDef(fp,title,boxpath,nst,nIfFlg);
            ReadBox(fp,boxpath,nst,nIfFlg);
            fseek(fp,RtnLine,SEEK_SET);
            //fgetws(wszLine,sizeof(wszLine)-1,fp);
            strLine = L"";
            continue;
        }

        wcout << strLine << endl;
        //wprintf(L"%ls\n",RetString.c_str());
        g_LastString = strLine;
        strLine = L"";
    }
}


/* main関数 */
int main(int argc,char *argv[])
{
    int nRet = 0;
    FILE *fp = NULL;
    int nLine = 0;
    wchar_t wszLine[LINE_SIZE] = {0};
    wstring strLine;
    wstring func=L"";
    long *RtnLine = 0;
    wchar_t szBoxPath[MAX_PATH]={0};
    struct timeval tv;

    gettimeofday(&tv,NULL);
    srand(tv.tv_usec);
   
    setlocale(LC_ALL,"");

    if(argc <= 2)
    {
        wprintf(L"Usage filename boxpath\n");
        return -1;
    }

    if(argc == 2)
    {
         wcscpy(szBoxPath,L"./");
    }
    else
    {
        mbstowcs(szBoxPath,argv[2],sizeof(szBoxPath)-1);
    }

    fp = fopen(argv[1],"r");
    if(fp == NULL)
    {
        wprintf(L"%s not exist.\n",argv[1]);
        return -1;
    }

    ReadBox(fp,szBoxPath,-1,0);

    fclose(fp);

    return 0;
}

0 件のコメント:

コメントを投稿