1 #ifndef FILEUTIL_HPP
2 #define FILEUTIL_HPP
3 
4 /*  $Id: fileutil.hpp 554977 2018-01-11 14:18:53Z gouriano $
5 * ===========================================================================
6 *
7 *                            PUBLIC DOMAIN NOTICE
8 *               National Center for Biotechnology Information
9 *
10 *  This software/database is a "United States Government Work" under the
11 *  terms of the United States Copyright Act.  It was written as part of
12 *  the author's official duties as a United States Government employee and
13 *  thus cannot be copyrighted.  This software/database is freely available
14 *  to the public for use. The National Library of Medicine and the U.S.
15 *  Government have not placed any restriction on its use or reproduction.
16 *
17 *  Although all reasonable efforts have been taken to ensure the accuracy
18 *  and reliability of the software and data, the NLM and the U.S.
19 *  Government do not and cannot warrant the performance or results that
20 *  may be obtained by using this software or data. The NLM and the U.S.
21 *  Government disclaim all warranties, express or implied, including
22 *  warranties of performance, merchantability or fitness for any particular
23 *  purpose.
24 *
25 *  Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 *   Several file utility functions/classes.
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/serialdef.hpp>
37 #include <memory>
38 
39 BEGIN_NCBI_SCOPE
40 
41 static const size_t MAX_FILE_NAME_LENGTH = 31;
42 
43 class SourceFile
44 {
45 public:
46     SourceFile(const string& name, bool binary = false);
47     SourceFile(const string& name, const list<string>& dirs,
48                bool binary = false);
49     ~SourceFile(void);
50 
operator CNcbiIstream&(void) const51     operator CNcbiIstream&(void) const
52         {
53             return *m_StreamPtr;
54         }
55 
56     enum EType {
57         eUnknown,  // Unknown type
58         eASN,      // ASN file
59         eDTD,      // DTD file
60         eXSD,      // XSD file
61         eWSDL,     // WSDL file
62         eJSON      // JSON schema file
63     };
64     EType GetType(void) const;
GetFileName(void) const65     string GetFileName(void) const
66         {
67             return m_Name;
68         }
69 
70 private:
71     string m_Name;
72     CNcbiIstream* m_StreamPtr;
73     bool m_Open;
74 
75     bool x_Open(const string& name, bool binary);
76 };
77 
78 class DestinationFile
79 {
80 public:
81     DestinationFile(const string& name, bool binary = false);
82     ~DestinationFile(void);
83 
operator CNcbiOstream&(void) const84     operator CNcbiOstream&(void) const
85         {
86             return *m_StreamPtr;
87         }
88 
89 private:
90     CNcbiOstream* m_StreamPtr;
91     bool m_Open;
92 };
93 
94 struct FileInfo {
FileInfoFileInfo95     FileInfo(void)
96         : type(ESerialDataFormat(eSerial_None))
97         { }
FileInfoFileInfo98     FileInfo(const string& n, ESerialDataFormat t)
99         : name(n), type(t)
100         { }
101 
operator const string&FileInfo102     operator const string&(void) const
103         { return name; }
104 
105     DECLARE_OPERATOR_BOOL(!name.empty());
106 
operator ==FileInfo107     bool operator==(const FileInfo& info) const
108     {
109         return name == info.name;
110     }
operator !=FileInfo111     bool operator!=(const FileInfo& info) const
112     {
113         return name != info.name;
114     }
115 
116     string name;
117     ESerialDataFormat type;
118 };
119 
120 class CDelayedOfstream : public CNcbiOstrstream
121 {
122 public:
123     CDelayedOfstream(const string& fileName);
124     virtual ~CDelayedOfstream(void);
125 
is_open(void) const126     bool is_open(void) const
127         {
128             return !m_FileName.empty();
129         }
130     void open(const string& fileName);
131     void close(void);
132 
133     void Discard(void);
134 
135 protected:
136     bool equals(void);
137     bool rewrite(void);
138 
139 private:
140     string m_FileName;
141     unique_ptr<CNcbiIfstream> m_Istream;
142     unique_ptr<CNcbiOfstream> m_Ostream;
143 };
144 
145 string MakeAbsolutePath(const string& path);
146 
147 // return combined dir and name, inserting if needed '/'
148 string Path(const string& dir, const string& name);
149 
150 // file name will be valid after adding at most addLength symbols
151 string MakeFileName(const string& s, size_t addLength = 0);
152 
153 // return base name of file i.e. without dir and extension
154 string BaseName(const string& path);
155 
156 // return dir name of file
157 string DirName(const string& path);
158 
159 bool IsLocalPath(const string& path);
160 
161 // Convert system-dependent path to the standard path
162 // ('\' ==> '/', ':' ==> '/', etc.)
163 string GetStdPath(const string& path);
164 
165 END_NCBI_SCOPE
166 
167 #endif
168