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
|
/*
* Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002
* Tama Communications Corporation
*
* This file is part of GNU GLOBAL.
*
* GNU GLOBAL 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; either version 2, or (at your option)
* any later version.
*
* GNU GLOBAL 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 St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include "die.h"
#include "dbop.h"
#include "gtagsop.h"
#include "makepath.h"
#include "gpathop.h"
#include "strlimcpy.h"
static DBOP *dbop;
static int _nextkey;
static int _mode;
static int opened;
static int created;
/*
* gpath_open: open gpath tag file
*
* i) dbpath GTAGSDBPATH
* i) mode 0: read only
* 1: create
* 2: modify
* i) flags DBOP_POSTGRES
* r) 0: normal
* -1: error
*/
int
gpath_open(dbpath, mode, flags)
const char *dbpath;
int mode;
int flags;
{
assert(opened == 0);
/*
* We create GPATH just first time.
*/
_mode = mode;
if (mode == 1 && created)
mode = 0;
dbop = dbop_open(makepath(dbpath, dbname(GPATH), NULL), mode, 0644, flags);
if (dbop == NULL)
return -1;
if (mode == 1)
_nextkey = 1;
else {
const char *p = dbop_get(dbop, NEXTKEY);
if (p == NULL)
die("nextkey not found in GPATH.");
_nextkey = atoi(p);
}
opened = 1;
return 0;
}
/*
* gpath_put: put path name
*
* i) path path name
*/
void
gpath_put(path)
const char *path;
{
char fid[32];
assert(opened == 1);
if (_mode == 1 && created)
return;
if (dbop_get(dbop, path) != NULL)
return;
snprintf(fid, sizeof(fid), "%d", _nextkey++);
dbop_put(dbop, path, fid);
dbop_put(dbop, fid, path);
}
/*
* gpath_path2fid: convert path into id
*
* i) path path name
* r) file id
*/
const char *
gpath_path2fid(path)
const char *path;
{
assert(opened == 1);
return dbop_get(dbop, path);
}
/*
* gpath_fid2path: convert id into path
*
* i) fid file id
* r) path name
*/
const char *
gpath_fid2path(fid)
const char *fid;
{
return dbop_get(dbop, fid);
}
/*
* gpath_delete: delete specified path record
*
* i) path path name
*/
void
gpath_delete(path)
const char *path;
{
const char *fid;
assert(opened == 1);
assert(_mode == 2);
assert(path[0] == '.' && path[1] == '/');
fid = dbop_get(dbop, path);
if (fid == NULL)
return;
dbop_delete(dbop, fid);
dbop_delete(dbop, path);
}
/*
* gpath_nextkey: return next key
*
* r) next id
*/
int
gpath_nextkey(void)
{
assert(_mode != 1);
return _nextkey;
}
/*
* gpath_close: close gpath tag file
*/
void
gpath_close(void)
{
char fid[32];
assert(opened == 1);
opened = 0;
if (_mode == 1 && created) {
dbop_close(dbop);
return;
}
snprintf(fid, sizeof(fid), "%d", _nextkey);
if (_mode == 1 || _mode == 2)
dbop_update(dbop, NEXTKEY, fid);
dbop_close(dbop);
if (_mode == 1)
created = 1;
}
/*
* gfind iterator using GPATH.
*
* gfind_xxx() does almost same with find_xxx() but much faster,
* because gfind_xxx() use GPATH (file index).
* If GPATH exist then you should use this.
*/
static DBOP *gfind_dbop;
static int gfind_opened;
static int gfind_first;
static char gfind_prefix[MAXPATHLEN+1];
/*
* gfind_open: start iterator using GPATH.
*/
void
gfind_open(dbpath, local)
const char *dbpath;
const char *local;
{
assert(gfind_opened == 0);
assert(gfind_first == 0);
gfind_dbop = dbop_open(makepath(dbpath, dbname(GPATH), NULL), 0, 0, 0);
if (gfind_dbop == NULL)
die("GPATH not found.");
strlimcpy(gfind_prefix, (local) ? local : "./", sizeof(gfind_prefix));
gfind_opened = 1;
gfind_first = 1;
}
/*
* gfind_read: read path without GPATH.
*
* r) path
*/
const char *
gfind_read(void)
{
assert(gfind_opened == 1);
if (gfind_first) {
gfind_first = 0;
return dbop_first(gfind_dbop, gfind_prefix, NULL, DBOP_KEY | DBOP_PREFIX);
}
return dbop_next(gfind_dbop);
}
/*
* gfind_close: close iterator.
*/
void
gfind_close(void)
{
dbop_close(gfind_dbop);
gfind_opened = gfind_first = 0;
}
|