-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cookie.c
606 lines (493 loc) · 20.2 KB
/
Cookie.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/****************************************************************************
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / PRE-AMBLE / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
\ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
*****************************************************************************
Procedural Map
~~~~~~~~~~~~~~
NB. the intention of this diagram is merely to illustrate where the
different procedures\functions are used, it is NOT really intended as
a flow diagram or anything else of that ilk.
NB/ Function names in brackets denote functions found in other source
files.
MainGUISequence
|
+- FlushDoubleClick
+- RefreshWin
+- SaveScreenName
+- FindFile
+- MainCookieSequence -------+- (SetUpScreen)
| +- OpenNormalFont
| +- OpenDataFile
| +- RandomizeFilePos
| +- FindNextCookie
| +- CountCookieSize -----------+- CheckEOC
| | +- ExpandTABs
| +- GetWindowSizeCorrect
| +- DoWindow
| +- ReReadCookieAndPrint ------+- ExpandTABs
| +- CloseDataFile
| +- CloseNormalFont
+- PopOldScreenToFront
****************************************************************************
Calculating Window Geometry
~~~~~~~~~~~~~~~~~~~~~~~~~~~
A great deal of effort has been put into finding a correct method for
opening the correct size of window for the selected text. As the algorithm
spreads itself naturally over many procedures it is worth summarizing here,
the technique employed.
- Firstly, the cookie at the specified location is read line by line
the length of each line (in pixels) being stored if it is the longest
so far.
- The number of lines in the cookie is noted.
- The width of the window is thus,
the longest line (maxlinelen) +
the height of the leftmost border of the system windows +
the size of the rightmost border
+ _xpad
- The height of the window is equal to
the number of lines in the cookie
* the intrinsic height of the font (Font_Handle->tf_Ysize)
+ the upper and lower borders
Also we need to add the required tween line spacing (_lspace)
multiplied by the number of lines less one; we should add the
values _ytop and _ybot.
It is worth noting that as we havn't actually opened the window yet, we
need to address the Screen Structure we have opened to find the size of the
border of the window once it is opened. eg/
EpigramsScr->WBorLeft
EpigramsScr->WBorTop
We need to take note, that we can't predict at this stage (using this
method) the size of the borders if we later in the programs development,
allow the addition of drag bars, sizing gadgets and so forth.
Another important note to make which would perhaps not otherwise be
obvious, is that some fonts will still look `funny' (needless space at the
top of even the tallest character etc.) despite our efforts to the
contrary; this is not something we can help and is a attribute that would
be intrinsic to the font design itself. In an attempt to remedy this we
have allowed the user the option to alter the top and bottom padding
seperately (_ytop and _ybot).
Non-System Window Borders
~~~~~~~~~~~~~~~~~~~~~~~~~
It is possible in Epigrams to open the window with non-system window
borders. This causes additional complexity in the source code. In the
event of a re-coding, note that changes in the following functions may be
necessary.
GetWindowSizeCorrect() -- The contents of this function are described
in detail above.
ReReadCookieAndPrint() -- In this function we work out where the pen
needs to be placed in both x and y terms. It should be apparent that this
will depend on the border strategy used.
****************************************************************************
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / INCLUDES / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
\ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
****************************************************************************/
#include "Public.h"
#include "Cookie.h"
/****************************************************************************
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / \ \ \ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / MAIN GUI SEQUENCE / / / / / / / / / / / / / / / / / / / / / / / / / /
\ \ \ \ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
****************************************************************************/
void MainGUISequence()
{
FlushDoubleClick();
RefreshWin();
SaveScreenName();
FindFile();
MainCookieSequence();
PopOldScreenToFront();
}
/***************************************************************************/
void RefreshWin()
{
// --- CLOSE WINDOW JUST IN CASE ONE IS ALREADY OPEN
if ( interface == TRUE ) {
if ( _popback == TRUE ) {
_popback = FALSE;
GUIGoAway( NOERROR );
_popback = TRUE;
} else
GUIGoAway( NOERROR );
}
}
/***************************************************************************/
void SaveScreenName()
{
// --- STORE ACTIVE SCREEN, IF RELEVENT, SO WE CAN POP BACK LATER
if ( _popback == TRUE )
ActiveScr = IntuitionBase->ActiveScreen;
}
/***************************************************************************/
void PopOldScreenToFront()
{
// --- BRING SCREEN TO FRONT
if ( _poptofront == TRUE )
ScreenToFront( Scr );
}
/***************************************************************************/
void FindFile()
{
// --- USE THIS UNTIL WE HAVE FUNCTION FOR RANDOMIZING FILES
strcpy( file_used, _dfilename );
}
/***************************************************************************/
/****************************************************************************
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / \ \ \ \ \ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / /
/ / MAIN COOKIE SEQUENCE / / / / / / / / / / / / / / / / / / / / / / / /
\ \ \ \ \ \ \ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / /
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
****************************************************************************/
void MainCookieSequence()
{
struct Cookie_Info info;
InitializeCookieInfo( &info );
if ( ! SetupScreen() ) {
GUIGoAway( NOSCREEN );
return;
}
if ( ! OpenNormalFont( &info ) ) {
char error_line_buff[MAX_FONT_NAME_SIZE + 32];
STRPTR error_line = error_line_buff;
strcpy( error_line, "Can't open `");
strcat( error_line, FilePart(_fontname) );
strcat( error_line, "'" );
system_state = DoErrors( WARNING,
error_line,
"not available on disk|low memory",
"Continue with Default|Quit" );
if ( system_state ) {
strcpy( _fontname, DEF_FONT);
_fontsize = DEF_FONT_SIZE;
}
return;
}
if ( ! OpenDataFile( &info ) ) {
char error_line_buff[MAX_FN_SIZE + 32];
STRPTR error_line = error_line_buff;
strcpy( error_line, "Can't open `");
strcat( error_line, FilePart(file_used) );
strcat( error_line, "'" );
system_state = DoErrors( WARNING,
error_line,
"low memory|not available on disk",
"Continue|Quit" );
return;
}
RandomizeFilePos( &info );
if ( ! FindNextCookie( &info ) )
return;
CountCookieSize( &info );
GetWindowSizeCorrect( &info );
if ( DoWindow( &info ) )
ReReadCookieAndPrint( &info );
CloseDataFile( &info );
CloseNormalFont( &info );
}
/***************************************************************************/
BOOL OpenNormalFont( struct Cookie_Info * info )
{
// --- SET FONT ATTRIBUTES
if ( _usescreenfont == TRUE ) {
Font_Attr.ta_Name = Scr->Font->ta_Name;
Font_Attr.ta_YSize = Scr->Font->ta_YSize;
} else {
Font_Attr.ta_Name = _fontname;
Font_Attr.ta_YSize = _fontsize;
}
Font_Attr.ta_Style = FS_NORMAL;
Font_Attr.ta_Flags = FPB_DISKFONT;
if ( info->Font_Handle = OpenDiskFont( &Font_Attr ) )
return TRUE;
else
return FALSE;
}
/***************************************************************************/
void CloseNormalFont( struct Cookie_Info * info )
{
if ( info->Font_Handle ) {
CloseFont( info->Font_Handle );
info->Font_Handle = NULL;
}
}
/***************************************************************************/
BOOL OpenDataFile( struct Cookie_Info * info )
{
if ( info->data_file_handle = Open( file_used, MODE_OLDFILE ) )
return TRUE;
else
return FALSE;
}
/***************************************************************************/
void CloseDataFile( struct Cookie_Info * info )
{
Close( info->data_file_handle );
}
/***************************************************************************/
void RandomizeFilePos( struct Cookie_Info * info )
{
ULONG data_file_size;
// --- FIND FILE SIZE
Seek( info->data_file_handle, 0, OFFSET_END );
data_file_size = Seek( info->data_file_handle, 0, OFFSET_BEGINNING );
// --- RANDOMIZE STARTPOINT IN FILE
byte_pos = PRNG( data_file_size, _random_mode );
}
/***************************************************************************/
BOOL FindNextCookie( struct Cookie_Info * info )
{
char error_line_buff[MAX_FN_SIZE + 32];
STRPTR error_line = error_line_buff;
if ( FindCookieDelimeter( info->data_file_handle, MARKER_CH ) == FALSE ) {
byte_pos = 0;
if ( FindCookieDelimeter( info->data_file_handle, MARKER_CH ) == FALSE )
strcpy( error_line, "No entries in `");
strcat( error_line, FilePart(file_used) );
strcat( error_line, "'" );
system_state = DoErrors( WARNING,
error_line,
"add some cookies to the file!",
"Continue|Quit" );
return FALSE;
}
// --- GET CURRENT FILE POSITION AFTER THE SHENANAGINS IN FindCookieDelimeter();
byte_pos = Seek( info->data_file_handle, 0, OFFSET_BEGINNING ) + 1;
Seek( info->data_file_handle, byte_pos, OFFSET_BEGINNING );
return TRUE;
}
/***************************************************************************/
/*
THIS FUNCTION NOW STORES THE COOKIE DATA IN MEMORY -- THEREBY SAVING
TIME LATER BY READING THE COOKIE FROM MEMORY AND NOT FROM THE FILE AGAIN
*/
void CountCookieSize( struct Cookie_Info * info )
{
struct Node * cookie_line;
char input_string[MAX_MAXLINESIZE];
struct RastPort temp_rast; // used to enable TextExtent to calculate width of string
WORD temp_len; // temporarily stores the length of
// the string in pixels - is assigned to
// the real length (maxlinelen),
// if it is greater than the
// current length
BOOL end_of_cookie = FALSE;
// --- ESTABLISH RASTPORT NOW -- FOR USE WITH TextExtent()
InitRastPort( &temp_rast );
SetFont( &temp_rast, info->Font_Handle );
// --- INITIALIZE VARIABLES
info->maxlinelen = 0;
info->numlines = 0;
// --- LOOP THROUGH COOKIE
do
{
if ( FGets( info->data_file_handle, input_string, _maxlinesize) == NULL ) // EOF probably
end_of_cookie = TRUE;
else
{
if ( CheckEOC(input_string, MARKER_CH) || (info->numlines > MAXNUMLINES) ) // OR for safety
end_of_cookie = TRUE;
else
{
// --- REPLACE LAST CHARACTER (A NEW LINE) WITH A NULL TERMINATOR
input_string[ strlen( input_string ) - 1 ] = '\0';
// --- GET RID OF ANY TAB CHARACTERS
ExpandTABs(input_string);
// --- STORE VALUES IF REQUIRED
if ( (temp_len = TextLength( &temp_rast, input_string, strlen(input_string))) > info->maxlinelen )
info->maxlinelen = temp_len;
// --- ADD COOKIE LINE TO THE LIST OF COOKIE LINES
cookie_line = malloc( sizeof(struct Node));
cookie_line->ln_Name = malloc( strlen(input_string) + 1);
strcpy( cookie_line->ln_Name, input_string );
AddTail( &info->cookie_contents, cookie_line );
// --- VALID DATA LINE SO INCREASE LINE COUNT
info->numlines ++;
}
}
}
while ( end_of_cookie != TRUE );
}
/***************************************************************************/
void GetWindowSizeCorrect( struct Cookie_Info * info )
{
UWORD top_border = Scr->WBorTop,
bottom_border = Scr->WBorBottom,
left_border = Scr->WBorLeft,
right_border = Scr->WBorRight;
if ( _bevel != BEV_SYSTEM )
if ((_bevel == BEV_RAISED) || (_bevel == BEV_RECESSED))
top_border = bottom_border = left_border = right_border = 1 ;
EpigramsWidth = (UWORD) ((_xpad * 2) + right_border + left_border + info->maxlinelen);
EpigramsHeight = (UWORD) ( _ytop
+ _ybot
+ ((info->Font_Handle->tf_YSize + _lspace) * info->numlines)
- _lspace
+ (top_border + bottom_border)
);
EpigramsLeft = (UWORD) ((Scr->Width - EpigramsWidth) / 2);
EpigramsTop = (UWORD) ((Scr->Height - EpigramsHeight) / 2);
}
/***************************************************************************/
BOOL DoWindow( struct Cookie_Info * info )
{
if ( PrepareGUIEnv() == TRUE ) {
// --- PREPARE PENS ETC.
SetFont( EpigramsWnd->RPort, info->Font_Handle );
SetAPen( EpigramsWnd->RPort, _pen );
SetBPen( EpigramsWnd->RPort, _paper );
SetDrMd( EpigramsWnd->RPort, _draw_mode );
return TRUE;
} else
return FALSE;
}
/***************************************************************************/
void ReReadCookieAndPrint( struct Cookie_Info * info )
{
struct Node * cookie_line;
ULONG ctr, offx, offy, orig_offx, orig_offy;
// --- INITIALIZE OFFSETS
if ( _bevel == BEV_SYSTEM )
{
orig_offx = offx = _xpad + EpigramsWnd->BorderLeft;
offy = _ytop + EpigramsWnd->BorderTop;
}
else
{
if ((_bevel == BEV_RAISED) || (_bevel == BEV_RECESSED))
{
orig_offx = offx = _xpad + 1;
offy = _ytop + 1;
}
}
orig_offy = offy += info->Font_Handle->tf_Baseline;
// --- SEEK TO APPROPRIATE POSITION
Seek( info->data_file_handle, byte_pos, OFFSET_BEGINNING );
// --- READ THROUGH COOKIE FOR THE APPROPRIATE NUMBER OF LINES
for ( ctr = 1; ctr <= info->numlines; ctr ++ )
{
Move( EpigramsWnd->RPort, offx, offy);
// --- GET NEXT COOKIE LINE FROM LIST
cookie_line = RemHead( &info->cookie_contents );
// --- PRINT LINE IN THE WINDOW
Text(EpigramsWnd->RPort, cookie_line->ln_Name, strlen(cookie_line->ln_Name));
// --- ADVANCE THE Y CO-ORD APPROPRIATELY
offy += info->Font_Handle->tf_YSize + _lspace;
// --- FREE MEMORY ASSOCIATED WITH NODE
free( cookie_line->ln_Name );
free( cookie_line );
}
}
/***************************************************************************/
/***************************************************************************/
BOOL FindCookieDelimeter( BPTR dfile, LONG markerch )
{
BOOL done = FALSE;
LONG checkch;
Seek( dfile, byte_pos, OFFSET_BEGINNING );
while ( done == FALSE ) {
checkch = FGetC(dfile);
if ( checkch == -1 )
return( FALSE );
else if ( checkch == markerch ) {
checkch = FGetC(dfile);
if ( checkch == -1 )
return( FALSE );
if ( checkch == markerch )
done = TRUE;
}
}
return( TRUE );
}
/***************************************************************************/
BOOL CheckEOC( STRPTR str, LONG ch )
{
if ( (str[0] == ch) && (str[1] == ch) )
return( TRUE );
else
return( FALSE );
}
/***************************************************************************/
void ExpandTABs( STRPTR str )
{
int ctr, ctrb, copyctr;
char copystr[MAX_MAXLINESIZE];
copyctr = 0;
for ( ctr = 0; ctr < strlen(str); ctr ++ ) {
if ( str[ctr] == '\t' ) {
for ( ctrb = 0; ctrb < _tabsize; ctrb ++ ) {
if ( strlen( str ) < _maxlinesize ) {
copystr[copyctr] = ' ';
copyctr ++;
}
}
} else {
copystr[copyctr] = str[ctr];
copyctr ++;
}
}
// --- TERMINATE COPY STRING
copystr[copyctr] = '\0';
// --- COPY NEW STRING BACK TO ORIGINAL STRING
strcpy( str, copystr );
}
/***************************************************************************/
/****************************************************************************
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / \ \ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / SAVE FAVOURITE / / / / / / / / / / / / / / / / / / / / / / / / / / /
\ \ \ \ \ \ \ \ \ / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
****************************************************************************/
// tHIS FUNCTION SAVES OUT THE CURRENT EPIGRAM INTO A FAVORITES FILE
// -- CALLED FROM IDCMP HANDLER IN GUI.c
void SaveFavourite()
{
struct Cookie_Info info;
struct Node * cookie_line;
ULONG ctr;
BPTR fave_file_handle;
InitializeCookieInfo( &info );
// --- OPEN SOURCE FILE
if ( ! OpenDataFile( &info ) ) {
system_state = DoErrors( WARNING,
"Can't open file",
"low memory|bad filename",
"Continue|Quit" );
return;
}
// --- OPEN TARGET FILE
if ( ! (fave_file_handle = Open( _favourites_file, MODE_READWRITE )) ) {
system_state = DoErrors( WARNING,
"Can't open favourites file",
"low memory|bad filename",
"Continue|Quit" );
return;
}
// --- JUMP TO THE APPROPRIATE POSITION OF DATA FILE
Seek( info.data_file_handle, byte_pos, OFFSET_BEGINNING );
// --- JUMP TO THE APPROPRIATE POSITION OF FAVE FILE
Seek( fave_file_handle, 0, OFFSET_END );
// --- ADD DELIMITER TO FAVES FILE
FPuts( fave_file_handle, COOKIE_DELIMITER );
// --- READ COOKIE
CountCookieSize( &info );
// --- DUMP COOKIE TO FAVE FILE
for ( ctr = 1; ctr <= info.numlines; ctr ++ )
{
// --- GET NEXT LINE FROM LIST
cookie_line = RemHead( &info.cookie_contents );
// --- PRINT LINE IN FAVOURITES FILE
FPuts( fave_file_handle, cookie_line->ln_Name );
}
CloseDataFile( &info );
Close( fave_file_handle );
}
/***************************************************************************/