aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llui/lltextparser.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2009-04-30 13:04:20 -0500
committerJacek Antonelli2009-04-30 13:07:16 -0500
commitca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e (patch)
tree8348301d0ac44a524f1819b777686bf086907d76 /linden/indra/llui/lltextparser.cpp
parentSecond Life viewer sources 1.22.11 (diff)
downloadmeta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.zip
meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.tar.gz
meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.tar.bz2
meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.tar.xz
Second Life viewer sources 1.23.0-RC
Diffstat (limited to '')
-rw-r--r--linden/indra/llui/lltextparser.cpp299
1 files changed, 299 insertions, 0 deletions
diff --git a/linden/indra/llui/lltextparser.cpp b/linden/indra/llui/lltextparser.cpp
new file mode 100644
index 0000000..925b118
--- /dev/null
+++ b/linden/indra/llui/lltextparser.cpp
@@ -0,0 +1,299 @@
1/**
2 * @file lltexteditor.cpp
3 * @brief LLTextEditor base class
4 *
5 * $LicenseInfo:firstyear=2001&license=viewergpl$
6 *
7 * Copyright (c) 2001-2009, Linden Research, Inc.
8 *
9 * Second Life Viewer Source Code
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at
21 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
22 *
23 * By copying, modifying or distributing this software, you acknowledge
24 * that you have read and understood your obligations described above,
25 * and agree to abide by those obligations.
26 *
27 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
28 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
29 * COMPLETENESS OR PERFORMANCE.
30 * $/LicenseInfo$
31 */
32
33#include "linden_common.h"
34
35#include "llsd.h"
36#include "llsdserialize.h"
37#include "llerror.h"
38#include "lluuid.h"
39#include "llstring.h"
40#include "message.h"
41#include "llmath.h"
42#include "v4color.h"
43#include "audioengine.h"
44#include "llwindow.h"
45#include "lldir.h"
46
47#include "lltextparser.h"
48//#include "lltexttospeech.h"
49
50// Routines used for parsing text for TextParsers and html
51
52LLTextParser* LLTextParser::sInstance = NULL;
53
54//
55// Constants
56//
57const F32 SOUND_GAIN = 1.0f;
58
59//
60// Member Functions
61//
62
63LLTextParser::~LLTextParser()
64{
65 sInstance=NULL;
66}
67
68// static
69LLTextParser* LLTextParser::getInstance()
70{
71 if (!sInstance)
72 {
73 sInstance = new LLTextParser();
74 sInstance->loadFromDisk();
75 }
76 return sInstance;
77}
78
79void LLTextParser::triggerAlerts(LLUUID agent_id, LLVector3d position, std::string text, LLWindow* viewer_window)
80{
81// bool spoken=FALSE;
82 for (S32 i=0;i<mHighlights.size();i++)
83 {
84 if (findPattern(text,mHighlights[i]) >= 0 )
85 {
86 if(gAudiop)
87 {
88 if ((std::string)mHighlights[i]["sound_lluuid"] != LLUUID::null.asString())
89 {
90 gAudiop->triggerSound(mHighlights[i]["sound_lluuid"].asUUID(), agent_id, SOUND_GAIN, LLAudioEngine::AUDIO_TYPE_UI, position);
91 }
92/*
93 if (!spoken)
94 {
95 LLTextToSpeech* text_to_speech = NULL;
96 text_to_speech = LLTextToSpeech::getInstance();
97 spoken = text_to_speech->speak((LLString)mHighlights[i]["voice"],text);
98 }
99 */
100 }
101 if (mHighlights[i]["flash"])
102 {
103 if (viewer_window && viewer_window->getMinimized())
104 {
105 viewer_window->flashIcon(5.f);
106 }
107 }
108 }
109 }
110}
111
112S32 LLTextParser::findPattern(const std::string &text, LLSD highlight)
113{
114 if (!highlight.has("pattern")) return -1;
115
116 std::string pattern=std::string(highlight["pattern"]);
117 std::string ltext=text;
118
119 if (!(bool)highlight["case_sensitive"])
120 {
121 ltext = utf8str_tolower(text);
122 pattern= utf8str_tolower(pattern);
123 }
124
125 size_t found=std::string::npos;
126
127 switch ((S32)highlight["condition"])
128 {
129 case CONTAINS:
130 found = ltext.find(pattern);
131 break;
132 case MATCHES:
133 found = (! ltext.compare(pattern) ? 0 : std::string::npos);
134 break;
135 case STARTS_WITH:
136 found = (! ltext.find(pattern) ? 0 : std::string::npos);
137 break;
138 case ENDS_WITH:
139 S32 pos = ltext.rfind(pattern);
140 if (pos >= 0 && (ltext.length()-pattern.length()==pos)) found = pos;
141 break;
142 }
143 return found;
144}
145
146LLSD LLTextParser::parsePartialLineHighlights(const std::string &text, const LLColor4 &color, S32 part, S32 index)
147{
148 //evil recursive string atomizer.
149 LLSD ret_llsd, start_llsd, middle_llsd, end_llsd;
150
151 for (S32 i=index;i<mHighlights.size();i++)
152 {
153 S32 condition = mHighlights[i]["condition"];
154 if ((S32)mHighlights[i]["highlight"]==PART && condition!=MATCHES)
155 {
156 if ( (condition==STARTS_WITH && part==START) ||
157 (condition==ENDS_WITH && part==END) ||
158 condition==CONTAINS || part==WHOLE )
159 {
160 S32 start = findPattern(text,mHighlights[i]);
161 if (start >= 0 )
162 {
163 S32 end = std::string(mHighlights[i]["pattern"]).length();
164 S32 len = text.length();
165 S32 newpart;
166 if (start==0)
167 {
168 start_llsd[0]["text"] =text.substr(0,end);
169 start_llsd[0]["color"]=mHighlights[i]["color"];
170
171 if (end < len)
172 {
173 if (part==END || part==WHOLE) newpart=END; else newpart=MIDDLE;
174 end_llsd=parsePartialLineHighlights(text.substr( end ),color,newpart,i);
175 }
176 }
177 else
178 {
179 if (part==START || part==WHOLE) newpart=START; else newpart=MIDDLE;
180
181 start_llsd=parsePartialLineHighlights(text.substr(0,start),color,newpart,i+1);
182
183 if (end < len)
184 {
185 middle_llsd[0]["text"] =text.substr(start,end);
186 middle_llsd[0]["color"]=mHighlights[i]["color"];
187
188 if (part==END || part==WHOLE) newpart=END; else newpart=MIDDLE;
189
190 end_llsd=parsePartialLineHighlights(text.substr( (start+end) ),color,newpart,i);
191 }
192 else
193 {
194 end_llsd[0]["text"] =text.substr(start,end);
195 end_llsd[0]["color"]=mHighlights[i]["color"];
196 }
197 }
198
199 S32 retcount=0;
200
201 //FIXME These loops should be wrapped into a subroutine.
202 for (LLSD::array_iterator iter = start_llsd.beginArray();
203 iter != start_llsd.endArray();++iter)
204 {
205 LLSD highlight = *iter;
206 ret_llsd[retcount++]=highlight;
207 }
208
209 for (LLSD::array_iterator iter = middle_llsd.beginArray();
210 iter != middle_llsd.endArray();++iter)
211 {
212 LLSD highlight = *iter;
213 ret_llsd[retcount++]=highlight;
214 }
215
216 for (LLSD::array_iterator iter = end_llsd.beginArray();
217 iter != end_llsd.endArray();++iter)
218 {
219 LLSD highlight = *iter;
220 ret_llsd[retcount++]=highlight;
221 }
222
223 return ret_llsd;
224 }
225 }
226 }
227 }
228
229 //No patterns found. Just send back what was passed in.
230 ret_llsd[0]["text"] =text;
231 LLSD color_sd = color.getValue();
232 ret_llsd[0]["color"]=color_sd;
233 return ret_llsd;
234}
235
236bool LLTextParser::parseFullLineHighlights(const std::string &text, LLColor4 *color)
237{
238 for (S32 i=0;i<mHighlights.size();i++)
239 {
240 if ((S32)mHighlights[i]["highlight"]==ALL || (S32)mHighlights[i]["condition"]==MATCHES)
241 {
242 if (findPattern(text,mHighlights[i]) >= 0 )
243 {
244 LLSD color_llsd = mHighlights[i]["color"];
245 color->setValue(color_llsd);
246 return TRUE;
247 }
248 }
249 }
250 return FALSE; //No matches found.
251}
252
253std::string LLTextParser::getFileName()
254{
255 std::string path=gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, "");
256
257 if (!path.empty())
258 {
259 path = gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, "highlights.xml");
260 }
261 return path;
262}
263
264LLSD LLTextParser::loadFromDisk()
265{
266 std::string filename=getFileName();
267 if (filename.empty())
268 {
269 llwarns << "LLTextParser::loadFromDisk() no valid user directory." << llendl;
270 }
271 else
272 {
273 llifstream file;
274 file.open(filename.c_str());
275 if (file.is_open())
276 {
277 LLSDSerialize::fromXML(mHighlights, file);
278 }
279 file.close();
280 }
281
282 return mHighlights;
283}
284
285bool LLTextParser::saveToDisk(LLSD highlights)
286{
287 mHighlights=highlights;
288 std::string filename=getFileName();
289 if (filename.empty())
290 {
291 llwarns << "LLTextParser::saveToDisk() no valid user directory." << llendl;
292 return FALSE;
293 }
294 llofstream file;
295 file.open(filename.c_str());
296 LLSDSerialize::toPrettyXML(mHighlights, file);
297 file.close();
298 return TRUE;
299}