1 /*******************************************************************************
2 ********************************************************************************
4 Copyright (c) 2008 Ahmed S. Badran
6 Licensed under the FreeBSD License (see LICENSE)
9 Description: Implementation.
10 Created: 09/17/2008 11:01:30 PM PDT
11 Author: Ahmed S. Badran (Ahmed B.), ahmed.badran@gmail.com
13 ********************************************************************************
14 *******************************************************************************/
16 #include "tree_builder.h"
20 #include <sys/types.h>
25 ctags_db::ctags_db (const string
& prj_
, const string
& prj_path_
, const
26 string
& prj_cache_
, const string
& prj_flist_
, const
27 string
& prj_ctags_db_
, const vector
<string
>&
29 prj(prj_
), prj_path(prj_path_
), prj_cache(prj_cache_
),
30 prj_flist(prj_flist_
), prj_ctags_db(prj_ctags_db_
),
31 extension_list(extensions_
)
38 tree_builder
* builder
= tree_builder::get_instance();
39 const fs_item
* tree
= builder
->build(prj_path
, extension_list
,
42 if ((tmp
= fopen(prj_flist
.c_str(), "w")) == 0) {
46 for (int i
= 0; i
< file_list
.size(); ++i
) {
47 fprintf(tmp
, "%s\n", file_list
[i
].c_str());
50 return build_db(prj_flist
, prj_ctags_db
);
54 ctags_db::add_file (const string
& new_file
)
57 if ((tmp
= fopen(prj_flist
.c_str(), "a+")) == 0) {
61 fprintf(tmp
, "%s\n", new_file
.c_str());
63 return build_db(prj_flist
, prj_ctags_db
);
67 ctags_db::remove_file(const string
& old_file
)
70 if ((tmp1
= fopen(prj_flist
.c_str(), "r")) == 0) {
74 string tmpfile
= prj_cache
;
75 tmpfile
+= "/tmp.txt";
76 if ((tmp1
= fopen(tmpfile
.c_str(), "w")) == 0) {
81 const unsigned MAX
= 2048;
82 char* buf
= (char*) malloc(sizeof(char) * MAX
);
87 memset(buf
, 0, sizeof(char) * MAX
);
89 while (getline(&buf
, &n
, tmp1
)) {
90 if (strcmp(buf
, old_file
.c_str()) == 0) {
93 fprintf(tmp2
, "%s", buf
);
99 if (rename(tmpfile
.c_str(), prj_flist
.c_str()) != 0) {
103 return build_db(prj_flist
, prj_ctags_db
);
109 return build_db(prj_flist
, prj_ctags_db
);
113 ctags_db::build_db (const string
& flist_file
, const string
& db_file
) const
117 args
.push_back("--c++-kinds=+p");
118 args
.push_back("--fields=+iaS");
119 args
.push_back("--extra=+q");
120 args
.push_back("-f");
122 args
.push_back(db_file
);
124 args
.push_back("-L");
126 args
.push_back(flist_file
);
127 return invoke_ctags(args
);
131 ctags_db::invoke_ctags (const vector
<string
>& args
) const
133 char* const * argv
= build_argv(args
);
135 if ((pid
= fork()) == -1) {
137 } else if (pid
!= 0) {
139 waitpid(pid
, &status
, 0);
140 if (WEXITSTATUS(status
) != 0) {
145 } else if (execvp("ctags", argv
) == -1 ) {
153 ctags_db::build_argv (const vector
<string
>& args
) const
156 argv
= new char*[args
.size() + 2];
157 argv
[0] = strdup("ctags");
158 argv
[args
.size() - 1] = 0;
159 for (int i
= 0; i
< args
.size(); ++i
) {
160 argv
[i
+ 1] = strdup(args
[i
].c_str());
165 ctags_db::~ctags_db ()