Code::Blocks  SVN r11506
filemanager.h
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 
6 #ifndef FILEMANAGER_H
7 #define FILEMANAGER_H
8 
9 #include <wx/thread.h>
10 #include <wx/string.h>
11 #include <wx/log.h>
12 
13 #include "backgroundthread.h"
14 
15 #undef new
16 #include <deque>
17 #include <memory>
18 
19 #ifndef CB_PRECOMP
20  #include <wx/file.h>
21  #include <wx/filename.h>
22  #include "configmanager.h"
23 #endif
24 
25 
26 class TiXmlDocument;
27 namespace TinyXML{ bool SaveDocument(const wxString&, TiXmlDocument*); }
28 
29 
30 // ***** class: LoaderBase *****
32 {
34  bool wait;
35 
36  void WaitReady()
37  {
38  if (wait)
39  {
40  wait = false;
41  sem.Wait();
42  }
43  };
44 
45 protected:
47  char *data;
48  size_t len;
49 
50  void Ready()
51  {
52  sem.Post();
53  };
54 
55 public:
56  LoaderBase() : wait(true), data(nullptr), len(0) {};
57  ~LoaderBase() override;
58  void operator()() override {};
59  wxString FileName() const { return fileName; };
60 
61  bool Sync();
62  char* GetData();
63  size_t GetLength();
64 };
65 
66 // ***** class: FileLoader *****
68 {
69 public:
70  FileLoader(const wxString& name) { fileName = name; };
71  void operator()() override;
72 };
73 
74 // ***** class: URLLoader *****
76 {
77 public:
78  URLLoader(const wxString& name) { fileName = name; };
79  void operator()() override;
80 private:
81  std::vector<char> mBuffer;
82 };
83 
84 // ***** class: NullLoader *****
86 {
87 public:
88  NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
89  void operator()() override{};
90 };
91 
92 // ***** class: EditorReuser *****
94 {
95 public:
100  EditorReuser(const wxString& name, const wxString& s)
101  {
102  fileName = name;
103  len = strlen(s.mb_str(wxConvUTF8));
104  data = new char[len + 1];
105  strcpy(data, (const char*)s.mb_str(wxConvUTF8));
106  Ready();
107  }
108  void operator()() override{};
109 };
110 
111 // ***** class: FileManager *****
112 class DLLIMPORT FileManager : public Mgr<FileManager>
113 {
117 public:
118  FileManager();
119  ~FileManager() override;
120 
129  cb_must_consume_result LoaderBase* Load(const wxString& file, bool reuseEditors = false);
130 
131  bool Save(const wxString& file, const wxString& data, wxFontEncoding encoding, bool bom);
132 
133 private:
134  friend bool TinyXML::SaveDocument(const wxString&, TiXmlDocument*);
135  bool SaveUTF8(const wxString& file, const char* data, size_t len);
136 
137  bool WriteWxStringToFile(wxFile& f, const wxString& data, wxFontEncoding encoding, bool bom);
138 };
139 
140 #endif
void WaitReady()
Definition: filemanager.h:36
URLLoader(const wxString &name)
Definition: filemanager.h:78
std::vector< char > mBuffer
Definition: filemanager.h:81
void operator()() override
Definition: filemanager.h:108
BackgroundThread urlLoaderThread
Definition: filemanager.h:116
void operator()() override
Definition: filemanager.h:89
const wxCharBuffer mb_str(const wxMBConv &conv=wxConvLibc) const
wxFontEncoding
char * data
Definition: filemanager.h:47
size_t len
Definition: filemanager.h:48
wxSemaError Post()
#define DLLIMPORT
Definition: settings.h:16
wxString fileName
Definition: filemanager.h:43
null_pointer_t nullptr
Definition: nullptr.cpp:16
wxString FileName() const
Definition: filemanager.h:59
NullLoader(const wxString &name, char *buffer, size_t size)
Definition: filemanager.h:88
BackgroundThread uncLoaderThread
Definition: filemanager.h:115
Definition: manager.h:183
void Ready()
Definition: filemanager.h:50
wxSemaError Wait()
bool SaveDocument(const wxString &, TiXmlDocument *)
Definition: tinywxuni.cpp:45
void operator()() override
Definition: filemanager.h:58
wxSemaphore sem
Definition: filemanager.h:33
BackgroundThread fileLoaderThread
Definition: filemanager.h:114
EditorReuser(const wxString &name, const wxString &s)
Gets a UTF8 representation of the editor&#39;s content.
Definition: filemanager.h:100
FileLoader(const wxString &name)
Definition: filemanager.h:70