← Back to team overview

dolfin team mailing list archive

Re: [Branch ~dolfin-core/dolfin/main] Rev 4998: Fix bug in File.cpp when creating directories.

 

On Tue, 2010-08-17 at 12:12 +0200, Anders Logg wrote:
> On Tue, Aug 17, 2010 at 09:46:22AM -0000, noreply@xxxxxxxxxxxxx wrote:
> 
> >  File::File(const std::string filename, std::string encoding)
> >  {
> > -  // Get directory and extension
> > +  // Get file path and extension
> >    const boost::filesystem::path path(filename);
> > -  const boost::filesystem::path directory = path.parent_path();
> > -  const std::string extension = path.extension();
> > +  const std::string extension = boost::filesystem::extension(path);
> 
> Is there a difference between the member function and the free
> function?
> 
> > -  // Create directory if necessary
> > -  if (!is_directory(directory))
> > +  // Create directory if we have a parent path
> > +  if ( path.has_parent_path() )
> >    {
> > +    const boost::filesystem::path directory = path.parent_path();
> >      cout << "Creating directory \"" << directory.string() << "\"." << endl;
> >      boost::filesystem::create_directories(directory);
> >    }
> 
> What was the problem here?
> 

Seg fault.

  if (!is_directory(directory))

seg faults if there is no directory.

This is why we need to get the buildbots green again ;)

Garth

> --
> Anders
> 
> 
> > @@ -49,24 +49,24 @@
> >      // Get suffix after discarding .gz
> >      const std::string ext = boost::filesystem::extension(boost::filesystem::basename(path));
> >      if (ext == ".xml")
> > -      file = new XMLFile(filename, true);
> > +      file.reset(new XMLFile(filename, true));
> >      else
> >        error("Unknown file type for \"%s\".", filename.c_str());
> >    }
> >    else if (extension == ".xml")
> > -    file = new XMLFile(filename, false);
> > +    file.reset(new XMLFile(filename, false));
> >    else if (extension == ".m")
> > -    file = new OctaveFile(filename);
> > +    file.reset(new OctaveFile(filename));
> >    else if (extension == ".py")
> > -    file = new PythonFile(filename);
> > +    file.reset(new PythonFile(filename));
> >    else if (extension == ".pvd")
> > -    file = new VTKFile(filename, encoding);
> > +    file.reset(new VTKFile(filename, encoding));
> >    else if (extension == ".raw")
> > -    file = new RAWFile(filename);
> > +    file.reset(new RAWFile(filename));
> >    else if (extension == ".xyz")
> > -    file = new XYZFile(filename);
> > +    file.reset(new XYZFile(filename));
> >    else if (extension == ".bin")
> > -    file = new BinaryFile(filename);
> > +    file.reset(new BinaryFile(filename));
> >    else
> >      error("Unknown file type for \"%s\".", filename.c_str());
> >  }
> > @@ -76,44 +76,42 @@
> >    switch (type)
> >    {
> >    case xml:
> > -    file = new XMLFile(filename, false);
> > +    file.reset(new XMLFile(filename, false));
> >      break;
> >    case matlab:
> > -    file = new MatlabFile(filename);
> > +    file.reset(new MatlabFile(filename));
> >      break;
> >    case octave:
> > -    file = new OctaveFile(filename);
> > +    file.reset(new OctaveFile(filename));
> >      break;
> >    case python:
> > -    file = new PythonFile(filename);
> > +    file.reset(new PythonFile(filename));
> >      break;
> >    case vtk:
> > -    file = new VTKFile(filename, encoding);
> > +    file.reset(new VTKFile(filename, encoding));
> >      break;
> >    case raw:
> > -    file = new RAWFile(filename);
> > +    file.reset(new RAWFile(filename));
> >      break;
> >    case xyz:
> > -    file = new XYZFile(filename);
> > +    file.reset(new XYZFile(filename));
> >      break;
> >    case binary:
> > -    file = new BinaryFile(filename);
> > +    file.reset(new BinaryFile(filename));
> >      break;
> >    default:
> > -    file = 0;
> >      error("Unknown file type for \"%s\".", filename.c_str());
> >    }
> >  }
> >  //-----------------------------------------------------------------------------
> >  File::File(std::ostream& outstream)
> >  {
> > -  file = new XMLFile(outstream);
> > +  file.reset(new XMLFile(outstream));
> >  }
> >  //-----------------------------------------------------------------------------
> >  File::~File()
> >  {
> > -  delete file;
> > -  file = 0;
> > +  // Do nothing
> >  }
> >  //-----------------------------------------------------------------------------
> >  void File::operator<<(const Function& u)
> > @@ -135,7 +133,10 @@
> >    std::ifstream file(filename.c_str());
> >    if (!file.is_open())
> >      return false;
> > -  file.close();
> > -  return true;
> > +  else
> > +  {
> > +    file.close();
> > +    return true;
> > +  }
> >  }
> >  //-----------------------------------------------------------------------------
> >
> > === modified file 'dolfin/io/File.h'
> > --- dolfin/io/File.h	2010-06-02 11:31:31 +0000
> > +++ dolfin/io/File.h	2010-08-17 09:43:44 +0000
> > @@ -15,6 +15,7 @@
> >  #include <ostream>
> >  #include <string>
> >  #include <utility>
> > +#include <boost/scoped_ptr.hpp>
> >  #include "GenericFile.h"
> >
> >  namespace dolfin
> > @@ -72,7 +73,7 @@
> >    private:
> >
> >      // Pointer to implementation (envelop-letter design)
> > -    GenericFile* file;
> > +    boost::scoped_ptr<GenericFile> file;
> >
> >    };
> >
> >
> 
> _______________________________________________
> Mailing list: https://launchpad.net/~dolfin
> Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dolfin
> More help   : https://help.launchpad.net/ListHelp





Follow ups

References