/***************************************************************************
**
**    $Header:   D:/EPSLDV1/SRC/LOG/DOPT.CPP   1.5   20 Jun 1996 09:29:30   ZJRD  $
**
**    $Log:   D:/EPSLDV1/SRC/LOG/DOPT.CPP  $
** 
**    Rev 1.5   20 Jun 1996 09:29:30   ZJRD
** EasyPack/SLD Version 1.20a
** 
**    Rev 1.3   10 Jun 1996 10:24:08   ZJRD
** EasyPack/SLD Version 1.20
** 
**    Rev 1.1   29 May 1996 09:35:46   ZJRD
** EasyPack/SLD Version 1.95
** 
****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
// dopt.cpp
//
// Michael Long
//                                                                           
// 5/16/1996
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"

#include "toolopt.h"    
#include "dc.h"
#include "dcoption.h"
#include "da.h"
#include "daoption.h"
#include "dl.h"
#include "dloption.h"
#include "dlib.h"
#include "dlibopti.h"
#include "de.h"
#include "deoption.h"
#include "dtab.h"
#include "projopt.h"
#include "dopt.h"   

/////////////////////////////////////////////////////////////////////////////
// operations
void DOption::SetOption(CWnd *pParent)
{
    CDTab dlg(&m_dcTool, &m_daTool, &m_dlTool,&m_dlibTool,&m_deTool, pParent);
    dlg.DoModal();
}

BOOL DOption::SaveOption(FILE * fp)
{                              
    ASSERT(fp);
    
    if(!m_dcTool.SaveOption(fp))
        return FALSE;
    if(!m_daTool.SaveOption(fp))
        return FALSE;
    if(!m_dlTool.SaveOption(fp))
        return FALSE;
    if(!m_dlibTool.SaveOption(fp))
        return FALSE;
    if(!m_deTool.SaveOption(fp))
        return FALSE;
   if((_stricmp(m_deTool.m_strPath," ")!=0)&&(_stricmp(m_deTool.m_strProg,"c:\\ ")!=0))
       m_fValid=TRUE;
    else
       m_fValid=FALSE;      
        
    return TRUE;
}
                
BOOL DOption::LoadOption(FILE * fp)
{                              
    ASSERT(fp);
    
    if(!m_dcTool.LoadOption(fp))
        return FALSE;
    if(!m_daTool.LoadOption(fp))
        return FALSE;
    if(!m_dlTool.LoadOption(fp))
        return FALSE;
    if(!m_dlibTool.LoadOption(fp))
        return FALSE;
    if(!m_deTool.LoadOption(fp))
        return FALSE;
     if((_stricmp(m_deTool.m_strPath," ")!=0)&&(_stricmp(m_deTool.m_strProg,"c:\\ ")!=0))
       m_fValid=TRUE;
    else
       m_fValid=FALSE;     
    return TRUE;
}                
    
// return 0:c file, 1:asm file, 2:other file(library)    
int DOption::GetFileType(LPCSTR lpcszFile) const
{              
    ASSERT(lpcszFile);

    char ext[_MAX_EXT];

    _splitpath(lpcszFile, NULL, NULL, NULL, ext);
    if(_stricmp(ext, ".c") == 0) {
        return 0;                
    } else if(_stricmp(ext, ".asm") == 0) {
        return 1;
    } else {
        return 2;
    }
}        

BOOL DOption::CreateLinkCmdFile(const CStringList &lst, LPCSTR lpcszProj)
{
    POSITION pos;
    CString str1, str2;
    FILE * fp;
    int i;
    
    fp = fopen(GetName(lpcszProj, ".cmd"), "wt");
    if(!fp)
        goto CMDFAIL;
             
    for(i = 0, pos = lst.GetHeadPosition(); pos != NULL; i ++) {
        str2 = lst.GetNext(pos);
        
        if(GetFileType(str2) != 2)
            str1 = GetName(str2, ".obj");
        else
            str1 = str2;
            
        if(fwrite(str1.GetBuffer(str1.GetLength()), str1.GetLength(), 1, fp) 
            != 1)
            goto CMDFAIL;
        else
            str1.ReleaseBuffer();
            
        if(i < lst.GetCount() - 1) {
            if(fputc(',', fp) == EOF)          
                goto CMDFAIL;
        } else {
            if(fputc(' ', fp) == EOF)          
                goto CMDFAIL;
        }
        if(fputc('\n', fp) == EOF)          
            goto CMDFAIL;
    }
    
     str1 = ",";
    if(fwrite(str1.GetBuffer(str1.GetLength()), str1.GetLength(), 1, fp) != 1)
        goto CMDFAIL;
    else 
        str1.ReleaseBuffer();
        
    if(fputc('\n', fp) == EOF)          
        goto CMDFAIL;
        
    str1 = m_dlibTool.GetOption();

    if(fwrite(str1.GetBuffer(str1.GetLength()), str1.GetLength(), 1, fp) != 1)
        goto CMDFAIL;
    else
        str1.ReleaseBuffer();

    if(fputc('\n', fp) == EOF)          
        goto CMDFAIL;

                      
    str1 = "to ";
    str1 += GetName(lpcszProj, ".omf");                   
    
    if(fwrite(str1.GetBuffer(str1.GetLength()), str1.GetLength(), 1, fp) != 1)
        goto CMDFAIL;
    else 
        str1.ReleaseBuffer();
        
   /* if(fputc('\n', fp) == EOF)          
        goto CMDFAIL; */
        
    str1 = m_dlTool.GetOption();

    if(fwrite(str1.GetBuffer(str1.GetLength()), str1.GetLength(), 1, fp) != 1)
        goto CMDFAIL;
    else
        str1.ReleaseBuffer();

    if(fputc('\n', fp) == EOF)          
        goto CMDFAIL;

    fclose(fp);
    return TRUE;
                     
CMDFAIL:
    AfxMessageBox("Failed to create link command file!");                    
    if(fp)
        fclose(fp);
    return FALSE;
}                                              

BOOL DOption::WriteBatchFile(FILE * fp, LPCSTR lpcszText)
{
    ASSERT(fp);
    
    if(fwrite(lpcszText, lstrlen(lpcszText), 1, fp) != 1) 
        goto BATFAIL;
    if(fputc('\n', fp) == EOF)
        goto BATFAIL;
        
    return TRUE;

BATFAIL:    
    fclose(fp); 
    AfxMessageBox("Failed to create batch file!");
    return FALSE;
}


void DOption::Build(const CStringList &lst, LPCSTR lpcszProj)
{
    POSITION pos;   
    CString str;
    CString strBuild;
    CString cstr;
    CString astr;
    CString lstr;
    if((m_dcTool.m_strPath)[(m_dcTool.m_strPath).GetLength()-1]!='\\')
    cstr = m_dcTool.m_strPath + "\\";
    if((m_daTool.m_strPath)[(m_daTool.m_strPath).GetLength()-1]!='\\')
    astr = m_daTool.m_strPath + "\\";
    if((m_dlTool.m_strPath)[(m_dlTool.m_strPath).GetLength()-1]!='\\')
    lstr = m_dlTool.m_strPath + "\\";    
    int nType;                   
    FILE * fp;
    
    fp = fopen(GetName(lpcszProj, ".bat"), "wt");
    if(!fp) {
        AfxMessageBox("Failed to create batch file!");
        return;
    }
    
    // compile or macro preprocess
    for(pos = lst.GetHeadPosition(); pos != NULL; ) {
        str = lst.GetNext(pos);
        nType = GetFileType(str);
        if(nType == 0) {    // c file
            strBuild =  cstr + "c51.exe "; 
            strBuild += str;
            strBuild += " ";
            strBuild += m_dcTool.GetOption();
            strBuild += " ";
            strBuild += " OJ( ";
            strBuild += GetName(str, ".obj");
            strBuild += ")";           
        } else if(nType == 1) { // asm file
            strBuild = astr + "A51.exe ";
            strBuild += str;
            strBuild += " ";
            strBuild += m_daTool.GetOption();            
            strBuild += " ";
            strBuild += " OJ( ";
            strBuild += GetName(str, ".obj");
            strBuild += ")";     
            
        } else {
            continue;
        }            
                
        if(!WriteBatchFile(fp, strBuild))
            return;
        
     }                                   
    
    // link
    if(!CreateLinkCmdFile(lst, lpcszProj))
        return;
    strBuild = lstr + "L51.exe ";
    strBuild += "@"; 
    strBuild += GetName(lpcszProj, ".cmd");
    if(!WriteBatchFile(fp, strBuild))
        return;

    fclose(fp);
    
    WinExec(GetName(lpcszProj, ".bat"), SW_SHOW);
}

void DOption::Editor(void)
  {
   CString str;             
   FILE *fp;                  
   fp = fopen("dedit.bat","wt");
    if(!fp) {
        AfxMessageBox("Failed to create batch file!");
        return;
    }
   
    char drive[_MAX_DRIVE], dir[_MAX_DIR];
    char fname[_MAX_FNAME],  ext[_MAX_EXT];
   
    _splitpath(m_deTool.m_strPath, drive, dir, fname, ext);
    if(_stricmp(ext,".exe")==0&&_stricmp(ext,".com")==0)
    {
     AfxMessageBox("Editor command error!");
     return;
    
     }
   str = m_deTool.GetOption();
   if(!WriteBatchFile(fp, str))
        return;
   
   fclose(fp);
   WinExec("dedit.bat", SW_SHOW);
} 
                               