Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / raima / fillnew.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: fillnew.c /main/3 1996/08/12 12:33:46 cde-ibm $ */
24 /*
25  *   COMPONENT_NAME: austext
26  *
27  *   FUNCTIONS: d_fillnew
28  *
29  *   ORIGINS: 157
30  *
31  *   OBJECT CODE ONLY SOURCE MATERIALS
32  */
33 /*-----------------------------------------------------------------------
34    fillnew.c -- db_VISTA (filled) record creation module
35
36    (C) Copyright 1987 by Raima Corp.
37 -----------------------------------------------------------------------*/
38
39 /* ********************** EDIT HISTORY *******************************
40
41  SCR    DATE    INI                   DESCRIPTION
42 ----- --------- --- -----------------------------------------------------
43   158 15-JUN-88 RSC inform key_bldcom NOT to complement compound keys
44       04-Aug-88 RTK MULTI_TASK changes
45
46 */
47 #include <stdio.h>
48 #include "vista.h"
49 #include "dbtype.h"
50
51 #define FALSE 0
52 #define TRUE 1
53
54
55 /* Create and fill a new record
56 */
57 d_fillnew( nrec, recval TASK_PARM DBN_PARM )
58 int nrec;       /* record number */
59 CONST char FAR *recval; /* record value */
60 TASK_DECL
61 DBN_DECL      /* database number */
62 {
63 #ifndef  NO_TIMESTAMP
64    ULONG timestamp;
65 #endif
66    DB_ADDR db_addr;
67    INT recnum, stat;
68    FILE_NO file;
69    F_ADDR rec_addr;
70    char FAR *ptr;
71    char key[256];
72    RECORD_ENTRY FAR *rec_ptr;
73    FIELD_ENTRY FAR *fld_ptr;
74    int fld, fldtot;
75
76    DB_ENTER(DB_ID TASK_ID LOCK_SET(RECORD_IO));
77
78    if (nrec_check(nrec, &nrec, (RECORD_ENTRY FAR * FAR *)&rec_ptr) != S_OKAY)
79       RETURN( db_status );
80    recnum = NUM2EXT(nrec, rt_offset);
81
82    /* check for duplicate keys */
83    db_addr = curr_rec;
84    for (fld = rec_ptr->rt_fields, fldtot = fld + rec_ptr->rt_fdtot,
85                                                    fld_ptr = &field_table[fld];
86         (fld < fldtot) || (fld_ptr->fd_type == COMKEY);
87         ++fld, ++fld_ptr) {
88       if ((fld_ptr->fd_key == UNIQUE) && !(fld_ptr->fd_flags & OPTKEYMASK)) {
89          if (fld_ptr->fd_type != COMKEY)
90             ptr = (char FAR *)recval + fld_ptr->fd_ptr - rec_ptr->rt_data;
91          else
92             key_bldcom(fld, (char FAR *)recval, ptr = key, FALSE); /* Don't complement */
93          d_keyfind(FLDMARK*(long)recnum + (fld - rec_ptr->rt_fields), ptr 
94                       TASK_PARM DBN_PARM);
95          curr_rec = db_addr;
96          if ( db_status == S_OKAY ) 
97             RETURN( db_status = S_DUPLICATE );
98       }
99    }
100    /* pull out the file number */
101    file = rec_ptr->rt_file;
102    db_addr = (NUM2EXT(file, ft_offset) & FILEMASK) << FILESHIFT;
103
104    /* select a record pointer to use */
105    if ( dio_pzalloc(file, &rec_addr) != S_OKAY )
106       RETURN( db_status );
107
108    db_addr |= rec_addr;
109
110    /* read record */
111    if (dio_read(db_addr, (char FAR * FAR *)&ptr, PGHOLD) != S_OKAY)
112       RETURN( db_status );
113
114    /* zero fill the record */
115    byteset(ptr, 0, rec_ptr->rt_len);
116
117    /* place the record number and db_addr at the start of the record */
118    bytecpy( ptr, &recnum, sizeof(INT) );
119    bytecpy( ptr+sizeof(INT), &db_addr, DB_ADDR_SIZE );
120 #ifndef  NO_TIMESTAMP
121    /* check for timestamp */
122    if ( rec_ptr->rt_flags & TIMESTAMPED ) {
123       timestamp = dio_pzgetts(file);
124       bytecpy( ptr + RECCRTIME, &timestamp, sizeof(LONG));
125       bytecpy( ptr + RECUPTIME, &timestamp, sizeof(LONG));
126    }
127    else timestamp = 0;
128 #endif
129    /* copy the record value into place */
130    bytecpy( ptr+rec_ptr->rt_data, recval, rec_ptr->rt_len-rec_ptr->rt_data );
131
132    /* for each keyed field, enter the key value into the key file */
133    for (fld = rec_ptr->rt_fields, fldtot = fld + rec_ptr->rt_fdtot,
134                                                    fld_ptr = &field_table[fld];
135         (fld < fldtot) || (fld_ptr->fd_type == COMKEY);
136         ++fld, ++fld_ptr) {
137       if ((fld_ptr->fd_key != 'n') && !(fld_ptr->fd_flags & OPTKEYMASK)) {
138          if ( fld_ptr->fd_type != COMKEY )
139             ptr = (char FAR *)recval + fld_ptr->fd_ptr - rec_ptr->rt_data;
140          else
141             key_bldcom(fld, (char FAR *)recval, ptr = key, TRUE);
142          if ((stat = key_insert(fld, ptr, db_addr)) != S_OKAY) {
143             r_delrec( nrec, db_addr );
144             dio_write(db_addr, NULL, PGFREE);
145             RETURN( db_status = stat );
146          }
147       }
148    }
149    /* 'touch' page, do page's update housekeeping */
150    if ( dio_write(db_addr, NULL, PGFREE) == S_OKAY ) {
151       curr_rec = db_addr;
152
153 #ifndef  NO_TIMESTAMP
154       if ( db_tsrecs ) cr_time = timestamp;
155 #endif
156    }
157    RETURN( db_status );
158 }
159 /* vpp -nOS2 -dUNIX -nBSD -nVANILLA_BSD -nVMS -nMEMLOCK -nWINDOWS -nFAR_ALLOC -f/usr/users/master/config/nonwin fillnew.c */