
#include "stdafx.h"
#include "stdafx.h"
#include "alias.h" 
#include "command.h"
    
CCommandArray *ComArray = NULL; 
CCommandArray *HiddenComArray = NULL;
//CCommandArray *AliasArray;                                          

char *szAppPath = "d:\\ep196\\maketab\\";  
void ShowLine(char*);
   
int MakeTable(void)
{                  

    int nMaxCom = sizeof(ComTable)/sizeof(ComTable[0]);
    CCommand *pNewCom;     
    CCommandRoot *pNewPara;
    int nParaNum = 0;         
    ComArray = new CCommandArray();  
    
    // add basic command to file
    for (int i=0; i<nMaxCom; i++)
    {
        pNewCom = new CCommand(ComTable[i].pszCommandName,
            ComTable[i].nMinKeyword,ComTable[i].pszHints);
    
        for (int j=0; j<ParaNum[i]; j++)
        {
            pNewPara = new CCommandRoot(
                ParaTable[nParaNum].pszCommandName,
                ParaTable[nParaNum].nMinKeyword);
            if(pNewCom->arrArgv.Add(pNewPara)>=0)    
                nParaNum++;
        }
        ComArray->Add(pNewCom);            
    }
         
    // add hiden command to file
    nMaxCom = sizeof(HiddenComTable)/sizeof(HiddenComTable[0]);
    nParaNum = 0;         
    HiddenComArray = new CCommandArray();
    for (i=0; i<nMaxCom; i++)
    {
        pNewCom = new CCommand(HiddenComTable[i].pszCommandName,
            HiddenComTable[i].nMinKeyword,HiddenComTable[i].pszHints);
    
        for (int j=0; j<HiddenParaNum[i]; j++)
        {
            pNewPara = new CCommandRoot(
                HiddenParaTable[nParaNum].pszCommandName,
                HiddenParaTable[nParaNum].nMinKeyword);
            if(pNewCom->arrArgv.Add(pNewPara)>=0)    
                nParaNum++;
        }
        HiddenComArray->Add(pNewCom);            
    }
         
        
    CString pFileName;
    pFileName = CString(szAppPath) + "command.dat";
    
    CFile f;
    char buf[512];
    if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite ) ) {
        #ifdef _DEBUG
          afxDump << "Unable to open file" << "\n";
        #endif                          
    	delete ComArray;          
    	delete HiddenComArray;
        return 2;
    }
    // store basic and hidden command to file.
    CArchive ar( &f, CArchive::store, 512, buf );
    ComArray->Serialize(ar);            
    HiddenComArray->Serialize(ar);
    
    delete ComArray;          
    delete HiddenComArray;
    
    return 1;
}   


int test_read()
{
    CString pFileName;
    pFileName = CString(szAppPath) + "command.dat";

    
    CFile f;
    char buf[512];
    if( !f.Open( pFileName, CFile::modeRead ) ) {
        #ifdef _DEBUG
          afxDump << "Unable to open file" << "\n";
        #endif   
        return -1;
    }
    CArchive ar( &f, CArchive::load, 512, buf );
    ComArray = new CCommandArray();
    HiddenComArray = new CCommandArray();   
    
    ComArray->Serialize(ar);
    HiddenComArray->Serialize(ar);          
    CCommand *pNewCom;     
    CCommandRoot *pNewPara;
    for(int i=0; i<ComArray->GetSize();i++)
    {
        pNewCom = (CCommand*)(*ComArray)[i];
        for(int j=0; j<pNewCom->arrArgv.GetSize();j++)
            pNewPara = (CCommandRoot*) pNewCom->arrArgv[j];
    }    
    
    for(i=0; i<HiddenComArray->GetSize();i++)
    {
        pNewCom = (CCommand*)(*HiddenComArray)[i];
        for(int j=0; j<pNewCom->arrArgv.GetSize();j++)
            pNewPara = (CCommandRoot*) pNewCom->arrArgv[j];
    }                               

    delete ComArray;            
    delete HiddenComArray;
    return 0;
} 

void ShowLine(char* pszMsg)
{
}
/*---------------------end of file---------------------------*/
    