-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.pl
executable file
·262 lines (217 loc) · 5.63 KB
/
api.pl
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
#!/usr/bin/perl -w
use strict;
my $base_path = $ENV{"HOME"}."/.projectdata";
#===========================functions definitions=============================
sub printUage()
{
print "usage:\n";
print "vit <op> <args...>\n";
print "<op>:\n";
print "create c:create a new project,with a project name and an absolute path\n";
print "delete d:delete the object.All data files will be deleted\n";
print "open o:open a project\n";
print "update u:update a project\n";
}
# @description: clear all project-related files and dir
# @param name: project name
# @return: result code
sub clear()
{
my $name = $_[0];
#open(MYFILE, "< $basedir/projects") or die "$!\n";
#open(SWPFILE, "> $basedir/~projects.swap") or die "$!\n";
#while(<MYFILE>)
#{
# if(m/$_[0] .*/)
# {
# next;
# }
# print SWPFILE $_;
#}
#system("rm -rf $basedir/projects && mv $basedir/~projects.swap $basedir/projects");
#rmdir("$basedir/$_[0]") or die "$!\n";
#system("rm -rf $basedir/$_[0]");
#close(MYFILE);
#return 0;
(! -d "$base_path/$name") || system "rm -rf $base_path/$name";
#return $?;
}
# @description: clean all the project-related data files
# @param name: project name
# @return:result code
sub clean()
{
my $name = $_[0];
#my $datadir = "$basedir/$_[0]";
#opendir(DIR, $datadir) or die "$!\n";
#while(my $file = readdir(DIR))
#{
# if(-f "$datadir/$file")
# {
# unlink "$datadir/$file";
# }
#}
#closedir(DIR);
#return 0;
system "rm -rf $base_path/$name/cscope.* $base_path/$name/tags";
#return $?;
}
# @description :parse FROM source dir TO data dir
# @param name :TO - project name
# @param path :FROM - project source path
sub parseProject()
{
my ($name, $path) = @_;
my $tmp;
my $cmd_str;
my @excldir;
my @exclfiletype;
#test if the project has existed
if(! -d "$base_path/$name" )
{
print "Parsing failure:no project dir\n";
return 1;
}
if(system "touch $base_path/$name/cscope.files")
{
print "Parsing error:creating file "
. "$base_path/$name/cscope.files failure\n";
&clear($name);
return 1;
}
@excldir = &get_conf_section("$base_path/$name/path_info", 'excluded_path');
@exclfiletype = &get_conf_section("$base_path/$name/path_info", 'excluded_filetype');
# = contruct the find string
$cmd_str .= "find $path ";
# cmd_str:exclude specified directory
if(@excldir == 0)
{
foreach(@excldir)
{
$cmd_str .= " -path \"$path/$_\" -prune -o ";
}
}
# cmd_str:exclude specified filetype
my $flag;
foreach(('c', 'cc', 'cpp', 'h', 'inl', 's', 'S', 'x', 'js'))
{
$flag = 0;
$tmp = $_;
if(@exclfiletype == 0)
{
foreach(@exclfiletype)
{
if($_ eq $tmp)
{
$flag = 1;
}
}
}
if($flag != 1)
{
$cmd_str .= " -name \"*.$tmp\" -o ";
}
}
# chop the last '-o ' and append other options
chop $cmd_str;
chop $cmd_str;
chop $cmd_str;
chdir("/"); # To let 'find' parse out with absolute path
#print "$cmd_str\n";
if(system "$cmd_str > $base_path/$name/cscope.files" || die "Parsing error:find failure\n")
{
print "Parsing failure:find failure\n";
&clear($name);
return 1;
}
if(system "cscope -bkq -i $base_path/$name/cscope.files -f $base_path/$name/cscope.out")
{
print "Parsing failure:cscope failure\n";
&clear($name);
return 1;
}
if(system "ctags -R -f $base_path/$name/tags $path")
{
print "parsing error:ctags error\n";
&clear($name);
return 1;
}
}
# @brief :get configuration section from a conf file
# @param file :file name
# @param secname :section name
# @return :undef if not found, array for the found
sub get_conf_section
{
my ($file, $secname) = @_;
my ($key, $value);
my @arr;
my %conf;
%conf = &parse_conf("$file");
while(($key, $value) = each %conf)
{
if($key eq $secname)
{
return @{$value};
}
}
return undef;
}
# @brief :update the value of specified section
# @param file :file name
# @param secname :section name
# @param values(ref to array) :new values used
# to update section
# @return :0 for success
sub set_conf_section
{
my ($file, $secname, $value) = @_;
my %content;
my ($key, $val);
%content = &parse_conf($file);
open FILE, ">$file" or return 1;
while(($key, $val) = each %content)
{
# = edit the specified section
if($key eq $secname)
{
$content{$key} = $value;
}
# = print everything to file
print FILE "$key\n";
foreach(@{$content{$key}})
{
print FILE "$_\n";
}
next;
}
close(FILE);
return 0;
}
# @brief :read conf file and parse as hash
# @param file :conf file name
# @return :number for error,content as hash parsed from file
sub parse_conf
{
my ($file) = @_;
my %content;
my ($key, $tem_value);
open FILE, $file or return undef;
while(<FILE>)
{
chomp $_;
if(m/\[(.*)\]/)
{
$key = $1;
$content{$key} = undef;
$tem_value = [];
next;
}
if(defined($tem_value))
{
push @{$tem_value}, $_;
$content{$key} = $tem_value;
}
}
return %content;
}