search.c

Go to the documentation of this file.
00001 
00002 /*  $Id: search.c 1259 2006-04-13 04:56:46Z sethdill $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 #include "memory.h"
00032 #include "cursor.h"
00033 #include "strings.h"
00034 #include "search.h"
00035 
00036 #include "langregexp.h"
00037 
00038 
00039 tysearchparameters searchparams;
00040 
00041 
00042 boolean isword (byte *ptext, long lentext, long pos, long len) {
00043     
00044     /*
00045     return true of the charater sequence indicated by the offset and 
00046     length (pos, len) in the text stream ptext begins and ends at word 
00047     boundaries.
00048     */
00049     
00050     #ifdef MACVERSION 
00051     
00052         OffsetTable offsets;
00053         //Code change by Timothy Paustian Sunday, June 25, 2000 12:50:40 PM
00054         //updated to System 7.0 version
00055         FindWordBreaks ((Ptr) ptext, lentext, pos, true, nil, offsets, smCurrentScript);
00056         
00057         if (offsets [0].offFirst != pos)
00058             return (false);
00059         
00060         FindWordBreaks ((Ptr) ptext, lentext, pos + len, false, nil, offsets, smCurrentScript);
00061         
00062         if (offsets [0].offSecond != pos + len)
00063             return (false);
00064     
00065     #endif
00066 
00067     #ifdef WIN95VERSION
00068         
00069         if ((pos > 0) && (ptext [pos - 1] != ' '))
00070             return (false);
00071         
00072         if ((pos + len < lentext) && (ptext [pos + len] != ' '))
00073             return (false);
00074 
00075     #endif
00076 
00077     return (true);
00078     } /*isword*/
00079 
00080 
00081 boolean textsearch (byte *ptext, long lentext, long *offset, long *lenmatch) {
00082 
00083     /*
00084     Note that regexptextsearch might cause relocation of memory,
00085     i.e. if ptext points to the contents of handle, the handle must be locked.
00086     */
00087     
00088     long ixstart = *offset;
00089     long ixmatch;
00090 
00091 #ifdef flregexpverbs
00092     if (searchparams.flregexp)
00093         return (regexptextsearch (ptext, lentext, offset, lenmatch));
00094 #endif
00095     
00096     while (true) {
00097         
00098         ixmatch = textpatternmatch (ptext + ixstart, lentext - ixstart, searchparams.bsfind, searchparams.flunicase);
00099         
00100         if (ixmatch < 0)
00101             return (false);
00102         
00103         ixmatch += ixstart;
00104         
00105         if (!searchparams.flwholewords || isword (ptext, lentext, ixmatch, stringlength (searchparams.bsfind))) {
00106             
00107             *offset = ixmatch;
00108             
00109             *lenmatch = stringlength (searchparams.bsfind);
00110             
00111             return (true);
00112             }
00113         
00114         ixstart = ixmatch + 1; /*continue after beginning of false match*/
00115         }
00116     } /*textsearch*/
00117 
00118 
00119 boolean handlesearch (Handle h, long *offset, long *lenmatch) {
00120 
00121     boolean fl;
00122 
00123     lockhandle (h);
00124     
00125     fl = textsearch (BIGSTRING (*h), gethandlesize (h), offset, lenmatch);
00126     
00127     unlockhandle (h);
00128     
00129     return (fl);
00130     } /*handlesearch*/
00131 
00132 
00133 boolean stringsearch (bigstring bs, short *ix, short *length) {
00134     
00135     long offset = *ix;
00136     long len;
00137     
00138     if (!textsearch (stringbaseaddress (bs), stringlength (bs), &offset, &len))
00139         return (false);
00140     
00141     *ix = offset;
00142     *length = len;
00143     
00144     return (true);
00145     } /*stringsearch*/
00146 
00147 /*
00148 void getsearchstring (bigstring bs) {
00149     
00150     copystring (searchparams.bsorigfind, bs);
00151     
00152     if (searchparams.flunicase)
00153         alllower (bs);
00154     }*/ /*getsearchstring*/
00155 
00156 
00157 void startnewsearch (boolean flzoomfound, boolean flreplaceall) {
00158     
00159     searchparams.ctreplaced = 0;
00160     
00161     searchparams.flzoomfound = flzoomfound;
00162     
00163     searchparams.flreplaceall = flreplaceall;
00164     } /*startnewsearch*/
00165 
00166 
00167 boolean startingtosearch (long refcon) {
00168     
00169     /*
00170     9/12/91: new routine, key to maintaining search scope...
00171     
00172     a search command has been given, and the caller is about to start the 
00173     search in its context (its window), indentified (uniquely) by refcon.
00174     
00175     we determine whether or not this search command should establish a new 
00176     context; if so, refcon becomes the searchrefcon and we return true.  
00177     currently, no callers need to know if a new context was established, and 
00178     the return value is ignored.
00179     
00180     as the search proceeds, we'll use the searchrefcon to determine if 
00181     whether searches should continue from a given context, and whether 
00182     they should wrap.
00183     
00184     12/17/91 dmb: clear searchparams.flwindowzoomed flag every time -- see 
00185     tablefind.c.
00186     */
00187     
00188     initbeachball (right);
00189     
00190     searchparams.flwindowzoomed = false; /*clear flag every time*/
00191     
00192     if (!searchparams.flfirsttime)
00193         return (false);
00194     
00195     searchparams.searchrefcon = refcon;
00196     
00197     return (true);
00198     } /*startingtosearch*/
00199 
00200 
00201 boolean searchshouldcontinue (long refcon) {
00202     
00203     /*
00204     9/12/91 dmb: refcon identifies the context that has just been searched. 
00205     the caller is asking whether or not it should surface to an enclosing 
00206     context (i.e. a parent table) to continue the search.
00207     
00208     we return true if the search isn't limited to a single object, or if 
00209     the context that was just searched isn't the context where the search 
00210     began.
00211     */
00212     
00213     if (!searchparams.floneobject)
00214         return (true);
00215     
00216     return (searchparams.searchrefcon != refcon);
00217     } /*searchshouldcontinue*/
00218 
00219 
00220 boolean searchshouldwrap (long refcon) {
00221     
00222     /*
00223     9/12/91 dmb: refcon identifies the context that has just been searched. 
00224     the caller is asking whether or not it should wrap around to the top of 
00225     its context to continue the search.
00226     
00227     we return true if the search shouldn't continue in an enclosing context, 
00228     and wrapping option is enabled.
00229     */
00230     
00231     if (searchshouldcontinue (refcon))
00232         return (false);
00233     
00234     return (searchparams.flwraparound);
00235     } /*searchshouldwrap*/
00236 
00237 
00238 void endcurrentsearch (void) {
00239     
00240     /*
00241     9/12/91 dmb: the caller is indicating that the next time a search command 
00242     is issued, it should not be a continuation of the last search.
00243     */
00244     
00245     searchparams.flfirsttime = true; /*set for next time*/
00246     
00247     searchparams.ctreplaced = 0;
00248     
00249     searchparams.searchrefcon = 0;
00250     } /*endcurrentsearch*/
00251 
00252 
00253 boolean initsearch (void) {
00254     
00255     searchparams.flunicase = true;
00256     
00257     searchparams.flwraparound = true;
00258     
00259     searchparams.flwholewords = false;
00260     
00261     searchparams.flonelevel = false;
00262     
00263     searchparams.flonetype = false;
00264     
00265     searchparams.floneobject = false;
00266     
00267     searchparams.flclosebehind = false;
00268     
00269     searchparams.flzoomfound = true;
00270     
00271     searchparams.flreplaceall = false;
00272     
00273     searchparams.flfirsttime = true;
00274     
00275     searchparams.flregexp = false;
00276     
00277     searchparams.searchrefcon = 0;
00278     
00279     setemptystring (searchparams.bsorigfind);
00280     
00281     setemptystring (searchparams.bsorigreplace);
00282     
00283     setemptystring (searchparams.bsfind);
00284     
00285     setemptystring (searchparams.bsreplace);
00286 
00287 #ifdef flregexpverbs
00288     
00289     searchparams.hcompiledpattern = nil;
00290     
00291     searchparams.hovector = nil;
00292 
00293 #endif
00294     
00295     return (true);
00296     } /*initsearch*/
00297 
00298 
00299 
00300 

Generated on Wed May 31 18:20:00 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6