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 INCLUDED_UNOTOOLS_TEXTSEARCH_HXX
22 : #define INCLUDED_UNOTOOLS_TEXTSEARCH_HXX
23 : #include <i18nlangtag/lang.h>
24 : #include <rtl/ustring.hxx>
25 : #include <com/sun/star/uno/Reference.h>
26 : #include <com/sun/star/lang/Locale.hpp>
27 : #include <com/sun/star/util/XTextSearch.hpp>
28 : #include <com/sun/star/util/SearchOptions.hpp>
29 :
30 : class CharClass;
31 :
32 : namespace com {
33 : namespace sun {
34 : namespace star {
35 : namespace util {
36 : struct SearchResult;
37 : }
38 : }
39 : }
40 : }
41 :
42 : namespace utl
43 : {
44 :
45 : // Utility class for searching
46 : class UNOTOOLS_DLLPUBLIC SearchParam
47 : {
48 : public:
49 : enum SearchType{ SRCH_NORMAL, SRCH_REGEXP, SRCH_LEVDIST };
50 :
51 : private:
52 : OUString sSrchStr; // the search string
53 : OUString sReplaceStr; // the replace string
54 :
55 : SearchType m_eSrchType; // search normal/regular/LevDist
56 :
57 : bool m_bWordOnly : 1; // used by normal search
58 : bool m_bSrchInSel : 1; // search only in the selection
59 : bool m_bCaseSense : 1;
60 :
61 : // values for the "weight Levenshtein-Distance"
62 : bool bLEV_Relaxed : 1;
63 : int nLEV_OtherX;
64 : int nLEV_ShorterY;
65 : int nLEV_LongerZ;
66 :
67 : // asian flags - used for the transliteration
68 : long nTransliterationFlags;
69 :
70 : public:
71 : SearchParam( const OUString &rText,
72 : SearchType eSrchType = SearchParam::SRCH_NORMAL,
73 : bool bCaseSensitive = true,
74 : bool bWordOnly = false,
75 : bool bSearchInSelection = false );
76 :
77 : SearchParam( const SearchParam& );
78 :
79 : ~SearchParam();
80 :
81 6 : const OUString& GetSrchStr() const { return sSrchStr; }
82 6 : const OUString& GetReplaceStr() const { return sReplaceStr; }
83 6 : SearchType GetSrchType() const { return m_eSrchType; }
84 :
85 6 : bool IsCaseSensitive() const { return m_bCaseSense; }
86 5 : bool IsSrchInSelection() const { return m_bSrchInSel; }
87 1 : bool IsSrchWordOnly() const { return m_bWordOnly; }
88 :
89 : void SetSrchStr( const OUString& rStr ) { sSrchStr = rStr; }
90 : void SetReplaceStr( const OUString& rStr ) { sReplaceStr = rStr; }
91 : void SetSrchType( SearchType eType ) { m_eSrchType = eType; }
92 :
93 : void SetCaseSensitive( int bFlag ) { m_bCaseSense = bFlag; }
94 : void SetSrchInSelection( int bFlag ) { m_bSrchInSel = bFlag; }
95 : void SetSrchWordOnly( int bFlag ) { m_bWordOnly = bFlag; }
96 :
97 0 : bool IsSrchRelaxed() const { return bLEV_Relaxed; }
98 0 : int GetLEVOther() const { return nLEV_OtherX; }
99 0 : int GetLEVShorter() const { return nLEV_ShorterY; }
100 0 : int GetLEVLonger() const { return nLEV_LongerZ; }
101 :
102 : void SetSrchRelaxed( int bFlag ) { bLEV_Relaxed = bFlag; }
103 : void SetLEVOther( int nValue ) { nLEV_OtherX = nValue; }
104 : void SetLEVShorter( int nValue ) { nLEV_ShorterY = nValue; }
105 : void SetLEVLonger( int nValue ) { nLEV_LongerZ = nValue; }
106 :
107 6 : long GetTransliterationFlags() const { return nTransliterationFlags; }
108 : void SetTransliterationFlags( long nValue ) { nTransliterationFlags = nValue; }
109 : };
110 :
111 : // Utility class for searching a substring in a string.
112 : // The following metrics are supported
113 : // - ordinary text (Bayer/Moore)
114 : // - regular expressions
115 : // - weighted Levenshtein distance
116 :
117 : // This class allows forward and backward searching!
118 :
119 : class UNOTOOLS_DLLPUBLIC TextSearch
120 : {
121 : static ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch >
122 : getXTextSearch( const ::com::sun::star::util::SearchOptions& rPara );
123 :
124 : com::sun::star::uno::Reference < com::sun::star::util::XTextSearch >
125 : xTextSearch;
126 :
127 : void Init( const SearchParam & rParam,
128 : const ::com::sun::star::lang::Locale& rLocale );
129 :
130 : public:
131 : // rText is the string being searched for
132 : // this first two CTORs are deprecated!
133 : TextSearch( const SearchParam & rPara, LanguageType nLanguage );
134 : TextSearch( const SearchParam & rPara, const CharClass& rCClass );
135 :
136 : TextSearch( const ::com::sun::star::util::SearchOptions& rPara );
137 : ~TextSearch();
138 :
139 : /* search in the (selected) text the search string:
140 : rScrTxt - the text, in which we search
141 : pStart - start position for the search
142 : pEnde - end position for the search
143 :
144 : RETURN values == true: something is found
145 : - pStart start pos of the found text,
146 : - pStart end pos of the found text,
147 : - pSrchResult - the search result with all found
148 : positions. Is only filled with more positions
149 : if the regular expression handles groups.
150 :
151 : == false: nothing found, pStart,pEnde unchanged.
152 :
153 : Definitions: start pos always inclusive, end pos always exclusive!
154 : The position must always in the right direction!
155 : search forward: start <= end
156 : search backward: end <= start
157 : */
158 : bool SearchForward( const OUString &rStr,
159 : sal_Int32* pStart, sal_Int32* pEnd,
160 : ::com::sun::star::util::SearchResult* pRes = 0 );
161 : bool SearchBackward( const OUString &rStr,
162 : sal_Int32* pStart, sal_Int32* pEnd,
163 : ::com::sun::star::util::SearchResult* pRes = 0 );
164 :
165 : void SetLocale( const ::com::sun::star::util::SearchOptions& rOpt,
166 : const ::com::sun::star::lang::Locale& rLocale );
167 :
168 : /* replace back references in the replace string by the sub expressions from the search result */
169 : void ReplaceBackReferences( OUString& rReplaceStr, const OUString &rStr, const ::com::sun::star::util::SearchResult& rResult );
170 :
171 : };
172 :
173 : } // namespace utl
174 :
175 : #endif
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|