~ubuntu-branches/debian/jessie/gnucash-docs/jessie

« back to all changes in this revision

Viewing changes to doc-tools/dbadd.c

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-04-20 00:19:06 UTC
  • Revision ID: james.westby@ubuntu.com-20040420001906-40kllkvvkxlxr9u7
Tags: upstream-1.8.4
ImportĀ upstreamĀ versionĀ 1.8.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************\
 
2
 * This program is free software; you can redistribute it and/or    *
 
3
 * modify it under the terms of the GNU General Public License as   *
 
4
 * published by the Free Software Foundation; either version 2 of   *
 
5
 * the License, or (at your option) any later version.              *
 
6
 *                                                                  *
 
7
 * This program is distributed in the hope that it will be useful,  *
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
 
10
 * GNU General Public License for more details.                     *
 
11
 *                                                                  *
 
12
 * You should have received a copy of the GNU General Public License*
 
13
 * along with this program; if not, contact:                        *
 
14
 *                                                                  *
 
15
 * Free Software Foundation           Voice:  +1-617-542-5942       *
 
16
 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
 
17
 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
 
18
 *                                                                  *
 
19
\********************************************************************/
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <errno.h>
 
24
#include <fcntl.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <sys/stat.h>
 
29
#include <sys/types.h>
 
30
#include <unistd.h>
 
31
 
 
32
/* needed for db.h with 'gcc -ansi -pedantic' */
 
33
#ifndef _BSD_SOURCE
 
34
#  define _BSD_SOURCE 1
 
35
#endif
 
36
 
 
37
#ifdef PREFER_DB1
 
38
#ifdef HAVE_DB1_DB_H
 
39
# include <db1/db.h>
 
40
#else
 
41
# ifdef HAVE_DB_185_H
 
42
#  include <db_185.h>
 
43
# else
 
44
#  include <db.h>
 
45
# endif
 
46
#endif
 
47
#else
 
48
#ifdef HAVE_DB_185_H
 
49
# include <db_185.h>
 
50
#else
 
51
# ifdef HAVE_DB_H
 
52
#  include <db.h>
 
53
# else
 
54
#  include <db1/db.h>
 
55
# endif
 
56
#endif
 
57
#endif
 
58
 
 
59
 
 
60
#define ZERO(Dbt) memset (&(Dbt), sizeof (DBT), 0)
 
61
 
 
62
static DB *database;
 
63
 
 
64
 
 
65
static void
 
66
usage (const char *name)
 
67
{
 
68
  fprintf (stderr, "Usage: %s database key1 value1 key2 value2 ...\n",
 
69
           name ? name : "");
 
70
  exit(1);
 
71
}
 
72
 
 
73
int
 
74
main (int argc, char *argv[])
 
75
{
 
76
  const char *db_name;
 
77
  int i;
 
78
 
 
79
  HASHINFO info;
 
80
 
 
81
  memset (&info, 0, sizeof (info));
 
82
  info.bsize = 256;
 
83
  info.ffactor = 8;
 
84
  info.nelem = (argc-2)>>1>0 ? (argc-2)>>1 : 1;
 
85
  info.cachesize = 0;
 
86
  info.hash = 0;
 
87
  info.lorder = 0;
 
88
 
 
89
  if (argc < 2)
 
90
    usage (argv[0]);
 
91
 
 
92
  if (argc % 2 != 0)
 
93
    usage (argv[0]);
 
94
 
 
95
  db_name = argv[1];
 
96
 
 
97
  database = dbopen (db_name, O_CREAT | O_RDWR, 0644, DB_HASH, &info);
 
98
  if (!database)
 
99
  {
 
100
    fprintf (stderr, "Error opening database %s: %s\n",
 
101
             db_name ? db_name : "",
 
102
             strerror (errno) ? strerror (errno) : "");
 
103
    exit (1);
 
104
  }
 
105
 
 
106
  for (i = 2; i < argc; i += 2)
 
107
  {
 
108
    DBT key;
 
109
    DBT value;
 
110
 
 
111
    ZERO (key);
 
112
    ZERO (value);
 
113
 
 
114
    key.data = argv[i];
 
115
    key.size = strlen (key.data);
 
116
 
 
117
    value.data = argv[i + 1];
 
118
    value.size = strlen (value.data) + 1;
 
119
 
 
120
    if (database->put (database, &key, &value, 0))
 
121
    {
 
122
      fprintf (stderr, "Error writing data.\n");
 
123
      exit (1);
 
124
    }
 
125
  }
 
126
 
 
127
  database->close (database);
 
128
 
 
129
  return 0;
 
130
}