Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileIO.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <windows.h>
4 #include <string>
5 #include <list>
6 
7 namespace NDirectory
8 {
9 #ifdef _WIN32
10  const wchar_t kDirSeparator = L'\\';
11 #else
12  const wchar_t kDirSeparator = L'/';
13 #endif
14  const wchar_t kExtensionSeparator = L'.';
15 
16  // Attempts to create the specified simple directory
17  // ie its immediate parent must exist
18  bool CreateDirectory(const std::wstring& dir);
19 
20  // Ensure the directory ends with a single kDirSeparator character.
21  void NormalizeDirectory(std::wstring& dir);
22 
23  // Get the file name from the given path -- excluding extension.
24  void GetFileName(const std::wstring& path, std::wstring& fileName);
25 
26  // Strip the file name, and extension, from the given path
27  void StripFile(const std::wstring& path);
28 
29  // Gets if the specified path points to a valid directory
30  bool IsDirectory(const std::wstring& path);
31 
32  // Checks if the specified file exists
33  bool IsValidFile(const std::wstring& path);
34 
35  // Gets the files (not any directories) within a directory matching
36  // the specified pattern
37  void FindFiles(const std::wstring& searchExp,
38  std::list<std::wstring>& files);
39 
40 #ifdef _WIN32
41  bool GetMyDocuments(std::wstring& path);
42 #endif
43 }
44 
45 class CFile
46 {
47 protected:
48  HANDLE hFile;
49  std::wstring file_name;
50 
51 public:
52  CFile();
53  ~CFile();
54 
55  bool Open(const std::wstring& file, DWORD dwAccess, DWORD dwShared, DWORD dwCreateDeposition);
56  bool Open(const std::wstring& file, DWORD dwAccess);
57  bool IsOpen() const;
58 
59  void Close();
60 
61  // Move the specified file to the specified location.
62  static bool Move(const std::wstring& file,
63  const std::wstring& newfile, bool overwrite);
64 
65  // Delete the specified file
66  static bool Delete(const std::wstring& file);
67 
69 
70  bool Seek(long distance);
71  bool SeekBegin();
72  bool SeekEnd();
73 };
74 
75 class CInFile : public CFile
76 {
77 private:
78  static const int kReadSize = 1 << 20; // 1 MB
79 public:
80  CInFile();
81  virtual ~CInFile();
82 
83  bool Open(const std::wstring& file);
84 
85  // Reads data until a line escape(\n or \r\n) or until maxCount - 1 is
86  // reached, which is the maximum number of characters (of size sizeof(T))
87  // to be read. The output buffer should be at least sizeof(T)*maxCount
88  // bytes. The output is always null terminated. found is set to true if
89  // newline found, or false otherwise.
90  template <class T>
91  bool ReadLine(T* out, DWORD maxCount, bool* found)
92  {
93  DWORD read;
94  if (!ReadSome((BYTE*)out, sizeof(T)*(maxCount-1), &read)) return false;
95  if (read == 0) return false; // maybe eof
96  DWORD nCharRead = read / sizeof(T);
97  size_t x = 0, end = 0;
98  if (found) *found = false;
99  for (; x < nCharRead; x++)
100  {
101  if (out[x] == T('\n')) {
102  if (found) *found = true;
103  break;
104  }
105  else if (out[x] == T('\r')) end = x;
106  }
107  if (end + 1 == x) out[end] = T('\0');
108  else out[x] = T('\0');
109  // seek to start of the next line
110  if (read != x) {
111  long distance = -(long)read + (x + 1);
112  if (distance != 0) Seek(distance);
113  }
114  return true;
115  }
116 
117  // Attempts to read all data until either success or an error
118  bool Read(BYTE* out, DWORD size, DWORD* processedSize);
119 
120  // Attempts to read data in 1MB blocks
121  // False indicated an error, otherwise keep calling.
122  bool ReadSome(BYTE* out, DWORD size, DWORD* written);
123 };
124 
125 class COutFile : public CFile
126 {
127 private:
128  static const int kWriteSize = 1 << 20; // 1 MB
129 
130 public:
131  COutFile();
132  virtual ~COutFile();
133 
134  // Attempts to write all data until either success or an error
135  bool Write(BYTE* data, DWORD size, DWORD* processedSize);
136 
137  // Attempts to write data in 1MB blocks
138  // False indicated an error, otherwise keep calling.
139  bool WriteSome(BYTE* data, DWORD size, DWORD* written);
140 };
141 
142 template <class T>
143 class CTempFile : public T
144 {
145 private:
146  bool do_delete;
147 
148 public:
149  CTempFile() : T(), do_delete(true) {}
150  virtual ~CTempFile() {
151  if (do_delete) {
152  Close();
153  CFile::Delete(file_name);
154  }
155  }
156 
157  void success() { do_delete = false; }
158 };