1/*
2 * Copyright 2003-2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stefano Ceccherini (stefano.ceccherini@gmail.com)
7 */
8
9#include "InlineInput.h"
10
11#include <cstdlib>
12
13struct clause
14{
15	int32 start;
16	int32 end;
17};
18
19
20InlineInput::InlineInput(BMessenger messenger)
21	:
22	fMessenger(messenger),
23	fActive(false),
24	fSelectionOffset(0),
25	fSelectionLength(0),
26	fNumClauses(0),
27	fClauses(NULL)
28{
29}
30
31
32InlineInput::~InlineInput()
33{
34	ResetClauses();
35}
36
37
38const BMessenger *
39InlineInput::Method() const
40{
41	return &fMessenger;
42}
43
44
45const char *
46InlineInput::String() const
47{
48	return fString.String();
49}
50
51
52void
53InlineInput::SetString(const char *string)
54{
55	fString = string;
56}
57
58
59bool
60InlineInput::IsActive() const
61{
62	return fActive;
63}
64
65
66void
67InlineInput::SetActive(bool active)
68{
69	fActive = active;
70}
71
72
73int32
74InlineInput::SelectionLength() const
75{
76	return fSelectionLength;
77}
78
79
80void
81InlineInput::SetSelectionLength(int32 length)
82{
83	fSelectionLength = length;
84}
85
86
87int32
88InlineInput::SelectionOffset() const
89{
90	return fSelectionOffset;
91}
92
93
94void
95InlineInput::SetSelectionOffset(int32 offset)
96{
97	fSelectionOffset = offset;
98}
99
100
101bool
102InlineInput::AddClause(int32 start, int32 end)
103{
104	void *newData = realloc(fClauses, (fNumClauses + 1) * sizeof(clause));
105	if (newData == NULL)
106		return false;
107
108	fClauses = (clause *)newData;
109	fClauses[fNumClauses].start = start;
110	fClauses[fNumClauses].end = end;
111	fNumClauses++;
112	return true;
113}
114
115
116bool
117InlineInput::GetClause(int32 index, int32 *start, int32 *end) const
118{
119	bool result = false;
120	if (index >= 0 && index < fNumClauses) {
121		result = true;
122		clause *clause = &fClauses[index];
123		if (start)
124			*start = clause->start;
125		if (end)
126			*end = clause->end;
127	}
128
129	return result;
130}
131
132
133int32
134InlineInput::CountClauses() const
135{
136	return fNumClauses;
137}
138
139
140void
141InlineInput::ResetClauses()
142{
143	fNumClauses = 0;
144	free(fClauses);
145	fClauses = NULL;
146}
147