~registry/codeblocks/trunk

« back to all changes in this revision

Viewing changes to src/include/filemanager.h

  • Committer: mandrav
  • Date: 2007-02-12 14:55:28 UTC
  • Revision ID: svn-v4:98b59c6a-2706-0410-b7d6-d2fa1a1880c9:trunk:3594
* First part of directories layout re-organization: moved all sdk header files to a new dir named "include".

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef FILEMANAGER_H
 
2
#define FILEMANAGER_H
 
3
 
 
4
#include <wx/thread.h>
 
5
#include <wx/string.h>
 
6
#include <wx/log.h>
 
7
 
 
8
#include "backgroundthread.h"
 
9
 
 
10
#undef new
 
11
#include <deque>
 
12
#include <memory>
 
13
 
 
14
#ifndef CB_PRECOMP
 
15
    #include <wx/file.h>
 
16
    #include <wx/filename.h>
 
17
    #include "configmanager.h"
 
18
#endif
 
19
 
 
20
class LoaderBase : public AbstractJob
 
21
{
 
22
    wxSemaphore sem;
 
23
    bool wait;
 
24
 
 
25
    void WaitReady()
 
26
    {
 
27
        if(wait)
 
28
        {
 
29
            wait = false;
 
30
            sem.Wait();
 
31
        }
 
32
    };
 
33
 
 
34
protected:
 
35
    wxString fileName;
 
36
    char *data;
 
37
    size_t len;
 
38
 
 
39
    void Ready()
 
40
    {
 
41
        sem.Post();
 
42
    };
 
43
 
 
44
public:
 
45
    LoaderBase() : wait(true), data(0), len(0) {};
 
46
    ~LoaderBase();
 
47
    void operator()() {};
 
48
    wxString FileName() const { return fileName; };
 
49
 
 
50
    bool Sync();
 
51
    char* GetData();
 
52
    size_t GetLength();
 
53
};
 
54
 
 
55
 
 
56
class FileLoader : public LoaderBase
 
57
{
 
58
public:
 
59
    FileLoader(const wxString& name) { fileName = name; };
 
60
    void operator()();
 
61
};
 
62
 
 
63
 
 
64
/*
 
65
* Delete a file after a grace period. This is useful since we do not know when the filesystem will sync its data to disk.
 
66
* In fact, the filesystem might be on a physically separate machine.
 
67
* Thus, whenever replacing an existing file with a new one, you would want to be sure the changes you made are really on disk
 
68
* before deleting any backup files (in case the user pulls the plug).
 
69
* The job does nothing but sleep (giving the OS an opportunity to flush caches), the actual work is done in the destructor.
 
70
* This enables you to feed the job to a BackgroundThread that owns its jobs (and gradually deletes them one by one).
 
71
* As the actual work is done in the destructor, no stale files will be left at application exit.
 
72
*/
 
73
class DelayedDelete : public AbstractJob
 
74
{
 
75
wxString target;
 
76
public:
 
77
    DelayedDelete(const wxString& name) : target(name){};
 
78
    void operator()()
 
79
    {
 
80
        unsigned int i = 20;
 
81
        while(--i)
 
82
        {
 
83
            if(Manager::IsAppShuttingDown()) // make sure we don't hang up the application for seconds
 
84
                break;
 
85
            wxMilliSleep(75);
 
86
        }
 
87
    };
 
88
    ~DelayedDelete()
 
89
    {
 
90
        wxLogNull nullLog;
 
91
        if(wxFile::Exists(target))
 
92
            wxRemove(target);
 
93
    };
 
94
};
 
95
 
 
96
 
 
97
 
 
98
class AutoBuffer
 
99
{
 
100
std::auto_ptr<char> ptr;
 
101
size_t len;
 
102
 
 
103
public:
 
104
    AutoBuffer() : ptr(0), len(0){};
 
105
    AutoBuffer(size_t initial) : ptr(new char[initial]), len(initial){};
 
106
 
 
107
    void Alloc(size_t size)
 
108
    {
 
109
        std::auto_ptr<char> tmp(new char[len + size]);
 
110
        if(ptr.get())
 
111
            memcpy(tmp.get(), ptr.get(), len);
 
112
        ptr = tmp;
 
113
    };
 
114
 
 
115
    void Append(char *add_buf, size_t add_len)
 
116
    {
 
117
        Alloc(add_len);
 
118
        memcpy(ptr.get() + len, add_buf, add_len);
 
119
        len += add_len;
 
120
    };
 
121
 
 
122
    size_t Length() const {return len;};
 
123
    char *Data() const {return ptr.get();};
 
124
};
 
125
 
 
126
 
 
127
class URLLoader : public LoaderBase
 
128
{
 
129
AutoBuffer buffer;
 
130
public:
 
131
    URLLoader(const wxString& name) { fileName = name; };
 
132
    void operator()();
 
133
};
 
134
 
 
135
 
 
136
class NullLoader : public LoaderBase
 
137
{
 
138
public:
 
139
    NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
 
140
    void operator()(){};
 
141
};
 
142
 
 
143
 
 
144
class FileManager : public Mgr<FileManager>
 
145
{
 
146
    BackgroundThread fileLoaderThread;
 
147
    BackgroundThread uncLoaderThread;
 
148
    BackgroundThread urlLoaderThread;
 
149
    BackgroundThread delayedDeleteThread;
 
150
public:
 
151
    FileManager() : fileLoaderThread(false), uncLoaderThread(false), urlLoaderThread(false){};
 
152
 
 
153
    LoaderBase* Load(const wxString& file, bool reuseEditors = false);
 
154
 
 
155
    bool Save(const wxString& file, const wxString& data, wxFontEncoding encoding, bool bom);
 
156
    bool Save(const wxString& file, const char* data, size_t len);
 
157
private:
 
158
    bool ReplaceFile(const wxString& old_file, const wxString& new_file);
 
159
};
 
160
 
 
161
 
 
162
#endif