/* CVSNT Generic API Copyright (C) 2004 Tony Hoyle and March-Hare Software Ltd This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Unix specific */ #include #include "../lib/api_system.h" #include #include #include #include #include "cvs_string.h" #include "FileAccess.h" CFileAccess::CFileAccess() { m_file = NULL; } CFileAccess::~CFileAccess() { close(); } bool CFileAccess::open(const char *filename, const char *mode) { m_file = fopen(filename,mode); if(!m_file) return false; return true; } bool CFileAccess::open(FILE *file) { if(m_file) return false; m_file = file; return true; } bool CFileAccess::close() { if(m_file) fclose(m_file); m_file = NULL; return true; } bool CFileAccess::isopen() { if(m_file) return true; return false; } bool CFileAccess::getline(cvs::string& line) { int c; if(!m_file) return false; line.reserve(256); line=""; while((c=fgetc(m_file))!=EOF) { if(c=='\n') break; line.append(1,(char)c); } if(c==EOF && line.empty()) return false; return true; } bool CFileAccess::getline(char *line, size_t length) { int c; int len = length; if(!m_file) return false; while(len && (c=fgetc(m_file))!=EOF) { if(c=='\n') break; *(line++)=(char)c; --len; } if(c==EOF && len==length) return false; return true; } bool CFileAccess::putline(const char *line) { if(!m_file) return false; if(fwrite(line,1,strlen(line),m_file)d_name,".") || !strcmp(dpp->d_name,"..")) continue; chdir(file); if(!remove(dpp->d_name,recursive)) return false; chdir(".."); } closedir(dp); if(rmdir(file)<0) return false; return true; } else { if(::remove(file)<0) return false; return true; } } bool CFileAccess::absolute(const char *file) { return file[0]=='/'; } int CFileAccess::uplevel(const char *file) { int level = 0; for(const char *p=file; *p;) { size_t l=strcspn(p,"/"); if(l==1 && *p=='.') level++; // Compensate for ./ else if(l==2 && *p=='.' && *(p+1)=='.') level+=2; // Compensate for ../ p+=l; if(*p) p++; level--; } return level; } bool CFileAccess::exists(const char *file) { struct stat st; if(stat(file,&st)) return false; return true; } CFileAccess::TypeEnum CFileAccess::type(const char *file) { struct stat st; if(stat(file,&st)) return typeNone; if(S_ISLNK(st.st_mode)) return typeSymlink; if(S_ISDIR(st.st_mode)) return typeDirectory; if(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) return typeDevice; if(S_ISREG(st.st_mode)) return typeFile; return typeOther; } cvs::string CFileAccess::mimetype(const char *filename) { return ""; // TODO: Implement for unix. Read from /etc/mime.types works on Linux.. // don't know if there's even a standard for other platforms. }