3 * @author Perry Rapp, Creator, 2003-2006
4 * @date Created: 2003-10
5 * @date Edited: 2006-02-20 (Perry Rapp)
7 * @brief Declaration of Unicode file classes.
13 namespace Poco { class SharedMemory; }
16 * @brief Interface to file classes in this module
23 * @brief A struct for error message or error code.
27 String desc; // valid if apiname empty
30 bool HasError() const;
32 String GetError() const;
35 virtual ~UniFile() { }
36 virtual bool OpenReadOnly(const String& filename) = 0;
37 virtual void Close() = 0;
38 virtual bool IsOpen() const = 0;
40 virtual String GetFullyQualifiedPath() const = 0;
41 virtual const UniError & GetLastUniError() const = 0;
43 virtual bool IsUnicode() = 0;
44 virtual bool ReadBom() = 0;
45 virtual bool HasBom() const = 0;
46 virtual void SetBom(bool bom) = 0;
48 virtual ucr::UNICODESET GetUnicoding() const = 0;
49 virtual void SetUnicoding(ucr::UNICODESET unicoding) = 0;
50 virtual int GetCodepage() const = 0;
51 virtual void SetCodepage(int codepage) = 0;
54 virtual bool ReadString(String & line, bool * lossy) = 0;
55 virtual bool ReadString(String & line, String & eol, bool * lossy) = 0;
56 virtual bool ReadStringAll(String & line) = 0;
57 virtual int GetLineNumber() const = 0;
58 virtual int64_t GetPosition() const = 0;
59 virtual bool WriteString(const String & line) = 0;
68 txtstats() { clear(); }
69 void clear() { ncrs = nlfs = ncrlfs = nzeros = nlosses = 0; }
71 virtual const txtstats & GetTxtStats() const = 0;
75 * @brief Check if there is error.
76 * @return true if there is an error.
78 inline bool UniFile::UniError::HasError() const
84 * @brief Clears the existing error.
86 inline void UniFile::UniError::ClearError()
92 * @brief Get the error string.
93 * @return Error string.
95 inline String UniFile::UniError::GetError() const
101 * @brief Local file access code used by both UniMemFile and UniStdioFile
103 * This class lacks an actual handle to a file
105 class UniLocalFile : public UniFile
111 virtual String GetFullyQualifiedPath() const override { return m_filepath; }
112 virtual const UniError & GetLastUniError() const override { return m_lastError; }
114 virtual ucr::UNICODESET GetUnicoding() const override { return m_unicoding; }
115 virtual void SetUnicoding(ucr::UNICODESET unicoding) override { m_unicoding = unicoding; }
116 virtual int GetCodepage() const override { return m_codepage; }
117 virtual void SetCodepage(int codepage) override {
118 m_codepage = codepage;
122 m_unicoding = ucr::UCS2LE;
126 m_unicoding = ucr::UCS2BE;
131 m_unicoding = ucr::UTF8;
135 m_unicoding = ucr::NONE;
140 virtual int GetLineNumber() const override { return m_lineno; }
141 virtual const txtstats & GetTxtStats() const override { return m_txtstats; }
142 virtual int64_t GetFileSize() const{ return m_filesize; }
144 bool IsUnicode() override;
147 virtual bool DoGetFileStatus();
148 virtual void LastErrorCustom(const String& desc);
151 int m_statusFetched; // 0 not fetched, -1 error, +1 success
155 int m_lineno; // current 0-based line of m_current
156 UniError m_lastError;
157 ucr::UNICODESET m_unicoding;
158 int m_charsize; // 2 for UCS-2, else 1
159 int m_codepage; // only valid if m_unicoding==ucr::NONE;
161 bool m_bom; /**< Did the file have a BOM when reading? */
162 bool m_bUnicodingChecked; /**< Has unicoding been checked for the file? */
163 bool m_bUnicode; /**< Is the file unicode file? */
167 * @brief Memory-Mapped disk file (read-only access)
169 class UniMemFile : public UniLocalFile
171 friend class UniMarkdownFile;
180 virtual ~UniMemFile() { Close(); }
182 virtual bool GetFileStatus();
184 virtual bool OpenReadOnly(const String& filename) override;
185 virtual bool Open(const String& filename);
186 virtual bool Open(const String& filename, AccessMode mode);
187 void Close() override;
188 virtual bool IsOpen() const override;
190 virtual bool ReadBom() override;
191 virtual bool HasBom() const override;
192 virtual void SetBom(bool bom) override;
195 virtual bool ReadString(String & line, bool * lossy) override;
196 virtual bool ReadString(String & line, String & eol, bool * lossy) override;
197 virtual bool ReadStringAll(String & line) override;
198 virtual int64_t GetPosition() const override { return m_current - m_base; }
199 virtual bool WriteString(const String & line) override;
200 unsigned char* GetBase() const { return m_base; }
202 // Implementation methods
204 virtual bool DoOpen(const String& filename, AccessMode mode);
206 // Implementation data
208 Poco::SharedMemory *m_hMapping;
209 unsigned char *m_base; // points to base of mapping
210 unsigned char *m_data; // similar to m_base, but after BOM if any
211 unsigned char *m_current; // current location in file
214 /** @brief Is it currently attached to a file ? */
215 inline bool UniMemFile::IsOpen() const
217 // We don't test the handle here, because we allow "opening" empty file
218 // but memory-mapping doesn't work on that, so that uses a special state
219 // of no handle, but linenumber of 0
220 return m_lineno >= 0;
224 * @brief Returns if file has a BOM bytes.
225 * @return true if file has BOM bytes, false otherwise.
227 inline bool UniMemFile::HasBom() const
233 * @brief Sets if file has BOM or not.
234 * @param [in] true to have a BOM in file, false to not to have.
236 inline void UniMemFile::SetBom(bool bom)
242 * @brief Regular buffered file (write-only access)
243 * (ReadString methods have never been implemented,
244 * because UniMemFile above is good for reading)
246 class UniStdioFile : public UniLocalFile
252 virtual bool GetFileStatus();
254 virtual bool OpenReadOnly(const String& filename) override;
255 virtual bool OpenCreate(const String& filename);
256 virtual bool OpenCreateUtf8(const String& filename);
257 virtual bool Open(const String& filename, const String& mode);
258 void Close() override;
260 virtual bool IsOpen() const override;
262 virtual bool ReadBom() override;
263 virtual bool HasBom() const override;
264 virtual void SetBom(bool bom) override;
267 virtual bool ReadString(String & line, bool * lossy) override;
268 virtual bool ReadString(String & line, String & eol, bool * lossy) override;
269 virtual bool ReadStringAll(String & line) override;
272 virtual int64_t GetPosition() const override;
274 virtual int WriteBom();
275 virtual bool WriteString(const String & line) override;
277 // Implementation methods
279 virtual bool DoOpen(const String& filename, const String& mode);
280 virtual void LastErrorCustom(const String& desc) override;
282 // Implementation data
285 int64_t m_data; // offset after any initial BOM
286 ucr::buffer m_ucrbuff;
289 /** @brief Is it currently attached to a file ? */
290 inline bool UniStdioFile::IsOpen() const
296 * @brief Returns if file has a BOM bytes.
297 * @return true if file has BOM bytes, false otherwise.
299 inline bool UniStdioFile::HasBom() const
305 * @brief Sets if file has BOM or not.
306 * @param [in] true to have a BOM in file, false to not to have.
308 inline void UniStdioFile::SetBom(bool bom)