]> granicus.if.org Git - taglib/commitdiff
Add support for file descriptor to FileStream (#832)
authorsafu9 <30684866+safu9@users.noreply.github.com>
Wed, 10 Oct 2018 16:25:02 +0000 (01:25 +0900)
committerStephen F. Booth <me@sbooth.org>
Wed, 10 Oct 2018 16:25:02 +0000 (12:25 -0400)
Add support for file descriptor

taglib/toolkit/tfilestream.cpp
taglib/toolkit/tfilestream.h

index 17a09f3daaac582ad9f5e963cecace68b06315c4..7e75e396a1c2902d151d1eeec87e0d236e42907c 100644 (file)
@@ -58,6 +58,11 @@ namespace
 #endif
   }
 
+  FileHandle openFile(const int fileDescriptor, bool readOnly)
+  {
+    return InvalidFileHandle;
+  }
+
   void closeFile(FileHandle file)
   {
     CloseHandle(file);
@@ -98,6 +103,11 @@ namespace
     return fopen(path, readOnly ? "rb" : "rb+");
   }
 
+  FileHandle openFile(const int fileDescriptor, bool readOnly)
+  {
+    return fdopen(fileDescriptor, readOnly ? "rb" : "rb+");
+  }
+
   void closeFile(FileHandle file)
   {
     fclose(file);
@@ -158,6 +168,25 @@ FileStream::FileStream(FileName fileName, bool openReadOnly)
   }
 }
 
+FileStream::FileStream(int fileDescriptor, bool openReadOnly)
+  : d(new FileStreamPrivate(""))
+{
+  // First try with read / write mode, if that fails, fall back to read only.
+
+  if(!openReadOnly)
+    d->file = openFile(fileDescriptor, false);
+
+  if(d->file != InvalidFileHandle)
+    d->readOnly = false;
+  else
+    d->file = openFile(fileDescriptor, true);
+
+  if(d->file == InvalidFileHandle)
+  {
+    debug("Could not open file using file descriptor");
+  }
+}
+
 FileStream::~FileStream()
 {
   if(isOpen())
index 96a476d65c65afa8523e8ae576660b790c39c1e2..aa4d71b305be56357b5f1f543b513374f42e903e 100644 (file)
@@ -54,6 +54,11 @@ namespace TagLib {
      */
     FileStream(FileName file, bool openReadOnly = false);
 
+    /*!
+     * Construct a File object and opens the \a file using file descriptor.
+     */
+    FileStream(int fileDescriptor, bool openReadOnly = false);
+
     /*!
      * Destroys this FileStream instance.
      */