2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
25 * (c) Copyright 1993, 1994 International Business Machines Corp. *
26 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
27 * (c) Copyright 1993, 1994 Novell, Inc. *
32 * $TOG: DtsDb.c /main/10 1998/10/23 13:48:04 mgreess $
34 * RESTRICTED CONFIDENTIAL INFORMATION:
36 * The information in this document is subject to special
37 * restrictions in a confidential disclosure agreement bertween
38 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
39 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
40 * Sun's specific written approval. This documment and all copies
41 * and derivative works thereof must be returned or destroyed at
44 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
51 #include <sys/types.h>
58 #include <Dt/DbReader.h>
60 #include <Dt/UserMsg.h>
61 #include "DtSvcLock.h"
63 extern char *strdup(const char *);
67 typedef int (*genfunc)(const void *, const void *);
69 static DtDtsDbDatabase **db_list;
70 static int num_db = 0;
73 _DtDtsDbPrintFields(DtDtsDbRecord *rec_ptr, FILE *fd)
76 DtDtsDbField *fld_ptr;
78 for(fld = 0; fld < rec_ptr->fieldCount; fld++)
80 fld_ptr = rec_ptr->fieldList[fld];
81 fprintf(fd, "\t\t[%d]\t%s\t%s\n", fld,
82 XrmQuarkToString(fld_ptr->fieldName),
84 fld_ptr->fieldValue:"(NULL)");
89 _DtDtsDbPrintRecords(DtDtsDbDatabase *db_ptr, FILE *fd)
92 DtDtsDbRecord *rec_ptr;
95 fprintf(fd, "%d Records\n", db_ptr->recordCount);
96 for(rec = 0; rec < db_ptr->recordCount; rec++)
98 rec_ptr = db_ptr->recordList[rec];
99 fprintf(fd, "\tRec[%d] name = %s\n\t%d Fields\n", rec,
100 XrmQuarkToString(rec_ptr->recordName),
101 rec_ptr->fieldCount);
102 _DtDtsDbPrintFields(rec_ptr, fd);
104 _DtSvcProcessUnlock();
108 _DtDtsDbPrint(FILE *org_fd)
112 DtDtsDbDatabase *db_ptr;
113 DtDtsDbRecord *rec_ptr;
117 for(db = 0; db < num_db; db++)
123 db_ptr = db_list[db];
127 if((fd = fopen(db_ptr->databaseName, "w")) == NULL)
130 DtProgName, DtError, NULL,
131 db_ptr->databaseName, NULL);
135 fprintf(fd, "DB[%d] ", db);
136 fprintf(fd, "name = %s\n", db_ptr->databaseName);
137 _DtDtsDbPrintRecords(db_ptr, fd);
144 _DtSvcProcessUnlock();
148 _DtDtsDbCompareRecordNames(DtDtsDbRecord **a, DtDtsDbRecord **b)
150 return ((*a)->recordName - (*b)->recordName);
154 _DtDtsDbCompareFieldNames(DtDtsDbField **a, DtDtsDbField **b)
156 return ((*a)->fieldName - (*b)->fieldName);
164 DtDtsDbDatabase **db;
168 db = db_list = (DtDtsDbDatabase **)calloc(num_db+3, sizeof(DtDtsDbDatabase *));
169 db_list[0] = (DtDtsDbDatabase *)calloc(1, sizeof(DtDtsDbDatabase));
170 db_list[0]->databaseName = (char *)strdup(DtDTS_DC_NAME);
171 db_list[0]->ActionSequenceNumber = 0;
174 db_list[1] = (DtDtsDbDatabase *)calloc(1, sizeof(DtDtsDbDatabase));
175 db_list[1]->databaseName = (char *)strdup(DtDTS_DA_NAME);
176 db_list[1]->ActionSequenceNumber = 0;
179 _DtSvcProcessUnlock();
190 for ( i = 0; db_list[i]; i++ );
194 list = (char **)calloc(i+1, sizeof(char *));
195 for ( i = 0; db_list[i]; i++ )
197 list[i] = (char *)strdup(db_list[i]->databaseName);
200 _DtSvcProcessUnlock();
205 _DtDtsDbAddDatabase( char *db_name )
208 DtDtsDbDatabase **new_db_list;
209 DtDtsDbDatabase *ret_db;
216 for ( i = 0; db_list[i]; i++ )
218 if ( !strcmp(db_list[i]->databaseName,db_name) )
221 * A database with the given name already exists.
222 * return a pointer to the existing database.
225 _DtSvcProcessUnlock();
231 * We now have a count of the existing databases.
232 * allocate enough space for the existing databases + the new one
233 * + a NULL pointer to terminate the vector.
236 new_db_list = (DtDtsDbDatabase **)calloc(i+2,sizeof(DtDtsDbDatabase *));
238 memmove(new_db_list,db_list,sizeof(DtDtsDbDatabase *) * i );
239 new_db_list[i] = (DtDtsDbDatabase *)calloc(1, sizeof(DtDtsDbDatabase));
240 new_db_list[i]->databaseName = strdup(db_name);
241 new_db_list[i]->ActionSequenceNumber = 0;
243 db_list = new_db_list;
247 _DtSvcProcessUnlock();
253 _DtDtsDbDeleteDb(DtDtsDbDatabase *db)
259 _DtDtsDbDeleteRecords(db);
260 free(db->databaseName);
263 for(i = 0; db_list[i]; i++)
272 db_list[i] = db_list[i+1];
280 _DtSvcProcessUnlock();
286 _DtDtsDbGet(char *name)
288 DtDtsDbDatabase *ret_db;
296 for(i = 0; db_list && db_list[i] && db_list[i]->databaseName; i++)
298 if(strcmp(db_list[i]->databaseName, name) == 0)
301 _DtSvcProcessUnlock();
306 _DtSvcProcessUnlock();
311 _DtDtsDbFieldSort(DtDtsDbRecord *rec, _DtDtsDbFieldCompare compare)
315 compare = _DtDtsDbCompareFieldNames;
317 qsort(rec->fieldList,
319 sizeof(DtDtsDbField *),
321 rec->compare = compare;
325 _DtDtsDbRecordSort(DtDtsDbDatabase *db, _DtDtsDbRecordCompare compare)
329 compare = _DtDtsDbCompareRecordNames;
331 qsort(db->recordList,
333 sizeof(DtDtsDbRecord *),
335 db->compare = compare;
339 _DtDtsDbGetField(DtDtsDbRecord *rec, char *name)
344 * Field names have been quarked so quark 'name' and
345 * do a linear search for the quark'ed field name.
347 XrmQuark tmp = XrmStringToQuark (name);
349 for (i = 0; i < rec->fieldCount; i++)
351 if (rec->fieldList[i]->fieldName == tmp)
353 return (rec->fieldList[i]);
360 _DtDtsDbGetFieldByName(DtDtsDbRecord *rec, char *name)
362 DtDtsDbField *result;
364 result = _DtDtsDbGetField(rec, name);
367 return(result->fieldValue);
377 _DtDtsDbGetRecordByName(DtDtsDbDatabase *db, char *name)
380 DtDtsDbRecord **result;
381 DtDtsDbRecord *s = &srch;
383 XrmQuark name_quark = XrmStringToQuark(name);
386 * If the fields are not sorted in alphanumeric order
387 * by name a binary search will fail. So do the slow but
388 * sure linear search.
390 if(db->compare != _DtDtsDbCompareRecordNames)
393 for (i = 0; i < db->recordCount; i++)
395 if (db->recordList[i]->recordName == name_quark)
397 return (db->recordList[i]);
403 srch.recordName = name_quark;
405 if(db->recordCount == 0 || db->recordList == NULL)
411 result = (DtDtsDbRecord **)bsearch(&s,
414 sizeof(DtDtsDbRecord *),
415 (genfunc)_DtDtsDbCompareRecordNames);
430 _DtDtsDbAddRecord(DtDtsDbDatabase *db)
432 DtDtsDbRecord **newlist;
433 int rec = db->recordCount;
435 db->compare = (_DtDtsDbRecordCompare)NULL;
438 newlist = (DtDtsDbRecord **)calloc(rec+PADMEM,
439 sizeof(DtDtsDbRecord *));
442 memmove(newlist, db->recordList,
443 rec*sizeof(DtDtsDbRecord *));
444 free(db->recordList);
446 db->recordList = newlist;
448 db->recordList[rec] = (DtDtsDbRecord *)calloc(1, sizeof(DtDtsDbRecord));
451 return(db->recordList[rec]);
455 _DtDtsDbDeleteRecord(DtDtsDbRecord *rec, DtDtsDbDatabase *db)
459 _DtDtsDbDeleteFields(rec);
462 for(i = 0; i < db->recordCount; i++)
464 if(db->recordList[i] == rec)
466 memmove( &(db->recordList[i]),
467 &(db->recordList[i+1]),
468 (db->recordCount - i - 1)*
469 sizeof(DtDtsDbRecord *));
479 _DtDtsDbDeleteRecords(DtDtsDbDatabase *db)
483 for(i = 0; i < db->recordCount; i++)
485 _DtDtsDbDeleteFields(db->recordList[i]);
486 free(db->recordList[i]);
488 free(db->recordList);
494 _DtDtsDbAddField(DtDtsDbRecord *rec)
496 DtDtsDbField **newlist;
497 int flds = rec->fieldCount;
501 newlist = (DtDtsDbField **)calloc(flds+PADMEM,
502 sizeof(DtDtsDbField *));
505 memmove(newlist, rec->fieldList,
506 flds*sizeof(DtDtsDbField *));
507 free(rec->fieldList);
509 rec->fieldList = newlist;
511 rec->fieldList[flds] = (DtDtsDbField *)calloc(1, sizeof(DtDtsDbField));
514 return(rec->fieldList[flds]);
518 _DtDtsDbDeleteField(DtDtsDbField *fld, DtDtsDbRecord *rec)
523 for(i = 0; i < rec->fieldCount; i++)
525 if(rec->fieldList[i] == fld)
527 memmove( &(rec->fieldList[i]),
528 &(rec->fieldList[i+1]),
529 (rec->fieldCount - i - 1)*
530 sizeof(DtDtsDbField *));
540 _DtDtsDbDeleteFields(DtDtsDbRecord *rec)
544 for(i = 0; i < rec->fieldCount; i++)
546 free(rec->fieldList[i]->fieldValue);
547 free(rec->fieldList[i]);
549 free(rec->fieldList);
555 _DtDtsMMCreateFile(DtDirPaths *dirs, const char *CacheFile)
557 return _MMWriteDb(dirs, num_db, db_list, CacheFile);