⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutil.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: fileutil.cpp,v $ * PRODUCTION Revision 1000.1  2004/06/01 19:43:09  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.28 * PRODUCTION * =========================================================================== *//*  $Id: fileutil.cpp,v 1000.1 2004/06/01 19:43:09 gouriano Exp $* ===========================================================================**                            PUBLIC DOMAIN NOTICE*               National Center for Biotechnology Information**  This software/database is a "United States Government Work" under the*  terms of the United States Copyright Act.  It was written as part of*  the author's official duties as a United States Government employee and*  thus cannot be copyrighted.  This software/database is freely available*  to the public for use. The National Library of Medicine and the U.S.*  Government have not placed any restriction on its use or reproduction.**  Although all reasonable efforts have been taken to ensure the accuracy*  and reliability of the software and data, the NLM and the U.S.*  Government do not and cannot warrant the performance or results that*  may be obtained by using this software or data. The NLM and the U.S.*  Government disclaim all warranties, express or implied, including*  warranties of performance, merchantability or fitness for any particular*  purpose.**  Please cite the author in any work or product based on this material.** ===========================================================================** Author: Eugene Vasilchenko** File Description:*   Some file utilities functions/classes.*/#include <ncbi_pch.hpp>#include <corelib/ncbistre.hpp>#include <corelib/ncbiutil.hpp>#include <corelib/ncbifile.hpp>#include <serial/datatool/fileutil.hpp>#include <serial/datatool/srcutil.hpp>#include <set>BEGIN_NCBI_SCOPEstatic const int BUFFER_SIZE = 4096;SourceFile::SourceFile(const string& name, bool binary)    : m_StreamPtr(0), m_Open(false){    if ( name == "stdin" || name == "-" ) {        m_StreamPtr = &NcbiCin;    }    else {        if ( !x_Open(name, binary) )            ERR_POST(Fatal << "cannot open file " << name);    }}SourceFile::SourceFile(const string& name, const list<string>& dirs,                       bool binary){    if ( name == "stdin" || name == "-" ) {        m_StreamPtr = &NcbiCin;    } else if ( !x_Open(name, binary) ) {        ITERATE(list<string>, dir, dirs) {            if ( x_Open(Path(*dir, name), binary) ) {                return;            }        }        ERR_POST(Fatal << "cannot open file " << name);    }}SourceFile::~SourceFile(void){    if ( m_Open ) {        delete m_StreamPtr;        m_StreamPtr = 0;        m_Open = false;    }}SourceFile::EType SourceFile::GetType(void) const{    CDirEntry entry(m_Name);    string ext(entry.GetExt());    if (NStr::CompareNocase(ext,".asn") == 0) {        return eASN;    } else if (NStr::CompareNocase(ext,".dtd") == 0) {        return eDTD;    }    return eUnknown;}bool SourceFile::x_Open(const string& name, bool binary){    m_Name = name;    m_StreamPtr = new CNcbiIfstream(name.c_str(),                                    binary?                                        IOS_BASE::in | IOS_BASE::binary:                                        IOS_BASE::in);    m_Open = m_StreamPtr->good();    if ( !m_Open ) {        delete m_StreamPtr;        m_StreamPtr = 0;    }    return m_Open;}DestinationFile::DestinationFile(const string& name, bool binary){    if ( name == "stdout" || name == "-" ) {        m_StreamPtr = &NcbiCout;        m_Open = false;    }    else {        m_StreamPtr = new CNcbiOfstream(name.c_str(),                                        binary?                                            IOS_BASE::out | IOS_BASE::binary:                                            IOS_BASE::out);        if ( !*m_StreamPtr ) {            delete m_StreamPtr;            m_StreamPtr = 0;            ERR_POST(Fatal << "cannot open file " << name);        }        m_Open = true;    }}DestinationFile::~DestinationFile(void){    if ( m_Open ) {        delete m_StreamPtr;    }}// default parameters#undef DIR_SEPARATOR_CHAR#undef DIR_SEPARATOR_CHAR2#undef DISK_SEPARATOR_CHAR#undef ALL_SEPARATOR_CHARS#define PARENT_DIR ".."#ifdef NCBI_OS_MSWIN#  define DIR_SEPARATOR_CHAR '\\'#  define DIR_SEPARATOR_CHAR2 '/'#  define DISK_SEPARATOR_CHAR ':'#  define ALL_SEPARATOR_CHARS ":/\\"#endif#ifdef NCBI_OS_MAC#  define DIR_SEPARATOR_CHAR ':'#  undef PARENT_DIR#endif#ifndef DIR_SEPARATOR_CHAR#  define DIR_SEPARATOR_CHAR '/'#endif#ifndef ALL_SEPARATOR_CHARS#  define ALL_SEPARATOR_CHARS DIR_SEPARATOR_CHAR#endifinlinebool IsDiskSeparator(char c){#ifdef DISK_SEPARATOR_CHAR    if ( c == DISK_SEPARATOR_CHAR )        return true;#endif    return false;}inlinebool IsDirSeparator(char c){#ifdef DISK_SEPARATOR_CHAR    if ( c == DISK_SEPARATOR_CHAR )        return true;#endif#ifdef DIR_SEPARATOR_CHAR2    if ( c == DIR_SEPARATOR_CHAR2 )        return true;#endif    return c == DIR_SEPARATOR_CHAR;}bool IsLocalPath(const string& path){    // determine if path is local to current directory    // exclude pathes like:    // "../xxx" everywhere    // "xxx/../yyy" everywhere    // "/xxx/yyy"  on unix    // "d:xxx" on windows    // "HD:folder" on Mac    if ( path.empty() )        return false;#ifdef NCBI_OS_MAC    if (path.find(DIR_SEPARATOR_CHAR) != NPOS  &&        path[0] != DIR_SEPARATOR_CHAR)        return false;#else    if ( IsDirSeparator(path[0]) )        return false;#endif    SIZE_TYPE pos;#ifdef PARENT_DIR    SIZE_TYPE parentDirLength = strlen(PARENT_DIR);    pos = 0;    while ( (pos = path.find(PARENT_DIR, pos)) != NPOS ) {        if ( pos == 0 || IsDirSeparator(path[pos - 1]) )            return false;        SIZE_TYPE end = pos + parentDirLength;        if ( end == path.size() || IsDirSeparator(path[end]) )            return false;        pos = end + 1;    }#endif#ifdef DISK_SEPARATOR_CHAR    if ( path.find(DISK_SEPARATOR_CHAR) != NPOS )        return false;#endif    return true;}string Path(const string& dir, const string& file){    if ( dir.empty() )        return file;    char lastChar = dir[dir.size() - 1];    if ( file.empty() )        _TRACE("Path(\"" << dir << "\", \"" << file << "\")");    // Avoid duplicate dir separators    if ( IsDirSeparator(lastChar) ) {        if ( IsDirSeparator(file[0]) )            return dir.substr(0, dir.size()-1) + file;    }    else {        if ( !IsDirSeparator(file[0]) )            return dir + DIR_SEPARATOR_CHAR + file;    }    return dir + file;}string BaseName(const string& path){    SIZE_TYPE dirEnd = path.find_last_of(ALL_SEPARATOR_CHARS);    string name;    if ( dirEnd != NPOS )        name = path.substr(dirEnd + 1);    else        name = path;    SIZE_TYPE extStart = name.rfind('.');    if ( extStart != NPOS )        name = name.substr(0, extStart);    return name;}string DirName(const string& path){    SIZE_TYPE dirEnd = path.find_last_of(ALL_SEPARATOR_CHARS);    if ( dirEnd != NPOS ) {        if ( dirEnd == 0 /* "/" root directory */ ||             IsDiskSeparator(path[dirEnd]) /* disk separator */ )             ++dirEnd; // include separator        return path.substr(0, dirEnd);    }    else {        return NcbiEmptyString;    }}string GetStdPath(const string& path){    string stdpath = path;#ifdef NCBI_OS_MAC    // Exlude leading ':' on Mac    if ( IsDirSeparator(stdpath[0]) ) {        stdpath = path.substr(1);    }    else {        // FIXME:  How do we translate an absolute pathname?    }#endif    // Replace each native separator character with the 'standard' one.    SIZE_TYPE ibeg = NStr::StartsWith(path, "http://", NStr::eNocase) ? 7 : 0;    for (SIZE_TYPE i=ibeg ; i < stdpath.size(); i++) {        if ( IsDirSeparator(stdpath[i]) )            stdpath[i] = '/';    }    string tmp = NStr::Replace(stdpath,"//","/",ibeg);    stdpath = NStr::Replace(tmp,"/./","/",ibeg);    return stdpath;}class SSubString{public:    SSubString(const string& value, size_t order)        : value(value), order(order)        {        }    struct ByOrder {        bool operator()(const SSubString& s1, const SSubString& s2) const            {                return s1.order < s2.order;            }    };    struct ByLength {        bool operator()(const SSubString& s1, const SSubString& s2) const            {                if ( s1.value.size() > s2.value.size() )                    return true;                if ( s1.value.size() < s2.value.size() )                    return false;                return s1.order < s2.order;            }    };    string value;    size_t order;};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -