neoWidgets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
util.h
Go to the documentation of this file.
1 #ifndef NEO_UTIL_H
2 #define NEO_UTIL_H
3 
8 namespace neo
10 {
20  enum WIN_TYPE
21  {
23  WIN_2000 = 1,
24  WIN_XP = 2,
26  WIN_VISTA = 4,
29  WIN_7 = 5,
31  WIN_8 = 6,
33  WIN_8_1 = 7,
35  };
36 
40  class OSVersion
41  {
42  public:
46  static const OSVersion& Get();
47 
48  WIN_TYPE windowsType;
49  OSVERSIONINFOEX info;
50  };
51 
55  class DynamicLib
56  {
57  public:
58  DynamicLib(const tstring& filename)
59  {
60  _lib = LoadLibrary(filename.c_str());
61  }
62 
63  ~DynamicLib()
64  {
65  if(_lib)
66  FreeLibrary(_lib);
67  }
68 
69  bool IsLoaded() { return _lib != 0; }
70  operator HMODULE() { return _lib; }
71  protected:
72  DynamicLib()
73  : _lib(0)
74  {}
75 
76  HMODULE _lib;
77  private:
78  DynamicLib(const DynamicLib& other) {}
79  DynamicLib& operator=(const DynamicLib& other) { return *this; }
80  };
81 
85  class LoadedLib : public DynamicLib
86  {
87  public:
88  LoadedLib(const tstring& filename)
89  {
90  _lib = GetModuleHandle(filename.c_str());
91  }
92 
93  // override destructor, we don't want to free the handle under any circumstances
94  ~LoadedLib()
95  {}
96  };
97 
98 #ifdef _DEBUG
99  inline HRESULT UseHR(HRESULT hr, LPCSTR file, long line)
100  {
101  if(FAILED(hr))
102  {
103  // convert HRESULT error to readable string
104  // this should work for standard Windows stuff
105  // for special APIs like DirectX 9, FormatMessage will not always have a translation
106  static const int bufferSize = 2000;
107  std::string buffer;
108  buffer.resize(bufferSize);
109  DWORD bytesWritten = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, hr, 0, &buffer[0], bufferSize, 0);
110  if(bytesWritten == 0)
111  {
112  DWORD error = GetLastError();
113  buffer = "FormatMessage failed: code " + std::to_string(error);
114  }
115  // prepend file and line in such a way that you can click this line in Visual C++
116  // and it jumps right to the code
117  // E:\example.cpp(123): Error
118  buffer = file + std::string("(") + std::to_string(line) + "): " + buffer;
119 
120  OutputDebugStringA(buffer.c_str());
121  }
122  return hr;
123  }
124 #endif
125 
126  // USEHR : passes the HRESULT along and outputs error
127  // CHECKHR : convert HRESULT to bool and outputs error
128 
129 #ifdef _DEBUG
130  #define USEHR(hr) UseHR((hr), __FILE__, __LINE__)
131  #define CHECKHR(hr) SUCCEEDED(UseHR((hr), __FILE__, __LINE__))
132 #else
133  #define USEHR(hr) hr
134  #define CHECKHR(hr) SUCCEEDED((hr))
135 #endif
136 
137  //-------------------------------------------------------------------------
138 
143  {
147  };
148 
152  class File
153  {
154  public:
158  File();
163  File(const tstring& filename, const tstring& openMode);
164  ~File();
165 
170  bool Open(const tstring& filename, const tstring& openMode);
171 
175  bool Close();
176 
180  bool IsOpen() const { return _fp != 0; }
181 
185  operator bool() const { return IsOpen(); }
186 
190  void Seek(int64_t offset, SEEK_FROM mode = FROM_START);
191 
195  int64_t Tell();
196 
201  int64_t Size();
202 
206  bool Eof();
207 
212  size_t Write(const void* buffer, size_t byteSize);
213 
217  bool Write(const std::string& buffer);
218 
223  size_t Read(void* buffer, size_t byteSize);
224 
229  bool ReadLine(std::string* line, const std::string& delim = "\n");
230 
234  bool ReadAll(std::string* all);
235 
239  FILE* GetInternalPtr() const { return _fp; }
240  private:
241  FILE* _fp;
242  };
243 
244  //----------------------------------------------------------------------------
245 
249  struct FileSearchItem : public WIN32_FIND_DATA
250  {
254  bool IsDirectory() const { return (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; }
255 
259  bool IsReadonly() const { return (dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0; }
260 
264  bool IsHidden() const { return (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0; }
265 
269  bool IsSystemFile() const { return (dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0; }
270 
274  bool IsTemporary() const { return (dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) != 0; }
275 
279  int64_t GetFilesize() const { return int64_t(nFileSizeHigh) * 0x100000000 + int64_t(nFileSizeLow); }
280 
284  tstring GetFilename() const { return cFileName; }
285  };
286 
287  //----------------------------------------------------------------------------
288 
318  {
319  public:
324  FileSearch(const tstring& searchPattern);
325  ~FileSearch();
326 
330  bool Ok() const;
331 
335  bool Done() const;
336 
340  bool Next();
341 
345  const FileSearchItem& Data() const;
346 
350  class iterator
351  {
352  public:
353  friend FileSearch;
354 
361  : _search(0)
362  , _end(false)
363  {}
364 
365 
366  const FileSearchItem& operator*() const { return _search->Data(); }
367  const FileSearchItem* operator->() const { return &_search->Data(); }
368  iterator& operator++()
369  {
370  // pre increment
371  assert(!_search->Done());
372  _search->Next();
373  return *this;
374  }
375  iterator operator++(int) const
376  {
377  // post increment
378  assert(!_search->Done());
379  _search->Next();
380  return *this;
381  }
382  bool operator==(const iterator& rhs) const
383  {
384  // abuse of != operator to check if the iteration has ended
385  if(_search && rhs._end)
386  return _search->Done();
387  return false;
388  }
389  bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
390  private:
391  iterator(FileSearch* search, bool end)
392  : _search(search)
393  , _end(end)
394  {}
395 
396  FileSearch* _search;
397  bool _end;
398  };
399 
403  iterator begin() { return iterator(this, false); }
404 
408  iterator end() { return iterator(0, true); }
409  private:
410  HANDLE _file;
411  bool _done;
412  FileSearchItem _wfd;
413  };
414 
415  //----------------------------------------------------------------------------
416 
420  class Path
421  {
422 
423  public:
427  static bool Exists(const tstring& path);
428 
432  static bool IsFile(const tstring& path);
433 
437  static bool IsDirectory(const tstring& path);
438 
442  static bool IsAbsolute(const tstring& path);
443 
447  static bool HasExt(const tstring& path);
448 
452  static tstring Join(const tstring& p1, const tstring& p2);
453 
457  static tstring Join(const tstring& p1, const tstring& p2, const tstring& p3);
458 
462  static tstring Join(const tstring& p1, const tstring& p2, const tstring& p3, const tstring& p4);
463 
467  static tstring GetDrive(const tstring& path);
468 
472  static tstring GetParent(const tstring& path);
473 
477  static tstring GetFilename(const tstring& path);
478 
482  static tstring GetFilenameWithoutExt(const tstring& path);
483 
487  static tstring GetExt(const tstring& path);
488 
493  static std::pair<tstring,tstring> SplitDrive(const tstring& path);
498  static std::pair<tstring,tstring> SplitParent(const tstring& path);
502  static std::pair<tstring,tstring> SplitExt(const tstring& path);
503 
507  static tstring ChangeFilename(const tstring& path, const tstring& name);
508 
512  static tstring ChangeExt(const tstring& path, const tstring& ext);
513 
517  static tstring GetCurrentDir();
518  private:
519  enum Drive_t
520  {
521  DRIVE_NONE,
522  DRIVE_LOCAL,
523  DRIVE_NETWORK,
524  };
525  static tstring::size_type _GetDriveEnd(const tstring& path, Drive_t* type);
526 
527  static tstring _GetSeparators()
528  {
529  return _T("/\\");
530  }
531  static bool _IsSeparator(TCHAR c)
532  {
533  return c == _T('/') || c == _T('\\');
534  }
535  };
536 
537 } // namespace neo
538 
539 #endif
bool IsSystemFile() const
Is the item reserved for system use?
Definition: util.h:269
static bool Exists(const tstring &path)
Check if the path points to an existing item.
Definition: util.inl:243
bool IsHidden() const
Is the item hidden in explorer?
Definition: util.h:264
bool Open(const tstring &filename, const tstring &openMode)
Opens a file with the specified mode (read/write, binary, ...)
Definition: util.inl:100
void Seek(int64_t offset, SEEK_FROM mode=FROM_START)
Move to a position inside the file.
Definition: util.inl:119
Windows 2008 Release 2 (same as Windows 7)
Definition: util.h:28
const FileSearchItem & Data() const
Data of the current file.
Definition: util.inl:235
bool IsReadonly() const
Is the item read only?
Definition: util.h:259
static tstring GetParent(const tstring &path)
Removes trailing file or directory from the path.
Definition: util.inl:334
bool Done() const
True if the iteration is at the end, meaning further calls to next won&#39;t work.
Definition: util.inl:220
iterator for STL algorithm integration
Definition: util.h:350
Windows Server 2003.
Definition: util.h:25
Windows 8.
Definition: util.h:31
bool Eof()
End of file.
Definition: util.inl:147
Windows XP.
Definition: util.h:24
Data about a file system item found by FileSearch.
Definition: util.h:249
static tstring GetDrive(const tstring &path)
Root of the path, either a drive letter (C:) or network root (\server)
Definition: util.inl:329
not sure if neoWidgets will even work for such systems
Definition: util.h:22
Retrieves address of already loaded DLL.
Definition: util.h:85
SEEK_FROM
Definition: util.h:142
bool IsDirectory() const
Is the item a directory?
Definition: util.h:254
static tstring GetFilename(const tstring &path)
Filename with extension.
Definition: util.inl:339
iterator()
Default constructor.
Definition: util.h:360
static tstring ChangeExt(const tstring &path, const tstring &ext)
Utility to change the extension while leaving the rest intact.
Definition: util.inl:475
FILE * GetInternalPtr() const
Direct access to internal file pointer.
Definition: util.h:239
Windows 2012 (same as Windows 8)
Definition: util.h:30
size_t Write(const void *buffer, size_t byteSize)
Write a block of memory to file.
Definition: util.inl:152
End of file.
Definition: util.h:146
tstring GetFilename() const
Return filename as string instance.
Definition: util.h:284
static tstring GetExt(const tstring &path)
Filename extension.
Definition: util.inl:349
Windows Vista.
Definition: util.h:26
Current file position.
Definition: util.h:145
Manipulation of Windows-style path strings.
Definition: util.h:420
File()
Empty constructor.
Definition: util.inl:82
static bool HasExt(const tstring &path)
Check for filename extension.
Definition: util.inl:269
Encapsulates loading of DLLs.
Definition: util.h:55
Start of file.
Definition: util.h:144
be forward compatible, the next windows version will surely come
Definition: util.h:34
static tstring Join(const tstring &p1, const tstring &p2)
Semi-intelligently combines parts into a correct path.
Definition: util.inl:283
iterator end()
STL algorithms integration.
Definition: util.h:408
static tstring ChangeFilename(const tstring &path, const tstring &name)
Utility to change the filename while leaving extension and parent intact.
Definition: util.inl:470
bool ReadAll(std::string *all)
Get the entire file as string.
Definition: util.inl:192
Windows 7.
Definition: util.h:29
Wrapper for the FindFirstFile API.
Definition: util.h:317
static const OSVersion & Get()
Singleton method.
Definition: util.inl:4
WIN_TYPE
Definition: util.h:20
Windows 2012 Release 2 (same as Windows 8.1)
Definition: util.h:32
static bool IsDirectory(const tstring &path)
Check if the item is a directory.
Definition: util.inl:256
static std::pair< tstring, tstring > SplitDrive(const tstring &path)
split path in two at the root
Definition: util.inl:381
static std::pair< tstring, tstring > SplitParent(const tstring &path)
split path into parent directory and filename
Definition: util.inl:409
Windows 8.1.
Definition: util.h:33
bool IsTemporary() const
Is the file marked temporary?
Definition: util.h:274
size_t Read(void *buffer, size_t byteSize)
Read from file into a block of memory.
Definition: util.inl:162
static tstring GetFilenameWithoutExt(const tstring &path)
Filename without extension.
Definition: util.inl:344
bool IsOpen() const
true if the file is open
Definition: util.h:180
int64_t Size()
Get size of file in bytes.
Definition: util.inl:138
static tstring GetCurrentDir()
Current working directory for this app.
Definition: util.inl:480
int64_t Tell()
Get current position inside the file.
Definition: util.inl:131
Windows 2000.
Definition: util.h:23
static bool IsAbsolute(const tstring &path)
Check if the path has a root.
Definition: util.inl:262
bool Ok() const
True if the search found at least one file.
Definition: util.inl:215
bool Close()
Closes the file.
Definition: util.inl:111
FileSearch(const tstring &searchPattern)
Starts the search.
Definition: util.inl:202
bool Next()
Iterate to next file in the search.
Definition: util.inl:225
bool ReadLine(std::string *line, const std::string &delim="\n")
Reads until one of the delimiters is found.
Definition: util.inl:167
iterator begin()
STL algorithms integration.
Definition: util.h:403
Contains the basic functions for file reading/writing.
Definition: util.h:152
Windows 2008 (same as Windows Vista)
Definition: util.h:27
int64_t GetFilesize() const
Utility to compute 64 bit filesize from members.
Definition: util.h:279
static std::pair< tstring, tstring > SplitExt(const tstring &path)
split path in two at the extension dot
Definition: util.inl:452
static bool IsFile(const tstring &path)
Check if the item is a file.
Definition: util.inl:250
Definition: util.h:40