1# home to all my small misc functions.
2
3use strict;
4use warnings;
5
6sub ci_sort
7{
8	&plog(3, "sub ci_sort:");
9	my @sortme2 = sort { lc($a) cmp lc($b) } @_;
10	return @sortme2;
11}
12
13# plog - print log
14
15# Notes:
16# Since i will be logging all subs (excluding plog itself, plug cannot call any namefix subs (recursion errors which locked linux on my system)
17
18sub plog
19{
20	my $level = shift;
21	my $text = shift;
22
23	if($level == 0)
24	{
25		$text = "ERROR>$text";
26
27		# CLI will (for now) always spit out & log errors
28		if($main::CLI)
29		{
30			open(FILE, ">>$main::log_file");
31			print FILE "$text\n";
32			close(FILE);
33
34			print "$text\n";
35
36			return 1;
37		}
38	}
39	else
40	{
41		$text = "DBG".$level."> ".$text;
42	}
43
44	if($level <= $main::debug)
45	{
46		open(FILE, ">>$main::log_file");
47		print FILE "$text\n";
48		close(FILE);
49		if($main::LOG_STDOUT)
50		{
51			print "$text\n";
52		}
53	}
54	if($level == 0 && $main::ERROR_NOTIFY)
55	{
56		&show_dialog("namefix.pl ERROR", "$text");
57	}
58	return;
59}
60
61sub clog
62{
63	open(FILE, ">$main::log_file");
64	close(FILE);
65}
66
67#--------------------------------------------------------------------------------------------------------------
68# save_file
69#--------------------------------------------------------------------------------------------------------------
70
71sub save_file
72{
73        my $file = shift;
74        my $t = shift;
75
76	&plog(3, "sub save_file: \"$file\"");
77#	print "savefile: $file : $t\n";
78        $t =~ s/^\n//g;		# no blank line @ start of file
79        $t =~ s/\n\n+/\n/g;	# no blank lines in file
80        open(FILE, ">$file") or die "ERROR: sub save_file, Couldnt open $file to write to. $!";
81        print FILE $t;
82        close(FILE);
83}
84
85sub file_append
86{
87	my $file = shift;
88	my $string = shift;
89
90	&plog(3, "sub file_append: \"$file\"");
91
92	open(FILE, ">>$file") or die "ERROR: Couldnt open $file to append to. $!";
93        print FILE $string;
94        close(FILE);
95}
96
97#--------------------------------------------------------------------------------------------------------------
98# read file
99#--------------------------------------------------------------------------------------------------------------
100
101sub readf
102{
103        my $file = $_[0];
104
105	&plog(3, "sub readf: \"$file\"");
106
107        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
108        my @file = <FILE>;
109        close(FILE);
110
111        # clean file of empty lines
112        $file =~ s/^\n//g;
113        $file =~ s/\n\n+/\n/g;
114
115        return @file;
116}
117
118#--------------------------------------------------------------------------------------------------------------
119# read and sort file
120#--------------------------------------------------------------------------------------------------------------
121
122sub readsf
123{
124        my $file = $_[0];
125
126	&plog(3, "sub readsf: \"$file\"");
127
128        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
129        my @file = <FILE>;
130        close(FILE);
131
132        # clean file of empty lines
133        $file = join('', sort @file);
134        $file =~ s/^\n//g;
135        $file =~ s/\n\n+/\n/g;
136        @file = split('\n+', $file);
137
138        return @file;
139}
140
141#--------------------------------------------------------------------------------------------------------------
142# read, sort and join file
143#--------------------------------------------------------------------------------------------------------------
144
145sub readsjf
146{
147        my $file = $_[0];
148	&plog(3, "sub readsjf: \"$file\"");
149        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
150        my @file = <FILE>;
151        close(FILE);
152        $file = join('', sort @file);
153        $file =~ s/^\n//g;
154        $file =~ s/\n\n+/\n/g;
155
156        return $file;
157}
158
159#--------------------------------------------------------------------------------------------------------------
160# read and join file
161#--------------------------------------------------------------------------------------------------------------
162
163sub readjf
164{
165        my $file = $_[0];
166	&plog(3, "sub readjf: \"$file\"");
167
168        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
169        my @file = <FILE>;
170        close(FILE);
171        $file = join('', @file);
172        $file =~ s/^\n//g;
173        $file =~ s/\n\n+/\n/g;
174
175        return $file;
176}
177
178#--------------------------------------------------------------------------------------------------------------
179# clear options
180#--------------------------------------------------------------------------------------------------------------
181
182sub clr_no_save
183{
184	&plog(3, "sub clr_no_save ");
185	# clear options that are never saved
186
187        $main::replace		= 0;
188	$main::rpwold         	= "";
189        $main::rpwnew         	= "";
190        $main::front_a		= 0;
191        $main::faw            	= "";
192        $main::end_a		= 0;
193        $main::eaw            	= "";
194
195	$main::id3_art_str	= "";
196	$main::id3_alb_str	= "";
197	$main::id3_com_str	= "";
198	$main::id3_gen_str 	= "Metal";
199	$main::id3_year_str 	= "";
200
201	$main::id3_art_set	= 0;
202	$main::id3_alb_set	= 0;
203	$main::id3_com_set	= 0;
204	$main::id3_gen_set 	= 0;
205        $main::id3_year_set 	= 0;
206
207	$main::id3v1_rm		= 0;
208	$main::id3v2_rm		= 0;
209}
210
211#--------------------------------------------------------------------------------------------------------------
212# Escape strings for use in regexp - wrote my own cos uri is fucked.
213#--------------------------------------------------------------------------------------------------------------
214
215sub escape_string
216{
217	my $s = shift;
218	&plog(5, "sub escape_string: \"$s\"");	# very noisy sub
219	$s =~ s/(\~|\`|\!|@|\#|\$|\%|\^|\&|\(|\)|\=|\+|\{|\[|\]|\}|\:|\;|\"|\'|\<|\,|\.|\>|\?)/"\\".$1/eg;
220	return $s;
221}
222
223
224
2251;