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
10
#include <wx/string.h>
13
#include "backgroundthread.h"
21
#include <wx/filename.h>
22
#include "configmanager.h"
25
class LoaderBase : public AbstractJob
50
LoaderBase() : wait(true), data(0), len(0) {};
53
wxString FileName() const { return fileName; };
61
class FileLoader : public LoaderBase
64
FileLoader(const wxString& name) { fileName = name; };
70
* Delete a file after a grace period. This is useful since we do not know when the filesystem will sync its data to disk.
71
* In fact, the filesystem might be on a physically separate machine.
72
* Thus, whenever replacing an existing file with a new one, you would want to be sure the changes you made are really on disk
73
* before deleting any backup files (in case the user pulls the plug).
74
* The job does nothing but sleep (giving the OS an opportunity to flush caches), the actual work is done in the destructor.
75
* This enables you to feed the job to a BackgroundThread that owns its jobs (and gradually deletes them one by one).
76
* As the actual work is done in the destructor, no stale files will be left at application exit.
78
class DelayedDelete : public AbstractJob
82
DelayedDelete(const wxString& name) : target(name){};
88
if(Manager::IsAppShuttingDown()) // make sure we don't hang up the application for seconds
95
wxLogNull nullLog; // leave this in place, DelayedDelete could in theory run after CodeBlocksApp::OnRun returns
96
if(wxFile::Exists(target))
105
std::auto_ptr<char> ptr;
109
AutoBuffer() : ptr(0), len(0){};
110
AutoBuffer(size_t initial) : ptr(new char[initial]), len(initial){};
112
void Alloc(size_t size)
114
std::auto_ptr<char> tmp(new char[len + size]);
116
memcpy(tmp.get(), ptr.get(), len);
120
void Append(const char* add_buf, size_t add_len)
123
memcpy(ptr.get() + len, add_buf, add_len);
127
size_t Length() const {return len;};
128
char *Data() const {return ptr.get();};
132
class URLLoader : public LoaderBase
136
URLLoader(const wxString& name) { fileName = name; };
141
class NullLoader : public LoaderBase
144
NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
149
class FileManager : public Mgr<FileManager>
151
BackgroundThread fileLoaderThread;
152
BackgroundThread uncLoaderThread;
153
BackgroundThread urlLoaderThread;
154
BackgroundThread delayedDeleteThread;
159
warn_unused LoaderBase* Load(const wxString& file, bool reuseEditors = false);
161
bool Save(const wxString& file, const wxString& data, wxFontEncoding encoding, bool bom);
162
bool Save(const wxString& file, const char* data, size_t len);
164
bool ReplaceFile(const wxString& old_file, const wxString& new_file);