Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / raima / keyfind.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 /* $XConsortium: keyfind.c /main/2 1996/05/09 04:09:43 drk $ */
24 /*
25  *   COMPONENT_NAME: austext
26  *
27  *   FUNCTIONS: Pi
28  *              chk_desc_key
29  *              d_keyfind
30  *
31  *   ORIGINS: 157
32  *
33  *   OBJECT CODE ONLY SOURCE MATERIALS
34  */
35 /*-----------------------------------------------------------------------
36    keyfind.c -- db_VISTA key find module.
37
38    (C) Copyright 1987 by Raima Corporation.
39 -----------------------------------------------------------------------*/
40
41 /* ********************** EDIT HISTORY *******************************
42
43  SCR    DATE    INI                   DESCRIPTION
44 ----- --------- --- -----------------------------------------------------
45       04-Aug-88 RTK MULTI_TASK changes
46   310 10-Aug-88 RSC Cleanup function prototype.
47 */
48
49 #include <stdio.h>
50 #include "vista.h"
51 #include "dbtype.h"
52
53 /* Internal function prototypes */
54 static void chk_desc_key(P1(int) Pi(FIELD_ENTRY FAR *) 
55                                         Pi(CONST char FAR *) Pi(char FAR *));
56
57 /* Find record thru key field
58 */
59 d_keyfind(field, fldval TASK_PARM DBN_PARM)
60 long  field;  /* field constant */
61 CONST char FAR *fldval; /* value of the data field */
62 TASK_DECL
63 DBN_DECL      /* database number */
64 {
65    int fld, rec;
66    DB_ADDR dba;
67    RECORD_ENTRY FAR *rec_ptr;
68    FIELD_ENTRY FAR *fld_ptr;
69    char ckey[256];
70
71    DB_ENTER(DB_ID TASK_ID LOCK_SET(RECORD_IO));
72
73    if ((nfld_check(field, &rec, &fld, (RECORD_ENTRY FAR * FAR *)&rec_ptr, (FIELD_ENTRY FAR * FAR *)&fld_ptr) != S_OKAY) ||
74        /* initialize key function operation */
75        (key_init(fld) != S_OKAY))
76       RETURN( db_status );
77
78    if ( fldval == NULL )
79       key_boundary(KEYFIND, &dba);
80    else {
81       /* locate record with specified key */
82       if (fld_ptr->fd_type == 'k') {
83          chk_desc_key(fld, fld_ptr, fldval, ckey);
84          fldval = ckey;
85       }
86       dba = NULL_DBA;
87       if ( key_locpos( fldval, &dba ) != S_OKAY )
88          RETURN( db_status );
89       
90       /* set current record to found db addr */
91       curr_rec = dba;
92    }
93 #ifndef  NO_TIMESTAMP
94    /* set timestamp */
95    if ( curr_rec && db_tsrecs )
96       d_utscr( &cr_time TASK_PARM );
97 #endif
98    RETURN( db_status = S_OKAY );
99 }
100
101
102 /* Check compound key value for descending fields
103 */
104 static void chk_desc_key(fld, fld_ptr, fldval, ckey)
105 int fld;
106 FIELD_ENTRY FAR *fld_ptr;
107 CONST char FAR *fldval;
108 char FAR *ckey;
109 {
110    register int kt_lc;                  /* loop control */
111 #ifndef  NO_FLOAT
112    float fv;
113    double dv;
114 #endif
115    char FAR *fptr;
116    char FAR *tptr;
117    FIELD_ENTRY FAR *kfld_ptr;
118    register KEY_ENTRY FAR *key_ptr;
119
120    /* complement descending compound key values */
121    for (kt_lc = size_kt - fld_ptr->fd_ptr,
122                                         key_ptr = &key_table[fld_ptr->fd_ptr];
123         (--kt_lc >= 0) && (key_ptr->kt_key == fld); ++key_ptr) {
124       kfld_ptr = &field_table[key_ptr->kt_field];
125       fptr = (char FAR *)fldval + key_ptr->kt_ptr;
126       tptr = ckey + key_ptr->kt_ptr;
127       if ( key_ptr->kt_sort == 'd' ) {
128          switch ( kfld_ptr->fd_type ) {
129 #ifndef  NO_FLOAT
130             case FLOAT:
131                bytecpy(&fv, fptr, sizeof(float));
132                fv = (float)0.0 - fv;
133                bytecpy(fptr, &fv, sizeof(float));
134                break;
135             case DOUBLE:
136                bytecpy(&dv, fptr, sizeof(double));
137                dv = 0.0 - dv;
138                bytecpy(fptr, &dv, sizeof(double));
139                break;
140 #endif
141             case CHARACTER:
142                key_cmpcpy(tptr, fptr, kfld_ptr->fd_len);
143                if ( kfld_ptr->fd_dim[0] > 1 && kfld_ptr->fd_dim[1] == 0 ) {
144                   /* make sure a null byte is at the end */
145                   tptr[kfld_ptr->fd_len-1] = '\0';
146                }
147                break;
148             default:
149                key_cmpcpy(tptr, fptr, kfld_ptr->fd_len);
150          }
151       }
152       else
153          bytecpy(tptr, fptr, kfld_ptr->fd_len);
154    }
155 }
156 /* vpp -nOS2 -dUNIX -nBSD -nVANILLA_BSD -nVMS -nMEMLOCK -nWINDOWS -nFAR_ALLOC -f/usr/users/master/config/nonwin keyfind.c */