From e076f88b612604c28e591b787bd2d25edc2e5e7c Mon Sep 17 00:00:00 2001
From: Julien Malik <julien.malik@c-s.fr>
Date: Thu, 21 Jun 2012 20:12:43 +0200
Subject: [PATCH] ENH: add append mode support for OGRDataSource

---
 .../OGRAdapters/otbOGRDataSourceWrapper.cxx   | 25 +++++++++++++++----
 .../OGRAdapters/otbOGRDataSourceWrapper.h     |  2 +-
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx
index daf8320766..ef7553b7d2 100644
--- a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx
+++ b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx
@@ -160,13 +160,14 @@ otb::ogr::DataSource::New(std::string const& filename, Modes::type mode)
 {
   Drivers::Init();
 
-  const bool update = mode & Modes::write;
+  const bool write = mode & Modes::write;
   // std::cout << "Opening datasource " << filename << " update=" << update << "\n";
   if (itksys::SystemTools::FileExists(filename.c_str()))
     {
-    if (!update)
+    if (mode & Modes::read)
       {
-      OGRDataSource * source = OGRSFDriverRegistrar::Open(filename.c_str(), update);
+      // Open in read mode
+      OGRDataSource * source = OGRSFDriverRegistrar::Open(filename.c_str(), FALSE);
       if (!source)
         {
         itkGenericExceptionMacro(<< "Failed to open OGRDataSource file "
@@ -176,7 +177,20 @@ otb::ogr::DataSource::New(std::string const& filename, Modes::type mode)
       res->UnRegister();
       return res;
       }
-    else
+    else if (mode & Modes::append)
+      {
+      // Open in "update" mode
+      OGRDataSource * source = OGRSFDriverRegistrar::Open(filename.c_str(), TRUE);
+      if (!source)
+        {
+        itkGenericExceptionMacro(<< "Failed to open OGRDataSource file "
+          << filename<<": " << CPLGetLastErrorMsg());
+        }
+      Pointer res = new DataSource(source);
+      res->UnRegister();
+      return res;
+      }
+    else if (mode & Modes::write)
       {
       // Attempt to delete the datasource if it already exists
       OGRDataSource * poDS = OGRSFDriverRegistrar::Open(filename.c_str(), TRUE);
@@ -205,7 +219,8 @@ otb::ogr::DataSource::New(std::string const& filename, Modes::type mode)
     } // if (itksys::SystemTools::FileExists(filename.c_str()))
   else
     {
-    if (! update)
+    // File does not exists
+    if (mode & Modes::read || mode & Modes::append)
       {
       itkGenericExceptionMacro(<< "No DataSource named <"<<filename<<"> exists,"
         " and the file opening mode does not permit updates. DataSource creation is thus aborted.");
diff --git a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h
index 8f2712e02c..19146ffb52 100644
--- a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h
+++ b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h
@@ -94,7 +94,7 @@ public:
    * \note Read/Write mode should have been <tt>read | write</tt>, but actually
    * OGR data source are always at least in read mode.
    */
-  struct Modes { enum type { invalid, read=1, write=2, MAX__ }; };
+  struct Modes { enum type { invalid, read=1, write=2, append=4, MAX__ }; };
 
   /**
    * Builder from an existing named data source.
-- 
GitLab