~ubuntu-branches/ubuntu/breezy/speech-dispatcher/breezy

« back to all changes in this revision

Viewing changes to src/server/index_marking.c

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2004-05-30 12:55:54 UTC
  • Revision ID: james.westby@ubuntu.com-20040530125554-iy8f3to3bw4cldv5
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 /*
 
3
  * index_marking.c -- Implements functions handling index marking
 
4
  *                    for Speech Dispatcher
 
5
  * 
 
6
  * Copyright (C) 2001,2002,2003 Brailcom, o.p.s
 
7
  *
 
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)
 
11
  * any later version.
 
12
  *
 
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.
 
17
  *
 
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.
 
22
  *
 
23
  * $Id: index_marking.c,v 1.7 2003/10/24 09:03:22 hanke Exp $
 
24
  */
 
25
 
 
26
#include "index_marking.h"
 
27
 
 
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
 
30
`@@'.*/
 
31
void
 
32
insert_index_marks(TSpeechDMessage *msg)
 
33
{
 
34
    GString *marked_text;
 
35
    int i;
 
36
    size_t len;
 
37
    char* pos;
 
38
    char character[6];
 
39
    char character2[6];
 
40
    gunichar u_char;
 
41
    int n = 0;
 
42
    int ret;
 
43
    int insert = 0;
 
44
 
 
45
    marked_text = g_string_new("");
 
46
 
 
47
    MSG2(5, "index_marking", "MSG before index marking: |%s|", msg->buf);
 
48
       
 
49
    pos = msg->buf;
 
50
    while(pos){
 
51
        ret = spd_utf8_read_char(pos, character);
 
52
        if (ret == 0 || (strlen(character) == 0)) break;
 
53
        u_char = g_utf8_get_char(character);
 
54
 
 
55
        if (u_char == '@'){
 
56
            g_string_append_printf(marked_text, "@@");
 
57
        }
 
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);
 
64
                break;            
 
65
            }
 
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);
 
69
                n++;
 
70
                MSG2(5, "index_marking", "MSG altering 2: |%s|", marked_text->str);
 
71
            }else{
 
72
                g_string_append_printf(marked_text, "%s%s", character, character2);
 
73
                MSG2(5, "index_marking", "MSG altering 3: |%s|", marked_text->str);
 
74
            }
 
75
        }
 
76
        else{
 
77
            g_string_append_printf(marked_text, "%s", character);
 
78
        }
 
79
        
 
80
        pos = g_utf8_find_next_char(pos, NULL);   
 
81
    }
 
82
 
 
83
    spd_free(msg->buf);
 
84
    msg->buf = marked_text->str;
 
85
    
 
86
    g_string_free(marked_text, 0);
 
87
 
 
88
    MSG2(5, "index_marking", "MSG after index marking: |%s|", msg->buf);
 
89
}
 
90
 
 
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_. */
 
98
char*
 
99
find_next_index_mark(char *buf, int *mark, char **begin)
 
100
{
 
101
    char *pos;
 
102
    char character[6];
 
103
    gunichar u_char;
 
104
    int index_mark = 0;
 
105
    GString *num;
 
106
    int ret;
 
107
 
 
108
    if (buf == NULL || mark == NULL) return NULL;
 
109
 
 
110
    pos = buf;    
 
111
    num = g_string_new("");
 
112
    while(pos){
 
113
        ret = spd_utf8_read_char(pos, character);
 
114
        if (ret == 0 || strlen(character) == 0) return NULL;
 
115
 
 
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 == '@'){
 
120
                index_mark = 0;
 
121
                pos = g_utf8_find_next_char(pos, NULL);
 
122
                continue;
 
123
            }
 
124
        }
 
125
        if (index_mark == 1) g_string_append(num, character);
 
126
        if (u_char == '@'){
 
127
            if (index_mark == 0){
 
128
                if (begin != NULL) *begin = pos;
 
129
                index_mark = 2;
 
130
            }
 
131
            else if (index_mark == 1){
 
132
                char *tailptr;
 
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);
 
137
                    return NULL;
 
138
                }
 
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);
 
143
                return pos;
 
144
            }
 
145
        }
 
146
        pos = g_utf8_find_next_char(pos, NULL);
 
147
    }        
 
148
}
 
149
 
 
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. */
 
153
char*
 
154
find_index_mark(TSpeechDMessage *msg, int mark)
 
155
{
 
156
    int i;
 
157
    char *pos;
 
158
    int im;
 
159
 
 
160
    pos = msg->buf; 
 
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;
 
164
    }
 
165
 
 
166
    MSG(5, "Index mark not found.");
 
167
    return NULL;
 
168
}
 
169
 
 
170
/* Deletes all index marks from the given text and substitutes
 
171
`@@' for `@'*/
 
172
char*
 
173
strip_index_marks(char *buf)
 
174
{
 
175
    char *pos;
 
176
    GString *str;
 
177
    char *strret;
 
178
    char character[6];
 
179
    char character2[6];
 
180
    int inside_mark = 0;
 
181
    int ret;
 
182
    gunichar u_char;
 
183
 
 
184
    str = g_string_new("");
 
185
    pos = buf;
 
186
 
 
187
    MSG(5, "index_marking", "Message before stripping index marks: |%s|", buf);
 
188
 
 
189
    while(pos){
 
190
        ret = spd_utf8_read_char(pos, character);
 
191
        if (ret == 0 || (strlen(character) == 0)) break;
 
192
        u_char = g_utf8_get_char(character);
 
193
 
 
194
        if (u_char == '@'){          
 
195
            if (inside_mark){
 
196
                inside_mark = 0;
 
197
            }else{                
 
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;            
 
201
                
 
202
                u_char = g_utf8_get_char(character2);
 
203
                if (u_char == '@'){
 
204
                    g_string_append_printf(str, "@");
 
205
                }else{
 
206
                    inside_mark = 1;
 
207
                }
 
208
            }
 
209
        }else{
 
210
          if (!inside_mark) g_string_append_printf(str, "%s", character);
 
211
        }
 
212
        
 
213
        pos = g_utf8_find_next_char(pos, NULL);   
 
214
    }
 
215
 
 
216
    strret = str->str;
 
217
    g_string_free(str, 0);
 
218
 
 
219
    MSG(5, "index_marking", "Message after stripping index marks: |%s|", strret);
 
220
 
 
221
   return strret;
 
222
}