-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathviewer.c
More file actions
1439 lines (1119 loc) · 31.1 KB
/
viewer.c
File metadata and controls
1439 lines (1119 loc) · 31.1 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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Teradesk. Copyright (c) 1993 - 2002 W. Klaren,
* 2002 - 2003 H. Robbers,
* 2003 - 2013 Dj. Vukovic
*
* This file is part of Teradesk.
*
* Teradesk is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Teradesk 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 Teradesk; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
*/
#include <library.h>
#include <xdialog.h>
#include <xscncode.h>
#include "resource.h"
#include "desk.h"
#include "error.h"
#include "font.h"
#include "xfilesys.h"
#include "config.h"
#include "screen.h"
#include "window.h"
#include "viewer.h"
#include "file.h"
#include "stringf.h"
#define FWIDTH 256 /* max.possible width of text window in text mode */
#define ASCII_PERC 80 /* if so many % of printable chars, it is a text file */
WINFO textwindows[MAXWINDOWS];
GRECT tmax;
XDFONT txt_font;
static _WORD displen = -1;
/*
* Determine number of lines in a text window
*/
static long txt_nlines(TXT_WINDOW *w)
{
return (w->hexmode) ? ((w->size + 15L) / 16) : w->tlines;
}
/*
* Determine width of a text window; depending on whether the display is
* in hex mode or text mode, it will be either a fixed value or a value
* determined from the longest line in the text. Line lengths are calculated
* from differences between pointers to line beginnings
* (returned width will never be more than FWIDTH).
*/
static _WORD txt_width(TXT_WINDOW *w, _WORD hexmode)
{
long i; /* line counter */
long ll; /* current line width */
long mw; /* maximum line width */
char **wl;
char *p; /* pointer to the beginning of the current line */
char *p1; /* same, but for the next line */
/*
* If display is in hex mode, return a fixed width (HEXLEN); otherwise
* scan pointers to all lines and find greatest difference between
* two adjecent ones- that would be the maximum line length.
* To each line length is added the total length of tab substitutes.
*/
if (hexmode == 1)
return HEXLEN + 1; /* fixed window width in hex mode */
/* text is scanned only if width had been reset to 0 */
if (w->twidth == 0)
{
_WORD ts = w->tabsize;
if (w->tlines > 100)
hourglass_mouse();
wl = w->lines;
mw = 0;
for (i = 0; i < w->tlines; i++)
{
p = *wl++;
p1 = *wl;
ll = 0;
do
{
ll++;
if (*p++ == '\t')
{
ll += (ts - (ll % ts));
}
} while (p < p1);
if (ll > mw)
mw = ll;
}
/* Add the right margin */
mw += MARGIN;
arrow_mouse();
} else
{
mw = w->twidth;
}
/*
* Window width should be at least (arbitrary) 16 characters
* but not more than FWIDTH characters
*/
mw = lminmax(16L, mw, FWIDTH);
return (_WORD) mw;
}
/********************************************************************
* *
* Functions for drawing a textwindow content. *
* *
********************************************************************/
/*
* Function for converting a hexadecimal digit
* to an ASCII character
*
* Parameters:
*
* x - hexadecimal number
*
* Resultt: ASCII character
*/
static char hexdigit(_WORD x)
{
x &= 0x000F;
/* this is shorter than using a table */
return (x < 10) ? x + '0' : x + ('A' - 10);
}
/*
* Create a line in hex mode for window display or printing.
* Note: offset from file beginning will not be displayed correctly
* for files larger than 256MB.
*/
void disp_hex(char *tmp, /* output buffer tmp[HEXLEN + 2] with data in hex mode */
char *p, /* pointer to the beginning of the 16-byte line of text */
long a, /* index of 1st byte of the line from the file beginning */
long size, /* file size */
bool toprint /* true if file is being printed */
)
{
long h = a;
_WORD i;
char *tmpj, pi;
if (a >= size)
{
*tmp = 0;
return;
}
/* tmp[0] to tmp[6] display line offset from file beginning */
for (i = 6; i >= 0; i--)
{
tmp[i] = hexdigit((_WORD) h);
h >>= 4;
}
tmpj = &tmp[7];
*tmpj++ = ':';
/* Hexadecimal (in h) and ASCII (in 58+i) representation of 16 bytes */
for (i = 0; i < 16; i++)
{
*tmpj++ = ' ';
if ((a + (long) i) < size)
{
pi = p[i];
*tmpj++ = hexdigit((_WORD) (pi >> 4));
*tmpj++ = hexdigit((_WORD) pi);
if ((pi == 0) || ((toprint == TRUE) && (pi < ' ')))
pi = '.';
} else
{
*tmpj++ = ' ';
*tmpj++ = ' ';
pi = ' ';
}
tmp[58 + i] = pi;
}
tmp[56] = ' ';
tmp[57] = ' ';
tmp[74] = 0;
}
/*
* Function for reading a line from the buffer of a
* window. Depending on the mode, the presentation is in ASCII or
* hexadecimal.
*
* Parameters:
*
* w - Pointer to WINFO structur
* dest - Buffer in which the line should be placed
* line - Number of the line
*
* Result: Length of the line
*/
static _WORD txt_line(TXT_WINDOW *w, /* pointer to window being processed */
char *dest, /* pointer to destination (output) string */
long line /* line index */
)
{
char *s; /* pointer to the source string- the real line */
char *d = dest; /* pointer to a location in destination string */
_WORD i; /* counter */
_WORD wpxm = w->px - MARGIN, m = w->columns; /* rightmost column in the text */
_WORD cnt = 0; /* input character count */
_WORD cntm = 0; /* left margin visible character count */
/* Don't process invisible or nonexistent lines */
if (line >= w->nlines)
{
*dest = 0;
return 0;
}
/* Left margin in text window, if visible */
for (i = w->px; i < MARGIN; i++)
{
*d++ = ' ';
cntm++;
}
m -= cntm;
if (w->hexmode == 0)
{
/* Create a line in text mode */
char ch;
_WORD tabsize = w->tabsize;
m += w->px; /* rightmost column in text */
/* If this line does not exist at all, return NULL */
if ((s = w->lines[line]) == NULL)
{
*dest = 0;
return 0;
}
/* The real line */
while (cnt < m)
{
if ((ch = *s++) == 0) /* null: ignore */
continue;
if (ch == '\n') /* linefeed: end of line */
break;
if (ch == '\r') /* carriage return: ignore */
continue;
if (ch == '\t') /* tab: substitute */
{
do
{
if (cnt >= wpxm)
*d++ = ' ';
cnt++;
} while (((cnt % tabsize) != 0) && (cnt < m));
continue;
}
if (cnt >= wpxm) /* if righter than left edge... */
*d++ = ch; /* set that character */
cnt++;
}
} else
{
/* Create a line in hex mode */
long a = 16L * line;
char tmp[HEXLEN + 2],
*p = &w->buffer[a];
if (w->px >= HEXLEN)
{
*dest = 0;
return 0;
}
disp_hex(tmp, p, a, w->size, FALSE);
s = &tmp[wpxm + cntm]; /* same as &tmp[w->px - (MARGIN - cntm)] */
while (cnt < m)
{
if ((*d++ = *s++) == 0)
{
d--;
break;
}
cnt++;
}
}
/* Finish the line with a null */
*d = 0;
return (_WORD) (d - dest);
}
/*
* Calculate the rectangle that encloses the text to be drawn.
*/
static void txt_comparea(TXT_WINDOW *w, /* pointer to window */
long line, /* line number */
_WORD str_len, /* length of the string */
GRECT *r, GRECT *work /* GRECT structure with the size of the window's work area */
)
{
r->g_x = work->g_x;
r->g_y = work->g_y + (_WORD) (line - w->py) * txt_font.ch;
r->g_w = str_len * txt_font.cw;
r->g_h = txt_font.ch;
}
/*
* Function for printing 1 character of a line. This one
* function is scrolled to the left and right of one
* window used.
*/
static void txt_prtchar(TXT_WINDOW *w, /* pointer to window */
_WORD column, /* column in which the character is printed */
_WORD nc, /* number of columns */
long line, /* line in which the character is printed */
GRECT *area, /* clipping rectangle */
GRECT *work /* Workspace of the window */
)
{
GRECT r, in;
_WORD len, c;
char s[FWIDTH];
c = column - w->px;
len = txt_line(w, s, line);
txt_comparea(w, line, len, &r, work);
r.g_x += c * txt_font.cw;
r.g_w = nc * txt_font.cw;
if (xd_rcintersect(area, &r, &in))
{
pclear(&in);
if (c < len)
{
s[min(c + nc, len)] = 0;
w_transptext(r.g_x, r.g_y, &s[c]);
}
}
}
/*
* Function for printing a line.
*/
void txt_prtline(TXT_WINDOW *w, /* Pointer to window */
long line, /* Line that will be printed */
GRECT *area, /* Clipping rectangle */
GRECT *work /* Workspace of the window */
)
{
GRECT r, r2;
_WORD len;
char s[FWIDTH];
len = txt_line(w, s, line);
txt_comparea(w, line, len, &r, work);
r2 = r;
r2.g_w = work->g_w;
if (rc_intersect(area, &r2))
pclear(&r2);
if (rc_intersect2(area, &r))
w_transptext(r.g_x, r.g_y, s);
}
/********************************************************************
* *
* Functions for scrolling text windows. *
* *
********************************************************************/
/*
* Retype a column of a window.
*/
void txt_prtcolumn(TXT_WINDOW *w, /* pointer naar window */
_WORD column, /* first column */
_WORD nc, /* number of columns */
GRECT *area, /* clipping rechthoek */
GRECT *work /* werkgebied window */
)
{
_WORD i;
for (i = 0; i < w->rows; i++)
txt_prtchar(w, column, nc, w->py + i, area, work);
}
/********************************************************************
* *
* Functions for handling selections of a menu item *
* a window menu. *
* *
********************************************************************/
/*
* Function for updating the window menu of a text window.
* If a macro is used instead of a function, a couple of bytes
* are saved
*/
#define set_menu(x) xw_menu_icheck((WINDOW *)(x), VMHEX, x->hexmode)
/*
* Function for setting the ASCII or HEXMODE
* of a window. This routine toggles hexmode on/off
* upon each call.
*/
static void txt_mode(TXT_WINDOW *w)
{
w->hexmode ^= 1;
w->nlines = txt_nlines(w);
w->columns = txt_width(w, w->hexmode);
wd_type_sldraw((WINDOW *) w);
set_menu(w); /* check the hexmode menu item */
}
/*
* Function for setting tabulator size of one
* textwindow
*/
static void txt_tabsize(TXT_WINDOW *w)
{
_WORD oldtab, newtab;
char *vtabsize = stabsize[VTABSIZE].ob_spec.tedinfo->te_ptext;
oldtab = w->tabsize;
itoa(oldtab, vtabsize, 10);
/* Act only if there was an OK in the dialog */
if (chk_xd_dialog(stabsize, VTABSIZE) == STOK)
{
if ((newtab = atoi(vtabsize)) < 1)
newtab = 1;
/* Do it only if tab width has changed */
if (newtab != oldtab)
{
w->tabsize = newtab;
w->twidth = 0; /* force txt_width */
w->twidth = txt_width(w, 0); /* in text mode, slower */
w->columns = txt_width(w, w->hexmode);
wd_type_sldraw((WINDOW *) w);
}
}
}
/*
* Aux. size-saving function- free some allocations related to text windows
*/
static void txt_free(TXT_WINDOW *w)
{
free(w->lines);
free(w->buffer);
}
/*
* Remove a text window and delete window structure
*/
static void txt_rem(TXT_WINDOW *w)
{
txt_free(w);
free(w->path);
w->winfo->used = FALSE;
w->winfo->typ_window = NULL;
xw_delete((WINDOW *) w);
}
/*
* Window close handler for text windows.
*
* Parameters:
*
* w - Pointer to window
*/
void txt_closed(WINDOW *w)
{
xw_close(w);
txt_rem((TXT_WINDOW *) w);
}
/*
* Window menu handler for text windows.
*
* Parameters:
*
* w - Pointer to window
* title - Selected menu title
* item - Selected menu item
*/
void txt_hndlmenu(WINDOW *w, _WORD title, _WORD item)
{
/* A switch structue is used in case new menu items are introduced */
switch (item)
{
case VMTAB:
txt_tabsize((TXT_WINDOW *) w);
break;
case VMHEX:
txt_mode((TXT_WINDOW *) w);
break;
}
wd_type_nofull(w);
xw_menu_tnormal(w, title, 1);
}
/********************************************************************
* *
* Functions for loading files in a window. *
* *
********************************************************************/
/*
* Funkties voor het tellen van de regels van een file.
* Return number of lines counted.
*/
static long count_lines(char *buffer, /* pointer to buffer contaning file */
long length /* buffer (file) length */
)
{
long cnt = length;
long n = 1;
char *s = buffer;
do
{
if (*s++ == '\n')
n++;
} while (--cnt > 0);
return n;
}
/*
* Funktie voor het maken van een lijst met pointers naar het begin
* van elke regel in de file.
*
* Line end is detected by "\n" character (linefeed)
* so this will be ok for TOS/DOS and Unix files but not for Mac files
* (there, lineend is carriage-return only).
*/
static void set_lines(char *buffer, /* pointer to buffer containing file */
char **lines, /* pointer to array of pointers to line beginnings */
long length /* file buffer length */
)
{
char *s = buffer;
char **p = lines;
long cnt = length;
*p++ = s;
do
{
if (*s++ == '\n') /* find lineends */
{
*p++ = s;
}
} while (--cnt > 0);
*p = buffer + length;
}
/*
* Check if a character is printable.
* This is a (sort of) substitute for the simpler Pure-C function isprint().
* BEWARE that this function accepts an extended range of characters
* compared to isprint();
*
* Acceptable range: ' ' to '~' (32 to 126), <tab>, <cr>, <lf>
* Also, characters higher than 127 (c < 0) are assumed to be printable
* for the sake of codepages of other languages.
*/
static bool isxprint(signed char c)
{
if ((c >= ' ' && c <= '~') || c == '\t' || c == '\r' || c == '\n' || c < 0)
return TRUE;
return FALSE;
}
/*
* Load a text file for the purpose of displaying in a window,
* or for the purpose of searching to find a string in the file
* or for any other purpose where found convenient.
* Function returns error code, if there is any, or 0 for OK.
* If pointer parameter "tlines" is NULL, it is assumed that the routine will
* be used for non-display purpose; then lines will not be counted and set.
* Note: for safety reasons, the routine allocates several bytes
* more than needed.
*/
static _WORD read_txtfile(const char *name, /* name of file to read */
char **buffer, /* location of the buffer to receive text */
long *flength, /* size of the file being read */
long *tlines, /* number of text lines in the file */
char ***lines /* pointer to array of pointers to beginnings of text lines */
)
{
_WORD handle; /* file handle */
_WORD error; /* error code */
long read; /* number of bytes read from the file */
XATTR attr; /* file attributes */
*flength = 0L;
/* Get file attributes (follow the link in x_attr) */
if ((error = (_WORD) x_attr(0, FS_INQ, name, &attr)) == 0)
{
*flength = attr.st_size; /* output param. file length */
/* Open the file */
if ((attr.st_mode & S_IFMT) != S_IFREG)
{
error = ENODEV;
} else if ((handle = x_open(name, O_DENYW | O_RDONLY)) >= 0)
{
/* Allocate buffer for complete file; read file into it */
if ((*buffer = malloc(*flength + 4L)) == NULL)
{
error = ENOMEM;
} else
{
read = x_read(handle, *flength, *buffer);
/* If completely read with success... */
if (read == *flength)
{
/* And this has been read for a display in a window */
if (tlines != NULL)
{
/*
* There must be a linefeed -after- the end of file
* or the display routine will not know when to stop
*/
(*buffer)[*flength] = '\n';
/* Count text lines in the file (in fact, in the buffer) */
*tlines = count_lines(*buffer, *flength);
/*
* Allocate memory for pointers to beginnings of lines,
* then find those beginnings.
*/
if (((*lines = malloc((*tlines + 1) * sizeof(char *))) != NULL))
set_lines(*buffer, *lines, *flength);
else
{
error = ENOMEM;
free(*lines);
free(*buffer);
}
}
} else
{
error = (read < 0) ? (_WORD) read : EIO;
free(*buffer);
}
}
x_close(handle);
} else
{
error = handle;
}
}
return error;
}
/*
* A shorter form of the above, with smaller number of arguments.
*/
_WORD read_txtf(const char *name, /* name of file to read */
char **buffer, /* location of the buffer to receive text */
long *flength /* size of the file being read */
)
{
return read_txtfile(name, buffer, flength, NULL, NULL);
}
/*
* Funktie voor het laden van een file in het geheugen.
* Bij een leesfout wordt een waarde ongelijk 0 terug gegeven.
* Alle buffers zijn dan vrijgegeven.
* File name is contained in w->path;
* ASCII or Hex mode is determined from the first 256 bytes of file.
* Af at least 80% of those characters are ASCII printable ones,
* it is assumed that text mode should be applied.
*/
static _WORD txt_read(TXT_WINDOW *w, /* pointer to window where the file will be displayed */
bool setmode /* if true, determine whether to use text or hex mode */
)
{
_WORD error;
hourglass_mouse();
/* Read complete file */
error = read_txtfile(w->path, &(w->buffer), &(w->size), &(w->tlines), &(w->lines));
arrow_mouse();
if (error != 0)
{
w->lines = NULL;
w->buffer = NULL;
} else
{
w->twidth = 0; /* force txt_width to do something */
w->twidth = txt_width(w, 0); /* width in text mode (slower) */
if (setmode)
{
char *b;
_WORD i;
_WORD e;
_WORD n = 0;
b = w->buffer;
e = (_WORD) lmin(w->size, 256L); /* longest possible line */
for (i = 0; i < e; i++)
{
if (isxprint(*b++))
n++;
}
n = (n * 100) / (e + 1); /* e can be 0 */
if (n > ASCII_PERC)
w->hexmode = 0;
else
w->hexmode = 1;
set_menu(w);
}
/* Determine window text length and width */
w->nlines = txt_nlines(w);
w->columns = txt_width(w, w->hexmode);
}
return error;
}
/*
* Read or reread a file into the same window.
* If filename is not given (is NULL), keep old name and reread old file.
*/
bool txt_reread(TXT_WINDOW *w, /* pointer to window into which the file is reread */
char *name, /* file name */
_WORD px, /* position of horizontal slider (column index) */
long py /* position of vertical slider (line index) */
)
{
txt_free(w);
if (name)
{
free(w->path);
w->path = name;
}
if (txt_read(w, TRUE) >= 0)
{
w->px = px;
w->py = py;
wd_type_title((TYP_WINDOW *) w); /* set title AND sliders */
wd_type_draw((TYP_WINDOW *) w, FALSE);
return TRUE;
}
return FALSE;
}
/*
* Compare contents of two buffers; return index of first difference.
* If buffers are identical, return buffer length
*/
static long compare_buf(char *buf1, /* pointer to buffer #1 */
char *buf2, /* pointer to buffer #2 */
long len /* how many bytes to compare */
)
{
long i;
for (i = 0; i < len; i++)
{
if (*buf1++ != *buf2++)
break;
}
return i;
}
/*
* Copy a number of bytes from source to destination, starting from
* index "pos", substituting all nulls with something else that is printable;
* Not more than "dl" bytes will be copied, or less if buffer end (nul char)
* is encountered first. Add a nul char at the end. In order for all this
* to work, "dl" must first be given a positive value.
*
* Note: "pos" should never be larger than "length" !!!! no checking done !
* ("length" is length from the beginning of source, not counted from "pos").
*/
void copy_unnull(char *dest, /* pointer to destination */
const char *source, /* pointer to source */
long length, /* length of source */
long pos, /* starting position in the soruce */
_WORD dl /* length of the display field */
)
{
_WORD i, n;
const char *s = source + pos;
char *d = dest;
n = (_WORD) lmin(length - pos, (long) dl);
for (i = 0; i < n; i++)
{
*d = *s++;
/* Some characters can't be printed; substitute them */
if (*d == 0)
*d = SUBST_DISP;
d++;
}
*d = 0;
}
/*
* Compare two files. Display differences found.
* Note: both files are completely read into memory
* (can be inconvenient for large files).
*
* This routine attempts to compare files with some intelligence:
* if a difference is found, the routine tries to resynchronize the
* contents of the files after a difference.
*/
void compare_files(WINDOW *w, _WORD n, _WORD *list)
{
char *name; /* name(s) of the file(s) */
char *buf1 = NULL; /* buffer for file #1 */
char *buf2 = NULL; /* buffer for file #2 */
_WORD i; /* a counter */
_WORD sw; /* size of compare window */
_WORD swx2; /* almost twice of above */
_WORD button; /* response in the alert box */
_WORD error = 0; /* error code from reading files */
long size1; /* size of file #1 */
long size2; /* size of file #2 */
long i1; /* index in the first buffer */
long i2; /* index in the second buffer */
long in; /* next start index for comparison */
long i1n; /* new i1 */
long i2n; /* new i2 */
long i2o = 0; /* previous i2 */
long nc; /* number of bytes to compare */
long ii; /* counter of synchronization attempts */
XDINFO info; /* dialog info structure */
bool sync = TRUE; /* false while attempting to resynchronize */
bool diff = FALSE; /* true if not equal files */