1
2/* -----------------------------------------------------------------------------
3 * dohint.h
4 *
5 *     This file describes internally managed objects.
6 *
7 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
8 *
9 * Copyright (C) 1999-2000.  The University of Chicago
10 * See the file LICENSE for information on usage and redistribution.
11 *
12 * $Id: dohint.h 9607 2006-12-05 22:11:40Z beazley $
13 * ----------------------------------------------------------------------------- */
14
15#ifndef _DOHINT_H
16#define _DOHINT_H
17
18#include "doh.h"
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <assert.h>
24#include <ctype.h>
25#include <stdarg.h>
26
27/* Hash objects */
28typedef struct {
29  DOH *(*doh_getattr) (DOH *obj, DOH *name);	/* Get attribute */
30  int (*doh_setattr) (DOH *obj, DOH *name, DOH *value);	/* Set attribute */
31  int (*doh_delattr) (DOH *obj, DOH *name);	/* Del attribute */
32  DOH *(*doh_keys) (DOH *obj);	/* All keys as a list */
33} DohHashMethods;
34
35/* List objects */
36typedef struct {
37  DOH *(*doh_getitem) (DOH *obj, int index);	/* Get item      */
38  int (*doh_setitem) (DOH *obj, int index, DOH *value);	/* Set item      */
39  int (*doh_delitem) (DOH *obj, int index);	/* Delete item   */
40  int (*doh_insitem) (DOH *obj, int index, DOH *value);	/* Insert item   */
41  int (*doh_delslice) (DOH *obj, int sindex, int eindex);	/* Delete slice  */
42} DohListMethods;
43
44/* File methods */
45typedef struct {
46  int (*doh_read) (DOH *obj, void *buffer, int nbytes);	/* Read bytes */
47  int (*doh_write) (DOH *obj, void *buffer, int nbytes);	/* Write bytes */
48  int (*doh_putc) (DOH *obj, int ch);	/* Put character */
49  int (*doh_getc) (DOH *obj);	/* Get character */
50  int (*doh_ungetc) (DOH *obj, int ch);	/* Unget character */
51  int (*doh_seek) (DOH *obj, long offset, int whence);	/* Seek */
52  long (*doh_tell) (DOH *obj);	/* Tell */
53  int (*doh_close) (DOH *obj);	/* Close */
54} DohFileMethods;
55
56/* String methods */
57typedef struct {
58  int (*doh_replace) (DOH *obj, DOH *old, DOH *rep, int flags);
59  void (*doh_chop) (DOH *obj);
60} DohStringMethods;
61
62/* -----------------------------------------------------------------------------
63 * DohObjInfo
64 * ----------------------------------------------------------------------------- */
65
66typedef struct DohObjInfo {
67  char *objname;		/* Object name        */
68
69  /* Basic object methods */
70  void (*doh_del) (DOH *obj);	/* Delete object      */
71  DOH *(*doh_copy) (DOH *obj);	/* Copy and object    */
72  void (*doh_clear) (DOH *obj);	/* Clear an object    */
73
74  /* I/O methods */
75  DOH *(*doh_str) (DOH *obj);	/* Make a full string */
76  void *(*doh_data) (DOH *obj);	/* Return raw data    */
77  int (*doh_dump) (DOH *obj, DOH *out);	/* Serialize on out   */
78
79  /* Length and hash values */
80  int (*doh_len) (DOH *obj);
81  int (*doh_hashval) (DOH *obj);
82
83  /* Compare */
84  int (*doh_cmp) (DOH *obj1, DOH *obj2);
85
86  /* Equal */
87  int (*doh_equal) (DOH *obj1, DOH *obj2);
88
89  /* Iterators */
90  DohIterator (*doh_first) (DOH *obj);
91  DohIterator (*doh_next) (DohIterator);
92
93  /* Positional */
94  void (*doh_setfile) (DOH *obj, DOHString_or_char *file);
95  DOH *(*doh_getfile) (DOH *obj);
96  void (*doh_setline) (DOH *obj, int line);
97  int (*doh_getline) (DOH *obj);
98
99  DohHashMethods *doh_hash;	/* Hash methods       */
100  DohListMethods *doh_list;	/* List methods       */
101  DohFileMethods *doh_file;	/* File methods       */
102  DohStringMethods *doh_string;	/* String methods     */
103  void *doh_reserved;		/* Reserved           */
104  void *clientdata;		/* User data          */
105} DohObjInfo;
106
107typedef struct {
108  void *data;			/* Data pointer */
109  DohObjInfo *type;
110  void *meta;			/* Meta data */
111  unsigned int flag_intern:1;	/* Interned object */
112  unsigned int flag_marked:1;	/* Mark flag. Used to avoid recursive loops in places */
113  unsigned int flag_user:1;	/* User flag */
114  unsigned int flag_usermark:1;	/* User marked */
115  unsigned int refcount:28;	/* Reference count (max 16 million) */
116} DohBase;
117
118/* Macros for decrefing and increfing (safe for null objects). */
119
120#define Decref(a)         if (a) ((DohBase *) a)->refcount--
121#define Incref(a)         if (a) ((DohBase *) a)->refcount++
122#define Refcount(a)       ((DohBase *) a)->refcount
123
124/* Macros for manipulating objects in a safe manner */
125#define ObjData(a)        ((DohBase *)a)->data
126#define ObjSetMark(a,x)   ((DohBase *)a)->flag_marked = x
127#define ObjGetMark(a)     ((DohBase *)a)->flag_marked
128#define ObjType(a)        ((DohBase *)a)->type
129
130extern DOH *DohObjMalloc(DohObjInfo *type, void *data);	/* Allocate a DOH object */
131extern void DohObjFree(DOH *ptr);	/* Free a DOH object     */
132
133#endif				/* DOHINT_H */
134