File: filesys.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (502 lines) | stat: -rw-r--r-- 16,023 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/////////////////////////////////////////////////////////////////////////////
// Name:        filesys.h
// Purpose:     interface of wxFileSystem, wxFileSystemHandler, wxFSFile
// Author:      wxWidgets team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////


/**
    Open Bit Flags
*/
enum wxFileSystemOpenFlags
{
    wxFS_READ = 1,      /**< Open for reading */
    wxFS_SEEKABLE = 4   /**< Returned stream will be seekable */
};


/**
    @class wxFileSystem

    This class provides an interface for opening files on different file systems.
    It can handle absolute and/or local filenames.

    It uses a system of handlers (see wxFileSystemHandler) to provide access to
    user-defined virtual file systems.

    @library{wxbase}
    @category{vfs}

    @see wxFileSystemHandler, wxFSFile, @ref overview_fs
*/
class wxFileSystem : public wxObject
{
public:
    /**
        Constructor.

        The initial current path of this object will be empty
        (i.e. GetPath() == wxEmptyString) which means that e.g. OpenFile()
        or FindFirst() functions will use current working directory as
        current path (see also wxGetCwd).
    */
    wxFileSystem();

    /**
        This static function adds new handler into the list of handlers
        (see wxFileSystemHandler) which provide access to virtual FS.

        Note that if two handlers for the same protocol are added, the last
        added one takes precedence.

        @note You can call:
              @code
              wxFileSystem::AddHandler(new My_FS_Handler);
              @endcode
              This is because (a) AddHandler is a static method, and (b) the
              handlers are deleted in wxFileSystem's destructor so that you
              don't have to care about it.
    */
    static void AddHandler(wxFileSystemHandler* handler);

    /**
       Remove a filesystem handler from the list of handlers.
    */
    static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
    
    /**
        Sets the current location. @a location parameter passed to OpenFile() is
        relative to this path.

        @remarks Unless @a is_dir is @true the @a location parameter is not the
                 directory name but the name of the file in this directory.

        All these commands change the path to "dir/subdir/":

        @code
        ChangePathTo("dir/subdir/xh.htm");
        ChangePathTo("dir/subdir", true);
        ChangePathTo("dir/subdir/", true);
        @endcode

        Example:
        @code
        f = fs->OpenFile("hello.htm"); // opens file 'hello.htm'
        fs->ChangePathTo("subdir/folder", true);
        f = fs->OpenFile("hello.htm"); // opens file 'subdir/folder/hello.htm' !!
        @endcode

        @param location
            the new location. Its meaning depends on the value of is_dir
        @param is_dir
            if @true location is new directory.
            If @false (the default) location is file in the new directory.
    */
    void ChangePathTo(const wxString& location, bool is_dir = false);

    /**
        Converts a wxFileName into an URL.

        @see URLToFileName(), wxFileName
    */
    static wxString FileNameToURL(const wxFileName& filename);

    /**
        Looks for the file with the given name @a file in a colon or semi-colon
        (depending on the current platform) separated list of directories in @a path.

        If the file is found in any directory, returns @true and the full path
        of the file in @a str, otherwise returns @false and doesn't modify @a str.

        @param pStr
            Receives the full path of the file, must not be @NULL
        @param path
            wxPATH_SEP-separated list of directories
        @param file
            the name of the file to look for
    */
    bool FindFileInPath(wxString* pStr, const wxString& path,
                        const wxString& file);

    /**
        Works like ::wxFindFirstFile().

        Returns the name of the first filename (within filesystem's current path)
        that matches @a wildcard.

        @param wildcard
            The wildcard that the filename must match
        @param flags
            One of wxFILE (only files), wxDIR (only directories) or 0 (both).
    */
    wxString FindFirst(const wxString& wildcard, int flags = 0);

    /**
        Returns the next filename that matches the parameters passed to FindFirst().
    */
    wxString FindNext();

    /**
        Returns the actual path (set by wxFileSystem::ChangePathTo).
    */
    wxString GetPath() const;

    /**
        This static function returns @true if there is a registered handler which can
        open the given location.
    */
    static bool HasHandlerForPath(const wxString& location);

    /**
        Opens the file and returns a pointer to a wxFSFile object or @NULL if failed.

        It first tries to open the file in relative scope (based on value passed to
        ChangePathTo() method) and then as an absolute path.

        Note that the user is responsible for deleting the returned wxFSFile.
        @a flags can be one or more of the ::wxFileSystemOpenFlags values
        combined together.

        A stream opened with just the default @e wxFS_READ flag may
        or may not be seekable depending on the underlying source.

        Passing @e "wxFS_READ | wxFS_SEEKABLE" for @a flags will back
        a stream that is not natively seekable with memory or a file
        and return a stream that is always seekable.

        @note
        The @a location argument is, despite this method's name @em not
        a filename. It is a "location", aka wxFileSystem URL (see
        @ref overview_fs).
    */
    wxFSFile* OpenFile(const wxString& location,
                       int flags = wxFS_READ);

    /**
        Converts URL into a well-formed filename.
        The URL must use the @c file protocol.
    */
    static wxFileName URLToFileName(const wxString& url);
};



/**
    @class wxFSFile

    This class represents a single file opened by wxFileSystem.
    It provides more information than wxWidgets' input streams
    (stream, filename, mime type, anchor).

    @note Any pointer returned by a method of wxFSFile is valid only as long as
          the wxFSFile object exists. For example a call to GetStream()
          doesn't @e create the stream but only returns the pointer to it.
          In other words after 10 calls to GetStream() you will have obtained
          ten identical pointers.

    @library{wxbase}
    @category{vfs,file}

    @see wxFileSystemHandler, wxFileSystem, @ref overview_fs
*/
class wxFSFile : public wxObject
{
public:
    /**
        Constructor. You probably won't use it. See the Note for details.

        It is seldom used by the application programmer but you will need it if
        you are writing your own virtual FS. For example you may need something
        similar to wxMemoryInputStream, but because wxMemoryInputStream doesn't
        free the memory when destroyed and thus passing a memory stream pointer
        into wxFSFile constructor would lead to memory leaks, you can write your
        own class derived from wxFSFile:

        @code
        class wxMyFSFile : public wxFSFile
        {
            private:
                void *m_Mem;
            public:
                wxMyFSFile(.....)
                ~wxMyFSFile() {free(m_Mem);}
                    // of course dtor is virtual ;-)
        };
        @endcode

        If you are not sure of the meaning of these params, see the description
        of the GetXXXX() functions.

        @param stream
            The input stream that will be used to access data
        @param location
            The full location (aka filename) of the file
        @param mimetype
            MIME type of this file. It may be left empty, in which
            case the type will be determined from file's extension (location must
            not be empty in this case).
        @param anchor
            Anchor. See GetAnchor() for details.
        @param modif
            Modification date and time for this file.
    */
    wxFSFile(wxInputStream* stream, const wxString& location,
             const wxString& mimetype, const wxString& anchor,
             wxDateTime modif);

    /**
        Detaches the stream from the wxFSFile object. That is, the
        stream obtained with GetStream() will continue its existence
        after the wxFSFile object is deleted.

        You will have to delete the stream yourself.
    */
    wxInputStream* DetachStream();

    /**
        Returns anchor (if present). The term of @b anchor can be easily
        explained using few examples:

        @verbatim
        index.htm#anchor                      // 'anchor' is anchor
        index/wx001.htm                       // NO anchor here!
        archive/main.zip#zip:index.htm#global // 'global'
        archive/main.zip#zip:index.htm        // NO anchor here!
        @endverbatim

        Usually an anchor is presented only if the MIME type is 'text/html'.
        But it may have some meaning with other files; for example myanim.avi#200
        may refer to position in animation or reality.wrl#MyView may refer
        to a predefined view in VRML.
    */
    const wxString& GetAnchor() const;

    /**
        Returns full location of the file, including path and protocol.

        Examples:
        @verbatim
        http://www.wxwidgets.org
        http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/archive.zip#zip:info.txt
        file:/home/vasek/index.htm
        relative-file.htm
        @endverbatim
    */
    const wxString& GetLocation() const;

    /**
        Returns the MIME type of the content of this file.

        It is either extension-based (see wxMimeTypesManager) or extracted from
        HTTP protocol Content-Type header.
    */
    const wxString& GetMimeType() const;

    /**
        Returns time when this file was modified.
    */
    wxDateTime GetModificationTime() const;

    /**
        Returns pointer to the stream.

        You can use the returned stream to directly access data.
        You may suppose that the stream provide Seek and GetSize functionality
        (even in the case of the HTTP protocol which doesn't provide
        this by default. wxHtml uses local cache to work around
        this and to speed up the connection).
    */
    wxInputStream* GetStream() const;
};



/**
    @class wxFileSystemHandler

    Classes derived from wxFileSystemHandler are used to access virtual file systems.

    Its public interface consists of two methods: wxFileSystemHandler::CanOpen
    and wxFileSystemHandler::OpenFile.

    It provides additional protected methods to simplify the process
    of opening the file: GetProtocol(), GetLeftLocation(), GetRightLocation(),
    GetAnchor(), GetMimeTypeFromExt().

    Please have a look at overview (see wxFileSystem) if you don't know how locations
    are constructed.

    Also consult the @ref overview_fs_wxhtmlfs "list of available handlers".

    Note that the handlers are shared by all instances of wxFileSystem.

    @remarks
    wxHTML library provides handlers for local files and HTTP or FTP protocol.

    @note
    The location parameter passed to OpenFile() or CanOpen() methods is always an
    absolute path. You don't need to check the FS's current path.

    @beginWxPerlOnly
    In wxPerl, you need to derive your file system handler class
    from @c Wx::PlFileSystemHandler.
    @endWxPerlOnly

    @library{wxbase}
    @category{vfs}

    @see wxFileSystem, wxFSFile, @ref overview_fs
*/
class wxFileSystemHandler : public wxObject
{
public:
    /**
        Constructor.
    */
    wxFileSystemHandler();

    /**
        Returns @true if the handler is able to open this file. This function doesn't
        check whether the file exists or not, it only checks if it knows the protocol.
        Example:

        @code
        bool MyHand::CanOpen(const wxString& location)
        {
            return (GetProtocol(location) == "http");
        }
        @endcode

        Must be overridden in derived handlers.
    */
    virtual bool CanOpen(const wxString& location) = 0;

    /**
        Works like ::wxFindFirstFile().

        Returns the name of the first filename (within filesystem's current path)
        that matches @e wildcard. @a flags may be one of wxFILE (only files),
        wxDIR (only directories) or 0 (both).

        This method is only called if CanOpen() returns @true.
    */
    virtual wxString FindFirst(const wxString& wildcard,
                               int flags = 0);

    /**
        Returns next filename that matches parameters passed to wxFileSystem::FindFirst.

        This method is only called if CanOpen() returns @true and FindFirst()
        returned a non-empty string.
    */
    virtual wxString FindNext();

    /**
        Returns the MIME type based on @b extension of @a location.
        (While wxFSFile::GetMimeType() returns real MIME type - either
         extension-based or queried from HTTP.)

        Example:
        @code
        GetMimeTypeFromExt("index.htm") == "text/html"
        @endcode
    */
    static wxString GetMimeTypeFromExt(const wxString& location);

    /**
        Opens the file and returns wxFSFile pointer or @NULL if failed.
        Must be overridden in derived handlers.

        @param fs
            Parent FS (the FS from that OpenFile was called).
            See the ZIP handler for details of how to use it.
        @param location
            The absolute location of file.
    */
    virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0;

protected:

    /**
        Returns the anchor if present in the location.
        See wxFSFile::GetAnchor for details.

        Example:
        @code
        GetAnchor("index.htm#chapter2") == "chapter2"
        @endcode

        @note the anchor is NOT part of the left location.
    */
    static wxString GetAnchor(const wxString& location);

    /**
        Returns the left location string extracted from @e location.

        Example:
        @code
        GetLeftLocation("file:myzipfile.zip#zip:index.htm") == "file:myzipfile.zip"
        @endcode
    */
    static wxString GetLeftLocation(const wxString& location);

    /**
        Returns the protocol string extracted from @a location.

        Example:
        @code
        GetProtocol("file:myzipfile.zip#zip:index.htm") == "zip"
        @endcode
    */
    static wxString GetProtocol(const wxString& location);

    /**
        Returns the right location string extracted from @a location.

        Example:
        @code
        GetRightLocation("file:myzipfile.zip#zip:index.htm") == "index.htm"
        @endcode
    */
    static wxString GetRightLocation(const wxString& location);
};


/**
    Input stream for virtual file stream files.

    The stream reads data from wxFSFile obtained from wxFileSystem. It is
    especially useful to allow using virtual files with other wxWidgets
    functions and classes working with streams, e.g. for loading images or
    animations from virtual files and not only physical ones.

    @library{wxbase}
    @category{streams}

    @see wxWrapperInputStream, wxFSFile

    @since 2.9.4
*/
class wxFSInputStream : public wxWrapperInputStream
{
public:
    /**
        Create a stream associated with the data of the given virtual file
        system file.

        @param filename
            The name of the input file passed to wxFileSystem::OpenFile().
        @param flags
            Combination of flags from wxFileSystemOpenFlags. ::wxFS_READ is
            implied, i.e. it is always added to the flags value.

        Use wxStreamBase::IsOk() to verify if the constructor succeeded.
    */
    wxFileInputStream(const wxString& filename, int flags = 0);

    /**
        Returns @true if the stream is initialized and ready.
    */
    bool IsOk() const;
};