Resolve many build warnings
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / DataBase.h
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: DataBase.h /main/6 1996/10/26 18:17:26 cde-hal $ -*- c++ -*- */
24
25 #ifndef __DataBase_h
26 #define __DataBase_h
27
28 #include "Exceptions.hh"
29 #include "object/c_codes.h" /* mmdb codes */
30 #include "oliasdb/olias_consts.h"
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 class PosixError : public Exception{
36
37 public:
38
39   enum { MAXMSG = 200 };
40   
41   PosixError(int error_no, const char *msg)
42   { f_errno = error_no;
43     *((char *) memcpy(f_msg, msg, MAXMSG+1) + MAXMSG+1) = '\0'; };
44
45   const char *msg(void) const { return f_msg; };
46   int error_no(void) const { return f_errno; };
47   
48   DECLARE_EXCEPTION(PosixError, Exception);
49   
50 private:
51   char f_msg[MAXMSG+1];
52   int f_errno;
53 };
54
55
56 #define DATABASE_DIRECTORY_MODE 0775
57
58 #define DATABASE_STDIO "-"
59
60 class DBTable;
61
62 class DB{
63 public:
64   /*
65    * USE: const char *dir = "/some/dir/for/all/the/tables";
66    *      DB *db = DB(dir);
67    *      assert(strcmp(db->path(), dir) == 0);
68    */
69   DB(const char *name); /* a directory, for now */
70   ~DB() { if ( f_name ) delete f_name; }
71   const char *path(void) { return f_name; };
72
73
74   typedef enum { READ, CREATE
75 #if DB_UPDATE
76                   , UPDATE
77 #endif
78                } Access;
79
80   /*
81    * USE: DBTable *t = db->table("NodeMeta", NODE_CODE, 4, CREATE);
82    *  where NODE_CODE is an MMDB object code, and 4
83    *  is the number of "columns" in the table, i.e. the
84    *  number of items you're going to pass to each call to
85    *  insert()
86    *
87    * CREATE creates a file named "NodeMeta" in db's directory.
88    * READ   opens a file named "NodeMeta" in db's directory.
89    */
90   DBTable *table(const char *name,
91                  int schema_code, int cols,
92                  int a = READ); /* throw PosixError */
93
94
95 private:
96   char *f_name;
97   
98 };
99
100 #define BT_NUM_DOC_FIELDS 6
101 #define BT_NUM_LOCATOR_FIELDS 3
102 #define BT_NUM_GRAPHIC_FIELDS 6
103 #define BT_NUM_STYLESHEET_FIELDS 3
104 #define BT_NUM_OLIAS_NODE_FIELDS 7
105
106 class DBTable{
107 friend class DB;
108 friend class DBCursor;
109
110 public:
111
112   ~DBTable();
113   
114   /*
115    * USE: int intlist = 4;
116    *      table->insert(INTEGER_CODE, 4,
117    *                    STRING_CODE, "xyz",
118    *                    -STRING_CODE, "abcd", 4,
119    *                    SHORT_LIST_CODE, 1, INTEGER_CODE, &intlist,
120    *                    NULL);
121    */
122   void insert(int typecode, ...);
123
124   void insert_untagged(int typecode, ...);
125
126   const char *name() { return f_name; };
127
128   void start_list();
129   void end_list();
130   
131 protected:
132   DBTable(DB* database, int schema_code, int cols, const char *name);
133
134   FILE *file(DB::Access);
135   
136 private:
137   FILE *f_file;
138   DB *f_database;
139   int f_schema_code;
140   int f_cols;
141   char *f_name;
142   int f_start; /* at start of linked list */
143 };
144
145
146 class DBCursor{
147  public:
148   DBCursor(DBTable &t);
149   ~DBCursor();
150
151   /*
152    * USE: DBTable t(...);
153    *      DBCurcor c(t);
154    *      const char *f1;
155    *      int f2
156    *
157    *      while(c.next(STRING_CODE, &f1,
158    *                   INTEGER_CODE, &f2,
159    *                   NULL)){
160    *        ...use f1, f2...
161    *      }
162    */
163   int next(int code, ...);
164
165   void undoNext(); /* seek back to the beginning of the last record read
166                     * throw(PosixError) on unseekable device
167                     */
168   void local_rewind(); /* restart at the beginning of the table file */
169   int  tell();         /* current position of the file */
170   void seekToRec( int pos ); /* seek directly to pos */
171
172  protected:
173   void string_field(FILE *, char **, int *);
174   void int_field(FILE *, int *);
175   void short_list(FILE *, int *, int, void*);
176
177  private:
178   DBTable *f_table;
179   class StringList *f_fields;
180   class StringList *f_list;
181   long f_start;
182 };
183
184
185 #endif /* __DataBase_h */