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

📄 test_ncbi_pipe.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: test_ncbi_pipe.cpp,v $ * PRODUCTION Revision 1000.3  2004/06/01 18:46:18  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.22 * PRODUCTION * =========================================================================== *//*  $Id: test_ncbi_pipe.cpp,v 1000.3 2004/06/01 18:46:18 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:  Vladimir Ivanov * * File Description:  Test program for the Pipe API * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbiargs.hpp>#include <corelib/ncbienv.hpp>#include <corelib/ncbi_system.hpp>#include <connect/ncbi_conn_stream.hpp>#include <stdio.h>#include <string.h>#if defined(NCBI_OS_MSWIN)#  include <io.h>#elif defined(NCBI_OS_UNIX)#  include <unistd.h>#else#   error "Pipe tests configured for Windows and Unix only."#endif#include <test/test_assert.h>  // This header must go lastUSING_NCBI_SCOPE;const int    kTestResult =      99;   // Tests exit codeconst size_t kBufferSize = 10*1024;   // I/O buffer size////////////////////////////////// Auxiliary functions//// Read from pipestatic EIO_Status s_ReadPipe(CPipe& pipe, void* buf, size_t size,                             size_t* n_read) {    size_t     total = 0;    size_t     cnt   = 0;    EIO_Status status;        do {        status = pipe.Read((char*)buf + total, size - total, &cnt);        cerr << "Read from pipe: " << cnt << endl;        cerr.write((char*)buf + total, cnt);        if ( cnt ) {            cerr << endl;        }        total += cnt;    } while (status == eIO_Success  &&  total < size);            *n_read = total;    cerr << "Read from pipe " << total << " bytes." << endl;    return status;}// Write to pipestatic EIO_Status s_WritePipe(CPipe& pipe, const void* buf, size_t size,                              size_t* n_written) {    size_t     total = 0;    size_t     cnt   = 0;    EIO_Status status;        do {        status = pipe.Write((char*)buf + total, size - total, &cnt);        cerr << "Written to pipe: " << cnt << endl;        cerr.write((char*)buf + total, cnt);        if ( cnt ) {            cerr << endl;        }        total += cnt;    } while (status == eIO_Success  &&  total < size);        *n_written = total;    cerr << "Written to pipe " << total << " bytes." << endl;    return status;}// Read from file streamstatic string s_ReadFile(FILE* fs) {    char   buf[kBufferSize];    size_t cnt = read(fileno(fs), buf, kBufferSize - 1);    buf[cnt] = 0;    string str = buf;    cerr << "Read from file stream " << cnt << " bytes:" << endl;    cerr.write(buf, cnt);    if ( cnt ) {        cerr << endl;    }    return str;}// Write to file streamstatic void s_WriteFile(FILE* fs, string message) {    write(fileno(fs), message.c_str(), message.length());    cerr << "Written to file stream " << message.length() << " bytes:" << endl;    cerr << message << endl;}// Read from iostreamstatic string s_ReadStream(istream& ios){    char   buf[kBufferSize];    size_t total = 0;    size_t size  = kBufferSize - 1;    for (;;) {        ios.read(buf + total, size);        size_t cnt = ios.gcount();        cerr << "Read from iostream: " << cnt << endl;        cerr.write(buf + total, cnt);        if ( cnt ) {            cerr << endl;        }        total += cnt;        size  -= cnt;        if (size == 0  ||  (cnt == 0  &&  ios.eof())) {            break;        }        ios.clear();    }    buf[total] = 0;    string str = buf;    cerr << "Read from iostream " << total << " bytes." << endl;    return str;}////////////////////////////////// Test application//class CTest : public CNcbiApplication{public:    void Init(void);    int Run(void);};void CTest::Init(void){    // Set error posting and tracing on maximum    SetDiagTrace(eDT_Enable);    SetDiagPostFlag(eDPF_All);    SetDiagPostLevel(eDiag_Info);}int CTest::Run(void){    // Initialization of variables and structures    const string app = GetArguments().GetProgramName();    string str;    vector<string> args;    char           buf[kBufferSize];    size_t         n_read    = 0;    size_t         n_written = 0;    int            exitcode  = 0;    string         message;    EIO_Status     status;    TProcessHandle handle;    // Create pipe object    CPipe pipe;    STimeout io_timeout    = {2,0};    STimeout close_timeout = {1,0};    assert(pipe.SetTimeout(eIO_Read,  &io_timeout)    == eIO_Success);    assert(pipe.SetTimeout(eIO_Write, &io_timeout)    == eIO_Success);    assert(pipe.SetTimeout(eIO_Close, &close_timeout) == eIO_Success);#if defined(NCBI_OS_UNIX)    // Pipe for reading (direct from pipe)    args.push_back("-l");    assert(pipe.Open("ls", args, CPipe::fStdIn_Close) == eIO_Success);    assert(s_WritePipe(pipe, buf, kBufferSize, &n_written) == eIO_Unknown);    assert(n_written == 0);    status = s_ReadPipe(pipe, buf, kBufferSize, &n_read);    assert(status == eIO_Success  ||  status == eIO_Closed);    assert(n_read > 0);    assert(pipe.Close(&exitcode) == eIO_Success);    assert(exitcode == 0);    // Pipe for reading (iostream)    CConn_PipeStream ios("ls", args, CPipe::fStdIn_Close);    s_ReadStream(ios);    assert(ios.GetPipe().Close(&exitcode) == eIO_Success);    assert(exitcode == 0);#elif defined (NCBI_OS_MSWIN)    string cmd = GetEnvironment().Get("COMSPEC");    // Pipe for reading (direct from pipe)    args.push_back("/c");    args.push_back("dir *.*");    assert(pipe.Open(cmd.c_str(), args, CPipe::fStdIn_Close) == eIO_Success);    assert(s_WritePipe(pipe, buf, kBufferSize, &n_written) == eIO_Unknown);    assert(n_written == 0);    status = s_ReadPipe(pipe, buf, kBufferSize, &n_read);    assert(status == eIO_Success  ||  status == eIO_Closed);    assert(n_read > 0);    assert(pipe.Close(&exitcode) == eIO_Success);    assert(exitcode == 0);    // Pipe for reading (iostream)    CConn_PipeStream ios(cmd.c_str(), args, CPipe::fStdIn_Close);    s_ReadStream(ios);    assert(ios.GetPipe().Close(&exitcode) == eIO_Success);    assert(exitcode == 0);#endif    // Pipe for writing (direct to pipe)

⌨️ 快捷键说明

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