Code::Blocks  SVN r11506
filegroupsandmasks.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 8300 $
6  * $Id: filegroupsandmasks.cpp 8300 2012-08-31 11:35:40Z jenslody $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/filegroupsandmasks.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include <wx/intl.h>
14 
15  #include "globals.h" // GetArrayFromString
16  #include "configmanager.h"
17  #include "manager.h"
18  #include "logmanager.h"
19 #endif
20 
21 #include "filegroupsandmasks.h"
22 
24 {
25  //ctor
26  Load();
27 
28  if (m_Groups.GetCount() == 0)
29  SetDefault(false); // No need to clear any groups
30 }
31 
33 {
34  // copy ctor
35  CopyFrom(rhs);
36 }
37 
39 {
40  //dtor
41  Save();
42  Clear();
43 }
44 
46 {
47  Clear();
48  for (unsigned int i = 0; i < rhs.m_Groups.GetCount(); ++i)
49  {
50  FileGroups* fg = new FileGroups;
51  FileGroups* otherfg = rhs.m_Groups[i];
52  fg->groupName = otherfg->groupName;
53  fg->fileMasks = otherfg->fileMasks;
54 
55  m_Groups.Add(fg);
56  }
57 }
58 
60 {
61  if (do_clear)
62  Clear();
63 
64  // only add default groups if none were loaded...
65  unsigned int group;
66 
67  group = AddGroup(_("Sources"));
68  SetFileMasks(group, _T("*.c;*.cpp;*.cc;*.cxx") );
69 
70  group = AddGroup(_("D Sources"));
71  SetFileMasks(group, _T("*.d") );
72 
73  group = AddGroup(_("Fortran Sources"));
74  SetFileMasks(group, _T("*.f;*.f77;*.for;*.fpp;*.f90;*.f95;*.f03;*.f08") );
75 
76  group = AddGroup(_("Java Sources"));
77  SetFileMasks(group, _T("*.java") );
78 
79  group = AddGroup(_("Headers"));
80  SetFileMasks(group, _T("*.h;*.hpp;*.hh;*.hxx") );
81 
82  group = AddGroup(_("ASM Sources"));
83  SetFileMasks(group, _T("*.asm;*.s;*.ss;*.s62") );
84 
85  group = AddGroup(_("Resources"));
86  SetFileMasks(group, _T("*.res;*.xrc;*.rc;*.wxs") );
87 
88  group = AddGroup(_("Scripts"));
89  SetFileMasks(group, _T("*.script") );
90 }
91 
93 {
94  Clear();
95  ConfigManager* conf = Manager::Get()->GetConfigManager(_T("project_manager"));
96  wxArrayString list = conf->EnumerateSubPaths(_T("/file_groups"));
97  for (unsigned int i = 0; i < list.GetCount(); ++i)
98  {
99  // new way (reading groups)
100  wxString key = _T("/file_groups/") + list[i];
101  unsigned int group = AddGroup(conf->Read(key + _T("/name")));
102  SetFileMasks(group, conf->Read(key + _T("/mask")));
103  }
104 }
105 
107 {
108  ConfigManager* conf = Manager::Get()->GetConfigManager(_T("project_manager"));
109  conf->DeleteSubPath(_T("/file_groups"));
110  for (unsigned int i = 0; i < m_Groups.GetCount(); ++i)
111  {
112  FileGroups* fg = m_Groups[i];
113  wxString key;
114  key << _T("/file_groups/group") << wxString::Format(_T("%u"), i) << _T("/") << _T("name");
115  conf->Write(key, fg->groupName);
116 
117  key.Clear();
118  key << _T("/file_groups/group") << wxString::Format(_T("%u"), i) << _T("/") << _T("mask");
119  // Clean-up file masks that appear twice or more
120  conf->Write(key, GetStringFromArray( MakeUniqueArray(fg->fileMasks, false), _T(";") ));
121  }
122 }
123 
125 {
126  for (unsigned int i = 0; i < m_Groups.GetCount(); ++i)
127  {
128  FileGroups* fg = m_Groups[i];
129  if (fg)
130  delete fg;
131  }
132  m_Groups.Clear();
133 }
134 
135 unsigned int FilesGroupsAndMasks::AddGroup(const wxString& name)
136 {
137  FileGroups* fg = new FileGroups;
138  fg->groupName = name;
139  m_Groups.Add(fg);
140  return m_Groups.GetCount() - 1;
141 }
142 
143 void FilesGroupsAndMasks::RenameGroup(unsigned int group, const wxString& newName)
144 {
145  if (group >= m_Groups.GetCount())
146  return;
147 
148  FileGroups* fg = m_Groups[group];
149  fg->groupName = newName;
150 }
151 
152 void FilesGroupsAndMasks::DeleteGroup(unsigned int group)
153 {
154  if (group >= m_Groups.GetCount())
155  return;
156 
157  FileGroups* fg = m_Groups[group];
158  delete fg;
159  m_Groups.Remove(fg);
160 }
161 
162 void FilesGroupsAndMasks::SetFileMasks(unsigned int group, const wxString& masks)
163 {
164  if (group >= m_Groups.GetCount())
165  return;
166 
167  FileGroups* fg = m_Groups[group];
168  // Clean-up file masks that appear twice or more
169  fg->fileMasks = MakeUniqueArray( GetArrayFromString(masks, _T(";")), false );
170 }
171 
173 {
174  return m_Groups.GetCount();
175 }
176 
178 {
179  if (group >= m_Groups.GetCount())
180  return wxEmptyString;
181  const FileGroups* fg = m_Groups[group];
182  return fg->groupName;
183 }
184 
186 {
187  if (group >= m_Groups.GetCount())
188  return wxEmptyString;
189  const FileGroups* fg = m_Groups[group];
190 
191  // Clean-up file masks that appear twice or more
192  return GetStringFromArray( MakeUniqueArray(fg->fileMasks, false) );
193 }
194 
195 bool FilesGroupsAndMasks::MatchesMask(const wxString& ext, unsigned int group) const
196 {
197  if (ext.IsEmpty() || group >= m_Groups.GetCount())
198  return false;
199  const FileGroups* fg = m_Groups[group];
200  for (unsigned int i = 0; i < fg->fileMasks.GetCount(); ++i)
201  {
202  if (ext.Lower().Matches(fg->fileMasks[i].Lower()))
203  return true;
204  }
205  return false;
206 }
DLLIMPORT wxArrayString GetArrayFromString(const wxString &text, const wxString &separator=DEFAULT_ARRAY_SEP, bool trimSpaces=true)
Definition: globals.cpp:134
bool Matches(const wxString &mask) const
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
wxString groupName
wxString Lower() const
unsigned int GetGroupsCount() const
Return total number of groups.
#define _T(string)
FileGroupsArray m_Groups
Internal storage for file groups ans masks.
DLLIMPORT wxString GetStringFromArray(const wxArrayString &array, const wxString &separator=DEFAULT_ARRAY_SEP, bool SeparatorAtEnd=true)
Definition: globals.cpp:122
wxString GetFileMasks(unsigned int group) const
Return a specific group file mask.
void Write(const wxString &name, const wxString &value, bool ignoreEmpty=false)
void CopyFrom(const FilesGroupsAndMasks &rhs)
copy ctor helper
wxArrayString EnumerateSubPaths(const wxString &path)
bool MatchesMask(const wxString &ext, unsigned int group) const
Return whether a file extension matches a file mask (group)
void Clear()
Clear any groups/masks.
wxString Read(const wxString &key, const wxString &defaultVal=wxEmptyString)
void SetFileMasks(unsigned int group, const wxString &masks)
Set file mask for a group (e.g.
wxString wxEmptyString
DLLIMPORT wxArrayString MakeUniqueArray(const wxArrayString &array, bool caseSens)
Definition: globals.cpp:198
const wxString & _(const wxString &string)
void Load()
Load groups/masks from config.
void RenameGroup(unsigned int group, const wxString &newName)
Rename a group.
void DeleteSubPath(const wxString &strPath)
bool IsEmpty() const
wxArrayString fileMasks
void Clear()
void SetDefault(bool do_clear=true)
Set the default file groups and masks.
void Save()
Save groups/masks to config.
size_t GetCount() const
wxString GetGroupName(unsigned int group) const
Return a specific group name.
static wxString Format(const wxString &format,...)
void DeleteGroup(unsigned int group)
Delete a group.
unsigned int AddGroup(const wxString &name)
Add a file group.