File: db4.1%2B%2B-stuff.cc

package info (click to toggle)
animals 201007161925-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 196 kB
  • ctags: 131
  • sloc: cpp: 1,809; makefile: 65
file content (377 lines) | stat: -rw-r--r-- 8,224 bytes parent folder | download | duplicates (2)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) 1997,2002 by Jim Lynch.
// Copyright (c) 2010 by Philipp Schafft.
// This software comes with NO WARRANTY WHATSOEVER.
//
// This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; version 2 dated June, 1991, or, at your
//    option, any LATER version.
//
//    This program 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program;  if not, write to the Free Software
//    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
//    02110-1301 USA
//
// On Debian Linux systems, the complete text of the GNU General
// Public License can be found in `/usr/doc/copyright/GPL' (on some
// installations) or /usr/share/common-licenses/GPL (on newer 
// ones).

#include "db4++-stuff.h"
#include "main.h"

#include <iostream>
#include <sstream>
#include <errno.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <malloc.h>

#include "str-conversions.h"

long lastKeyAssigned;

int Write(Db &theFile, const std::string &theKey, const std::string &theValue)
{
  int putBad;
  Dbt key;
  Dbt value;
  
  key.set_data((void*) theKey.c_str());
  key.set_size(strlen(theKey.c_str()) + 1);
  
  value.set_data((void*) theValue.c_str());
  value.set_size(strlen(theValue.c_str()) + 1);
  
  try
    {
      putBad = theFile.put(NULL, &key, &value, 0);
    }
  catch(DbException &e)
    {
      std::cerr << "db2++ Write(Db filehandle, ";
      std::cerr << '"' << theKey << '"';
      std::cerr << ", ";
      std::cerr << '"' << theValue << '"';
      std::cerr << "): " << e.what() << std::endl;
      putBad = 1; // or throw game exception here?
    }

  return ! putBad;
}

int Read(Db &theFile, const std::string &theKey, std::string &theValue)
{
  int getBad;
  Dbt key;
  Dbt value;
  
  key.set_data((void*) theKey.c_str());
  key.set_size(strlen(theKey.c_str()) + 1);
  
  try
    {
      getBad = theFile.get(NULL, &key, &value, 0);
    }
  catch(DbException &e)
    {
      std::cerr << "db3++ Read(Db filehandle, ";
      std::cerr << '"' << theKey << '"';
      std::cerr << ", ";
      std::cerr << "(some value placeholder)";
      std::cerr << "): " << e.what() << std::endl;
      getBad = 1; // or throw game exception here?
    }

  if(value.get_data())
    {
      theValue = (char*) value.get_data();
    }
  else
    {
      theValue = "";
    }

  return ! getBad;
}



Db *OpenFile(const char * dbfile)
{
  Db *theFile;
  struct stat dummy;
  
  if(stat(dbfile, &dummy) == -1) // stat failed?
    {
      if(errno == ENOENT) // file not found?
	{
	  int createBad;
	  
	  // create the file here, fully; ready for animals.
	  
	  mode_t saveMode = umask(0);
	  
	  try
	    {
	      theFile = new Db(NULL, 0);
	      createBad = theFile->open
                           (
			     NULL, /* transaction object ptr (unused) */
		             dbfile,
			     NULL, /* name of database within file (unused) */
			     DB_BTREE /* use B+Tree structure */,
			     DB_CREATE,
			     0660
		           );
	    }
	  catch(DbException &e)
	    {
	      std::cerr << "db2++ Open(): while trying to create";
	      std::cerr << ": " << e.what() << std::endl;
	    }
	  
	  umask(saveMode);
	  
	  if(createBad) // can't create?
	    {
	      // errno may be set now.
	      // Deal with it...
	      
	      std::cout << progName << ": OpenFile: file " << dbfile
		        << ": errno=" << errno << std::endl;
	      
	      exit(-1);
	    }
	  else
	    {
	      // new gdbm file. Add initial infrastructure.

	      int putBad;

	      putBad = ! Write(*theFile, "last", "0");

	      //Dbt key;
	      //Dbt lastAddedKey;
	      //
	      //key.set_data((void*) "last");
	      //key.set_size(5);
	      //
	      //lastAddedKey.set_data((void*) "0");
	      //lastAddedKey.set_size(2);
	      //
	      //try
	      //{
	      //  putBad = theFile->put(NULL, &key, &lastAddedKey, 0);
	      //}
	      //catch(DbException &e)
	      //{
	      //  cerr << e.what() << endl;
	      //  putBad = 1; // or throw game exception here?
	      //}
	    }
	} // endif(errno == ENOENT)
      else
	{
	}
    }
  else
    {
      int chmod_res, openBad;
      
      try
	{
	  theFile = new Db(NULL, 0);
	  openBad = theFile->open
	               (
			NULL, /* transaction obj ptr (unused) */
		        dbfile,
			NULL, /* name of database within file (unused) */
			DB_BTREE /* use B+Tree structure */,
			0,
			0660
		      );
	}
      catch(DbException &e)
	{
	  std::cerr << e.what() << std::endl;
	  openBad = 1; // or throw game exception here?
	}
      
      if(openBad) // can't open existing db file?
	{
	  // either or both of gdbm_errno and errno may be set now.
	  // Deal with it...
	  
	  std::cout << progName << ": OpenFile: file " << dbfile;
	  std::cout << ": errno=" << errno << std::endl;
	  
	  exit(-1);
	}
      else
	{
	  // file is open for update; get "last" into lastKeyAssigned
	  
	  std::string lastString;
	  int getBad;
	  
	  getBad = ! Read(*theFile, "last", lastString);

	  //Dbt key((void*) "last", 5);
	  //Dbt value;
	  //
	  //try
	  //  {
	  //    getBad = theFile->get(NULL, &key, &value, 0);
	  //  }
	  //catch(DbException &e)
	  //  {
	  //    cerr << e.what() << endl;
	  //    getBad = 1; // or throw a game exception?
	  //  }
	  //
	  
	  if(lastString == "") // should not be null; 
	                       // if it is, "last" is missing
	    {
	      std::cout << "'last' key value missing: database seems corrupt";
	      std::cout << std::endl;
	      
	      theFile->close(0);
	      exit(-1);
	    }
	  
	  std::istringstream *theLastStream 
	    = new std::istringstream(lastString.c_str());
	  
	  (*theLastStream) >> lastKeyAssigned;
	  
	  delete theLastStream;
	}
    }
  
  return theFile;
}

void CloseFile(Db &handle)
{
  try
    {
      handle.close(0);
    }
  catch(DbException &e)
    {
      std::cerr << e.what() << std::endl;
      // throw game exception here?
    }
}

void SetLast(Db &handle, const std::string &newKey)
{
  int wrOK = Write(handle, "last", newKey);
  
  // Dbt key((void*) "last", 5);
  // char *p;
  // 
  // p = MakeCStringCopy(newKey);
  // 
  // Dbt content((void*) p, strlen(p) + 1);
  // 
  // try
  //   {
  //     handle.del(NULL, &key, 0);
  //     handle.put(NULL, &key, &content, 0);
  //   }
  // catch(DbException &e)
  //   {
  //     std::cerr << e.what() << endl;
  //     // maybe propigate the exception to a game exception
  //   }
  // 
  // delete[] p;
}

std::string GetLast(Db &handle)
{
  std::string result;
  
  int rdOK = Read(handle, "last", result);
  
  // Dbt content;
  // Dbt key((void*) "last", 5);
  // int getOK = 1;
  // 
  // try
  //   {
  //     handle.get(NULL, &key, &content, 0);
  //   }
  // catch(DbException &e)
  //   {
  //     cerr << e.what() << endl;
  //     getOK = 0;
  //   }
  //
  // if(getOK)
  //   {
  //     if(content.get_data())
  //       {
  //         result = (char*) content.get_data();
  //       }
  //   }
  // else
  //   {
  //     // throw game exception?
  //   }
  
  return result;
}

int IsAnimal(Db &handle, const std::string &theKey)
{
  int result = 0;
  std::string theValue;
  int getOK = Read(handle, theKey, theValue);
  
  char firstChar = theValue[0];
  result = (firstChar == 'a');
  
  // char *p = MakeCStringCopy(theKey);
  // Dbt key((void*) p, strlen(p) + 1);
  // Dbt content;
  // 
  // try
  //   {
  //     handle.get(NULL, &key, &content, 0);
  //   }
  // catch(DbException &e)
  //   {
  //     cerr << e.what() << endl;
  //     getOK = 0;
  //   }
  // 
  // if(p)
  //   delete[] p;
  // 
  // if(getOK)
  //   {
  //     p = (char *) content.get_data();
  //
  //     if(p)
  //       if(*p == 'a')
  //         result = 1;
  //   }
  // else
  //   {
  //     // perhaps throw an exception here relevent to the game...
  //   }
  
  return result;
}