Merge pull request #18 from wruppelx/master
[asdcplib.git] / src / KM_fileio.h
index b38429f144818b758eb0fc7323c3be248f4bc9ac..60e1e6a3c37988d61e5968841e9e224f8eece206 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2004-2009, John Hurst
+Copyright (c) 2004-2016, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -37,6 +37,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #ifdef KM_WIN32
 # include <io.h>
+# include "dirent_win.h"
 #else
 # include <dirent.h>
 # include <unistd.h>
@@ -55,21 +56,50 @@ namespace Kumu
   class DirScanner
     {
     public:
-#ifdef KM_WIN32
-      __int64               m_Handle;
-      struct _finddatai64_t m_FileInfo;
-#else
       DIR*       m_Handle;
-#endif
 
       DirScanner(void);
       ~DirScanner() { Close(); }
 
-      Result_t Open(const char*);
+      Result_t Open(const std::string&);
       Result_t Close();
       Result_t GetNext(char*);
     };
 
+
+  // 
+  enum DirectoryEntryType_t {
+    DET_FILE,
+    DET_DIR,
+    DET_DEV,
+    DET_LINK
+  };
+
+  //
+  class DirScannerEx
+  {
+    std::string m_Dirname;
+    DIR*       m_Handle;
+
+    KM_NO_COPY_CONSTRUCT(DirScannerEx);
+
+  public:
+    
+    DirScannerEx();
+    ~DirScannerEx() { Close(); }
+
+    Result_t Open(const std::string& dirname);
+    Result_t Close();
+
+
+    inline Result_t GetNext(std::string& next_item_name) {
+      DirectoryEntryType_t ft;
+      return GetNext(next_item_name, ft);
+    }
+
+    Result_t GetNext(std::string& next_item_name, DirectoryEntryType_t& next_item_type);
+  };
+
 #ifdef KM_WIN32
   typedef __int64  fsize_t;
   typedef __int64  fpos_t;
@@ -133,10 +163,11 @@ namespace Kumu
   bool        PathIsFile(const std::string& Path); // true if the path exists in the filesystem and is a file
   bool        PathIsDirectory(const std::string& Path); // true if the path exists in the filesystem and is a directory
   fsize_t     FileSize(const std::string& Path); // returns the size of a regular file, 0 for a directory or device
+  std::string PathCwd();
   bool        PathsAreEquivalent(const std::string& lhs, const std::string& rhs); // true if paths point to the same filesystem entry
 
   // Returns free space and total space available for the given path
-  Result_t   FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu::fsize_t& total_space);
+  Result_t    FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu::fsize_t& total_space);
 
   // split and reassemble paths as lists of path components
   PathCompList_t& PathToComponents(const std::string& Path, PathCompList_t& CList, char separator = '/'); // removes '//'
@@ -148,6 +179,7 @@ namespace Kumu
   std::string PathMakeAbsolute(const std::string& Path, char separator = '/'); // compute position of relative path using getcwd()
   std::string PathMakeLocal(const std::string& Path, const std::string& Parent); // remove Parent from front of Path, if it exists
   std::string PathMakeCanonical(const std::string& Path, char separator = '/'); // remove '.' and '..'
+  bool        PathResolveLinks(const std::string& link_path, std::string& resolved_path, char separator = '/');
 
   // common operations
   std::string PathBasename(const std::string& Path, char separator = '/'); // returns right-most path element (list back())
@@ -155,6 +187,11 @@ namespace Kumu
   std::string PathGetExtension(const std::string& Path); // returns everything in the right-most element following the right-most '.'
   std::string PathSetExtension(const std::string& Path, const std::string& Extension); // empty extension removes '.' as well
 
+  std::string PathJoin(const std::string& Path1, const std::string& Path2, char separator = '/');
+  std::string PathJoin(const std::string& Path1, const std::string& Path2, const std::string& Path3, char separator = '/');
+  std::string PathJoin(const std::string& Path1, const std::string& Path2,
+                      const std::string& Path3, const std::string& Path4, char separator = '/');
+
 
   //------------------------------------------------------------------------------------------
   // Path Search
@@ -174,7 +211,7 @@ namespace Kumu
   {
   public:
     virtual ~PathMatchAny() {}
-    inline bool Match(const std::string& s) const { return true; }
+    inline bool Match(const std::string&) const { return true; }
   };
 
 #ifndef KM_WIN32
@@ -215,6 +252,8 @@ namespace Kumu
   PathList_t& FindInPaths(const IPathMatch& Pattern, const PathList_t& SearchPaths,
                          PathList_t& FoundPaths, bool one_shot = false, char separator = '/');
 
+  std::string GetExecutablePath(const std::string& default_path);
+
   //------------------------------------------------------------------------------------------
   // Directory Manipulation
   //------------------------------------------------------------------------------------------
@@ -228,6 +267,9 @@ namespace Kumu
   // Recursively remove a file or directory
   Result_t DeletePath(const std::string& pathname);
 
+  // Remove the path only if it is a directory that is empty.
+  Result_t DeleteDirectoryIfEmpty(const std::string& path);
+
   //------------------------------------------------------------------------------------------
   // File I/O Wrappers
   //------------------------------------------------------------------------------------------
@@ -235,10 +277,10 @@ namespace Kumu
   // Instant IO for strings
   //
   // Reads an entire file into a string.
-  Result_t ReadFileIntoString(const char* filename, std::string& outString, ui32_t max_size = 8 * Megabyte);
+  Result_t ReadFileIntoString(const std::string& filename, std::string& outString, ui32_t max_size = 8 * Megabyte);
 
   // Writes a string to a file, overwrites the existing file if present.
-  Result_t WriteStringIntoFile(const char* filename, const std::string& inString);
+  Result_t WriteStringIntoFile(const std::string& filename, const std::string& inString);
 
   // Instant IO for archivable objects
   //
@@ -258,6 +300,16 @@ namespace Kumu
   Result_t WriteBufferIntoFile(const Kumu::ByteString& Buffer, const std::string& Filename);
 
 
+#ifdef KM_WIN32_UTF8
+  //------------------------------------------------------------------------------------------
+  // wide char support for win32 file I/O
+  //------------------------------------------------------------------------------------------
+
+  //
+  Result_t wbstr_to_utf8(const Kumu::ByteString& in, std::string& out);
+  Result_t utf8_to_wbstr(const std::string& in, Kumu::ByteString& out);
+#endif
+
   //------------------------------------------------------------------------------------------
   // File I/O
   //------------------------------------------------------------------------------------------
@@ -275,7 +327,7 @@ namespace Kumu
       FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
       virtual ~FileReader() { Close(); }
 
-      Result_t OpenRead(const char*) const;                          // open the file for reading
+      Result_t OpenRead(const std::string&) const;                          // open the file for reading
       Result_t Close() const;                                        // close the file
       fsize_t  Size() const;                                         // returns the file's current size
       Result_t Seek(Kumu::fpos_t = 0, SeekPos_t = SP_BEGIN) const;   // move the file pointer
@@ -305,8 +357,8 @@ namespace Kumu
       FileWriter();
       virtual ~FileWriter();
 
-      Result_t OpenWrite(const char*);                               // open a new file, overwrites existing
-      Result_t OpenModify(const char*);                              // open a file for read/write
+      Result_t OpenWrite(const std::string&);                               // open a new file, overwrites existing
+      Result_t OpenModify(const std::string&);                              // open a file for read/write
 
       // this part of the interface takes advantage of the iovec structure on
       // platforms that support it. For each call to Writev(const byte_t*, ui32_t, ui32_t*),