forked from windoze/tvision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileview.cc
282 lines (248 loc) · 6.36 KB
/
fileview.cc
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Turbo Vision - Version 2.0
*
* Copyright (c) 1994 by Borland International
* All Rights Reserved.
*
Modified by Robert H�hne to be used for RHIDE.
Modified to compile with gcc v3.x by Salvador E. Tropea, with the help of
Andris Pavenis.
Modified by Salvador E. Tropea to compile for 64 bits architectures.
*
*
*/
// SET: Moved the standard headers here because according to DJ
// they can inconditionally declare symbols like NULL
#define Uses_stdio
#define Uses_string
#define Uses_stdlib
#define Uses_ctype
#define Uses_fcntl
#define Uses_sys_stat
#define Uses_filelength
#define Uses_unistd
#define Uses_AllocLocal
#define Uses_MsgBox
#define Uses_TKeys
#define Uses_TScroller
#define Uses_TDrawBuffer
#define Uses_TRect
#define Uses_TProgram
#define Uses_TDeskTop
#define Uses_TStreamableClass
#define Uses_TFileViewer
#define Uses_TFileDialog
#include <tv.h>
#if !defined( NO_STREAM )
__link(RScroller)
__link(RScrollBar)
const char * const TFileViewer::name = "TFileViewer";
TStreamableClass RFileView( TFileViewer::name,
TFileViewer::build,
__DELTA(TFileViewer)
);
void *TFileViewer::read(ipstream& is)
{
char *fName;
TScroller::read(is);
fName = is.readString();
fileName = NULL;
readFile(fName);
delete fName;
return this;
}
void TFileViewer::write(opstream& os)
{
TScroller::write(os);
os.writeString(fileName);
}
TStreamable *TFileViewer::build()
{
return new TFileViewer( streamableInit );
}
#endif // NO_STREAM
void TFileViewer::changeBounds(const TRect & bounds)
{
TScroller::changeBounds(bounds);
setLimit(size.x + width,size.y + count);
}
TFileViewer::TFileViewer( const TRect& bounds,
TScrollBar *aHScrollBar,
TScrollBar *aVScrollBar,
const char *aFileName) :
TScroller( bounds, aHScrollBar, aVScrollBar ),
buffer(NULL), real_bufsize(0), width(0), count(0)
{
growMode = gfGrowHiX | gfGrowHiY;
isValid = True;
fileName = 0;
helpCtx = hcFileViewer;
readFile( aFileName );
delta.x = delta.y = 0;
}
TFileViewer::~TFileViewer()
{
if (buffer) free(buffer);
delete[] fileName;
fileLines->removeAll();
CLY_destroy(fileLines);
}
void TFileViewer::draw()
{
const char *p;
AllocLocalStr(s,size.x+1);
ushort c = getColor(0x0301);
for( int i = 0; i < size.y; i++ )
{
TDrawBuffer b;
b.moveChar( 0, ' ', c, size.x );
if( delta.y + i < fileLines->getCount() )
{
p = operator[](delta.y+i);
if( p == 0 || strlen(p) < (unsigned)delta.x )
s[0] = EOS;
else
{
strncpy( s, p+delta.x, size.x );
s[size.x] = EOS;
}
b.moveStr( 0, s, c );
}
writeBuf( 0, i, size.x, 1, b );
}
}
void TFileViewer::scrollDraw()
{
TScroller::scrollDraw();
draw();
}
void TFileViewer::readFile( const char *fName )
{
if (fileName) delete[] fileName;
limit.x = 0;
fileName = newStr( fName );
int fileToView = open(fName, O_RDONLY | O_TEXT, 0);
if (fileToView <= 0)
{
messageBox(mfError | mfOKButton ,
_("Could not open '%s' for reading"),fName);
isValid = False;
}
else
{
size_t bufsize = filelength(fileToView) + 1;
buffer = (char *)malloc(bufsize);
real_bufsize = ::read(fileToView,buffer,bufsize);
char *start,*end,*bufend = buffer + real_bufsize;
start = buffer;
while (start < bufend)
{
end = start;
while (end < bufend && *end != '\n') end++;
*end = 0;
width = max( (size_t)width, strlen( start ) );
count++;
start = end + 1;
}
int i;
fileLines = new TLineCollection(count, 5);
fileLines->count = count;
for (i=0,start = buffer;i<count;i++)
{
fileLines->items[i] = (void *)(start-buffer);
while (*start++);
}
isValid = True;
}
count = fileLines->getCount();
setLimit(size.x + width, size.y + count);
close(fileToView);
}
void TFileViewer::saveFile(const char *fname)
{
FILE *f;
f = fopen(fname,"wt+");
for (int i=0;i<count;i++)
fprintf(f,"%s\n",operator[](i));
fclose(f);
}
void TFileViewer::handleEvent(TEvent &event)
{
TScroller::handleEvent(event);
switch (event.what)
{
case evBroadcast:
switch (event.message.command)
{
case cmFileViewAnswer:
clearEvent(event);
break;
default:
break;
}
break;
case evCommand:
switch (event.message.command)
{
case cmSaveFile:
{
char fname[256];
TFileDialog *dialog;
dialog = new TFileDialog("*",_("Save to file"),
_("~N~ame"),fdOKButton,0);
if (TProgram::deskTop->execView(dialog) != cmCancel)
{
dialog->getData(fname);
saveFile(fname);
}
CLY_destroy(dialog);
break;
}
default:
break;
}
break;
default:
break;
}
}
void TFileViewer::insertLine(const char *line)
{
size_t len = strlen(line)+1;
buffer = (char *)realloc(buffer,real_bufsize+len);
memcpy(buffer+real_bufsize,line,len);
fileLines->insert((void*)(uintptr_t)real_bufsize);
real_bufsize += len;
count++;
len--;
width = width < (int)len ? (int)len : width;
setLimit(size.x + width, size.y + count);
}
void TFileViewer::setState( ushort aState, Boolean enable )
{
TScroller::setState( aState, enable );
if( enable && (aState & sfExposed) )
setLimit( limit.x, limit.y );
}
Boolean TFileViewer::valid( ushort )
{
return isValid;
}
const char * const TFileViewer::operator [](int index)
{
if (index >= count) return NULL;
return (const char *)(buffer + (uintptr_t)fileLines->at(index));
}
static int winNumber = 0;
TFileWindow::TFileWindow( const char *fileName ) :
TWindowInit( &TFileWindow::initFrame ),
TWindow( TProgram::deskTop->getExtent(), fileName, winNumber++ )
{
options |= ofTileable;
TRect r( getExtent() );
r.grow(-1, -1);
insert(new TFileViewer( r,
standardScrollBar(sbHorizontal | sbHandleKeyboard),
standardScrollBar(sbVertical | sbHandleKeyboard),
fileName) );
}