NCBI C++ ToolKit
file_manifest.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: file_manifest.cpp 71226 2016-02-22 16:33:44Z elisovdn $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Cheinan Marks
27  *
28  * File Description:
29  * C++ class to handle GPipe manifest files. These are action node
30  * newline-separated, one column file path manifests. Do not confuse
31  * this manifest with the queue XML manifest in gpipe/objects/manifest.
32  */
33 
34 #include <ncbi_pch.hpp>
35 
36 #include <corelib/ncbiargs.hpp>
37 #include <corelib/ncbistre.hpp>
38 #include <corelib/ncbifile.hpp>
39 #include <util/file_manifest.hpp>
40 #include <fstream>
41 #include <algorithm>
42 
43 
45 
46 
47 CFileManifest::CFileManifest( const string & manifest_path )
48  : m_ManifestPath( manifest_path )
49 {
50  x_Init();
51 }
52 
53 CFileManifest::CFileManifest( const CArgValue& manifest_path )
54  : m_ManifestPath( manifest_path.AsString() )
55 {
56  x_Init();
57 }
58 
60 {
61  if ( m_ManifestPath.empty() ) {
62  NCBI_THROW( CManifestException, eEmptyManifestName, "" );
63  }
64 }
65 
67 {
68  // Check each file whether it can be opened for reading.
69  CFile manifest_file( m_ManifestPath );
70  if ( ! manifest_file.IsFile() ) {
71  NCBI_THROW( CManifestException, eCantOpenManifestForRead,
73  }
74  CNcbiIfstream manifest_stream( m_ManifestPath.c_str() );
75  if ( ! manifest_stream ) {
76  NCBI_THROW( CManifestException, eCantOpenManifestForRead,
78  }
79 
80  CManifest_CI manifest_line_iter( manifest_stream );
81  CManifest_CI end_iter;
82  while ( manifest_line_iter != end_iter ) {
83  CFile file( *manifest_line_iter );
84  if ( ! file.IsFile() ) {
85  string error_string = "Manifest: " + m_ManifestPath;
86  error_string += " Bad file: ";
87  error_string += *manifest_line_iter;
88  NCBI_THROW( CManifestException, eCantOpenFileInManifest,
89  error_string );
90  }
91  CNcbiIfstream manifest_file_stream( manifest_line_iter->c_str() );
92  if ( ! manifest_file_stream ) {
93  string error_string = "Manifest: " + m_ManifestPath;
94  error_string += " Cannot read file: ";
95  error_string += *manifest_line_iter;
96  NCBI_THROW( CManifestException, eCantOpenFileInManifest,
97  error_string );
98  }
99  ++manifest_line_iter;
100  }
101 }
102 
103 
104 vector<string> CFileManifest::GetAllFilePaths() const
105 {
106  CNcbiIfstream manifest_stream( m_ManifestPath.c_str() );
107  if ( ! manifest_stream ) {
108  NCBI_THROW( CManifestException, eCantOpenManifestForRead,
109  m_ManifestPath );
110  }
111 
112  CManifest_CI manifest_line_iter( manifest_stream );
113  CManifest_CI end_iter;
114  vector<string> file_paths;
115  string path;
116 
117  for( ; manifest_line_iter != end_iter; ++manifest_line_iter) {
118  path = CDirEntry::CreateAbsolutePath(*manifest_line_iter);
119  path = CDirEntry::NormalizePath(path);
120  file_paths.push_back(path);
121  }
122 
123  return file_paths;
124 }
125 
126 
128 {
129  // Read the first file path and check for the second.
130  // Throw if the second is present. An empty manifest is ok.
131  // We return an empty string.
132  string only_file_path;
133 
134  CNcbiIfstream manifest_stream( m_ManifestPath.c_str() );
135  if ( ! manifest_stream ) {
136  NCBI_THROW( CManifestException, eCantOpenManifestForRead,
137  m_ManifestPath );
138  }
139 
140  CManifest_CI manifest_line_iter( manifest_stream );
141  CManifest_CI end_iter;
142  if ( manifest_line_iter != end_iter ) {
143  only_file_path = *manifest_line_iter++;
144  if ( manifest_line_iter != end_iter ) {
145  NCBI_THROW( CManifestException, eMoreThanOneFile, m_ManifestPath );
146  }
147  }
148 
149  return only_file_path;
150 }
151 
152 
153 void CFileManifest::WriteManyFilePaths( const vector<string> & file_paths )
154 {
155  CNcbiOfstream manifest_stream( m_ManifestPath.c_str() );
156  if ( ! manifest_stream ) {
157  NCBI_THROW( CManifestException, eCantOpenManifestForWrite,
158  m_ManifestPath );
159  }
160  ostream_iterator<string> manifest_iterator( manifest_stream, "\n" );
161  copy( file_paths.begin(), file_paths.end(), manifest_iterator );
162 }
163 
164 
CArgValue –.
Definition: ncbiargs.hpp:184
Iterator to walk the files in the manifest.
vector< string > GetAllFilePaths() const
Returns all the file paths referenced by the manifest.
CFileManifest(const string &manifest_path)
string m_ManifestPath
string GetSingleFilePath() const
Returns the first file path in the manifest.
void Validate() const
Check the manifest for consistency and open all the referenced files.
void WriteManyFilePaths(const vector< string > &file_paths)
Write a list of files to a manifest. Will overwrite any previous data.
CFile –.
Definition: ncbifile.hpp:1604
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
static string NormalizePath(const string &path, EFollowLinks follow_links=eIgnoreLinks)
Normalize a path.
Definition: ncbifile.cpp:820
static string CreateAbsolutePath(const string &path, ERelativeToWhat rtw=eRelativeToCwd)
Get an absolute path from some, possibly relative, path.
Definition: ncbifile.cpp:665
bool IsFile(EFollowLinks follow=eFollowLinks) const
Check whether a directory entry is a file.
Definition: ncbifile.hpp:3940
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
FILE * file
Defines command line argument related classes.
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
Modified on Thu May 02 14:32:47 2024 by modify_doxy.py rev. 669887