BeTextFile.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
11 
12 #include <Bentley\BeFile.h>
13 #include <Bentley\RefCounted.h>
14 
16 
19 
21 enum class TextFileOpenType
22  {
23  Read = 0,
24  Write = 1,
25  Append = 2,
26  };
27 
29 enum class TextFileEncoding
30  {
31  CurrentLocale = 0,
32  Utf8 = 1,
33  Utf16 = 2,
34  };
35 
37 enum class TextFileOptions
38  {
39  None = 0,
40  KeepNewLine = 0x1,
41  NewLinesToSpace = 0x5,
42  };
43 
46  {
47  Success = 0,
48  Eof,
49  BadParameter,
50  };
51 
54  {
55  Success = 0,
56  Error,
57  BadParameter,
58  };
59 
62 
63 /*=================================================================================**/
67 struct BeTextFile : public RefCountedBase
68  {
69  static const UInt32 BUFFER_SIZE = 100;
70 private:
71  WString m_fileSpec;
72  TextFileOpenType m_openType;
73  BeFile* m_file;
74  TextFileEncoding m_encoding;
75  TextFileOptions m_options;
76  mbstate_t m_mbState;
77  UInt32 m_startOfTextData;
78 
79  bool m_reachedEnd;
80  bool m_noData;
81  bool m_atStart;
82  byte* m_currentByte;
83  byte* m_endBuffer;
84  byte m_readBuffer[BUFFER_SIZE+2]; // used only for reading. 2 extra bytes are to make sure we can push a byte or WChar back into the buffer.
85 
86  BeFileStatus ReadBuffer ();
87  byte ByteFromBuffer ();
88  UInt16 UInt16FromBuffer ();
89  TextFileEncoding ReadFileEncoding (TextFileOpenType openType, TextFileEncoding proposedEncoding);
90  void ResetToFileStart ();
91  void RestoreByteToBuffer (UInt16 restore);
92  void RestoreUInt16ToBuffer (UInt16 restore);
93  void WriteEncodingBOM ();
94 
95 protected:
99  BENTLEYDLL_EXPORT BeFileStatus OpenFile (WCharCP fullFileSpec, TextFileOpenType openType, TextFileOptions options, TextFileEncoding encoding);
100 
101 public:
111  BENTLEYDLL_EXPORT static BeTextFilePtr Open (BeFileStatus& status, WCharCP fullFileSpec, TextFileOpenType openType, TextFileOptions options, TextFileEncoding encoding = TextFileEncoding::CurrentLocale);
112 
115  BENTLEYDLL_EXPORT void Close();
116 
122  BENTLEYDLL_EXPORT TextFileReadStatus GetLine (WStringR textLine);
123 
129  BENTLEYDLL_EXPORT TextFileWriteStatus PutLine (WCharCP textLine, bool addCarriageReturn);
130 
133  BENTLEYDLL_EXPORT WChar GetChar ();
134 
139  BENTLEYDLL_EXPORT TextFileWriteStatus PrintfTo (bool toStdOutAlso, WCharCP format, ...);
140 
141 
145 
152  BENTLEYDLL_EXPORT BeFileStatus SetPointer (UInt64 position, BeFileSeekOrigin origin);
153 
157  BENTLEYDLL_EXPORT BeFileStatus GetPointer (UInt64& position);
158 
159  };
160 
162 
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
TextFileReadStatus
The possible return values for text file reads.
Definition: BeTextFile.h:45
TextFileOptions
The possible options for text file reading.
Definition: BeTextFile.h:37
Open for both read and write.
wchar_t WChar
Definition: Bentley.h:223
Open for write access.
uint16_t UInt16
Definition: Bentley.r.h:118
TextFileEncoding
The possible BeTextFile open modes.
Definition: BeTextFile.h:29
wchar_t const * WCharCP
Definition: Bentley.h:224
uint32_t UInt32
Definition: Bentley.r.h:128
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
relevant only for getString, default behavior is to return the line without the newlines.
Encountered the end of the file.
BeFileSeekOrigin
Possible origin modes for the SetPosition method.
Definition: BeFile.h:67
Write current locale multibyte output file.
A bad argument was passed to the method.
relevant only for getString, also turns on KeepNewLine.
Open for read access.
unsigned char byte
Definition: Bentley.r.h:144
TextFileOpenType
The possible BeTextFile open modes.
Definition: BeTextFile.h:21
uint64_t UInt64
Definition: Bentley.r.h:131
Concrete class that can be used to implement the reference-counting pattern.
Definition: RefCounted.h:109
BeFile provides a way to open a file and methods to access its contents and attributes.
Definition: BeFile.h:78
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
RefCountedPtr< struct BeTextFile > BeTextFilePtr
A reference counted pointer to a BeTextFile instance.
Definition: BeTextFile.h:61
Provides Bentley implemented file management functions (Bentley/BeFile.h).
Write little-endian UTF-16 encoded output file.
TextFileWriteStatus
The possible return values for text file writes.
Definition: BeTextFile.h:53
Reads and Writes Text Files regardless of whether they are encoded on disk as locale-encoded ASCII...
Definition: BeTextFile.h:67
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:51
BeFileStatus
The possible status returns for the BeFile methods.
Definition: BeFile.h:28

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.