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/gfilutl2.cpp

317 lines
8.6 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) 1990-1999 Odinn Sorensen
// 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$
// ------------------------------------------------------------------
// File utility functions
// ------------------------------------------------------------------
#include <gfilutil.h>
#ifndef __HAVE_DRIVES__
#include <pwd.h>
#endif
#ifdef __WIN32__
#include <windows.h>
#endif
2009-04-19 20:49:49 +00:00
#include <stdlib.h>
#include <string.h>
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
2009-11-29 12:20:28 +00:00
// Adds the directory-delimiter character into end of string ('\\' in DOS-based, '/' in unix-based OS)
// Replace wrong directory-delimiter character with good.
2000-02-25 10:15:17 +00:00
std::string& AddBackslash(std::string& p) {
2000-02-25 10:15:17 +00:00
for(size_t posn = 0; (posn=p.find(GOLD_WRONG_SLASH_CHR, posn)) != p.npos; posn++)
2000-10-20 11:14:13 +00:00
p[posn] = GOLD_SLASH_CHR;
2000-02-25 10:15:17 +00:00
if(p[p.length()-1] != GOLD_SLASH_CHR)
p += GOLD_SLASH_STR;
return p;
}
// ------------------------------------------------------------------
// Remove one trailing directory-delimiter character ('\\' in DOS-based, '/' in unix-based OS)
std::string& StripBackslash(std::string& p) {
std::string::iterator pend = p.end();
if(isslash(*pend))
p.erase(pend);
return p;
}
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
// Get size of file. Return values:
// positive number or zero - size of file,
// negative number - error (and error code is stored in errno)
long GetFilesize(const std::string& filename) {
if(filename.empty()) {
errno = EINVAL;
return -1;
}
struct stat info;
if(stat(filename.c_str(), &info) == 0)
return info.st_size;
return -1;
}
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
2009-11-29 12:20:28 +00:00
// Add path to filename, if no path is set. Don't chech size of 'path', be careful!
2000-02-25 10:15:17 +00:00
void MakePathname(std::string& pathname, const std::string& path, const std::string& name) {
2000-02-25 10:15:17 +00:00
Path pn;
MakePathname(pn, path.c_str(), name.c_str());
pathname = pn;
}
// ------------------------------------------------------------------
2009-11-29 12:20:28 +00:00
// Copy pathname with enviroment variables substitution and adds directory delimiter char.
// Copy not more sizeof(Path) characters (__dst should be type "Path" or equvalence, size is GMAXPATH)
2000-02-25 10:15:17 +00:00
void PathCopy(std::string& dst, const char* src) {
2000-02-25 10:15:17 +00:00
dst = src;
2000-10-20 11:14:13 +00:00
strschg_environ(dst);
2000-02-25 10:15:17 +00:00
AddBackslash(dst);
}
2011-02-17 23:00:49 +00:00
void PathCopy(std::string& dst, const std::string& src) {
dst = src;
strschg_environ(dst);
AddBackslash(dst);
}
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
2009-11-29 12:20:28 +00:00
// DOS-style enviroment variables substitution in string.
2011-02-17 23:00:49 +00:00
// In unix-like enviroment also tilde substitution in string.
2000-02-25 10:15:17 +00:00
int strschg_environ(std::string& s) {
2000-02-25 10:15:17 +00:00
std::string fnd;
2000-02-25 10:15:17 +00:00
int replaced = 0;
size_t posn, posn1;
2011-02-17 22:05:27 +00:00
if (s.length() > 1) {
while(((posn=s.find('%')) != s.npos) and ((posn1=s.find('%', posn+1)) != s.npos)) {
fnd = s.substr(posn+1, posn1-1-posn);
const char* rep = getenv(fnd.c_str());
rep = rep ? rep : "";
s.replace(posn, posn1-posn+1, rep);
replaced++;
}
2000-02-25 10:15:17 +00:00
}
#ifndef __HAVE_DRIVES__
if( s[0] == '~' ) {
2005-11-06 11:50:08 +00:00
struct passwd *pe=NULL;
2011-02-17 22:05:27 +00:00
size_t slash = 1;
2005-11-06 11:50:08 +00:00
2011-02-17 22:05:27 +00:00
if( (s.length() > 1) and not isslash(s[1]) ) {
slash = s.find_first_of(GOLD_SLASH_STR GOLD_WRONG_SLASH_STR, 1);
fnd.assign(s,1,slash-1);
pe = getpwnam(fnd.c_str());
}else {
2005-11-06 11:50:08 +00:00
pe = getpwuid(getuid());
2001-11-26 15:28:39 +00:00
}
2000-02-25 10:15:17 +00:00
if(pe != NULL) {
fnd.assign(pe->pw_dir);
if ( slash != std::string::npos )
fnd += s.substr(slash);
s=fnd;
2011-02-17 22:05:27 +00:00
replaced++;
2000-02-25 10:15:17 +00:00
}
}
#endif
return replaced;
}
// ------------------------------------------------------------------
2009-11-29 12:20:28 +00:00
// Change dirname to real full pathname
2000-02-25 10:15:17 +00:00
bool maketruepath(std::string &dirname) {
2000-02-25 10:15:17 +00:00
bool ok = true;
std::string ndirname;
2000-02-25 10:15:17 +00:00
char cwd[GMAXPATH];
getcwd(cwd, GMAXPATH);
#ifdef __HAVE_DRIVES__
#ifdef __WIN32__
char expanded[GMAXPATH];
char *k;
if(GetFullPathName(dirname.c_str(), GMAXPATH, expanded, &k) == 0) {
dirname = cwd;
ok = false;
}
else {
if(access(expanded, R_OK) == 0)
dirname = expanded;
else {
dirname = cwd;
ok = false;
}
}
#elif defined __OS2__
char expanded[GMAXPATH];
if(_fullpath(expanded, dirname.c_str(), GMAXPATH) == 0) {
if(access(expanded, R_OK) == 0)
dirname = expanded;
else {
dirname = cwd;
ok = false;
}
}
else {
dirname = cwd;
ok = false;
}
#elif defined __DJGPP__
char expanded[GMAXPATH];
_fixpath(dirname.c_str(), expanded);
if(access(expanded, R_OK) == 0)
dirname = expanded;
else {
dirname = cwd;
ok = false;
2005-11-06 11:50:08 +00:00
}
#else
2000-02-25 10:15:17 +00:00
long inspos = -1;
2002-11-04 18:59:51 +00:00
if((dirname.length() == 2) and (dirname[1] == ':'))
2000-02-25 10:15:17 +00:00
inspos = 2;
2002-11-04 18:59:51 +00:00
else if((dirname.length() < 2) or (dirname[1] != ':') or isslash(dirname[0]))
2000-02-25 10:15:17 +00:00
inspos = 0;
2002-11-04 18:59:51 +00:00
else if(not isslash(dirname[2]))
2000-02-25 10:15:17 +00:00
inspos = 2;
if(inspos != -1) {
char chdrive[] = " :";
chdrive[0] = (inspos == 2) ? dirname[0] : cwd[0];
2002-11-04 18:59:51 +00:00
if(not chdir(chdrive)) {
2000-02-25 10:15:17 +00:00
char dwd[GMAXPATH];
getcwd(dwd, GMAXPATH);
if(isslash(dirname[0]))
ndirname = chdrive;
else {
ndirname = dwd;
ndirname += GOLD_SLASH_CHR;
2000-02-25 10:15:17 +00:00
}
ndirname += dirname.c_str() + inspos;
dirname = ndirname;
} else {
dirname = cwd;
ok = false;
}
chdir(cwd);
}
#endif
2000-02-25 10:15:17 +00:00
#else
2002-11-04 18:59:51 +00:00
if(not dirname.empty() and (dirname[0] == '~')) {
2001-11-26 15:28:39 +00:00
char* lname;
2000-02-25 10:15:17 +00:00
const char *p = dirname.c_str()+1;
2002-11-04 18:59:51 +00:00
if((dirname.length() != 1) and not isslash(*p)) {
while(*p and not isslash(*p))
2000-02-25 10:15:17 +00:00
ndirname += *p++; // get user name
2001-11-26 15:28:39 +00:00
} else {
if ((lname = getlogin()) == NULL)
lname = getenv("LOGNAME");
ndirname = lname; // get current user name
}
2000-02-25 10:15:17 +00:00
struct passwd *pe = getpwnam(ndirname.c_str()); // get home
if(pe != NULL) {
ndirname = pe->pw_dir;
ndirname += GOLD_SLASH_CHR;
2000-02-25 10:15:17 +00:00
if(isslash(*p))
++p;
ndirname += p;
dirname = ndirname;
} else {
dirname = cwd;
ok = false;
}
2002-11-04 18:59:51 +00:00
} else if(not dirname.empty() and not isslash(dirname[0])) {
2000-02-25 10:15:17 +00:00
ndirname = cwd;
ndirname += GOLD_SLASH_CHR;
2000-02-25 10:15:17 +00:00
ndirname += dirname;
dirname = ndirname;
}
#endif
size_t posn;
for(posn = 0; (posn=dirname.find(GOLD_WRONG_SLASH_CHR, posn)) != dirname.npos; posn++)
dirname[posn] = GOLD_SLASH_CHR;
2000-02-25 10:15:17 +00:00
size_t skipfrom, skipto;
#ifdef __HAVE_DRIVES__ /* Prevent from destroying UNC names */
if(dirname.length() > 1) {
while((skipfrom=dirname.find("\\\\", 1)) != dirname.npos)
dirname.erase(skipfrom, 1);
}
#endif
2000-02-25 10:15:17 +00:00
size_t len = dirname.length();
#ifdef __HAVE_DRIVES__
2002-11-04 18:59:51 +00:00
while((len > 3) and isslash(dirname[--len]))
2000-02-25 10:15:17 +00:00
#else
2002-11-04 18:59:51 +00:00
while((len > 1) and isslash(dirname[--len]))
2000-02-25 10:15:17 +00:00
#endif
dirname.erase(len, 1);
if(access(dirname.c_str(), R_OK)) {
dirname = cwd;
ok = false;
2000-10-20 11:14:13 +00:00
}
#ifdef __HAVE_DRIVES__
if((dirname.length() > 2) and (dirname[0] == '\\') and (dirname[1] == '\\'))
posn = 2;
else
#endif
posn = 0;
for(; (posn=dirname.find('\\', posn)) != dirname.npos; posn++)
2000-02-25 10:15:17 +00:00
dirname[posn] = '/';
while((skipto=dirname.find("/../")) != dirname.npos) {
skipfrom = (skipto == 0) ? 0 : dirname.rfind('/', skipto-1);
skipto += 2;
dirname.erase(skipfrom, skipto-skipfrom+1);
}
while((skipfrom=dirname.find("/./")) != dirname.npos)
dirname.erase(skipfrom, 2);
len = dirname.length();
2002-11-04 18:59:51 +00:00
if(len > 2 and not strcmp(&(dirname.c_str()[len-2]), "/."))
2000-02-25 10:15:17 +00:00
dirname.erase(len-2, 2);
len = dirname.length();
#ifdef __HAVE_DRIVES__
2002-11-04 18:59:51 +00:00
if((len > 3) and isslash(dirname[--len]))
2000-02-25 10:15:17 +00:00
#else
2002-11-04 18:59:51 +00:00
if((len > 1) and isslash(dirname[--len]))
2000-02-25 10:15:17 +00:00
#endif
dirname.erase(len, 1);
return ok;
}
// ------------------------------------------------------------------