This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gdirposx.cpp

179 lines
4.3 KiB
C++
Raw Normal View History

2000-02-25 10:15:17 +00:00
// This may look like C code, but it is really -*- C++ -*-
// ------------------------------------------------------------------
// The Goldware Library
// Copyright (C) 1999-2000 Alexander S. Aganichev
// ------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307, USA
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// Directory maintenance class.
// ------------------------------------------------------------------
#include <gdirposx.h>
#include <gstrall.h>
#include <gwildmat.h>
2002-09-24 14:58:44 +00:00
#ifndef _MSC_VER
2000-02-25 10:15:17 +00:00
#include <dirent.h>
2002-09-24 14:58:44 +00:00
#else
#include <io.h>
#endif
2000-02-25 10:15:17 +00:00
#ifndef __HAVE_DRIVES__
#include <pwd.h>
#endif
// ------------------------------------------------------------------
gdirentry::gdirentry() : name(), dirname(NULL)
{
}
// ------------------------------------------------------------------
gdirentry::~gdirentry()
{
}
// ------------------------------------------------------------------
gposixdir::gposixdir() : dirname(), ret(), entries()
{
ok = false;
}
// ------------------------------------------------------------------
gposixdir::gposixdir(const char *name) : dirname(), ret(), entries()
{
cd(name);
}
// ------------------------------------------------------------------
gposixdir::~gposixdir()
{
}
// ------------------------------------------------------------------
2002-09-24 14:58:44 +00:00
#ifndef _MSC_VER
2000-02-25 10:15:17 +00:00
void gposixdir::cd(const char *name, bool relative)
{
std::string ndirname;
2002-11-04 18:59:51 +00:00
if(not *name)
2000-02-25 10:15:17 +00:00
name = ".";
if(relative) {
dirname += "/";
dirname += name;
} else
dirname = name;
ok = maketruepath(dirname);
entries.clear();
DIR *d = opendir(dirname.c_str());
if(d == NULL)
ok = false;
else {
struct dirent *de;
while((de=readdir(d)) != NULL) {
ndirname = de->d_name;
#ifdef __HAVE_DRIVES__
2002-11-04 18:59:51 +00:00
if((ndirname != ".") and not ((ndirname == "..") and streql(dirname.c_str()+1, ":/")))
2000-02-25 10:15:17 +00:00
#else
2002-11-04 18:59:51 +00:00
if((ndirname != ".") and not ((ndirname == "..") and (dirname == "/")))
2000-02-25 10:15:17 +00:00
#endif
entries.push_back(ndirname);
}
closedir(d);
rewind();
}
}
2002-09-24 14:58:44 +00:00
#else
void gposixdir::cd(const char *name, bool relative)
{
std::string ndirname;
2002-11-04 18:59:51 +00:00
if(not *name)
2002-09-24 14:58:44 +00:00
name = ".";
if(relative) {
dirname += "/";
dirname += name;
} else
dirname = name;
ok = maketruepath(dirname);
entries.clear();
ndirname = dirname;
if (ndirname[ndirname.size()-1] != '/')
ndirname += "/";
ndirname += "*";
2002-09-24 14:58:44 +00:00
struct _finddata_t de;
long d = _findfirst(ndirname.c_str(), &de);
if(d == -1) {
if(is_dir(dirname))
ok = true;
else
ok = false;
}
else {
do {
ndirname = de.name;
#ifdef __HAVE_DRIVES__
2002-11-04 18:59:51 +00:00
if((ndirname != ".") and not ((ndirname == "..") and streql(dirname.c_str()+1, ":/")))
2002-09-24 14:58:44 +00:00
#else
2002-11-04 18:59:51 +00:00
if((ndirname != ".") and not ((ndirname == "..") and (dirname == "/")))
2002-09-24 14:58:44 +00:00
#endif
entries.push_back(ndirname);
} while(_findnext(d, &de) == 0);
_findclose(d);
rewind();
}
}
#endif
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
const gdirentry *gposixdir::nextentry(const char *mask, bool nameonly)
{
while(last_entry < entries.size()) {
2002-11-04 18:59:51 +00:00
if(mask and not gwildmat(entries[last_entry].c_str(), mask)) {
2000-02-25 10:15:17 +00:00
++last_entry;
continue;
}
ret.name = entries[last_entry];
ret.dirname = dirname.c_str();
2002-11-04 18:59:51 +00:00
if(not nameonly) {
2001-12-17 15:44:55 +00:00
std::string pn = ret.dirname;
pn += "/";
pn += ret.name;
size_t skipfrom;
while((skipfrom=pn.find("//")) != pn.npos)
pn.erase(skipfrom, 1);
2000-02-25 10:15:17 +00:00
stat(pn.c_str(), &ret.stat_info);
2001-12-17 15:44:55 +00:00
}
2000-02-25 10:15:17 +00:00
++last_entry;
return &ret;
}
return NULL;
}
// ------------------------------------------------------------------