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

184 lines
4.8 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;
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
gposixdir::gposixdir(const char *name) : dirname(), ret(), entries()
{
cd(name);
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
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;
if(not *name)
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;
2000-02-25 10:15:17 +00:00
#ifdef __HAVE_DRIVES__
if((ndirname != ".") and not ((ndirname == "..") and streql(dirname.c_str()+1, ":/")))
2000-02-25 10:15:17 +00:00
#else
if((ndirname != ".") and not ((ndirname == "..") and (dirname == "/")))
2000-02-25 10:15:17 +00:00
#endif
entries.push_back(ndirname);
}
closedir(d);
rewind();
}
2000-02-25 10:15:17 +00:00
}
2002-09-24 14:58:44 +00:00
#else
void gposixdir::cd(const char *name, bool relative)
{
std::string ndirname;
if(not *name)
name = ".";
if(relative) {
dirname += "/";
dirname += name;
} else
dirname = name;
ok = maketruepath(dirname);
entries.clear();
ndirname = dirname;
if ((ndirname.end() > ndirname.begin()) && (*(ndirname.end()-1) == '/'))
ndirname += "*";
else
ndirname += "/*";
struct _finddata_t de;
#if defined(_MSC_VER) && _MSC_VER>1800
intptr_t d = _findfirst(ndirname.c_str(), &de);
#else
long d = _findfirst(ndirname.c_str(), &de);
#endif
if(d == -1) {
if(is_dir(dirname))
ok = true;
else
ok = false;
}
else {
do {
ndirname = de.name;
2002-09-24 14:58:44 +00:00
#ifdef __HAVE_DRIVES__
if((ndirname != ".") and not ((ndirname == "..") and streql(dirname.c_str()+1, ":/")))
2002-09-24 14:58:44 +00:00
#else
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();
}
2002-09-24 14:58:44 +00:00
}
#endif
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
const gdirentry *gposixdir::nextentry(const char *mask, bool nameonly)
{
while(last_entry < entries.size()) {
if(mask and not gwildmat(entries[last_entry].c_str(), mask)) {
++last_entry;
continue;
}
ret.name = entries[last_entry];
ret.dirname = dirname.c_str();
if(not nameonly) {
std::string pn = ret.dirname;
pn += "/";
pn += ret.name;
size_t skipfrom;
while((skipfrom=pn.find("//")) != pn.npos)
pn.erase(skipfrom, 1);
stat(pn.c_str(), &ret.stat_info);
}
++last_entry;
return &ret;
}
return NULL;
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------