Blame view

tools/scripts/perlmod/Utils.pm 2.45 KB
e6be5137b   Jean-François Rey   reinitialized pro...
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
  package Utils;
  use strict;
  use vars qw(@ISA @EXPORT);
  use Exporter;
  @ISA = qw(Exporter);
  @EXPORT = qw(fill_hash_from_array  fill_array_from_dir fill_array_from_file fill_hash_from_file fill_file_from_array); 
  
  #-----------------------------------------------------
  # Author : Emmanuel FERREIRA  (v.2)
  # Contact: emmanuel.ferreira0194@gmail.com
  # Date : 04/11/11
  # Brief : this file provide a curly library utilities
  #-----------------------------------------------------
  
  #---------------------------------------------------------------
  # SUBROUTINES
  #---------------------------------------------------------------
  
  #
  # \brief fill a hashtable with an formatted array (each item <=> key=value)
  # \param arrayRef the array reference
  # \param hashRef the hashtable reference
  #
  sub fill_hash_from_array
  {
  	my ($arrayRef, $hashRef)=@_;
  
  	foreach my $line (@$arrayRef)
  	{
  		chomp($line);
  		if (length($line) > 1)
  		{
  			$line=~/(.*)=(.*)/i;
  			$hashRef->{$1}=$2;
  		}
  	}
  }
  
  #
  # \brief fill a referenced array whith a directory content
  # \param directory path
  # \param file filter
  # \param reference to an array
  #
  sub fill_array_from_dir
  {
  	my ($dirPath, $filter, $array) = @_;
  	my $dir;
  	opendir($dir, $dirPath) or die("Cannot open directory : $dirPath");
  	if($filter)
  	{
  		@{$array} = grep( /$filter$/, readdir($dir));
  	}
  	else
  	{
  		@{$array} = readdir($dir);
  	}
  	closedir($dir); 
  }
  
  #
  # \brief fill a referenced array whith a file content
  # \param filepath
  # \param content filter (regex containing items that the program must ignore)
  # \param reference to an array
  #
  sub fill_array_from_file
  {
  	my($filePath, $contentFilter, $array) = @_;
  	my $file;
  	
  	open($file, $filePath) or die("Cannot open file : $filePath");
  	if($contentFilter)
  	{
  		@{$array} = grep(!/$contentFilter/, <$file>);
  	}
  	else
  	{
  		@{$array} = <$file>;
  	}
  	close($file);
  }
  
  #
  # \brief fill an hashtable from a fileConten
  # \param filepath
  # \param sep separator
  # \param reference to an hashtable
  #
  sub fill_hash_from_file
  {
  	my($filePath, $sep, $hashtable) = @_;
  	my $file;
  	open($file, $filePath) or die ("Cannot open file : $filePath");
  	while(<$file>)
  	{
  		chomp;
  		my @values = split(/$sep/);
  		${$hashtable}{$values[0]} = $values[1];
  	}
  	close($file);
  }
  
  #
  # \brief fill a file with an array
  # \param filepath
  # \param reference to an array
  #
  sub fill_file_from_array
  {
  	my ($filePath, @array) = @_;
  	my $file;
  	open($file, ">$filePath") or die ("Cannot open file : $filePath");
  	foreach(@array)
  	{
  		print $file $_;
  	}
  	close($file);
  }
  1;