dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Query / QueryTerm.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*      copyright (c) 1994,1995,1996 FUJITSU LIMITED    */
24 /*      All Rights Reserved                             */
25
26 /*
27  * $XConsortium: QueryTerm.cc /main/6 1996/07/05 16:16:19 rws $
28  *
29  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
30  * All rights reserved.  Unpublished -- rights reserved under
31  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
32  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
33  * OR DISCLOSURE.
34  * 
35  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
36  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
37  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
38  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
39  * INTERNATIONAL, LTD.
40  * 
41  *                         RESTRICTED RIGHTS LEGEND
42  * Use, duplication, or disclosure by the Government is subject
43  * to the restrictions as set forth in subparagraph (c)(l)(ii)
44  * of the Rights in Technical Data and Computer Software clause
45  * at DFARS 252.227-7013.
46  *
47  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
48  *                  1315 Dell Avenue
49  *                  Campbell, CA  95008
50  * 
51  */
52
53
54 #define C_QueryTerm
55 #define C_QueryGroup
56 #define L_Query
57
58 #include "Prelude.h"
59
60 #include <string.h>
61 #include <ctype.h>
62
63 #if defined(sun)
64 #if defined(SVR4)
65 #define SunOS5
66 #else
67 #define SunOS4
68 #endif
69 #endif
70
71 #if defined(UseWideChars)
72 # if !defined(SunOS5)
73 #  define wslen(a) wcslen(a)
74 # endif
75 # if defined(SunOS4)
76 #   define mbstowcs(a,b,c) Xmbstowcs(a,b,c)
77 #   define wcstombs(a,b,c) Xwcstombs(a,b,c)
78 # elif defined(_IBMR2)
79 #   include <wcstr.h>
80 # endif
81 #endif
82
83 #include <wchar.h>
84 #if defined(USL) || defined(linux) || defined(CSRG_BASED)
85 #include <wctype.h>
86 #endif
87
88 #define TML_CHAR_TYPE wchar_t
89
90 #include <assert.h>
91
92 unsigned int QueryTerm::f_caps = 0;
93
94 // /////////////////////////////////////////////////////////////////
95 // class constructor
96 // /////////////////////////////////////////////////////////////////
97
98 QueryTerm::QueryTerm (QueryGroup *parent, QueryTerm *previous, QueryTerm *next)
99 : f_parent (parent), f_previous (previous), f_next (next),
100   f_prefix (PFX_CONTAIN), f_connective (C_NONE),
101   f_weight (strdup("")), f_proximity (strdup("")),
102   f_term_string_fixed (TRUE),
103   f_type (TYPE_SIMPLE)
104 {
105     f_term_string = strdup(""); 
106     // Link it into the list. 
107     if (previous != NULL)
108         f_previous->f_next = this;
109     else
110         parent->f_term_list = this;
111     if (next)
112         f_next->f_previous = this;
113 }
114
115
116 // /////////////////////////////////////////////////////////////////
117 // class destructor
118 // /////////////////////////////////////////////////////////////////
119
120 QueryTerm::~QueryTerm()
121 {
122   if (f_previous != NULL)
123     f_previous->f_next = f_next;
124   else
125     f_parent->f_term_list = f_next;
126   if (f_next != NULL)
127     f_next->f_previous = f_previous;
128   free (f_weight);
129   free (f_proximity);
130   if (f_type == TYPE_GROUP)
131     delete f_group_term;
132   else
133     free (f_term_string);
134 }
135
136
137 // /////////////////////////////////////////////////////////////////
138 // cleanup_term_string - parse out leading and trailing spaces
139 // /////////////////////////////////////////////////////////////////
140
141 void
142 QueryTerm::cleanup_term_string()
143 {
144 #ifdef UseQSearch
145   // NOTE: full-text search is a major feature of QSearch, so do not
146   //       cut off surrounding spaces - 9/22/94 kamiya
147
148   char* p = f_term_string;
149
150   int n_bytes;
151
152   for (char* last = p + strlen(p); p < last; p += n_bytes) {
153         n_bytes = mblen(p, MB_CUR_MAX);
154         assert( n_bytes > 0 );
155         if (n_bytes == 1 && isspace(*p))
156             *p = ' ';
157   }
158
159   assert( p == last );
160
161 #else 
162   // NOTE: this used to be done in place, but is now done in separate buffer
163   // for wide char support 
164   TML_CHAR_TYPE *first, *last;
165   TML_CHAR_TYPE *wcbuf;
166   int length, wclen;
167   length = strlen(f_term_string);
168
169   wcbuf = new TML_CHAR_TYPE[length + 1];
170
171 #ifdef UseWideChars
172   wclen = mbstowcs(wcbuf, f_term_string, length + 1);
173   assert( *(wcbuf + wclen) == (TML_CHAR_TYPE)'\0' );
174 #else
175   strcpy((char *) wcbuf, f_term_string);
176   wclen = length ;
177 #endif
178
179   first = wcbuf;
180
181   // Strip leading white space 
182   while (iswspace (*first))
183     first++;
184
185   // Skip to the end of the string if not already there.
186   last = wcbuf + wclen - 1;
187   while ((last > first ) && iswspace (*last)) // strip trailing spaces
188     last--;
189
190   // Truncate whitespace if there was any. 
191   if (last < wcbuf + wclen -1)
192     {
193       last++;          // advance to first trailing space 
194       *last = (TML_CHAR_TYPE)'\0';    // truncate it
195     }
196
197   // Copy back over space, if any. 
198   if ((first != wcbuf) || (last != wcbuf + wclen))
199     {
200 #ifdef UseWideChars
201       wcstombs(f_term_string, first, length + 1);
202 #else
203       strcpy(f_term_string, (const char *) first);
204 #endif
205     }
206   delete[] wcbuf;
207   f_term_string_fixed = TRUE;
208 #endif
209 }