summaryrefslogtreecommitdiff
path: root/fpcsrc/utils/sim_pasc/add_run.c
blob: c7f6a75d174a323bfb6f4ce851084a2929cfaef1 (plain)
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
/*	This file is part of the software similarity tester SIM.
	Written by Dick Grune, Vrije Universiteit, Amsterdam.
	$Id: add_run.c,v 2.5 2001/11/08 12:30:28 dick Exp $
*/

#include	<malloc.h>

#include	"sim.h"
#include	"runs.h"
#include	"percentages.h"
#include	"options.h"
#include	"error.h"
#include	"add_run.h"

static void set_chunk(
	struct chunk *,
	struct text *,
	unsigned int,
	unsigned int
);

static void set_pos(
	struct position *,
	int,
	struct text *,
	unsigned int
);

void
add_run(struct text *txt0, unsigned int i0,
	struct text *txt1, unsigned int i1,
	unsigned int size
) {
	/*	Adds the run of given size to our collection.
	*/
	register struct run *r = (struct run *)malloc(sizeof (struct run));

	if (!r) fatal("out of memory");
	set_chunk(&r->rn_cn0, txt0, i0 - txt0->tx_start, size);
	set_chunk(&r->rn_cn1, txt1, i1 - txt1->tx_start, size);
	r->rn_size = size;

	if (option_set('p') ? add_to_percentages(r) : add_to_runs(r)) {
		/* OK */
	}
	else	fatal("out of memory");
}

static void
set_chunk(struct chunk *cnk, struct text *txt,
	  unsigned int start, unsigned int size
) {
	/*	Fill the chunk *cnk with info about the piece of text
		in txt starting at start extending over size tokens.
	*/
	cnk->ch_text = txt;
	set_pos(&cnk->ch_first, 0, txt, start);
	set_pos(&cnk->ch_last, 1, txt, start + size - 1);
}

static void
set_pos(struct position *pos, int type, struct text *txt, unsigned int start) {
	/* Fill a single struct position */
	pos->ps_next = txt->tx_pos;
	txt->tx_pos = pos;

	pos->ps_type = type;
	pos->ps_tk_cnt = start;
	pos->ps_nl_cnt = -1;		/* uninitialized */
}