1 /*
2  * Copyright (c) 2004, 2010 Tama Communications Corporation
3  *
4  * This file is part of GNU GLOBAL.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 #ifdef STDC_HEADERS
23 #include <stdlib.h>
24 #endif
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #else
28 #include <strings.h>
29 #endif
30 
31 #include "global.h"
32 #include "assoc.h"
33 #include "htags.h"
34 #include "path2url.h"
35 
36 static ASSOC *assoc;
37 static int nextkey;
38 
39 /**
40  * load_gpath: load gpath tag file.
41  *
42  * load the contents of GPATH file into the memory.
43  */
44 void
load_gpath(const char * dbpath)45 load_gpath(const char *dbpath)
46 {
47 	DBOP *dbop;
48 	const char *path;
49 	int n;
50 
51 	assoc = assoc_open();
52 	nextkey = 0;
53 	dbop = dbop_open(makepath(dbpath, dbname(GPATH), NULL), 0, 0, 0);
54 	if (dbop == NULL)
55 		die("cannot open '%s'.", makepath(dbpath, dbname(GPATH), NULL));
56 	for (path = dbop_first(dbop, "./", NULL, DBOP_PREFIX | DBOP_KEY); path; path = dbop_next(dbop)) {
57 		const char *no = dbop_lastdat(dbop, NULL);
58 
59 		path += 2;			/* remove './' */
60 		assoc_put(assoc, path, no);
61 		n = atoi(no);
62 		if (n > nextkey)
63 			nextkey = n;
64 	}
65 	dbop_close(dbop);
66 }
67 /**
68  * unload_gpath: unload gpath tag file.
69  *
70  * unload the contents of GPATH file from memory.
71  */
72 void
unload_gpath(void)73 unload_gpath(void)
74 {
75 	assoc_close(assoc);
76 }
77 /**
78  * path2fid: convert the path name into the file id.
79  *
80  *	@param[in]	path	path name
81  *	@return		id
82  */
83 const char *
path2fid(const char * path)84 path2fid(const char *path)
85 {
86 	static char number[32];
87 	const char *p;
88 
89 	if (strlen(path) > MAXPATHLEN)
90 		die("path name too long. '%s'", path);
91 	/*
92 	 * accept both aaa and ./aaa.
93 	 */
94 	if (*path == '.' && *(path + 1) == '/')
95 		path += 2;
96 	p = assoc_get(assoc, path);
97 	if (!p) {
98 		snprintf(number, sizeof(number), "%d", ++nextkey);
99 		assoc_put(assoc, path, number);
100 		p = number;
101 	}
102 	return p;
103 }
104 /**
105  * path2fid_readonly: convert the path name into the file id.
106  *
107  *	@param[in]	path	path name
108  *	@return		id
109  */
110 const char *
path2fid_readonly(const char * path)111 path2fid_readonly(const char *path)
112 {
113 	if (strlen(path) > MAXPATHLEN)
114 		die("path name too long. '%s'", path);
115 	/*
116 	 * accept both aaa and ./aaa.
117 	 */
118 	if (*path == '.' && *(path + 1) == '/')
119 		path += 2;
120 	return assoc_get(assoc, path);
121 }
122