Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : : #include "unotools/unotoolsdllapi.h"
20 : :
21 : : #ifndef _UNOTOOLS_TEXTSEARCH_HXX
22 : : #define _UNOTOOLS_TEXTSEARCH_HXX
23 : : #include <i18npool/lang.h>
24 : : #include <rtl/ustring.hxx>
25 : : #include <tools/string.hxx>
26 : : #include <com/sun/star/uno/Reference.h>
27 : : #include <com/sun/star/lang/Locale.hpp>
28 : : #include <com/sun/star/util/XTextSearch.hpp>
29 : : #include <com/sun/star/util/SearchOptions.hpp>
30 : :
31 : : class CharClass;
32 : :
33 : : namespace com {
34 : : namespace sun {
35 : : namespace star {
36 : : namespace util {
37 : : struct SearchResult;
38 : : }
39 : : }
40 : : }
41 : : }
42 : :
43 : : // ............................................................................
44 : : namespace utl
45 : : {
46 : : // ............................................................................
47 : :
48 : : // Utility class for searching
49 [ # # ]: 0 : class UNOTOOLS_DLLPUBLIC SearchParam
50 : : {
51 : : public:
52 : : enum SearchType{ SRCH_NORMAL, SRCH_REGEXP, SRCH_LEVDIST };
53 : :
54 : : private:
55 : : String sSrchStr; // the search string
56 : : String sReplaceStr; // the replace string
57 : :
58 : : SearchType m_eSrchType; // search normal/regular/LevDist
59 : :
60 : : int m_bWordOnly : 1; // used by normal search
61 : : int m_bSrchInSel : 1; // search only in the selection
62 : : int m_bCaseSense : 1; //
63 : :
64 : : // values for the "weight Levenshtein-Distance"
65 : : int bLEV_Relaxed : 1;
66 : : int nLEV_OtherX;
67 : : int nLEV_ShorterY;
68 : : int nLEV_LongerZ;
69 : :
70 : : // asian flags - used for the transliteration
71 : : long nTransliterationFlags;
72 : :
73 : : public:
74 : : SearchParam( const String &rText,
75 : : SearchType eSrchType = SearchParam::SRCH_NORMAL,
76 : : sal_Bool bCaseSensitive = sal_True,
77 : : sal_Bool bWordOnly = sal_False,
78 : : sal_Bool bSearchInSelection = sal_False );
79 : :
80 : : // Wrapper to use OUString as parameter
81 : 0 : SearchParam( const ::rtl::OUString &rText,
82 : : SearchType eSrchType = SearchParam::SRCH_NORMAL,
83 : : sal_Bool bCaseSensitive = sal_True,
84 : : sal_Bool bWordOnly = sal_False,
85 : : sal_Bool bSearchInSelection = sal_False )
86 [ # # ]: 0 : {
87 [ # # ]: 0 : String rText2(rText);
88 [ # # ][ # # ]: 0 : SearchParam( rText2, eSrchType, bCaseSensitive, bWordOnly, bSearchInSelection );
[ # # ]
89 : 0 : }
90 : :
91 : : SearchParam( const SearchParam& );
92 : :
93 : 0 : const String& GetSrchStr() const { return sSrchStr; }
94 : 0 : const String& GetReplaceStr() const { return sReplaceStr; }
95 : 0 : SearchType GetSrchType() const { return m_eSrchType; }
96 : :
97 : 0 : int IsCaseSensitive() const { return m_bCaseSense; }
98 : 0 : int IsSrchInSelection() const { return m_bSrchInSel; }
99 : 0 : int IsSrchWordOnly() const { return m_bWordOnly; }
100 : :
101 : :
102 : : void SetSrchStr( const String& rStr ) { sSrchStr = rStr; }
103 : : void SetReplaceStr( const String& rStr ) { sReplaceStr = rStr; }
104 : : void SetSrchType( SearchType eType ) { m_eSrchType = eType; }
105 : :
106 : : void SetCaseSensitive( int bFlag ) { m_bCaseSense = bFlag; }
107 : : void SetSrchInSelection( int bFlag ) { m_bSrchInSel = bFlag; }
108 : : void SetSrchWordOnly( int bFlag ) { m_bWordOnly = bFlag; }
109 : :
110 : 0 : int IsSrchRelaxed() const { return bLEV_Relaxed; }
111 : 0 : int GetLEVOther() const { return nLEV_OtherX; }
112 : 0 : int GetLEVShorter() const { return nLEV_ShorterY; }
113 : 0 : int GetLEVLonger() const { return nLEV_LongerZ; }
114 : :
115 : : void SetSrchRelaxed( int bFlag ) { bLEV_Relaxed = bFlag; }
116 : : void SetLEVOther( int nValue ) { nLEV_OtherX = nValue; }
117 : : void SetLEVShorter( int nValue ) { nLEV_ShorterY = nValue; }
118 : : void SetLEVLonger( int nValue ) { nLEV_LongerZ = nValue; }
119 : :
120 : 0 : long GetTransliterationFlags() const { return nTransliterationFlags; }
121 : : void SetTransliterationFlags( long nValue ) { nTransliterationFlags = nValue; }
122 : : };
123 : :
124 : : // Utility class for searching a substring in a string.
125 : : // The following metrics are supported
126 : : // - ordinary text (Bayer/Moore)
127 : : // - regular expressions
128 : : // - weighted Levenshtein distance
129 : : //
130 : : // This class allows forward and backward searching!
131 : :
132 : : class UNOTOOLS_DLLPUBLIC TextSearch
133 : : {
134 : : static ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch >
135 : : getXTextSearch( const ::com::sun::star::util::SearchOptions& rPara );
136 : :
137 : : com::sun::star::uno::Reference < com::sun::star::util::XTextSearch >
138 : : xTextSearch;
139 : :
140 : : void Init( const SearchParam & rParam,
141 : : const ::com::sun::star::lang::Locale& rLocale );
142 : :
143 : : public:
144 : : // rText is the string being searched for
145 : : // this first two CTORs are deprecated!
146 : : TextSearch( const SearchParam & rPara, LanguageType nLanguage );
147 : : TextSearch( const SearchParam & rPara, const CharClass& rCClass );
148 : :
149 : : TextSearch( const ::com::sun::star::util::SearchOptions& rPara );
150 : : ~TextSearch();
151 : :
152 : : /* search in the (selected) text the search string:
153 : : rScrTxt - the text, in in which we search
154 : : pStart - start position for the search
155 : : pEnde - end position for the search
156 : :
157 : : RETURN values == sal_True: something is found
158 : : - pStart start pos of the found text,
159 : : - pStart end pos of the found text,
160 : : - pSrchResult - the search result with all found
161 : : positions. Is only filled with more positions
162 : : if the regular expression handles groups.
163 : :
164 : : == sal_False: nothing found, pStart,pEnde unchanged.
165 : :
166 : : Definitions: start pos always inclusive, end pos always exclusive!
167 : : The position must always in the right direction!
168 : : search forward: start <= end
169 : : search backward: end <= start
170 : : */
171 : : int SearchFrwrd( const String &rStr,
172 : : xub_StrLen* pStart, xub_StrLen* pEnde,
173 : : ::com::sun::star::util::SearchResult* pSrchResult = 0 );
174 : : sal_Bool SearchForward( const ::rtl::OUString &rStr,
175 : : sal_Int32* pStart, sal_Int32* pEnd,
176 : : ::com::sun::star::util::SearchResult* pRes = 0 );
177 : : int SearchBkwrd( const String &rStr,
178 : : xub_StrLen* pStart, xub_StrLen* pEnde,
179 : : ::com::sun::star::util::SearchResult* pSrchResult = 0 );
180 : :
181 : : void SetLocale( const ::com::sun::star::util::SearchOptions& rOpt,
182 : : const ::com::sun::star::lang::Locale& rLocale );
183 : :
184 : : /* replace back references in the replace string by the sub expressions from the search result */
185 : : void ReplaceBackReferences( String& rReplaceStr, const String &rStr, const ::com::sun::star::util::SearchResult& rResult );
186 : :
187 : : };
188 : :
189 : : // ............................................................................
190 : : } // namespace utl
191 : : // ............................................................................
192 : :
193 : : #endif
194 : :
195 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|