dir-ops.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <cerrno>
00028 #include <cstdlib>
00029 #include <cstring>
00030 
00031 #include <list>
00032 #include <string>
00033 
00034 #include "sysdir.h"
00035 
00036 #include "dir-ops.h"
00037 #include "file-ops.h"
00038 #include "lo-error.h"
00039 #include "lo-sysdep.h"
00040 #include "str-vec.h"
00041 
00042 bool
00043 dir_entry::open (const std::string& n)
00044 {
00045   fail = true;
00046 
00047   if (! n.empty ())
00048     name = n;
00049 
00050   if (! name.empty ())
00051     {
00052       close ();
00053 
00054       std::string fullname = file_ops::tilde_expand (name);
00055 
00056       dir = static_cast<void *> (gnulib::opendir (fullname.c_str ()));
00057 
00058       if (dir)
00059         fail = false;
00060       else
00061         errmsg = gnulib::strerror (errno);
00062     }
00063   else
00064     errmsg = "dir_entry::open: empty file name";
00065 
00066   return ! fail;
00067 }
00068 
00069 string_vector
00070 dir_entry::read (void)
00071 {
00072   string_vector retval;
00073 
00074   if (ok ())
00075     {
00076       std::list<std::string> dirlist;
00077 
00078       struct dirent *dir_ent;
00079 
00080       while ((dir_ent = gnulib::readdir (static_cast<DIR *> (dir))))
00081         {
00082           if (dir_ent)
00083             dirlist.push_back (dir_ent->d_name);
00084           else
00085             break;
00086         }
00087 
00088       retval = string_vector (dirlist);
00089     }
00090 
00091   return retval;
00092 }
00093 
00094 void
00095 dir_entry::close (void)
00096 {
00097   if (dir)
00098     gnulib::closedir (static_cast<DIR *> (dir));
00099 
00100   dir = 0;
00101 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines