tablecompare.c

Go to the documentation of this file.
00001 
00002 /*  $Id: tablecompare.c 1216 2006-04-06 02:45:49Z karstenw $    */
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 "strings.h"
00032 #include "langexternal.h"
00033 #include "tableinternal.h"
00034 #include "tableverbs.h"
00035 
00036 
00037 
00038 
00039 static short tablecomparenames (hdlhashnode hnode1, hdlhashnode hnode2) {
00040     
00041     /*
00042     2004-11-09 aradke: optimized by calling compareidentifiers which does
00043     an in-place case-insensitive compare instead of copying both key strings,
00044     converting both copies completely to lowercase, and running a case-sensitive
00045     compare on the resulting lowercase strings.
00046     */
00047 
00048     return (compareidentifiers ((**hnode1).hashkey, (**hnode2).hashkey));
00049     
00050     /*
00051     bigstring bs1, bs2;
00052     
00053     gethashkey (hnode1, bs1);
00054 
00055     gethashkey (hnode2, bs2);
00056     
00057     alllower (bs1); /%comparison is unicase%/
00058     
00059     alllower (bs2);
00060     
00061     return (comparestrings (bs1, bs2));
00062     */
00063     } /*tablecomparenames*/
00064 
00065 
00066 #if !flruntime
00067 
00068 static short tablecomparekinds (hdlhashnode hnode1, hdlhashnode hnode2) {
00069     
00070     tyvaluerecord val1, val2;
00071     register tyvaluetype t1, t2;
00072     
00073     val1 = (**hnode1).val;
00074     
00075     val2 = (**hnode2).val;
00076     
00077     t1 = val1.valuetype;
00078     
00079     t2 = val2.valuetype;
00080     
00081     if ((t1 == externalvaluetype) && (t2 == externalvaluetype)) {
00082         
00083         register hdlexternalhandle h1 = (hdlexternalhandle) val1.data.externalvalue;
00084         register hdlexternalhandle h2 = (hdlexternalhandle) val2.data.externalvalue;
00085         register tyexternalid id1 = (tyexternalid) (**h1).id;
00086         register tyexternalid id2 = (tyexternalid) (**h2).id;
00087         
00088         if (id1 == id2)
00089             return (tablecomparenames (hnode1, hnode2));
00090             
00091         return (langexternalcomparetypes (id1, id2));
00092         }
00093     
00094     if (t1 == externalvaluetype) /*scalars sort before externals*/
00095         return (1);
00096         
00097     if (t2 == externalvaluetype) /*scalars sort before externals*/
00098         return (-1);
00099         
00100     if (t1 == t2)
00101         return (tablecomparenames (hnode1, hnode2));
00102         
00103     return (sgn (t1 - t2));
00104     } /*tablecomparekinds*/
00105 
00106 
00107 static short tablecomparevalues (hdlhashtable htable, hdlhashnode hnode1, hdlhashnode hnode2) {
00108     
00109     /*
00110     2.1b2 dmb: new version, corrects bugs that led to semi-random sorts.  now 
00111     only values of the same kind are compared, and the valuetype is a strict 
00112     secondary sort.
00113     */
00114     
00115     register hdlhashnode h1 = hnode1;
00116     register hdlhashnode h2 = hnode2;
00117     tyvaluerecord val1, val2, vreturned;
00118     register boolean fllessthan = false;
00119     register boolean flequal = false;
00120     boolean flcomparable;
00121     
00122     flcomparable = (**h1).val.valuetype == (**h2).val.valuetype;
00123     
00124     if (flcomparable) {
00125         
00126         pushhashtable (htable); /*any temps are allocated in this table*/
00127         
00128         disablelangerror (); /*protect us from error dialogs*/
00129         
00130         copyvaluerecord ((**h1).val, &val1);
00131         
00132         copyvaluerecord ((**h2).val, &val2);
00133         
00134         flcomparable = LTvalue (val1, val2, &vreturned);
00135         
00136         cleartmpstack (); /*temps no longer needed*/
00137         
00138         fllessthan = vreturned.data.flvalue;
00139         
00140         if (flcomparable && !fllessthan) {
00141             
00142             copyvaluerecord ((**h1).val, &val1);
00143             
00144             copyvaluerecord ((**h2).val, &val2);
00145             
00146             flcomparable = EQvalue (val1, val2, &vreturned);
00147             
00148             cleartmpstack (); /*temps no longer needed*/
00149             
00150             flequal = vreturned.data.flvalue;
00151             }
00152         
00153         enablelangerror ();
00154         
00155         pophashtable ();
00156         }
00157     
00158     if ((!flcomparable) || flequal) 
00159         return (tablecomparekinds (hnode1, hnode2));
00160     
00161     if (fllessthan)
00162         return (-1);
00163     else
00164         return (1);
00165     } /*tablecomparevalues*/
00166     
00167 #endif
00168 
00169 short tablecomparenodes (hdlhashtable htable, hdlhashnode hnode1, hdlhashnode hnode2) {
00170     
00171     /*
00172     3/31/93 dmb: return a signed value indicating less than (-1), equality (0), 
00173     or greater than (1). modified all routines that we call to do the same.
00174     */
00175     
00176     #if flruntime
00177     
00178         return (tablecomparenames (hnode1, hnode2));
00179     
00180     #else
00181         
00182         switch ((**htable).sortorder) {
00183             
00184             case sortbyname: 
00185                 return (tablecomparenames (hnode1, hnode2));
00186                 
00187             case sortbyvalue:
00188                 return (tablecomparevalues (htable, hnode1, hnode2));
00189                 
00190             case sortbykind:
00191                 return (tablecomparekinds (hnode1, hnode2));
00192             
00193             default:
00194                 return (0);
00195             } /*switch*/
00196         
00197     #endif
00198     
00199     } /*tablecomparenodes*/
00200 
00201     
00202 #if !flruntime
00203 
00204 static hdlhashnode nextnodecompare;
00205 static langcomparenodescallback origcomparenodescallback;
00206 
00207 
00208 static short tableoverridecomparenodes (hdlhashtable htable, hdlhashnode hnode1, hdlhashnode hnode2) {
00209 #pragma unused (htable, hnode1)
00210 
00211     if (hnode2 == nextnodecompare)
00212         return (-1);
00213     else
00214         return (1);
00215     } /*tableoverridecomparenodes*/
00216 
00217 
00218 void tableoverridesort (hdlhashnode hnext) {
00219     
00220     /*
00221     temporarily override the current sort, forcing the next node insertion 
00222     to "sorted" before the indicated node
00223     */
00224     
00225     origcomparenodescallback = langcallbacks.comparenodescallback;
00226     
00227     langcallbacks.comparenodescallback = &tableoverridecomparenodes;
00228     
00229     nextnodecompare = hnext; /*set global for tableeditcomparenodes*/
00230     } /*tableoverridesort*/
00231 
00232 
00233 void tablerestoresort (void) {
00234 
00235     langcallbacks.comparenodescallback = origcomparenodescallback; /*restore*/
00236     } /*tablerestoresort*/
00237 
00238 #endif
00239 
00240 
00241 

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