www.pudn.com > TurboPadSource.tar.gz > project.cpp


/** 
*    \file Project implementation 
* 
*    Implementation of Project functions. 
*/ 
 
#include "project.h" 
 
Project::Project() 
{ 
    numFiles = 0; 
} 
 
Project::Project(const wxString &fileName) : path(fileName) 
{ 
    name = wxFileNameFromPath(fileName); 
    Load(path); 
} 
 
Project::Project(const wxString &prjName, const wxString &prjPath) 
{ 
    numFiles = 0; 
    Create(prjName, prjPath); 
} 
 
void Project::AddFrame(wxWindow* frame) 
{ 
    frames.Add(frame); 
} 
 
void Project::AddFile(const wxString &fileName, bool upd = true) 
{ 
    wxString nfiles; 
 
    numFiles++;             // File is being added, so increment file count and 
    files.Add(fileName);    // add the fileName to the array 
 
    nfiles.Printf("%ld", numFiles); 
 
    prjFile.Open(path); 
 
    prjFile.AddLine(""); 
    prjFile.AddLine("[" + nfiles + "]"); 
    prjFile.AddLine(fileName); 
 
    prjFile.Write(); 
    prjFile.Close(); 
 
    if (upd)  // If the file count needs updating 
        UpdateFileCount(); 
} 
 
void Project::Close() 
{ 
    name = ""; 
    path = ""; 
    buildCommand = ""; 
    numFiles = 0; 
 
    frames.Empty(); 
    files.Empty(); 
} 
 
bool Project::Create(const wxString &prjName, const wxString &prjPath) 
{ 
    Close(); 
 
    name = prjName; 
    path = prjPath; 
 
    if (wxFileExists(path)) 
        wxRemoveFile(path); 
 
    prjFile.Create(path); 
 
    prjFile.AddLine("[Project]"); 
    prjFile.AddLine("Project Name: " + name); 
    prjFile.AddLine("Files: 0"); 
    prjFile.AddLine("Build:"); 
 
    prjFile.Write(); 
    prjFile.Close(); 
 
    return true; 
} 
 
bool Project::IsOpen() 
{ 
    return name != "" && path != ""; 
} 
 
PrjErr Project::Load(const wxString &fileName) 
{ 
    Close(); 
 
    path = fileName; 
    wxString line = ""; 
 
    if (name == "") name = wxFileNameFromPath(fileName); 
 
    if (!wxFileExists(path)) 
        return DoesntExist; 
 
    if (!prjFile.Open(path)) 
    { 
        prjFile.Close(); 
        return OpenFile; 
    } 
 
    for (unsigned int i = 1; i < prjFile.GetLineCount(); i++) 
    { 
        line = prjFile.GetLine(i); 
 
        if (line.Contains("Project Name:")) 
        { 
            line = line.AfterFirst(':'); 
            RemoveWhitespace(line); 
 
            name = line; 
        } 
 
        else if (line.Contains("Files:")) 
        { 
            line = line.AfterFirst(':'); 
            RemoveWhitespace(line); 
 
            if (line.IsNumber()) 
                line.ToLong(&numFiles); 
 
            else return Parse; 
        } 
 
        else if (line.Contains("Build:")) 
        { 
            line = line.AfterFirst(':'); 
            RemoveWhitespace(line); 
 
            buildCommand = line; 
        } 
 
        else if (wxFileExists(line)) 
            files.Add(line); 
    } 
 
    prjFile.Close(); 
    return None; 
} 
 
bool Project::Ready() 
{ 
    if (name == "" || path == "" 
            || !wxFileExists(path) 
            || frames.GetCount() != files.GetCount()) 
        return false; 
 
    return true; 
} 
 
inline void Project::RemoveWhitespace(wxString &str) 
{ 
    str.Trim(true); 
    str.Trim(false); 
} 
 
void Project::SetBuildCommand(wxString newBuildCommand) 
{ 
    buildCommand = newBuildCommand; 
} 
 
void Project::SetName(wxString newName) 
{ 
    name = newName; 
} 
 
void Project::SetPath(wxString newPath) 
{ 
    path = newPath; 
} 
 
void Project::UpdateFileCount() 
{ 
    wxString nfiles; 
    wxString line; 
 
    nfiles.Printf("%ld", numFiles); 
 
    prjFile.Open(path); 
 
    for (unsigned int i = 1; i < prjFile.GetLineCount(); i++) 
    { 
        line = prjFile.GetLine(i); 
 
        if (line.Contains("Files:")) 
        { 
            prjFile.InsertLine("Files: " + nfiles, i); 
            prjFile.RemoveLine(i + 1); 
            break; 
        } 
    } 
 
    prjFile.Write(); 
    prjFile.Close(); 
}