File: db.c

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (120 lines) | stat: -rwxr-xr-x 2,884 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *   DIS/x : An implementation of the IEEE 1278.1 protocol
 *
 *   Copyright (C) 1996, Riley Rainey (rainey@netcom.com)
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of either:
 *
 *   a) the GNU Library General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your
 *   option) any later version.  A description of the terms and conditions
 *   of the GLPL may be found in the "COPYING.LIB" file.
 *
 *   b) the "Artistic License" which comes with this Kit.  Information
 *   about this license may be found in the "Artistic" file.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License or the Artistic License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   Information describing how to contact the author can be found in the
 *   README file.
 */
#ifdef HAVE_NDBM
#include <gdbm-ndbm.h>
#else
#include <sdbm.h>
#endif
#include <dis/dis.h>
#include <sys/file.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <string.h>
#include <memory.h>
#include <stdio.h>

DBM *entities;

int
OpenDatabase ()
{
	entities = dbm_open ("dis-entities", O_RDONLY, 0644);
	if (!entities) {
		entities = dbm_open ("../data/dis-entities", O_RDONLY, 0644);
		if (!entities) {
		    fprintf (stderr, "Unable to open dis-entities database\n");
		    fprintf (stderr, "Perhaps simxd was run from the wrong directory\n");
		    exit (1);
		}
	}
	return 0;
}

int
FetchEntity (dis_entity_type *id, char *s, int size)
{
	datum	key, value;
	int	i;

	key.dptr = (char *) id;
	key.dsize = sizeof (dis_entity_type);

	value = dbm_fetch (entities, key);
	if (value.dptr) {
		memcpy (s, value.dptr, size);
		-- size;
		i = value.dsize > size ? size : value.dsize;
		s[i] = '\0';
		return 0;
	}
	return -1;
}

/*
 *  Under construction.
 */

int
FindEntity (char * pattern, dis_entity_type * match,
	char * s, int size)
{
	datum	key, value;
	char	*p;
	int	len;
	static	char *pat;

	if (pattern) {
	    key = dbm_firstkey (entities);
	    if (pat) {
	    	free (pat);
	    }
	    pat = strdup (pattern);
	}
	else {
	    key = dbm_nextkey (entities);
	}
	
	len = strlen(pat);
	
	for ( ; key.dptr ; key = dbm_nextkey(entities)) {
	    value = dbm_fetch (entities, key);
	    if ((p = strchr (value.dptr, *pattern)) != NULL) {
		if (strncasecmp (p, pattern, len) == 0) {
			memcpy (key.dptr, match,
				sizeof (dis_entity_type));
			strncpy (s, value.dptr, size);
			return 1;
		}
	    }
	}

	*s = '\0';
	return 0;
}