| 1 | ################################################################################ | ||
| 2 | # This is CodeManager | ||
| 3 | # Copyright 2009-2013 by Waldemar Biernacki | ||
| 4 | # http://codemanager.sao.pl\n" . | ||
| 5 | # | ||
| 6 | # License statement: | ||
| 7 | # | ||
| 8 | # This program/library is free software; you can redistribute it | ||
| 9 | # and/or modify it under the same terms as Perl itself. | ||
| 10 | # | ||
| 11 | # Last modified (DMYhms): 13-01-2013 20:15:38. | ||
| 12 | ################################################################################ | ||
| 13 | |||
| 14 | package Prima::CodeManager::Misc; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use POSIX; | ||
| 20 | |||
| 21 | ################################################################################ | ||
| 22 | |||
| 23 | sub read_file_at_once { | ||
| 24 | my( $self, $file ) = @_ ; | ||
| 25 | |||
| 26 | return unless -f $file; | ||
| 27 | my $content; | ||
| 28 | |||
| 29 | local *FH; | ||
| 30 | sysopen( FH, $file, O_RDONLY ) or $self->{ERROR} = "Can't open $file: $!"; | ||
| 31 | |||
| 32 | -f FH and sysread FH, $content, -s FH; | ||
| 33 | |||
| 34 | return $content; | ||
| 35 | } | ||
| 36 | |||
| 37 | ################################################################################ | ||
| 38 | |||
| 39 | sub read_ini_file { | ||
| 40 | my ( $self, $ini_file, $global ) = @_; | ||
| 41 | |||
| 42 | my $group = ''; | ||
| 43 | |||
| 44 | if ( open ( my $FH, "<$ini_file" )) { | ||
| 45 | |||
| 46 | while ( my $wiersz = <$FH> ) { | ||
| 47 | next if $wiersz =~ /^(;|#|--)/; | ||
| 48 | |||
| 49 | $wiersz =~ s/\n*//g; | ||
| 50 | $wiersz =~ s/\r*//g; | ||
| 51 | $wiersz =~ s/\t*//g; | ||
| 52 | next unless $wiersz; | ||
| 53 | |||
| 54 | $wiersz =~ s/^([^#]*)#.*$/$1/; | ||
| 55 | |||
| 56 | if ( $wiersz =~ /^\s*\[(.+)\]/ ) { | ||
| 57 | $group = $1; | ||
| 58 | } else { | ||
| 59 | |||
| 60 | if ( $wiersz =~ /^([^=]+?)\s*=\s*(.*)$/ ) { | ||
| 61 | my $name = $1; | ||
| 62 | my $object = $2; | ||
| 63 | $object =~ s/%\[([^%]*)\]([^%]*)%/$global->{$1}->{$2}/g; | ||
| 64 | $global->{$group}->{$name} = $object if $group && $name; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | close ($FH); | ||
| 69 | } | ||
| 70 | |||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | ################################################################################ | ||
| 75 | |||
| 76 | sub write_ini_file { | ||
| 77 | my ( $self, $ini_file ) = @_; | ||
| 78 | |||
| 79 | foreach my $group (sort keys %::_GLOBAL ) { | ||
| 80 | my %hasz = %{$::_GLOBAL{$group}}; | ||
| 81 | |||
| 82 | foreach my $name (sort keys %hasz ) { | ||
| 83 | print "$group\[$name\]=",$hasz{$name},"\n"; | ||
| 84 | } | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | ################################################################################ | ||
| 92 | |||
| 93 | sub read_one_row { | ||
| 94 | my ( $self, $FH ) = ( shift, shift ); | ||
| 95 | |||
| 96 | my $wiersz = <$FH>; | ||
| 97 | $wiersz = '' unless $wiersz; | ||
| 98 | if ( $wiersz =~ /\\/ ) { | ||
| 99 | $wiersz =~ s/^(.*)\\.*/$1/; | ||
| 100 | $wiersz =~ s/^(.*)#.*/$1/; | ||
| 101 | $wiersz .= ' ' unless $wiersz =~ /(,|;|`)\t*$/; #` | ||
| 102 | $wiersz .= $self-> read_one_row( $FH ) | ||
| 103 | } | ||
| 104 | return $wiersz; | ||
| 105 | } | ||
| 106 | |||
| 107 | ################################################################################ | ||
| 108 | |||
| 109 | sub write_to_file { | ||
| 110 | my ( $self, $file, $content ) = ( shift, shift, shift ); | ||
| 111 | |||
| 112 | if ( open (my $FH , ">>$file" )) { | ||
| 113 | print $FH $content if $content; | ||
| 114 | close $FH; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | #------------------------------------------------------------------------------- | ||
| 119 | |||
| 120 | sub read_file { | ||
| 121 | my ( $self, $file, $content ) = ( shift, shift, '' ); | ||
| 122 | |||
| 123 | if ( open (my $FH , "<$file" )) { | ||
| 124 | while ( my $row = <$FH> ) { $content .= $row } | ||
| 125 | close $FH; | ||
| 126 | } | ||
| 127 | |||
| 128 | return $content; | ||
| 129 | } | ||
| 130 | |||
| 131 | ################################################################################ | ||
| 132 | |||
| 133 | sub czas { | ||
| 134 | my ( $self, $format, $time ) = ( shift, shift, shift ); | ||
| 135 | my ($s,$m,$h,$D,$M,$Y) = localtime; | ||
| 136 | ($s,$m,$h,$D,$M,$Y) = localtime $time if defined $time; | ||
| 137 | |||
| 138 | $s = substr('00'.$s,-2); | ||
| 139 | $m = substr('00'.$m,-2); | ||
| 140 | $h = substr('00'.$h,-2); | ||
| 141 | $format =~ s/s/$s/g; | ||
| 142 | $format =~ s/m/$m/g; | ||
| 143 | $format =~ s/h/$h/g; | ||
| 144 | |||
| 145 | $D = substr('00'.$D,-2); | ||
| 146 | $M = substr('00'.($M+1),-2); | ||
| 147 | $Y+= 1900; | ||
| 148 | $format =~ s/DD/$D/g; | ||
| 149 | # if ( $format =~ /MR/ ) { | ||
| 150 | # $M = roman($M); | ||
| 151 | # $M =~ tr/a-z/A-Z/; | ||
| 152 | # } | ||
| 153 | # $format =~ s/MR/$M/g; | ||
| 154 | $format =~ s/MM/$M/g; | ||
| 155 | $format =~ s/YYYY/$Y/g; | ||
| 156 | $format =~ s/YY/substr($Y,-2)/ge; | ||
| 157 | |||
| 158 | return $format; | ||
| 159 | } | ||
| 160 | |||
| 161 | ################################################################################ | ||
| 162 | |||
| 163 | sub licz_kolor | ||
| 164 | { | ||
| 165 | my ( $self, $col1, $col2, $l1, $inw ) = @_; | ||
| 166 | |||
| 167 | $l1 = 0 unless $l1; $l1 += 0; $l1 = 0 if $l1 < 0; $l1 = 1 if $l1 > 1; | ||
| 168 | |||
| 169 | my $l2 = 1 - $l1; | ||
| 170 | |||
| 171 | $inw = 0 unless $inw; | ||
| 172 | $inw = 255 if $inw; | ||
| 173 | |||
| 174 | my @c1; | ||
| 175 | my @c2; | ||
| 176 | my @re; | ||
| 177 | |||
| 178 | $c1[2] = $col1 % 256; $col1 = ( $col1 - $c1[2] ) / 256; | ||
| 179 | $c1[1] = $col1 % 256; $col1 = ( $col1 - $c1[1] ) / 256; | ||
| 180 | $c1[0] = $col1 % 256; | ||
| 181 | |||
| 182 | $c2[2] = $col2 % 256; $col2 = ( $col2 - $c2[2] ) / 256; | ||
| 183 | $c2[1] = $col2 % 256; $col2 = ( $col2 - $c2[1] ) / 256; | ||
| 184 | $c2[0] = $col2 % 256; | ||
| 185 | |||
| 186 | $re[0] = int( $l1 * $c1[0] + $l2 * $c2[0] ); | ||
| 187 | $re[0] = 0 if $re[0] < 0; | ||
| 188 | $re[0] = 255 if $re[0] > 255; | ||
| 189 | |||
| 190 | $re[1] = int( $l1 * $c1[1] + $l2 * $c2[1] ); | ||
| 191 | $re[1] = 0 if $re[1] < 0; | ||
| 192 | $re[1] = 255 if $re[1] > 255; | ||
| 193 | |||
| 194 | $re[2] = int( $l1 * $c1[2] + $l2 * $c2[2] ); | ||
| 195 | $re[2] = 0 if $re[2] < 0; | ||
| 196 | $re[2] = 255 if $re[2] > 255; | ||
| 197 | |||
| 198 | if ( $inw ) { | ||
| 199 | return 256 * 256 * (255-$re[0]) + 256 * (255-$re[1]) + (255-$re[2]); | ||
| 200 | } else { | ||
| 201 | return 256 * 256 * $re[0] + 256 * $re[1] + $re[2]; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | ################################################################################ | ||
| 206 | |||
| 207 | sub angle_color | ||
| 208 | { | ||
| 209 | my ( $self, $phi, $white, $black ) = @_; | ||
| 210 | |||
| 211 | $phi %= 360; | ||
| 212 | |||
| 213 | $white = 255 unless defined $white; | ||
| 214 | $white %= 256; | ||
| 215 | |||
| 216 | $black = 0 unless defined $black; | ||
| 217 | $black %= 256; | ||
| 218 | |||
| 219 | if ( $black > $white ) { | ||
| 220 | my $tmp = $white; | ||
| 221 | $white = $black; | ||
| 222 | $black = $tmp; | ||
| 223 | } | ||
| 224 | |||
| 225 | my $gamma = int ( ( $white - $black ) * ( $phi % 60 ) / 60 ); | ||
| 226 | my ( $red, $gre, $blu ) = (0,0,0); | ||
| 227 | |||
| 228 | if ( $phi < 60 ) { ( $red, $gre, $blu ) = ( $black+$gamma, $white, $black ) } | ||
| 229 | elsif ( $phi < 120 ) { ( $red, $gre, $blu ) = ( $white, $white-$gamma, $black ) } | ||
| 230 | elsif ( $phi < 160 ) { ( $red, $gre, $blu ) = ( $white, $black, $black+$gamma ) } | ||
| 231 | elsif ( $phi < 240 ) { ( $red, $gre, $blu ) = ( $white-$gamma, $black, $white ) } | ||
| 232 | elsif ( $phi < 300 ) { ( $red, $gre, $blu ) = ( $black, $black+$gamma, $white ) } | ||
| 233 | elsif ( $phi < 360 ) { ( $red, $gre, $blu ) = ( $black, $white, $white-$gamma ) } | ||
| 234 | |||
| 235 | return int( 1.0 * ( $red + 256 * $gre + 256 * 256 * $blu )); | ||
| 236 | } | ||
| 237 | |||
| 238 | ######################################################################################## | ||
| 239 | |||
| 240 | sub create_user_home_directory | ||
| 241 | { | ||
| 242 | my ( $self, $home_directory ) = @_; | ||
| 243 | |||
| 244 | return if -e $home_directory; | ||
| 245 | |||
| 246 | mkdir "$home_directory"; | ||
| 247 | $self-> write_to_file ( "$home_directory/.exists" , '' ); | ||
| 248 | |||
| 249 | my $CodeManager_CodeManager =<< "_CODE_MANAGER_"; | ||
| 250 | [GLOBAL] | ||
| 251 | #group of projects | ||
| 252 | group = System | ||
| 253 | |||
| 254 | #name of project | ||
| 255 | name = CodeManager | ||
| 256 | |||
| 257 | ############## PROJECT TREE CONFIGURATION ############# | ||
| 258 | #main project encoding | ||
| 259 | #can be redefined in each sub-project [DIRECTORY] | ||
| 260 | CodeManager_encoding= UTF8 | ||
| 261 | #this is the height of one tree project branch: | ||
| 262 | #if none or less than 16 then tree_itemHeight = 16 | ||
| 263 | tree_itemHeight = 18 | ||
| 264 | #the indent of next level branch relative to the parent one. | ||
| 265 | #if none or less than 16 then tree_itemIndent = 16 | ||
| 266 | tree_itemIndent = 16 | ||
| 267 | #font name of the branches description | ||
| 268 | #if none then DejaVu Sans Mono is taken | ||
| 269 | tree_fontName = DejaVu Sans Mono | ||
| 270 | #size and height of branch description font. | ||
| 271 | #only one is taken; height - if > 0 - has higher rank than size | ||
| 272 | #if none then tree_fontSize = 0.625 * tree_itemHeight | ||
| 273 | tree_fontSize = 10 | ||
| 274 | tree_fontHeight = 14 | ||
| 275 | |||
| 276 | ############## EDITOR PANEL ############# | ||
| 277 | #additional space between lines. Can be negative! | ||
| 278 | #It is possible to dynamically change by pressing AltDown/AltUp keys | ||
| 279 | editor_lineSpace = 2 | ||
| 280 | |||
| 281 | #editor font size or height (similar rules to the tree configuration ones) | ||
| 282 | #It is possible to dynamically change by pressing CtrlDown/CtrlUp keys | ||
| 283 | editor_fontSize = 11 | ||
| 284 | editor_fontHeight = 14 | ||
| 285 | |||
| 286 | #Font family should be chosen the mono type, but whatever! | ||
| 287 | editor_fontName = DejaVu Sans Mono | ||
| 288 | |||
| 289 | ############## NOTEBOOK CONFIGURATION ############# | ||
| 290 | |||
| 291 | notebook_fontSize = 9 | ||
| 292 | notebook_fontHeight = 12 | ||
| 293 | notebook_fontName = Arial | ||
| 294 | |||
| 295 | [EXTERNAL_EDITORS] | ||
| 296 | png = gimp | ||
| 297 | exe = no edit! | ||
| 298 | |||
| 299 | [DIRECTORY] | ||
| 300 | name = My projects | ||
| 301 | image = cm.png | ||
| 302 | linux = ~/projects | ||
| 303 | windows = ~/projects | ||
| 304 | extensions = cm | ||
| 305 | |||
| 306 | [DIRECTORY] | ||
| 307 | name = My templates | ||
| 308 | image = pl.png | ||
| 309 | linux = ~/templates | ||
| 310 | windows = ~/templates | ||
| 311 | extensions = pl|pm|ini | ||
| 312 | directories = all | ||
| 313 | |||
| 314 | [DIRECTORY] | ||
| 315 | name = CodeManager | ||
| 316 | image = cm.png | ||
| 317 | linux = %CodeManager% | ||
| 318 | windows = %CodeManager% | ||
| 319 | extensions = all | ||
| 320 | ext_exclude = \.exists | ||
| 321 | directories = all | ||
| 322 | dir_exclude = \.svn | ||
| 323 | |||
| 324 | _CODE_MANAGER_ | ||
| 325 | |||
| 326 | mkdir "$home_directory/projects"; | ||
| 327 | $self-> write_to_file ( "$home_directory/projects/.exists" , '' ); | ||
| 328 | $self-> write_to_file ( "$home_directory/projects/CodeManager.cm" , $CodeManager_CodeManager ); | ||
| 329 | |||
| 330 | mkdir "$home_directory/templates"; | ||
| 331 | $self-> write_to_file ( "$home_directory/templates/.exists" , '' ); | ||
| 332 | $self-> write_to_file ( "$home_directory/templates/templates.ini" , '' ); | ||
| 333 | |||
| 334 | mkdir "$home_directory/templates/default"; | ||
| 335 | $self-> write_to_file ( "$home_directory/templates/default/.exists" , '' ); | ||
| 336 | |||
| 337 | mkdir "$home_directory/templates/list"; | ||
| 338 | $self-> write_to_file ( "$home_directory/templates/list/.exists" , '' ); | ||
| 339 | |||
| 340 | return; | ||
| 341 | } | ||
| 342 | |||
| 343 | ################################################################################ | ||
| 344 | |||
| 345 | 1; | ||
| 346 | |||
| 347 | __END__ |