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
179 virtual ~UniMemFile() { Close(); }
181 virtual bool GetFileStatus();
183 virtual bool OpenReadOnly(const String& filename) override;
184 virtual bool Open(const String& filename);
185 virtual bool Open(const String& filename, AccessMode mode);
186 void Close() override;
187 virtual bool IsOpen() const override;
189 virtual bool ReadBom() override;
190 virtual bool HasBom() const override;
191 virtual void SetBom(bool bom) override;
194 virtual bool ReadString(String & line, bool * lossy) override;
195 virtual bool ReadString(String & line, String & eol, bool * lossy) override;
196 virtual bool ReadStringAll(String & line) override;
197 virtual int64_t GetPosition() const override { return m_current - m_base; }
198 virtual bool WriteString(const String & line) override;
199 unsigned char* GetBase() const { return m_base; }
201 // Implementation methods
203 virtual bool DoOpen(const String& filename, AccessMode mode);
205 // Implementation data
207 Poco::SharedMemory *m_hMapping;
208 unsigned char *m_base; // points to base of mapping
209 unsigned char *m_data; // similar to m_base, but after BOM if any
210 unsigned char *m_current; // current location in file
213 /** @brief Is it currently attached to a file ? */
214 inline bool UniMemFile::IsOpen() const
216 // We don't test the handle here, because we allow "opening" empty file
217 // but memory-mapping doesn't work on that, so that uses a special state
218 // of no handle, but linenumber of 0
219 return m_lineno >= 0;
223 * @brief Returns if file has a BOM bytes.
224 * @return true if file has BOM bytes, false otherwise.
226 inline bool UniMemFile::HasBom() const
232 * @brief Sets if file has BOM or not.
233 * @param [in] true to have a BOM in file, false to not to have.
235 inline void UniMemFile::SetBom(bool bom)
241 * @brief Regular buffered file (write-only access)
242 * (ReadString methods have never been implemented,
243 * because UniMemFile above is good for reading)
245 class UniStdioFile : public UniLocalFile
251 virtual bool GetFileStatus();
253 virtual bool OpenReadOnly(const String& filename) override;
254 virtual bool OpenCreate(const String& filename);
255 virtual bool OpenCreateUtf8(const String& filename);
256 virtual bool Open(const String& filename, const String& mode);
257 void Close() override;
259 virtual bool IsOpen() const override;
261 virtual bool ReadBom() override;
262 virtual bool HasBom() const override;
263 virtual void SetBom(bool bom) override;
266 virtual bool ReadString(String & line, bool * lossy) override;
267 virtual bool ReadString(String & line, String & eol, bool * lossy) override;
268 virtual bool ReadStringAll(String & line) override;
271 virtual int64_t GetPosition() const override;
273 virtual int WriteBom();
274 virtual bool WriteString(const String & line) override;
276 // Implementation methods
278 virtual bool DoOpen(const String& filename, const String& mode);
279 virtual void LastErrorCustom(const String& desc) override;
281 // Implementation data
284 int64_t m_data; // offset after any initial BOM
285 ucr::buffer m_ucrbuff;
288 /** @brief Is it currently attached to a file ? */
289 inline bool UniStdioFile::IsOpen() const
295 * @brief Returns if file has a BOM bytes.
296 * @return true if file has BOM bytes, false otherwise.
298 inline bool UniStdioFile::HasBom() const
304 * @brief Sets if file has BOM or not.
305 * @param [in] true to have a BOM in file, false to not to have.
307 inline void UniStdioFile::SetBom(bool bom)