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

📄 ncbiargs.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbiargs.cpp,v $ * PRODUCTION Revision 1000.1  2004/06/01 19:08:48  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.48 * PRODUCTION * =========================================================================== *//*  $Id: ncbiargs.cpp,v 1000.1 2004/06/01 19:08:48 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. * * =========================================================================== * * Authors:  Denis Vakatov, Anton Butanayev * * File Description: *   Command-line arguments' processing: *      descriptions  -- CArgDescriptions,  CArgDesc *      parsed values -- CArgs,             CArgValue *      exceptions    -- CArgException, ARG_THROW() *      constraints   -- CArgAllow;  CArgAllow_{Strings,Integers,Doubles} * */#include <ncbi_pch.hpp>#include <corelib/ncbiargs.hpp>#include <corelib/ncbienv.hpp>#include <algorithm>BEGIN_NCBI_SCOPE////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  Include the private header//#define NCBIARGS__CPP#include "ncbiargs_p.hpp"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  Constants//static const string s_AutoHelp("h");static const string s_ExtraName("....");/////////////////////////////////////////////////////////////////////////////string s_ArgExptMsg(const string& name, const string& what, const string& attr){    return string("Argument \"") + (name.empty() ? s_ExtraName : name) +        "\". " + what + ":  `" + attr + "'";}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  CArg_***::   classes representing various types of argument value////    CArgValue////       CArg_NoValue     : CArgValue////       CArg_String      : CArgValue//          CArg_Integer     : CArg_String//          CArg_Double      : CArg_String//          CArg_Boolean     : CArg_String//          CArg_InputFile   : CArg_String//          CArg_OutputFile  : CArg_String//    /////////////////////////////////////////////////////////  CArgValue::CArgValue::CArgValue(const string& name)    : m_Name(name){    if ( !CArgDescriptions::VerifyName(m_Name, true) ) {        NCBI_THROW(CArgException,eInvalidArg,            "Invalid argument name: " + m_Name);    }}CArgValue::~CArgValue(void){    return;}/////////////////////////////////////////////////////////  Overload the comparison operator -- to handle "CRef<CArgValue>" elements//  in "CArgs::m_Args" stored as "set< CRef<CArgValue> >"//inline bool operator< (const CRef<CArgValue>& x, const CRef<CArgValue>& y){    return x->GetName() < y->GetName();}/////////////////////////////////////////////////////////  CArg_NoValue::inline CArg_NoValue::CArg_NoValue(const string& name)    : CArgValue(name){    return;}bool CArg_NoValue::HasValue(void) const{    return false;}#define THROW_CArg_NoValue \    NCBI_THROW(CArgException,eNoValue, s_ArgExptMsg(GetName(), \        "Optional argument must have a default value", "NULL"));const string& CArg_NoValue::AsString    (void) const { THROW_CArg_NoValue; }int           CArg_NoValue::AsInteger   (void) const { THROW_CArg_NoValue; }double        CArg_NoValue::AsDouble    (void) const { THROW_CArg_NoValue; }bool          CArg_NoValue::AsBoolean   (void) const { THROW_CArg_NoValue; }CNcbiIstream& CArg_NoValue::AsInputFile (void) const { THROW_CArg_NoValue; }CNcbiOstream& CArg_NoValue::AsOutputFile(void) const { THROW_CArg_NoValue; }void          CArg_NoValue::CloseFile   (void) const { THROW_CArg_NoValue; }/////////////////////////////////////////////////////////  CArg_String::inline CArg_String::CArg_String(const string& name, const string& value)    : CArgValue(name),      m_String(value){    return;}bool CArg_String::HasValue(void) const{    return true;}const string& CArg_String::AsString(void) const{    return m_String;}int CArg_String::AsInteger(void) const{ NCBI_THROW(CArgException,eWrongCast,s_ArgExptMsg(GetName(),    "Attempt to cast to a wrong (Integer) type", AsString()));}double CArg_String::AsDouble(void) const{ NCBI_THROW(CArgException,eWrongCast,s_ArgExptMsg(GetName(),    "Attempt to cast to a wrong (Double) type", AsString()));}bool CArg_String::AsBoolean(void) const{ NCBI_THROW(CArgException,eWrongCast,s_ArgExptMsg(GetName(),    "Attempt to cast to a wrong (Boolean) type", AsString()));}CNcbiIstream& CArg_String::AsInputFile(void) const{ NCBI_THROW(CArgException,eWrongCast,s_ArgExptMsg(GetName(),    "Attempt to cast to a wrong (InputFile) type", AsString()));}CNcbiOstream& CArg_String::AsOutputFile(void) const{ NCBI_THROW(CArgException,eWrongCast,s_ArgExptMsg(GetName(),    "Attempt to cast to a wrong (OutputFile) type", AsString()));}void CArg_String::CloseFile(void) const{ NCBI_THROW(CArgException,eWrongCast,s_ArgExptMsg(GetName(),    "Attempt to close an argument of non-file type", AsString()));}/////////////////////////////////////////////////////////  CArg_Integer::inline CArg_Integer::CArg_Integer(const string& name, const string& value)    : CArg_String(name, value){    try {        m_Integer = NStr::StringToInt(value);    } catch (CException& e) {        NCBI_RETHROW(e,CArgException,eConvert, s_ArgExptMsg(GetName(),            "Argument cannot be converted",value));    }}int CArg_Integer::AsInteger(void) const{    return m_Integer;}/////////////////////////////////////////////////////////  CArg_Double::inline CArg_Double::CArg_Double(const string& name, const string& value)    : CArg_String(name, value){    try {        m_Double = NStr::StringToDouble(value);    } catch (CException& e) {        NCBI_RETHROW(e,CArgException,eConvert,            s_ArgExptMsg(GetName(),"Argument cannot be converted",value));    }}double CArg_Double::AsDouble(void) const{    return m_Double;}/////////////////////////////////////////////////////////  CArg_Boolean::inline CArg_Boolean::CArg_Boolean(const string& name, bool value)    : CArg_String(name, NStr::BoolToString(value)){    m_Boolean = value;}inline CArg_Boolean::CArg_Boolean(const string& name, const string& value)    : CArg_String(name, value){    try {        m_Boolean = NStr::StringToBool(value);    } catch (CException& e) {        NCBI_RETHROW(e,CArgException,eConvert, s_ArgExptMsg(GetName(),            "Argument cannot be converted",value));    }}bool CArg_Boolean::AsBoolean(void) const{    return m_Boolean;}/////////////////////////////////////////////////////////  CArg_InputFile::void CArg_InputFile::x_Open(void) const{    if ( m_InputFile )        return;    if (AsString() == "-") {        m_InputFile  = &cin;        m_DeleteFlag = false;    } else if ( !AsString().empty() ) {        m_InputFile  = new CNcbiIfstream(AsString().c_str(),                                         IOS_BASE::in | m_OpenMode);        if (!m_InputFile  ||  !*m_InputFile) {            delete m_InputFile;            m_InputFile = 0;        } else {            m_DeleteFlag = true;        }    }    if ( !m_InputFile ) {        NCBI_THROW(CArgException,eNoFile, s_ArgExptMsg(GetName(),            "File is not accessible",AsString()));    }}CArg_InputFile::CArg_InputFile(const string& name, const string& value,                               IOS_BASE::openmode openmode,                               bool               delay_open): CArg_String(name, value),  m_OpenMode(openmode),  m_InputFile(0),  m_DeleteFlag(true){    if ( !delay_open )        x_Open();}CArg_InputFile::~CArg_InputFile(void){    if (m_InputFile  &&  m_DeleteFlag)        delete m_InputFile;}CNcbiIstream& CArg_InputFile::AsInputFile(void) const{    x_Open();    return *m_InputFile;}void CArg_InputFile::CloseFile(void) const{    if ( !m_InputFile ) {        ERR_POST(Warning << s_ArgExptMsg( GetName(),            "CArg_InputFile::CloseFile: File was not opened", AsString()));/*                 CArgException(GetName(),                               "CArg_InputFile::CloseFile -- file not opened",                               AsString()).what());*/        return;    }    if ( m_DeleteFlag ) {        delete m_InputFile;        m_InputFile = 0;    }}/////////////////////////////////////////////////////////  CArg_OutputFile::void CArg_OutputFile::x_Open(void) const{    if ( m_OutputFile )        return;    if (AsString() == "-") {        m_OutputFile = &cout;        m_DeleteFlag = false;    } else if ( !AsString().empty() ) {        m_OutputFile = new CNcbiOfstream(AsString().c_str(),                                         IOS_BASE::out | m_OpenMode);        if (!m_OutputFile  ||  !*m_OutputFile) {            delete m_OutputFile;            m_OutputFile = 0;        } else {            m_DeleteFlag = true;        }    }    if ( !m_OutputFile ) {        NCBI_THROW(CArgException,eNoFile, s_ArgExptMsg(GetName(),            "File is not accessible",AsString()));    }}CArg_OutputFile::CArg_OutputFile(const string& name, const string& value,                                 IOS_BASE::openmode openmode,                                 bool               delay_open)    : CArg_String(name, value),      m_OpenMode(openmode),      m_OutputFile(0),      m_DeleteFlag(true){    if ( !delay_open )        x_Open();}CArg_OutputFile::~CArg_OutputFile(void){    if (m_OutputFile  &&  m_DeleteFlag)        delete m_OutputFile;}CNcbiOstream& CArg_OutputFile::AsOutputFile(void) const{    x_Open();    return *m_OutputFile;}void CArg_OutputFile::CloseFile(void) const{    if ( !m_OutputFile ) {        ERR_POST(Warning << s_ArgExptMsg( GetName(),            "CArg_InputFile::CloseFile: File was not opened", AsString()));        return;    }    if ( m_DeleteFlag ) {        delete m_OutputFile;        m_OutputFile = 0;    }}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  CArgDesc***::   abstract base classes for argument descriptions////    CArgDesc////    CArgDescMandatory  : CArgDesc//    CArgDescOptional   : virtual CArgDescMandatory//    CArgDescDefault    : virtual CArgDescOptional////    CArgDescSynopsis///////////////////////////////////////////////////////////  CArgDesc::CArgDesc::CArgDesc(const string& name, const string& comment)    : m_Name(name), m_Comment(comment){    if ( !CArgDescriptions::VerifyName(m_Name) ) {        NCBI_THROW(CArgException,eInvalidArg,            "Invalid argument name: " + m_Name);    }}CArgDesc::~CArgDesc(void){    return;}

⌨️ 快捷键说明

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