root/trunk/Files.h

Revision 5, 5.1 kB (checked in by qbert, 6 years ago)

Initial ( and last :( ) commit

Line 
1 #ifndef __filenames_h__
2 #define __filenames_h__
3
4 #include <string>
5 #include "filefuncs.h"
6 #include "Utf8_16.h"
7 #ifndef tstring
8     typedef std::basic_string<TCHAR> tstring;
9 #endif
10
11 #define CFILE_CouldNotSaveError _T("%s could not be saved in the specified location.\nThis could be due to an absent disk, a broken network connection, or a full disk.\nDo you want to save in another location?")
12
13 #define CFILE_CouldNotLoadError _T("%s could not be opened .\nThis could be because the file no longer exists, a disk is absent or due to a broken network connection.")
14
15 #define CFILE_SaveAccessDenied _T("%s could not be saved because access was denied.\nThis could be due to the file being marked read-only, or the disk being write-protected.\nDo you want to save in another location?")
16
17 #define CFILE_LoadAccessDenied _T("%s could not be opened because access was denied.\nThis could be because you do not have sufficient rights\nto open this file.")
18
19 #define CFILE_SaveDiskFullError _T("%s could not be saved because the disk is full.\nDo you want to save in another location?")
20
21 #define CFILE_SaveShareViolation _T("%s could not be saved because another program or user is\nusing the same file.\nDo you want to save in another location?")
22
23 #define CFILE_LoadShareViolation _T("%s could not be opened because another program or user is\nusing the same file.")
24
25 #define CFILE_NetSaveError _T("%s could not be saved because of a network error. This\ncould be because of a broken network\nconnection or because the network was too busy.\nDo you want to save in another location?")
26
27 #define CFILE_NetLoadError _T("%s could not be opened because of a network error. This\ncould be because of a broken network\nconnection or because the network was too busy.")
28
29 const int blockSize = 131072;
30
31 class CElephantFile
32 {
33     public:
34
35         enum OpenFlags {
36             modeRead            = 0x0000,
37             modeBinary          = 0x0000,
38             modeWrite           = 0x0001,
39             modeReadWrite       = 0x0002,
40             modeText            = 0x0008
41         };
42
43         enum EFrom {
44             current     = SEEK_CUR, // Current position of file pointer.
45             end         = SEEK_END, // End of file.
46             begin       = SEEK_SET  // Beginning of file.
47         };
48
49         CElephantFile();
50         virtual ~CElephantFile();
51        
52         bool Open(LPCTSTR filename, UINT flags = 0);
53         int Read(void* lpBuf, UINT nCount);
54         int Write(void* lpBuf, UINT nCount);
55         void Close();
56         void Seek(UINT offset, EFrom from = begin);
57
58         //int ShowError(LPCTSTR filename, bool bOpen = true);
59
60         long GetPosition() const;
61         long GetLength();
62
63     protected:
64         FILE* m_file;
65 };
66
67 class CTextFile : public CElephantFile
68 {
69     public:
70         CTextFile() : CElephantFile() {}
71         virtual ~CTextFile() {}
72
73 #ifndef Elephant_NO_CSTRING
74         bool ReadLine(CString& line);
75 #endif
76         bool WriteLine(LPCTSTR line);
77 };
78
79
80 class CElephantFileName
81 {
82 public:
83     CElephantFileName() : m_FileName(_T("")){}
84     CElephantFileName(const CElephantFileName& copy){*this = copy;}
85     CElephantFileName(LPCTSTR filename){m_FileName = filename;}
86     CElephantFileName(const tstring& copy){*this = copy;}
87    
88     // Operators
89    
90     CElephantFileName& operator = (LPCTSTR filename);           ///< Set the filename to "filename".
91     CElephantFileName& operator = (const tstring& filename);    ///< Set the filename to "filename".
92     CElephantFileName& operator = (const CElephantFileName& filename);  ///< Change to filename.filename.
93    
94     // Operations
95     void ChangeExtensionTo(LPCTSTR newext);             ///< Change the extension of the filename.
96     void ChangePathTo(LPCTSTR newpath);                 ///< Change the path of the filename to newpath.
97    
98     tstring GetExtension();
99     tstring GetFileName();
100     tstring GetFileName_NoExt();
101     tstring GetPath();
102     void GetPath(tstring& buf);                         ///< Return c:\temp\ of c:\temp\dat.dat
103     void GetFileName_NoExt(tstring& buf);               ///< Return the filename part of c:\temp\filename.dat
104     void GetFileName(tstring& buf);                     ///< Return the filename.dat part of c:\temp\filename.dat
105     tstring& Sanitise();                                ///< Fix up the filename to standard form.
106
107     bool IsRelativePath();                              ///< Return true if it's a relative path.
108     tstring GetRelativePath(LPCTSTR path);              ///< Return the relative path string required to get to path.
109     bool CanGetRelativePath(LPCTSTR path);              ///< Return true if we can build a relative path to 'path'.
110     void Root(LPCTSTR rootPath);                        ///< Root the relative filename in rootPath.
111
112     bool IsSubElementOf(LPCTSTR path);                  ///< Return true if this file is below path in the file system.
113     bool PathIsParentElementOf(LPCTSTR path);           ///< Return true if the path is below us in the file system.
114    
115
116     /**
117      * GetFileAge returns the integer dos date of the
118      * file represented by this class. If the file does
119      * not exist, the function returns -1.
120      */
121     int GetFileAge();                                   ///< Get the age of the file.
122
123     int GetLength();
124     LPCTSTR c_str();
125
126     const tstring& ToLower();
127
128     operator tstring() {return m_FileName;}
129
130     static tstring GetCurrentDirectory();
131
132 protected:
133     tstring m_FileName;
134
135     int GetLastSlashPos();
136     int GetLastDotPos(tstring* str=NULL);
137 };
138
139 int FileAge(LPCTSTR FileName);
140 bool CreateDirectoryRecursive(LPCTSTR pszDirectory, LPSECURITY_ATTRIBUTES lpSA = NULL);
141
142 #endif
Note: See TracBrowser for help on using the browser.