-
Notifications
You must be signed in to change notification settings - Fork 0
/
catCommsIndex.pl
232 lines (200 loc) · 5.36 KB
/
catCommsIndex.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
#!/usr/bin/perl
use warnings;
require 'xmlParser.pl';
my $indexFileName = "/media/niki/niki/Documents/Uni/Perl/project/catComms/fileIndex.xml";
sub getIndexFile()
{
local $/; # prevents reading only the first line of a file
# open the index file
open(FILE, $indexFileName) or die "Can't read index file' [$!]\n";
$indexFileContent = <FILE>;
close (FILE);
my %indexHash = xmlParser($indexFileContent);
return %indexHash;
}
sub tagsForFileEntry($)
{
my $fileEntry = shift || die "tagsForFileEntry called with no input";
my $tagsHash = $fileEntry->{tags};
my $tagsArray = $tagsHash->{tag};
return @$tagsArray;
}
sub hasFileWithName($)
{
my $fileName = shift || die "hasFileWithName called with no file name";
@allFiles = allFilePaths();
foreach $file (@allFiles)
{
if ($file eq $fileName)
{
print("File with name ($fileName) already exists\n");
return 1;
}
}
return 0;
}
sub fileHasCategoryWithName($$)
{
my $fileName = shift || die "fileHasCategoryWithName called with no file name";
my $categoryName = shift || die "fileHasCategoryWithName called with no category name";
@allCategories = categoriesForFile($fileName);
foreach $category (@allCategories)
{
if ($category eq $categoryName)
{
print("File with name ($fileName) already has category named ($categoryName)\n");
return 1;
}
}
return 0;
}
sub addCategoryForFile($$)
{
my $filePath = shift || die "addCategoryForFile called with no file name";
my $categoryName = shift || die "addCategoryForFile called with no category";
my %indexhash = getIndexFile();
my @allFiles = $indexhash{file};
foreach $file (@allFiles)
{
foreach $fileEl (@$file)
{
my $path = $fileEl->{path};
if ($path eq $filePath)
{
if (not fileHasCategoryWithName($filePath, $categoryName))
{
my @categories = tagsForFileEntry($fileEl);
push(@categories, $categoryName);
$fileEl->{tags}{tag} = \@categories;
xmlSaveWithRootItem(\%indexhash, $indexFileName, "index");
last;
}
}
}
}
}
sub removeCategoryForFile($$)
{
my $filePath = shift || die "removeCategoryForFile called with no file name";
my $categoryName = shift || die "addCategoryForFile called with no category";
my %indexhash = getIndexFile();
my @allFiles = $indexhash{file};
foreach $file (@allFiles)
{
foreach $fileEl (@$file)
{
my $path = $fileEl->{path};
if ($path eq $filePath)
{
my @categories = tagsForFileEntry($fileEl);
# find the specified category in the categories array
my $arrayIndex = 0;
$arrayIndex++ until (($categories[$arrayIndex] eq $categoryName) or ($arrayIndex >= scalar(@categories) - 1));
if ($arrayIndex < scalar(@categories))
{
# the category has been found (and arrayIndex hold the index of the element in the array)
# remove this category and save the index file
splice(@categories, $arrayIndex, 1);
$fileEl->{tags}{tag} = \@categories;
xmlSaveWithRootItem(\%indexhash, $indexFileName, "index");
last;
}
}
}
}
}
sub categoriesForFile($)
{
my $filePath = shift || die "categoriesForFile called with no input";
my %indexhash = getIndexFile();
my @allFiles = $indexhash{file};
foreach $file (@allFiles)
{
foreach $fileEl (@$file)
{
my $path = $fileEl->{path};
if ($path eq $filePath)
{
return tagsForFileEntry($fileEl);
}
}
}
return ();
}
sub allFilePaths()
{
my %indexhash = getIndexFile();
my @allFiles = $indexhash{file};
my @filePaths = ();
foreach $file (@allFiles)
{
foreach $fileEl (@$file)
{
my $path = $fileEl->{path};
push(@filePaths, $path);
}
}
return @filePaths;
}
sub addFileWithCategories($$)
{
my $fileName = shift || die "addFileWithCategories called with no file name";
#splice(@_, 0, 1); #|| die "addFileWithCategories called with no categories";
my @categories = @ARGV;
my %indexhash = getIndexFile();
my $allFiles = $indexhash{file};
if (not hasFileWithName($fileName))
{
# create entry for the categories
my %tagHash = ("tag" => \@categories);
my %tagsHash; # = ("tags" => \%tagHash, "path" => $fileName);
$tagsHash{path} = $fileName;
$tagsHash{tags} = \%tagHash;
# add the file to the xml structure and serialize it
push(@$allFiles, \%tagsHash);
$indexhash{file} = $allFiles;
xmlSaveWithRootItem(\%indexhash, $indexFileName, "index");
}
}
sub removeFile($)
{
my $fileName = shift || die "removeFile called with no file name";
my %indexhash = getIndexFile();
my $allFiles = $indexhash{file};
#foreach $file (@$allFiles)
#{
foreach $index ((0 .. scalar(@$allFiles) - 1))
{
print "Current file: " . $allFiles->[$index]{path} . "\n";
if ($fileName eq $allFiles->[$index]{path})
{
splice(@$allFiles, $index, 1);
$indexhash{file} = $allFiles;
xmlSaveWithRootItem(\%indexhash, $indexFileName, "index");
print "Removed index: $index\n";
}
}
#}
}
#
#
# Test
#
#
# my %indexhash = getIndexFile();
# my $indexValue = $indexhash{file}[0]{tags}{tag}[0];
# my @testCategories = categoriesForFile("./sampleData/sampleText");
# foreach my $category (@testCategories)
# {
# print("Found category: $category\n");
# }
# addCategoryForFile("./sampleData/sampleText", "test");
# addFileWithCategories("testFile", ("reading", "writing"));
# removeFile("testFile");
# my @allFiles = allFilePaths();
# foreach my $filePath (@allFiles)
# {
# print("Found file: $filePath\n");
# }
# print("First file name: $indexValue\n");
1;