1 //------------------------------------------------------------------------------
2 // emTextFileModel.h
3 //
4 // Copyright (C) 2004-2008,2010,2014,2017-2018 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20 
21 #ifndef emTextFileModel_h
22 #define emTextFileModel_h
23 
24 #ifndef emFileModel_h
25 #include <emCore/emFileModel.h>
26 #endif
27 
28 
29 class emTextFileModel : public emFileModel {
30 
31 public:
32 
33 	static emRef<emTextFileModel> Acquire(
34 		emContext & context, const emString & name, bool common=true
35 	);
36 
37 	// All the following getters may be called by multiple threads
38 	// concurrently. See emTextFilePanel::Paint(..).
39 
40 	const emArray<char> & GetContent() const;
41 
42 	enum CEType {
43 		CE_BINARY,
44 		CE_7BIT,
45 		CE_8BIT,
46 		CE_UTF8,
47 		CE_UTF16LE,
48 		CE_UTF16BE
49 	};
50 	CEType GetCharEncoding() const;
51 
52 	enum LBEType {
53 		LBE_NONE,
54 		LBE_DOS,
55 		LBE_MAC,
56 		LBE_UNIX,
57 		LBE_MIXED
58 	};
59 	LBEType GetLineBreakEncoding() const;
60 
61 	int GetLineCount() const;
62 
63 	int GetColumnCount() const;
64 
65 	int GetLineStart(int lineIndex) const;
66 		// Index to the content: first character of a line.
67 
68 	int GetLineEnd(int lineIndex) const;
69 		// Index to the content: one after last character of a line
70 		// without line feed.
71 
72 	emUInt8 GetRelativeLineIndent(int lineIndex) const;
73 	emUInt8 GetRelativeLineWidth(int lineIndex) const;
74 		// Indent and width of a line in units of ColumnCount/255.
75 
76 protected:
77 
78 	emTextFileModel(emContext & context, const emString & name);
79 	virtual ~emTextFileModel();
80 	virtual void ResetData();
81 	virtual void TryStartLoading();
82 	virtual bool TryContinueLoading();
83 	virtual void QuitLoading();
84 	virtual void TryStartSaving();
85 	virtual bool TryContinueSaving();
86 	virtual void QuitSaving();
87 	virtual emUInt64 CalcMemoryNeed();
88 	virtual double CalcFileProgress();
89 
90 private:
91 
92 	emArray<char> Content;
93 
94 	CEType CharEncoding;
95 	LBEType LineBreakEncoding;
96 	int LineCount;
97 	int ColumnCount;
98 	int * LineStarts;
99 	emUInt8 * RelativeLineIndents;
100 	emUInt8 * RelativeLineWidths;
101 
102 	struct LoadingState {
103 		int Stage;
104 		double Progress;
105 		FILE * File;
106 		emUInt64 FileSize;
107 		emUInt64 FileRead;
108 		char Buf[4096];
109 		int Statistics[256];
110 		int StartPos,Pos,Row,Col,Col1,Col2;
111 		bool FoundCR,FoundLF,FoundCRLF;
112 		emMBState MBState;
113 	};
114 	LoadingState * L;
115 };
116 
GetContent()117 inline const emArray<char> & emTextFileModel::GetContent() const
118 {
119 	return Content;
120 }
121 
GetCharEncoding()122 inline emTextFileModel::CEType emTextFileModel::GetCharEncoding() const
123 {
124 	return CharEncoding;
125 }
126 
GetLineBreakEncoding()127 inline emTextFileModel::LBEType emTextFileModel::GetLineBreakEncoding() const
128 {
129 	return LineBreakEncoding;
130 }
131 
GetLineCount()132 inline int emTextFileModel::GetLineCount() const
133 {
134 	return LineCount;
135 }
136 
GetColumnCount()137 inline int emTextFileModel::GetColumnCount() const
138 {
139 	return ColumnCount;
140 }
141 
GetLineStart(int lineIndex)142 inline int emTextFileModel::GetLineStart(int lineIndex) const
143 {
144 	return LineStarts[lineIndex];
145 }
146 
GetRelativeLineIndent(int lineIndex)147 inline emUInt8 emTextFileModel::GetRelativeLineIndent(int lineIndex) const
148 {
149 	return RelativeLineIndents[lineIndex];
150 }
151 
GetRelativeLineWidth(int lineIndex)152 inline emUInt8 emTextFileModel::GetRelativeLineWidth(int lineIndex) const
153 {
154 	return RelativeLineWidths[lineIndex];
155 }
156 
157 
158 #endif
159