aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm9/source/wordcompletion.cpp
blob: c66286d64d959c411e852a14f1abf69177d773d3 (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
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
#include "wordcompletion.h"
#include "osystem_ds.h"
#include "engines/agi/agi.h"	// Caution for #define for NUM_CHANNELS, causes problems in mixer_intern.h

#ifdef ENABLE_AGI

namespace DS {
// Default dictionary is about 64Kb, so 128Kb should be enough for future expansion
#define WORD_BUFFER_SIZE (128 * 1024)

// Default dictionary has ~8000 words
#define MAX_WORD_COUNT 16000

char wordBuffer[WORD_BUFFER_SIZE];
int wordBufferPos = 0;

char* wordBufferPtr[MAX_WORD_COUNT];
int wordBufferPtrPos = 0;

void addAutoCompleteLine(const char *line) {

	while (*line != 0) {
		char word[32];
		int length;

		// Skip the T9-style numbers
		while (*line != ' ') {
			line++;
		}
		line++;

		do {
			length = 0;

			if (*line == ' ') line++;


			// Copy the new word
			do {
				word[length++] = *line++;
			} while ((*line != '\0') && (*line != ' ') && (*line != '\n'));

			word[length] = '\0';


			// Store a pointer to the start of the word
			wordBufferPtr[wordBufferPtrPos++] = &wordBuffer[wordBufferPos];

			// copy the new word into the buffer
			strcpy(&wordBuffer[wordBufferPos], word);
			wordBufferPos += strlen(word) + 1;
		} while (*line == ' ');
	}
}

int stringCompare(const void* a, const void* b) {
	const char** as = (const char **) a;
	const char** bs = (const char **) b;

	return scumm_stricmp(*as, *bs);
}

void sortAutoCompleteWordList() {
	// Sort the whole word list into alphabetical order
	qsort((void *)wordBufferPtr, wordBufferPtrPos, 4, stringCompare);
}

// Sends the current available words to the virtual keyboard code for display
bool findWordCompletions(const char* input) {
	int min = 0;
	int max = wordBufferPtrPos - 1;
	char *word;
	int position;
	char partialWord[32];

	// Early out if dictionary not loaded
	if (wordBufferPtrPos == 0)
		return false;

	OSystem_DS* system = (OSystem_DS *) g_system;
	system->clearAutoComplete();

	int start = 0;
	for (int r = strlen(input) - 1; r>0; r--) {
		if (input[r] == ' ') {
			start = r + 1;
			break;
		}
	}
	strcpy(partialWord, &input[start]);

	if (*partialWord == 0) {
		return false;
	}

	do {
		position = min + ((max - min) / 2);

		// Get the word from the dictonary line
		word = wordBufferPtr[position];

		// Now check to see if the word is before or after the stub we're after
		int result = scumm_stricmp(partialWord, word);

		if (result == 0) {
			// We've found the whole word.  Aren't we good.
			break;
		} else if (result > 0) {
			// We're too early, so change the minimum position
			min = position + 1;
		} else if (result < 0) {
			// We're too early, so change the maximum position
			max = position - 1;
		}

//		consolePrintf("Word: %s, (%d, %d) result: %d\n", word, min, max, result);

	} while (max - min > 0);

	position = min;
	word = wordBufferPtr[position];
	//consolePrintf("Final word: %s\n", word);



	system->setCharactersEntered(strlen(partialWord));


	bool match = true;


	for (int r = 0; partialWord[r] != 0; r++) {
		if (word[r] != partialWord[r]) {
			match = false;
			break;
		}
	}

	if (!match) {
		position++;
		if (position == wordBufferPtrPos)
			return false;
		word = wordBufferPtr[position];
//		consolePrintf("Final word: %s\n", word);
	}


	match = true;

	do {

		for (int r = 0; partialWord[r] != 0; r++) {
			if (word[r] != partialWord[r]) {
				match = false;
				break;
			}
		}

		if (match) {
			system->addAutoComplete(word);
		}

		position++;
		if (position < wordBufferPtrPos) {
			word = wordBufferPtr[position];
		}

	} while ((match) && (position < wordBufferPtrPos));

	return true;

}

}
#endif