seeking out problem file.
[feisty_meow.git] / nucleus / library / filesystem / huge_file.h
1 #ifndef HUGE_FILE_CLASS
2 #define HUGE_FILE_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : huge_file                                                         *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2007-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include "byte_filer.h"
19
20 #include <basis/astring.h>
21 #include <basis/outcome.h>
22 #include <basis/common_outcomes.h>
23 #include <basis/enhance_cpp.h>
24
25 namespace filesystem {
26
27 //! Supports reading and writing to very large files, > 4 gigabytes.
28 /*!
29   The standard file I/O functions only handle files up to 4 gigabytes.  This
30   class extends the range to essentially unlimited sizes, as long as the
31   operating system can accurately do relative seeks and can read/write to
32   files of the size needed.
33 */
34
35 class huge_file
36 {
37 public:
38   huge_file(const basis::astring &filename, const basis::astring &permissions);
39     //!< opens "filename" for access, where it presumably is a very large file.
40     /*!< see byte filer for a description of the permissions.
41     */
42   virtual ~huge_file();
43
44   DEFINE_CLASS_NAME("huge_file");
45
46   enum outcomes {
47     OKAY = basis::common::OKAY,
48     FAILURE = basis::common::FAILURE,
49     ACCESS_DENIED = basis::common::ACCESS_DENIED,
50     BAD_INPUT = basis::common::BAD_INPUT
51   };
52
53   bool good() const;
54     //!< reports if the file was opened successfully.
55
56   bool eof() const;
57     //!< reports when the file pointer has reached the end of the file.
58
59   double length();
60     //!< expensive operation accesses the file to find length.
61
62   double file_pointer() const { return _file_pointer; }
63     //!< returns where we currently are in the file.
64
65   basis::outcome seek(double new_position,
66           byte_filer::origins origin = byte_filer::FROM_CURRENT);
67     //!< move the file pointer to "new_position" if possible.
68     /*!< the relative seek is the easiest type of seek to accomplish with a
69     huge file.  the other types are also supported, but take a bit more to
70     implement. */
71
72   basis::outcome move_to(double absolute_posn);
73     //!< simpler seek just goes from current location to "absolute_posn".
74
75   basis::outcome read(basis::byte_array &to_fill, int desired_size, int &size_read);
76     //!< reads "desired_size" into "to_fill" if possible.
77     /*!< "size_read" reports how many bytes were actually read. */
78
79   basis::outcome write(const basis::byte_array &to_write, int &size_written);
80     //!< stores the array "to_write" into the file.
81     /*!< "size_written" reports how many bytes got written. */
82
83   bool truncate();
84     //!< truncates the file after the current position.
85
86   basis::outcome touch();
87     //<! creates the file if it doesn't exist, or updates the time on the file.
88
89   void flush();
90     //!< forces any pending writes to actually be saved to the file.
91
92 private:
93   byte_filer *_real_file;  //!< supports us but is subject to 4g limit.
94   double _file_pointer;  //!< position in the file if OS is tracking us.
95 };
96
97 } //namespace.
98
99 #endif
100