3
* index_marking.c -- Implements functions handling index marking
4
* for Speech Dispatcher
6
* Copyright (C) 2001,2002,2003 Brailcom, o.p.s
8
* This is free software; you can redistribute it and/or modify it
9
* under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2, or (at your option)
13
* This software is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this package; see the file COPYING. If not, write to
20
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
* Boston, MA 02111-1307, USA.
23
* $Id: index_marking.c,v 1.7 2003/10/24 09:03:22 hanke Exp $
26
#include "index_marking.h"
28
/* Insert index marks to the given message _msg_. Index marks have
29
the form @number@, the numbers begin with 0. It also escape `@' with
32
insert_index_marks(TSpeechDMessage *msg)
45
marked_text = g_string_new("");
47
MSG2(5, "index_marking", "MSG before index marking: |%s|", msg->buf);
51
ret = spd_utf8_read_char(pos, character);
52
if (ret == 0 || (strlen(character) == 0)) break;
53
u_char = g_utf8_get_char(character);
56
g_string_append_printf(marked_text, "@@");
58
else if ((u_char == '.') || (u_char == '?') || (u_char == '!')){
59
pos = g_utf8_find_next_char(pos, NULL);
60
ret = spd_utf8_read_char(pos, character2);
61
if ((ret == 0) || (strlen(character2) == 0)){
62
g_string_append_printf(marked_text, "%s", character);
63
MSG2(5, "index_marking", "MSG altering 1: |%s|", marked_text->str);
66
u_char = g_utf8_get_char(character2);
67
if (g_unichar_isspace(u_char)){
68
g_string_append_printf(marked_text, "%s@%d@%s", character, n, character2);
70
MSG2(5, "index_marking", "MSG altering 2: |%s|", marked_text->str);
72
g_string_append_printf(marked_text, "%s%s", character, character2);
73
MSG2(5, "index_marking", "MSG altering 3: |%s|", marked_text->str);
77
g_string_append_printf(marked_text, "%s", character);
80
pos = g_utf8_find_next_char(pos, NULL);
84
msg->buf = marked_text->str;
86
g_string_free(marked_text, 0);
88
MSG2(5, "index_marking", "MSG after index marking: |%s|", msg->buf);
91
/* Finds the next index mark, starting from the pointer
92
_buf_. If the index mark is encountered, it's number is
93
returned in _mark_, the position after it's end is returned
94
as the return value of this function and the position
95
of it's beginning is returned in _*begin_*, if _begin_
96
is not NULL. It returns NULL if it encounters the end
97
of the string _buf_. */
99
find_next_index_mark(char *buf, int *mark, char **begin)
108
if (buf == NULL || mark == NULL) return NULL;
111
num = g_string_new("");
113
ret = spd_utf8_read_char(pos, character);
114
if (ret == 0 || strlen(character) == 0) return NULL;
116
u_char = g_utf8_get_char(character);
117
if (index_mark == 2){
118
if (u_char != '@') index_mark = 1;
119
else if (u_char == '@'){
121
pos = g_utf8_find_next_char(pos, NULL);
125
if (index_mark == 1) g_string_append(num, character);
127
if (index_mark == 0){
128
if (begin != NULL) *begin = pos;
131
else if (index_mark == 1){
133
*mark = strtol(num->str, &tailptr, 0);
134
if (tailptr == num->str){
135
MSG(4, "index_marking",
136
"Error: Invalid index mark -- Not a number! (%s)\n", num->str);
139
g_string_free(num, 1);
140
pos = g_utf8_find_next_char(pos, NULL);
141
if (pos == NULL) return NULL;
142
MSG(5, "index_marking", "returning position of index %d", *mark);
146
pos = g_utf8_find_next_char(pos, NULL);
150
/* Finds the index mark specified in _mark_ by iterating
151
with find_next_index_mark() through _msg->buf_. Returns
152
the position after it's end. */
154
find_index_mark(TSpeechDMessage *msg, int mark)
161
MSG(5, "index_marking", "Trying to find index mark %d in |%s|", mark, msg->buf);
162
while(pos = find_next_index_mark(pos, &im, NULL)){
163
if (im == mark) return pos;
166
MSG(5, "Index mark not found.");
170
/* Deletes all index marks from the given text and substitutes
173
strip_index_marks(char *buf)
184
str = g_string_new("");
187
MSG(5, "index_marking", "Message before stripping index marks: |%s|", buf);
190
ret = spd_utf8_read_char(pos, character);
191
if (ret == 0 || (strlen(character) == 0)) break;
192
u_char = g_utf8_get_char(character);
198
pos = g_utf8_find_next_char(pos, NULL);
199
ret = spd_utf8_read_char(pos, character2);
200
if ((ret == 0) || (strlen(character2) == 0)) break;
202
u_char = g_utf8_get_char(character2);
204
g_string_append_printf(str, "@");
210
if (!inside_mark) g_string_append_printf(str, "%s", character);
213
pos = g_utf8_find_next_char(pos, NULL);
217
g_string_free(str, 0);
219
MSG(5, "index_marking", "Message after stripping index marks: |%s|", strret);