-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGapBufferManager.cpp
More file actions
235 lines (209 loc) · 5.86 KB
/
GapBufferManager.cpp
File metadata and controls
235 lines (209 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// GapBufferManager.cpp
// Copyright 2013 Michael T. Duffy.
// contact: mduffor@gmail.com
// This file is part of TEdit.
//
// TEdit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// TEdit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TEdit. If not, see <http://www.gnu.org/licenses/>.
//
#include <string.h>
#include "Types.hpp"
#include "Debug.hpp"
ASSERTFILE (__FILE__)
#include "GapBuffer.hpp"
#include "GapBufferManager.hpp"
//-----------------------------------------------------------------------------
GapBufferManager::GapBufferManager ()
{
pBuffers = NULL;
pCurrent = NULL;
};
//-----------------------------------------------------------------------------
GapBufferManager::~GapBufferManager ()
{
FreeAllBuffers();
};
//-----------------------------------------------------------------------------
VOID GapBufferManager::FreeAllBuffers()
{
while (pBuffers != NULL)
{
GapBuffer * pBufferToDelete = pBuffers;
pBuffers = pBuffers->pNext;
delete pBufferToDelete;
}
};
//-----------------------------------------------------------------------------
VOID GapBufferManager::SetCurrent (const char * szNameIn)
{
pCurrent = GetBuffer (szNameIn);
};
//-----------------------------------------------------------------------------
VOID GapBufferManager::SetCurrent (GapBuffer* pBufferIn)
{
GapBuffer * pSearch = pBuffers;
while (pSearch != NULL)
{
if (pSearch == pBufferIn)
{
pCurrent = pBufferIn;
return;
}
pSearch = pSearch->pNext;
}
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::GetCurrent (VOID)
{
return pCurrent;
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::GetBuffer (const char * szNameIn)
{
GapBuffer * pSearch = pBuffers;
while (pSearch != NULL)
{
if (strcmp (pSearch->GetName(), szNameIn) == 0)
{
return (pSearch);
}
pSearch = pSearch->pNext;
}
return (NULL);
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::GetBufferByFileName (const char * szFileNameIn)
{
GapBuffer * pSearch = pBuffers;
while (pSearch != NULL)
{
if (strcmp (pSearch->GetFileName(), szFileNameIn) == 0)
{
return (pSearch);
}
pSearch = pSearch->pNext;
}
return (NULL);
};
//-----------------------------------------------------------------------------
VOID GapBufferManager::SetNextToCurrent (VOID)
{
if (pCurrent != NULL)
{
pCurrent = pCurrent->pNext;
}
// if we reached the end of the list, loop around.
if (pCurrent == NULL)
{
pCurrent = pBuffers;
}
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::CreateBuffer (const char * szNameIn)
{
GapBuffer * pBufferNew = new GapBuffer();
if (pBufferNew == NULL)
{
return (NULL);
}
pBufferNew->SetName (szNameIn);
// add to the end of the list
GapBuffer * pBufferLast = NULL;
GapBuffer * pBufferSearch = pBuffers;
while (pBufferSearch != NULL)
{
pBufferLast = pBufferSearch;
pBufferSearch = pBufferSearch->pNext;
}
if (pBufferLast == NULL)
{
pBuffers = pBufferNew;
pBuffers->pPrev = NULL;
}
else
{
pBufferLast->pNext = pBufferNew;
pBufferNew->pPrev = pBufferLast;
};
pBufferNew->pNext = NULL;
SetCurrent (pBufferNew);
return (pBufferNew);
};
//-----------------------------------------------------------------------------
VOID GapBufferManager::ClearBuffer (const char * szNameIn)
{
GapBuffer * pToClear = GetBuffer (szNameIn);
if (pToClear != NULL)
{
pToClear->Clear();
};
};
//-----------------------------------------------------------------------------
VOID GapBufferManager::DeleteBuffer (const char * szNameIn)
{
GapBuffer * pToDelete = GetBuffer (szNameIn);
if (pToDelete != NULL)
{
if (pToDelete->pPrev != NULL)
{
pToDelete->pPrev->pNext = pToDelete->pNext;
}
else
{
pBuffers = pToDelete->pNext;
}
if (pToDelete->pNext != NULL)
{
pToDelete->pNext->pPrev = pToDelete->pPrev;
};
delete pToDelete;
};
};
//-----------------------------------------------------------------------------
const char * GapBufferManager::GetCurrentBufferName ()
{
if (pCurrent != NULL)
{
return pCurrent->GetName ();
}
return NULL;
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::OpenFile (const char * pszFileNameIn)
{
// Make sure it isn't already open!
GapBuffer * pBuffer = GetBufferByFileName (pszFileNameIn);
if (pBuffer != NULL)
{
return (pBuffer);
};
// TODO: Reduce name to just the file name instead of the entire path.
pBuffer = CreateBuffer (pszFileNameIn);
pBuffer->AllocBuffer (256);
pBuffer->SetFileName (pszFileNameIn);
pBuffer->Load ();
pBuffer->FillGap ();
pBuffer->SetCursor(1, 0);
return (pBuffer);
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::GetFirstBuffer (VOID)
{
return (pBuffers);
};
//-----------------------------------------------------------------------------
GapBuffer * GapBufferManager::GetNextBuffer (GapBuffer * pCurrBuffer)
{
if (pCurrBuffer == NULL) return (NULL);
return (pCurrBuffer->pNext);
};