-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf.c
729 lines (583 loc) · 13.5 KB
/
buf.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
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
/*
This component plays a very important part in the program .
If I can make it work a bit faster , maybe I will make the whole
interpreter be more powerful , so after I design the prototype of
the interpreter , the first thing was to improve these functions .
*/
/*
A figure for my scheme:
{-------------------------------}
{ data pond }
{-------------------------------}
| |
| | load_data()
\_/
0-buffer_size-1 buffer_size+1-2buffer_size
+-------+-----+-+-------+-----+-+
| |0| |0|
+-------+-----+-+-------+-----+-+
^ ^
| |
buffer1 buffer2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "buf.h"
#include "err.h"
static char data_pond[DATA_POND_SIZE+1]; /* the data center */
static char buffer[BUFFER_SIZE<<1+2]; /* add two more senitel */
static int offset_data_pond=0; /* how many data have been accessed */
static int data_length=0; /* how many data stored in the pond */
static int forward=0; /* forward reading pointer */
static int current_used_buffer=0; /* as the name */
static int lock=0; /* indicate if the buffer can be refreshed */
static int end_of_file=FALSE;
extern FILE* src_descriptor; /* from the opt.h file */
void init_buffer()
{
int end=0;
memset(buffer,'\0',(BUFFER_SIZE<<1+2)); /* initialize the container */
end=BUFFER_ENDPOS(0)+1;
buffer[end]=EOS;
end=BUFFER_ENDPOS(1)+1;
buffer[end]=EOS;
}
int load_data()
{
data_length=fread(data_pond,sizeof(char),DATA_POND_SIZE,src_descriptor);
if (data_length==0)
{
fclose(src_descriptor);
return FALSE;
}
MAKE_STRING(data_pond,data_length);
offset_data_pond=0;
}
int input_buffer(int which_buffer)
{
/* I don't justify the property of the buffer you gave */
int buffer_start=BUFFER_STAPOS(which_buffer);
int buffer_end=BUFFER_ENDPOS(which_buffer);
int is_leading_spaces=FALSE;
if (data_length==0)
return FALSE;
while (1)
{
if (offset_data_pond==data_length)
{
if (load_data()==FALSE)
{
offset_data_pond=data_length;
break;
}
}
/* A simple preprocessor included */
/* Preserve all newline control character but remove all leading spaces */
while(buffer_start<=buffer_end)
{
if (data_pond[offset_data_pond]==END_OF_LINE)
{
buffer_start=buffer_end;
break;
}
if (data_pond[offset_data_pond]=='#') /* remove comments */
{
while (data_pond[++offset_data_pond]!='\n' && offset_data_pond<data_length);
is_leading_spaces=TRUE;
continue;
}
if (is_leading_spaces==TRUE &&
(data_pond[offset_data_pond]==' ' || /* remove spaces */
data_pond[offset_data_pond]=='\t'))
{
while (data_pond[++offset_data_pond]==' ' && offset_data_pond<data_length);
while (data_pond[offset_data_pond++]=='\t' && offset_data_pond<data_length);
--offset_data_pond;
}
if (offset_data_pond==data_length)
break;
while((buffer[buffer_start++]=data_pond[offset_data_pond++])!='#' &&
offset_data_pond<data_length && buffer_start<=buffer_end);
if (--buffer_start==buffer_end)
break;
if (offset_data_pond==data_length)
break;
else
--offset_data_pond;
is_leading_spaces=FALSE;
/* read one more characters */
}
if (buffer_start<buffer_end)
;
else
break;
}
forward=BUFFER_STAPOS(which_buffer);
return TRUE;
}
char buffer_getchar()
{
int n=BUFFER_ENDPOS(current_used_buffer);
static int next_buffer=0;
/* Maybe there was a section of comments , we must consider this situation */
/* This time I don't remove any spaces , I can't promise to be right */
while (1)
{
if (buffer[forward]=='#')
while (buffer[++forward]!='\n' && forward<=n);
if (buffer[forward]=='\n')
{
++lineno;
while (buffer[++forward]=='\n')
++lineno;
}
if (buffer[forward]==EOS)
{
next_buffer=(current_used_buffer+1) & 1;
if (is_boundary(current_used_buffer)==TRUE)
return END_OF_STREAM;
if (is_read_only(next_buffer)==TRUE)
;
else
if (input_buffer(next_buffer)==FALSE)
return FALSE;
sweep_swlock(current_used_buffer);
n=BUFFER_ENDPOS(next_buffer);
forward=BUFFER_STAPOS(next_buffer);
current_used_buffer=next_buffer;
continue;
}
else if (buffer[forward]==END_OF_FILE)
{
end_of_file=TRUE;
return END_OF_STREAM;
}
break;
}
return buffer[forward++];
}
void buffer_ungetchar()
{
/* To unget a char is not so easy as you imagine .
Self swapping lock is devised for this . */
int mylock=0;
if (is_boundary(current_used_buffer)==TRUE)
{
if (forward==(BUFFER_STAPOS(current_used_buffer)))
return;
else
;
}
else
{
GET_LOCK(SW,current_used_buffer);
lock &=mylock;
if (forward==0)
{
forward=BUFFER_ENDPOS(1);
return;
}
else if (forward==(BUFFER_STAPOS(1)))
{
forward=BUFFER_ENDPOS(0);
return;
}
}
--forward;
}
int buffer_get_interval(int start_offset,int read_offset,char* store)
{
int start_pos=(forward+start_offset) & (TOTAL_SIZE-1);
int current_pos=forward;
int next_buffer=(current_used_buffer+1)&1;
int i=0;
int skip_gap=FALSE;
/* lock current buffer and read forward ( omit comments ) */
lock_buffer(SW,current_used_buffer);
if (start_pos>(BUFFER_ENDPOS(current_used_buffer))) {
if (is_read_only(next_buffer)==TRUE)
return FALSE;
input_buffer(next_buffer);
}
forward=start_pos;
while (i<read_offset) {
if (buffer[forward]=='#')
while (buffer[++forward]!='\n' && buffer[forward]!=END_OF_FILE);
if (buffer[forward]==END_OF_FILE)
break;
if (buffer[forward]==EOS) {
if (skip_gap==FALSE) {
if (is_read_only(next_buffer)==TRUE)
break;
input_buffer(next_buffer);
forward=BUFFER_STAPOS(next_buffer);
}
else
break;
}
store[i]=buffer[forward];
++i;
++forward;
}
forward=current_pos; /* restore the original values and make it locked */
lock_buffer(SW,next_buffer);
return i+1;
}
int buffer_cond_read(read_cmp my_cond,char* my_store)
{
int i=FALSE;
char t=buffer_getchar();
while (my_cond(t)==TRUE)
{
my_store[++i]=t;
t=buffer_getchar();
}
buffer_ungetchar();
return i+1;
}
void buffer_flush(int which_buffer)
{
/*
This action will clean up the buffer , caution ! */
if (is_read_only(current_used_buffer)==TRUE)
return;
forward=BUFFER_STAPOS(which_buffer);
MAKE_STRING(buffer,forward);
}
int buffer_getline(char* usr_buf)
{
/* Do this job , I must solve three problems .
First is that the current pointed character is a '\n' symbol , so I must skip all of that .
Second , I also shuld skip all the leading spaces .
Third , when be at the end of file , but there's not a '\n' symbol , I should add it
*/
int cup=0; /* current pos */
char t=0;
t=buffer_getchar();
while (t!=END_OF_FILE && t!=';') /* a wrapper of getchar , so it's safe */
{
usr_buf[cup++]=t;
t=buffer_getchar();
}
buffer_ungetchar();
MAKE_STRING(usr_buf,cup);
return cup;
}
/* To call getchar to implement is too slow , I want it to run faster */
void buffer_skip_sentence()
{
char t=0;
int next_buffer=0;
int total=0; /* at most I skip MAX_EXPR_LENGTH characters */
while (total<=MAX_EXPR_LENGTH)
{
t=buffer[forward];
if (t==EOS)
{
next_buffer=(current_used_buffer+1)&1;
if (is_boundary(current_used_buffer)==TRUE)
unlock_buffer(RB,current_used_buffer);
if (is_read_only(next_buffer)==TRUE)
;
else
if (input_buffer(next_buffer)==FALSE)
{
--forward;
return;
}
sweep_swlock(current_used_buffer);
forward=BUFFER_STAPOS(next_buffer);
current_used_buffer=next_buffer;
continue;
}
else if (t==';')
break;
else if (t==END_OF_FILE)
{
end_of_file=TRUE;
break;
}
else if (t=='\n')
++lineno;
++total;
++forward;
}
++forward; // skip the semiconlon
}
/*=========================More about searching============================================ */
// Just in current buffer
int buffer_searchc(const char k,int offset)
{
int start_pos=forward+offset;
int i=start_pos-1;
int n=BUFFER_ENDPOS(current_used_buffer);
int en_com=FALSE; /* if encounter the comments */
while (++i<=n)
{
if (buffer[i]=='#')
{
en_com=TRUE;
continue;
}
if (en_com==TRUE)
{
while (++i<=n && buffer[i]!='\n');
en_com=FALSE;
continue;
}
else
{
if (buffer[i]==k)
return i-start_pos;
}
}
return FALSE;
}
// it promises to search until the next buffer ending
int buffer_gbl_searchc(const char k , int offset)
{
/* To execute properly , you mustn't lock the buffer */
/* To understand my approach is a bit difficult , please read developer guide */
int start_pos=forward+offset;
int i=start_pos-1;
int gap=0; /* if has crossed the gap */
int en_com=FALSE; /* if encounter the comments */
int n=BUFFER_ENDPOS(current_used_buffer);
while (1)
{
++i;
if (buffer[i]=='#')
{
en_com=TRUE;
continue;
}
else if (buffer[i]==EOS)
{
if (gap++)
break;
current_used_buffer=(current_used_buffer+1)&1;
if (input_buffer(current_used_buffer)==FALSE)
return FALSE;
lock_buffer(SW_LOCK,current_used_buffer); /* triky */
i=BUFFER_STAPOS(current_used_buffer);
n=BUFFER_ENDPOS(current_used_buffer);
continue;
}
if (en_com==TRUE)
{
while (++i<=n && buffer[i]!='\n');
en_com=FALSE;
continue;
}
else
{
if (buffer[i]==k)
return i-start_pos;
}
}
return FALSE;
}
// it also provides the functionality to search in next buffer
int buffer_searchc_until(const char k, int offset , const char d)
{
int start_pos=forward+offset;
int i=start_pos-1;
int gap=0; /* if has crossed the gap */
int en_com=FALSE; /* if encounter the comments */
int n=BUFFER_ENDPOS(current_used_buffer);
int found=0;
while (1)
{
++i;
if (buffer[i]=='#')
{
en_com=TRUE;
continue;
}
else if (buffer[i]==EOS)
{
if (gap++)
break;
current_used_buffer=(current_used_buffer+1)&1;
if (input_buffer(current_used_buffer)==FALSE)
return FALSE;
lock_buffer(SW_LOCK,current_used_buffer); /* triky */
i=BUFFER_STAPOS(current_used_buffer);
n=BUFFER_ENDPOS(current_used_buffer);
continue;
}
if (en_com==TRUE)
{
while (++i<=n && buffer[i]!='\n');
en_com=FALSE;
continue;
}
else
{
if (buffer[i]==k)
{
found=i;
break;
}
else if (buffer[i]==d)
break;
}
}
if (found==0)
return FALSE;
else
return found-start_pos;
}
int move_to(int next_pos)
{
/* We have three conditions:
1.in the same position;
2.currently be 1 , wants to be 2;
2.currently be 2 , wants to be 1;
and also I must consider the read/write lock .
*/
if (is_boundary(current_used_buffer)==TRUE)
{
if (next_pos>=(BUFFER_STAPOS(current_used_buffer)) &&
next_pos<=(BUFFER_ENDPOS(current_used_buffer)))
{
forward=next_pos;
return TRUE;
}
}
else
{
forward=next_pos;
return TRUE;
}
return FALSE;
}
int move_add_to(int offset)
{
if (is_boundary(current_used_buffer)==TRUE)
{
if ((forward+=offset)>=(BUFFER_STAPOS(current_used_buffer)) &&
forward<=(BUFFER_ENDPOS(current_used_buffer)))
return TRUE;
else
forward-=offset;
}
else
{
forward+=offset;
return TRUE;
}
return FALSE;
}
/*============================================Misc functions===========================*/
int has_no_data()
{
/* We justify this from two aspects:
1.There's no unread data in pond;
2.There's no unused data in buffer;
*/
if (data_length==0 && end_of_file==TRUE)
return TRUE;
return FALSE;
}
int get_pointer()
{
lock_buffer(1,current_used_buffer); /* hard to understand */
return forward; /* the absolute offset */
}
int lock_buffer(int type,int which)
{
/* Why I should bring in the concept of lock ?
If any function get the buffer pointer stored in its context , and later it wants to
be restored , but the buffer has been flushed , then it will get a wrong answer .
This is complete new to this version , I will discuss it in the developer guide .
*/
int signal=0;
switch (type)
{
case RB_LOCK:
signal=GET_LOCK(RB,which);
break;
case RO_LOCK:
signal=GET_LOCK(RO,which);
break;
case SW_LOCK:
signal=GET_LOCK(SW,which);
break;
default:
return FALSE; /* perhaps will not execute forever */
}
lock &=signal;
return TRUE;
}
int unlock_buffer(int type, int which)
{
int signal=0;
switch (type)
{
case RB_LOCK:
signal=(~GET_LOCK(RB,which));
break;
case RO_LOCK:
signal=(~GET_LOCK(RO,which));
break;
case SW_LOCK:
signal=(~GET_LOCK(SW,which));
break;
default:
return FALSE;
}
lock &=signal;
return TRUE;
}
int is_read_only(int which)
{
int signal=GET_LOCK(RB,which);
if ((lock & signal)!=0)
return TRUE;
signal=GET_LOCK(RO,which);
if ((lock & signal)!=0)
return TRUE;
signal=GET_LOCK(SW,which);
if ((lock & signal)!=0)
return TRUE;
return FALSE;
}
int is_boundary(int which)
{
int signal=GET_LOCK(RB,which);
if ((lock & signal)!=0)
return TRUE;
return FALSE;
}
void sweep_swlock(int which)
{
int rev_lock=GET_LOCK(SW,which);
lock &=(~rev_lock);
}
int GET_LOCK(int type,int which)
{
switch (type)
{
case RB_LOCK:
if (which==0)
return MAKE_LOCK(RB,1);
else
return MAKE_LOCK(RB,2);
case RO_LOCK:
if (which==0)
return MAKE_LOCK(RO,1);
else
return MAKE_LOCK(RO,2);
case SW_LOCK:
if (which==0)
return MAKE_LOCK(SW,1);
else
return MAKE_LOCK(SW,2);
default:
break;
}
return 1987224; /* cannot reach here */
}