~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/include/filemanager.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
class LoaderBase : public AbstractJob
 
26
{
 
27
    wxSemaphore sem;
 
28
    bool wait;
 
29
 
 
30
    void WaitReady()
 
31
    {
 
32
        if(wait)
 
33
        {
 
34
            wait = false;
 
35
            sem.Wait();
 
36
        }
 
37
    };
 
38
 
 
39
protected:
 
40
    wxString fileName;
 
41
    char *data;
 
42
    size_t len;
 
43
 
 
44
    void Ready()
 
45
    {
 
46
        sem.Post();
 
47
    };
 
48
 
 
49
public:
 
50
    LoaderBase() : wait(true), data(0), len(0) {};
 
51
    ~LoaderBase();
 
52
    void operator()() {};
 
53
    wxString FileName() const { return fileName; };
 
54
 
 
55
    bool Sync();
 
56
    char* GetData();
 
57
    size_t GetLength();
 
58
};
 
59
 
 
60
 
 
61
class FileLoader : public LoaderBase
 
62
{
 
63
public:
 
64
    FileLoader(const wxString& name) { fileName = name; };
 
65
    void operator()();
 
66
};
 
67
 
 
68
 
 
69
/*
 
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.
 
77
*/
 
78
class DelayedDelete : public AbstractJob
 
79
{
 
80
wxString target;
 
81
public:
 
82
    DelayedDelete(const wxString& name) : target(name){};
 
83
    void operator()()
 
84
    {
 
85
        unsigned int i = 20;
 
86
        while(--i)
 
87
        {
 
88
            if(Manager::IsAppShuttingDown()) // make sure we don't hang up the application for seconds
 
89
                break;
 
90
            wxMilliSleep(75);
 
91
        }
 
92
    };
 
93
    ~DelayedDelete()
 
94
    {
 
95
        wxLogNull nullLog; // leave this in place, DelayedDelete could in theory run after CodeBlocksApp::OnRun returns
 
96
        if(wxFile::Exists(target))
 
97
            wxRemove(target);
 
98
    };
 
99
};
 
100
 
 
101
 
 
102
 
 
103
class AutoBuffer
 
104
{
 
105
std::auto_ptr<char> ptr;
 
106
size_t len;
 
107
 
 
108
public:
 
109
    AutoBuffer() : ptr(0), len(0){};
 
110
    AutoBuffer(size_t initial) : ptr(new char[initial]), len(initial){};
 
111
 
 
112
    void Alloc(size_t size)
 
113
    {
 
114
        std::auto_ptr<char> tmp(new char[len + size]);
 
115
        if(ptr.get())
 
116
            memcpy(tmp.get(), ptr.get(), len);
 
117
        ptr = tmp;
 
118
    };
 
119
 
 
120
    void Append(const char* add_buf, size_t add_len)
 
121
    {
 
122
        Alloc(add_len);
 
123
        memcpy(ptr.get() + len, add_buf, add_len);
 
124
        len += add_len;
 
125
    };
 
126
 
 
127
    size_t Length() const {return len;};
 
128
    char *Data() const {return ptr.get();};
 
129
};
 
130
 
 
131
 
 
132
class URLLoader : public LoaderBase
 
133
{
 
134
AutoBuffer buffer;
 
135
public:
 
136
    URLLoader(const wxString& name) { fileName = name; };
 
137
    void operator()();
 
138
};
 
139
 
 
140
 
 
141
class NullLoader : public LoaderBase
 
142
{
 
143
public:
 
144
    NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
 
145
    void operator()(){};
 
146
};
 
147
 
 
148
 
 
149
class FileManager : public Mgr<FileManager>
 
150
{
 
151
    BackgroundThread fileLoaderThread;
 
152
    BackgroundThread uncLoaderThread;
 
153
    BackgroundThread urlLoaderThread;
 
154
    BackgroundThread delayedDeleteThread;
 
155
public:
 
156
    FileManager();
 
157
    ~FileManager();
 
158
 
 
159
    warn_unused LoaderBase* Load(const wxString& file, bool reuseEditors = false);
 
160
 
 
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);
 
163
private:
 
164
    bool ReplaceFile(const wxString& old_file, const wxString& new_file);
 
165
};
 
166
 
 
167
 
 
168
#endif