1 /*	This file is part of the software similarity tester SIM.
2 	Written by Dick Grune, Vrije Universiteit, Amsterdam.
3 	$Id: add_run.c,v 2.5 2001/11/08 12:30:28 dick Exp $
4 */
5 
6 #include	<malloc.h>
7 
8 #include	"sim.h"
9 #include	"runs.h"
10 #include	"percentages.h"
11 #include	"options.h"
12 #include	"error.h"
13 #include	"add_run.h"
14 
15 static void set_chunk(
16 	struct chunk *,
17 	struct text *,
18 	unsigned int,
19 	unsigned int
20 );
21 
22 static void set_pos(
23 	struct position *,
24 	int,
25 	struct text *,
26 	unsigned int
27 );
28 
29 void
add_run(struct text * txt0,unsigned int i0,struct text * txt1,unsigned int i1,unsigned int size)30 add_run(struct text *txt0, unsigned int i0,
31 	struct text *txt1, unsigned int i1,
32 	unsigned int size
33 ) {
34 	/*	Adds the run of given size to our collection.
35 	*/
36 	register struct run *r = (struct run *)malloc(sizeof (struct run));
37 
38 	if (!r) fatal("out of memory");
39 	set_chunk(&r->rn_cn0, txt0, i0 - txt0->tx_start, size);
40 	set_chunk(&r->rn_cn1, txt1, i1 - txt1->tx_start, size);
41 	r->rn_size = size;
42 
43 	if (option_set('p') ? add_to_percentages(r) : add_to_runs(r)) {
44 		/* OK */
45 	}
46 	else	fatal("out of memory");
47 }
48 
49 static void
set_chunk(struct chunk * cnk,struct text * txt,unsigned int start,unsigned int size)50 set_chunk(struct chunk *cnk, struct text *txt,
51 	  unsigned int start, unsigned int size
52 ) {
53 	/*	Fill the chunk *cnk with info about the piece of text
54 		in txt starting at start extending over size tokens.
55 	*/
56 	cnk->ch_text = txt;
57 	set_pos(&cnk->ch_first, 0, txt, start);
58 	set_pos(&cnk->ch_last, 1, txt, start + size - 1);
59 }
60 
61 static void
set_pos(struct position * pos,int type,struct text * txt,unsigned int start)62 set_pos(struct position *pos, int type, struct text *txt, unsigned int start) {
63 	/* Fill a single struct position */
64 	pos->ps_next = txt->tx_pos;
65 	txt->tx_pos = pos;
66 
67 	pos->ps_type = type;
68 	pos->ps_tk_cnt = start;
69 	pos->ps_nl_cnt = -1;		/* uninitialized */
70 }
71