NCBI C++ ToolKit
file_utils.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: file_utils.hpp 102617 2024-06-12 13:07:39Z zaretska $
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  * Author: Vahram Avagyan
27  *
28  */
29 
30 /// @file file_utils.hpp
31 /// General file processing routines and structures.
32 ///
33 /// Defines a class combining various general file processing routines
34 /// and structures necessary for reading and writing binary files used
35 /// for Gene information retrieval.
36 
37 #ifndef OBJTOOLS_BLAST_GENE_INFO_READER___FILE_UTILS__HPP
38 #define OBJTOOLS_BLAST_GENE_INFO_READER___FILE_UTILS__HPP
39 
40 //==========================================================================//
41 
43 
44 #include <corelib/ncbifile.hpp>
45 
47 
48 
49 //==========================================================================//
50 
51 /// CGeneFileUtils
52 ///
53 /// Class that combines Gene info file processing routines and structures.
54 ///
55 /// The class combines various general file processing routines
56 /// and structures necessary for reading and writing binary files used
57 /// for Gene information retrieval. These include opening, reading and
58 /// writing uniformly structured binary files (e.g. consisting of integer
59 /// tuples), as well as providing a uniform storage/retrieval mechanism for
60 /// Gene info objects that is independent of any text formatting issues.
61 
63 {
64 public:
65  /// STwoIntRecord - a pair of integers.
66  ///
67  /// Structure to read/write to binary files
68  /// such as Gi to Offset, Gi to Gene ID, etc.
70  {
71  /// First integer field of the record.
72  int n1;
73 
74  /// Second integer field of the record.
75  int n2;
76  };
77 
78  /// SMultiIntRecord - an n-tuple of integers.
79  ///
80  /// Structure to read/write to binary files
81  /// such as Gene ID to Gi (storing fixed n integers per record).
82  template <int k_nFields>
84  {
85  /// Array of integer fields of the record.
86  int n[k_nFields];
87  };
88 
89 public:
90  /// Check if a directory exists, given its name.
91  static bool CheckDirExistence(const string& strDir);
92 
93  /// Check if a file exists, given its name.
94  static bool CheckExistence(const string& strFile);
95 
96  /// Get the length of a file, given its name.
97  static Int8 GetLength(const string& strFile);
98 
99  /// Open the given text file for reading.
100  static bool OpenTextInputFile(const string& strFileName,
101  CNcbiIfstream& in);
102 
103  /// Open the given binary file for reading.
104  static bool OpenBinaryInputFile(const string& strFileName,
105  CNcbiIfstream& in);
106 
107  /// Open the given text file for writing.
108  static bool OpenTextOutputFile(const string& strFileName,
109  CNcbiOfstream& out);
110 
111  /// Open the given binary file for writing.
112  static bool OpenBinaryOutputFile(const string& strFileName,
113  CNcbiOfstream& out);
114 
115  /// Write a pair of integers to the file.
116  static void WriteRecord(CNcbiOfstream& out,
117  STwoIntRecord& record);
118 
119  /// Read a pair of integers from the file.
120  static void ReadRecord(CNcbiIfstream& in,
121  STwoIntRecord& record);
122 
123  /// Write an n-tuple of integers to the file.
124  template <int k_nFields>
125  static void WriteRecord(CNcbiOfstream& out,
127 
128  /// Read an n-tuple of integers from the file.
129  template <int k_nFields>
130  static void ReadRecord(CNcbiIfstream& in,
132 
133  /// Write a Gene info object to the file.
134  ///
135  /// Writes the Gene info object to a binary file, in a format
136  /// that is independent of Gene info text formatting. Updates
137  /// the current offset variable to point to the end of the
138  /// written record.
141  int& nCurrentOffset);
142 
143  /// Read a Gene info object from the file.
144  ///
145  /// Reads a Gene info object from a binary file, assuming
146  /// it was written using WriteGeneInfo. The object is read
147  /// from the location pointed at by the offset variable.
149  int nOffset,
151 };
152 
153 //==========================================================================//
154 
155 inline void CGeneFileUtils::
157  STwoIntRecord& record)
158 {
159  out.write((char*)(&record.n1), sizeof(int));
160  out.write((char*)(&record.n2), sizeof(int));
161 }
162 
163 inline void CGeneFileUtils::
165  STwoIntRecord& record)
166 {
167  in.read((char*)(&record.n1), sizeof(int));
168  in.read((char*)(&record.n2), sizeof(int));
169 }
170 
171 template <int k_nFields>
172 inline void CGeneFileUtils::
175 {
176  for (int iField = 0; iField < k_nFields; iField++)
177  out.write((char*)(&record.n[iField]), sizeof(int));
178 }
179 
180 template <int k_nFields>
181 inline void CGeneFileUtils::
184 {
185  for (int iField = 0; iField < k_nFields; iField++)
186  in.read((char*)(&record.n[iField]), sizeof(int));
187 }
188 
189 //==========================================================================//
190 
192 
193 #endif
194 
CGeneFileUtils.
Definition: file_utils.hpp:63
static bool CheckExistence(const string &strFile)
Check if a file exists, given its name.
static bool OpenBinaryOutputFile(const string &strFileName, CNcbiOfstream &out)
Open the given binary file for writing.
static bool CheckDirExistence(const string &strDir)
Check if a directory exists, given its name.
static void ReadRecord(CNcbiIfstream &in, STwoIntRecord &record)
Read a pair of integers from the file.
Definition: file_utils.hpp:164
static Int8 GetLength(const string &strFile)
Get the length of a file, given its name.
static bool OpenTextInputFile(const string &strFileName, CNcbiIfstream &in)
Open the given text file for reading.
static bool OpenTextOutputFile(const string &strFileName, CNcbiOfstream &out)
Open the given text file for writing.
static bool OpenBinaryInputFile(const string &strFileName, CNcbiIfstream &in)
Open the given binary file for reading.
static void WriteGeneInfo(CNcbiOfstream &out, CRef< CGeneInfo > info, int &nCurrentOffset)
Write a Gene info object to the file.
static void ReadGeneInfo(CNcbiIfstream &in, int nOffset, CRef< CGeneInfo > &info)
Read a Gene info object from the file.
static void WriteRecord(CNcbiOfstream &out, STwoIntRecord &record)
Write a pair of integers to the file.
Definition: file_utils.hpp:156
CRef –.
Definition: ncbiobj.hpp:618
std::ofstream out("events_result.xml")
main entry point for tests
Gene information class and related interfaces.
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#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
#define NCBI_XOBJREAD_EXPORT
Definition: ncbi_export.h:1315
yy_size_t n
static MDB_envinfo info
Definition: mdb_load.c:37
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
std::istream & in(std::istream &in_, double &x_)
SMultiIntRecord - an n-tuple of integers.
Definition: file_utils.hpp:84
int n[k_nFields]
Array of integer fields of the record.
Definition: file_utils.hpp:86
STwoIntRecord - a pair of integers.
Definition: file_utils.hpp:70
int n1
First integer field of the record.
Definition: file_utils.hpp:72
int n2
Second integer field of the record.
Definition: file_utils.hpp:75
Modified on Fri Sep 20 14:57:06 2024 by modify_doxy.py rev. 669887