-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwxcHelper.cpp
151 lines (133 loc) · 4.66 KB
/
wxcHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "wxcHelper.h"
#include <wx/msgdlg.h>
#include <wx/ffile.h>
#include <cbproject.h>
#include <manager.h>
#include <projectmanager.h>
#include <globals.h>
#include <wx/filefn.h>
#include <wx/zipstrm.h>
#include <wx/wfstream.h>
bool wxcHelper::ExtractFileFromZip(const wxString& zipPath, const wxString& filename, const wxString& targetDir, wxString &targetFileName)
{
wxZipEntry * entry(NULL);
wxFFileInputStream in(zipPath);
wxZipInputStream zip(in);
// Make sure the target directory exists...
wxFileName::Mkdir(targetDir, 0777, wxPATH_MKDIR_FULL);
entry = zip.GetNextEntry();
while ( entry ) {
wxString name = entry->GetName();
if (name == filename) {
targetFileName = wxFileName(targetDir, filename).GetFullPath();
wxFFileOutputStream out( targetFileName );
zip.Read(out);
out.Close();
delete entry;
return true;
}
wxDELETE(entry);
entry = zip.GetNextEntry();
}
return false;
}
bool wxcHelper::CreateEmptyFile( const wxFileName& fn )
{
if ( fn.FileExists() ) {
if ( ::cbMessageBox(_("A file with this name already exists, would you like to replace it?"), wxT("wxCrafter"), wxYES_NO|wxCENTER|wxICON_QUESTION) != wxID_YES ) {
return false;
}
// Remove the current file
if ( !::wxRemoveFile( fn.GetFullPath() ) ) {
return false;
}
}
// Create an empty file
wxFFile fp(fn.GetFullPath(), wxT("w+b"));
if( !fp.IsOpened() ) {
wxString errmsg;
errmsg << _("Failed to create file '") << fn.GetFullPath() << wxT("'");
::wxMessageBox(errmsg, wxT("wxCrafter"), wxICON_ERROR|wxCENTER|wxOK);
return false;
}
return true;
}
void wxcHelper::AddFileToProject(cbProject* proj, const wxFileName& file, bool compile)
{
std::vector<wxFileName> files;
files.push_back( file );
AddFilesToProject(proj, files, compile);
}
void wxcHelper::AddFilesToProject(cbProject* proj, const std::vector<wxFileName>& files, bool compile)
{
wxFileName folderPath(proj->GetFilename());
proj->BeginAddFiles();
for(size_t i=0; i<files.size(); ++i) {
wxFileName fn = files.at(i);
fn.MakeRelativeTo( folderPath.GetPath() );
ProjectFile* pf(NULL);
for(int targetIndex=0; targetIndex<proj->GetBuildTargetsCount(); ++targetIndex) {
pf = proj->AddFile( targetIndex, fn.GetFullPath(), compile, compile );
}
if ( pf ) {
// This is the only sane way I found to add the file all build targets:
// clear the array and then iterate them one by one and add them
pf->buildTargets.Clear();
for(int targetIndex=0; targetIndex<proj->GetBuildTargetsCount(); ++targetIndex) {
pf->buildTargets.Add( proj->GetBuildTarget(targetIndex)->GetTitle() );
}
}
}
proj->EndAddFiles();
// Save and rebuild the project tree
Manager::Get()->GetProjectManager()->SaveAllProjects();
Manager::Get()->GetProjectManager()->GetUI().RebuildTree();
}
cbProject* wxcHelper::GetProject(const wxString& name)
{
cbProject *selectedProject ( NULL );
ProjectsArray* projects = Manager::Get()->GetProjectManager()->GetProjects();
for ( size_t i=0; i<projects->GetCount(); ++i ) {
if ( projects->Item(i)->GetTitle() == name ) {
selectedProject = projects->Item(i);
break;
}
}
return selectedProject;
}
void wxcHelper::GetAllFiles(FilesList& files, const wxString& filterExt)
{
wxString filterExtLowerCase = filterExt;
filterExtLowerCase.MakeLower();
ProjectsArray* projects = Manager::Get()->GetProjectManager()->GetProjects();
for ( size_t i=0; i<projects->GetCount(); ++i ) {
cbProject* pProj = projects->Item(i);
const FilesList& fileList = pProj->GetFilesList();
FilesList::const_iterator iter = fileList.begin();
for( ; iter != fileList.end(); ++iter ) {
if( filterExtLowerCase.IsEmpty() || filterExtLowerCase == (*iter)->file.GetExt().MakeLower() ) {
files.insert( (*iter) );
}
}
}
}
bool wxcHelper::ReadFileContent(const wxString& filepath, wxString& content)
{
wxFFile fp(filepath, wxT("r+b"));
if ( fp.IsOpened() ) {
fp.ReadAll(&content, wxConvUTF8);
fp.Close();
return true;
}
return false;
}
bool wxcHelper::WriteFileContent(const wxString& filepath, const wxString& content)
{
wxFFile fp(filepath, wxT("w+b"));
if ( fp.IsOpened() ) {
fp.Write( content );
fp.Close();
return true;
}
return false;
}