| 1 | ################################################################################ | ||
| 2 | # Parse::GEDA::Gschem.pm # | ||
| 3 | ################################################################################ | ||
| 4 | # | ||
| 5 | # DESCRIPTION: | ||
| 6 | # Collection of routines used to parse and write back schematic files of the | ||
| 7 | # format specified by gEDA gschem schematic capture tool as detailed at: | ||
| 8 | # http://www.geda.seul.org/wiki/geda:file_format_spec | ||
| 9 | # | ||
| 10 | # EXAMPLE: | ||
| 11 | # my @schFiles = (); # array of schematic file names | ||
| 12 | # my @files = (); # array of schematic objects | ||
| 13 | # bakSchFiles(\@schFiles); # backup schematic files in bak/year-m-d_h-m-s/ | ||
| 14 | # $files = @{readSchFiles(\@schFiles)}; # parses the schematic files | ||
| 15 | # writeMsg(1, Dumper(\@files)); # prints out the entire data structure | ||
| 16 | # writeSchFiles(\@files); # write the data structure into schematic files | ||
| 17 | # | ||
| 18 | # REQUIREMENTS: | ||
| 19 | # perl 5.10 | ||
| 20 | # | ||
| 21 | ################################################################################ | ||
| 22 | # | ||
| 23 | # Copyright (C) 2008, JP Fricker. All rights reserved. | ||
| 24 | # | ||
| 25 | # This file is part of Parse::GEDA::Gschem | ||
| 26 | # | ||
| 27 | # Parse::GEDA::Gschem is free software: you can redistribute it and/or modify | ||
| 28 | # it under the terms of the GNU General Public License as published by | ||
| 29 | # the Free Software Foundation, either version 3 of the License, or | ||
| 30 | # (at your option) any later version. | ||
| 31 | # | ||
| 32 | # Parse::GEDA::Gschem is distributed in the hope that it will be useful, | ||
| 33 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 34 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 35 | # GNU General Public License for more details. | ||
| 36 | # | ||
| 37 | # You should have received a copy of the GNU General Public License | ||
| 38 | # along with Parse::GEDA::Gschem. If not, see <http://www.gnu.org/licenses/>. | ||
| 39 | # | ||
| 40 | ################################################################################ | ||
| 41 | package Parse::GEDA::Gschem; | ||
| 42 | |||
| 43 | use 5.10.0; | ||
| 44 | use feature "switch"; | ||
| 45 | use File::Copy; | ||
| 46 | use File::Basename; | ||
| 47 | use Parse::RecDescent; | ||
| 48 | $::RD_HINT = 1; | ||
| 49 | #$::RD_TRACE = 30; # Uncomment this if you want much detail about the parsing process | ||
| 50 | use Data::Dumper; | ||
| 51 | use Storable qw(dclone); | ||
| 52 | #use re 'debug'; | ||
| 53 | #use re 'eval'; | ||
| 54 | |||
| 55 | BEGIN { | ||
| 56 | use Exporter (); | ||
| 57 | our ($VERSION, @ISA, @EXPORT); | ||
| 58 | |||
| 59 | # set the version for version checking | ||
| 60 | use version; $VERSION = qv('1.00'); | ||
| 61 | |||
| 62 | @ISA = qw(Exporter); | ||
| 63 | @EXPORT = qw( | ||
| 64 | writeMsg | ||
| 65 | bakSchFiles | ||
| 66 | readSchFiles | ||
| 67 | writeSchFiles | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | ################################################################################ | ||
| 71 | |||
| 72 | our $VERBOSE = 0; # set it to greater values to get more details | ||
| 73 | our $ERRORFILENAME = $1.".log" if ($0 =~ m/.*\/([^\/]+?(\.pl){0})(\.pl)?$/); | ||
| 74 | |||
| 75 | |||
| 76 | #============================================================================== | ||
| 77 | # Write Message | ||
| 78 | #============================================================================== | ||
| 79 | sub writeMsg | ||
| 80 | { | ||
| 81 | my ($verbosity) = shift @_; | ||
| 82 | my ($message) = shift @_; | ||
| 83 | |||
| 84 | open(ErrorFile,">>$ERRORFILENAME") or die "Unable to open error file $ERRORFILENAME for writing.\n"; | ||
| 85 | |||
| 86 | print(ErrorFile $message); | ||
| 87 | print($message) if ($verbosity <= $VERBOSE); | ||
| 88 | |||
| 89 | } # writeMsg | ||
| 90 | |||
| 91 | #============================================================================== | ||
| 92 | # Backup schematic files | ||
| 93 | #============================================================================== | ||
| 94 | sub bakSchFiles { | ||
| 95 | use strict; | ||
| 96 | my @schFiles = @{shift @_}; | ||
| 97 | my @bakFiles; | ||
| 98 | my $bakDir = "bak"; | ||
| 99 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); | ||
| 100 | |||
| 101 | writeMsg (1, "Creating backup directory $bakDir ...\n"); | ||
| 102 | mkdir($bakDir); | ||
| 103 | ($! =~ m/|File exists/) or die "Can't create backup directory $bakDir : $!\n"; | ||
| 104 | |||
| 105 | $bakDir .= sprintf("/%04d-%02d-%02d_%02d-%02d-%02d", $year+1900, $mon, $mday, $hour, $min, $sec); | ||
| 106 | writeMsg (1, "Creating backup directory $bakDir ...\n"); | ||
| 107 | mkdir($bakDir); | ||
| 108 | ($! =~ m/|File exists/) or die "Can't create backup directory $bakDir : $!\n"; | ||
| 109 | |||
| 110 | if (1) { # change it to 0 if you don't want to save the program itself | ||
| 111 | writeMsg (1, "Copying program file to backup directory ...\n"); | ||
| 112 | my $progFile = $bakDir."/".$1 if ($0 =~ m/.*\/([^\/]+(\.pl)?)$/); | ||
| 113 | copy($0, $progFile) or die "Can't copy $0 to $progFile : $!\n"; | ||
| 114 | } | ||
| 115 | |||
| 116 | writeMsg (1, "Copying backup files to backup directory ...\n"); | ||
| 117 | foreach my $schFile (@schFiles) { | ||
| 118 | my $bakFile = $bakDir."/".$schFile; | ||
| 119 | push(@bakFiles, ($bakFile)); | ||
| 120 | mkdir(dirname($bakFile)); | ||
| 121 | ($! =~ m/|File exists/) or die "Can't create backup directory $bakDir : $!\n"; | ||
| 122 | copy($schFile, $bakFile) or die "Can't copy $schFile to $bakFile : $!\n"; | ||
| 123 | } | ||
| 124 | [ @bakFiles ] | ||
| 125 | } # bakSchFiles | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | #============================================================================== | ||
| 130 | # Read in sch files and parse them to build and return an array | ||
| 131 | #============================================================================== | ||
| 132 | sub readSchFiles { | ||
| 133 | use strict; | ||
| 134 | my @schFiles = @{shift @_}; | ||
| 135 | |||
| 136 | my $grammar = q{ | ||
| 137 | |||
| 138 | # See how to write such grammar at | ||
| 139 | # http://search.cpan.org/~dconway/Parse-RecDescent-v1.95.1/lib/Parse/RecDescent.pm | ||
| 140 | |||
| 141 | # Grammar is based on the gschem sch/sym file format | ||
| 142 | # http://geda.seul.org/wiki/geda:file_format_spec | ||
| 143 | |||
| 144 | File: | ||
| 145 | <skip: qr//> /\\A/ | ||
| 146 | <skip: qr//> Object(s) | ||
| 147 | { $ return = [ @{$item[-1]} ]; } | ||
| 148 | <skip: qr//> /\\n?\\z/ | ||
| 149 | |||
| 150 | Object: | ||
| 151 | <skip: qr//> | ||
| 152 | ( | ||
| 153 | Net | ||
| 154 | | Component | ||
| 155 | | Pin | ||
| 156 | | Version | ||
| 157 | | Line | ||
| 158 | | Box | ||
| 159 | | Circle | ||
| 160 | | Arc | ||
| 161 | | Attribute # same as Text with first string formated as name=value | ||
| 162 | | Text | ||
| 163 | | Picture | ||
| 164 | ) | ||
| 165 | { $return = $item[-1]; } | ||
| 166 | <skip: $item[1]> | ||
| 167 | | <error> | ||
| 168 | |||
| 169 | Version: | ||
| 170 | /^v\\s+/ <commit> | ||
| 171 | Int(2 /\\s+/) | ||
| 172 | { | ||
| 173 | $return = { ( | ||
| 174 | type => 'v', | ||
| 175 | version => $item[-1]->[0], # version of gEDA/gaf that wrote this file | ||
| 176 | fileformat_version => $item[-1]->[1], # gEDA/gaf file format version number | ||
| 177 | ) }; | ||
| 178 | } | ||
| 179 | /\\n/ | ||
| 180 | | <error?> <reject> | ||
| 181 | |||
| 182 | Line: | ||
| 183 | /^L\\s+/ <commit> | ||
| 184 | Int(10 /\\s+/) | ||
| 185 | { | ||
| 186 | $return = { ( | ||
| 187 | type => 'L', | ||
| 188 | x1 => $item[-1]->[ 0], # First X coordinate | ||
| 189 | y1 => $item[-1]->[ 1], # First Y coordinate | ||
| 190 | x2 => $item[-1]->[ 2], # Second X coordinate | ||
| 191 | y2 => $item[-1]->[ 3], # Second Y coordinate | ||
| 192 | color => $item[-1]->[ 4], # Color index | ||
| 193 | width => $item[-1]->[ 5], # Width of line | ||
| 194 | capstyle => $item[-1]->[ 6], # Line cap style | ||
| 195 | dashstyle => $item[-1]->[ 7], # Type of dash style | ||
| 196 | dashlength => $item[-1]->[ 8], # Length of dash | ||
| 197 | dashspace => $item[-1]->[ 9], # Space inbetween dashes | ||
| 198 | ) }; | ||
| 199 | } | ||
| 200 | /\\n/ | ||
| 201 | Attributes_Section | ||
| 202 | { | ||
| 203 | if ( @{$item[-1]} ) { | ||
| 204 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 205 | } else { 1 } | ||
| 206 | } | ||
| 207 | | <error?> <reject> | ||
| 208 | |||
| 209 | |||
| 210 | Attributes_Section: | ||
| 211 | /\\{\\n/ <commit> | ||
| 212 | Attribute(s) | ||
| 213 | { | ||
| 214 | if ( @{$item[-1]} ) { | ||
| 215 | $return = [ @{$item[-1]} ]; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | /\\}\\n/ | ||
| 219 | | <error?> <reject> | ||
| 220 | | { [] } | ||
| 221 | |||
| 222 | Attribute: | ||
| 223 | /^T\\s+/ <commit> | ||
| 224 | Int(9 /\\s+/) | ||
| 225 | { | ||
| 226 | $return = { ( | ||
| 227 | type => 'T', | ||
| 228 | x => $item[-1]->[ 0], # First X coordinate | ||
| 229 | y => $item[-1]->[ 1], # First Y coordinate | ||
| 230 | color => $item[-1]->[ 2], # Color index | ||
| 231 | size => $item[-1]->[ 3], # Size of textrdinate | ||
| 232 | visibility => $item[-1]->[ 4], # Visibility of text | ||
| 233 | show_name_value => $item[-1]->[ 5], # Attribute visibility control | ||
| 234 | angle => $item[-1]->[ 6], # Angle of the text | ||
| 235 | alignment => $item[-1]->[ 7], # Alignment/origin of the text | ||
| 236 | num_lines => $item[-1]->[ 8], # Number of lines of text (1 based) | ||
| 237 | ) }; | ||
| 238 | } | ||
| 239 | /\\n/ | ||
| 240 | name | ||
| 241 | { $return->{name} = $item[-1] } | ||
| 242 | /=/ | ||
| 243 | String[$return->{num_lines}] | ||
| 244 | { $return->{value} = $item[-1] } | ||
| 245 | /\\n/ | ||
| 246 | | <error?> <reject> | ||
| 247 | |||
| 248 | name: /\\w+/ | ||
| 249 | |||
| 250 | Text: | ||
| 251 | /^T\\s+/ <commit> | ||
| 252 | Int(9 /\\s+/) | ||
| 253 | { | ||
| 254 | $return = { ( | ||
| 255 | type => 'T', | ||
| 256 | x => $item[-1]->[ 0], # First X coordinate | ||
| 257 | y => $item[-1]->[ 1], # First Y coordinate | ||
| 258 | color => $item[-1]->[ 2], # Color index | ||
| 259 | size => $item[-1]->[ 3], # Size of textrdinate | ||
| 260 | visibility => $item[-1]->[ 4], # Visibility of text | ||
| 261 | show_name_value => $item[-1]->[ 5], # Attribute visibility control | ||
| 262 | angle => $item[-1]->[ 6], # Angle of the text | ||
| 263 | alignment => $item[-1]->[ 7], # Alignment/origin of the text | ||
| 264 | num_lines => $item[-1]->[ 8], # Number of lines of text (1 based) | ||
| 265 | ) }; | ||
| 266 | } | ||
| 267 | /\\n/ | ||
| 268 | String[$return->{num_lines}] | ||
| 269 | { $return->{string} = $item[-1] } | ||
| 270 | /\\n/ | ||
| 271 | | <error?> <reject> | ||
| 272 | |||
| 273 | String: # takes the number of lines to be fetched as first argument | ||
| 274 | /[^\\n]*/ | ||
| 275 | # if $arg[0] == 1 we don't need to fetch other lines | ||
| 276 | ( <reject: !($arg[0] == 1)> | /\\n/ String[$arg[0]-1] ) | ||
| 277 | { | ||
| 278 | if ($arg[0] gt 1) { | ||
| 279 | $return = $item[1]."\\n".$item[2] | ||
| 280 | } else { | ||
| 281 | $return = $item[1]; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | Picture: | ||
| 286 | /^G\\s+/ <commit> | ||
| 287 | Int(7 /\\s+/) | ||
| 288 | { | ||
| 289 | $return = { ( | ||
| 290 | type => 'G', | ||
| 291 | x => $item[-1]->[ 0], # Lower left X coordinate | ||
| 292 | y => $item[-1]->[ 1], # Lower left Y coordinate | ||
| 293 | width => $item[-1]->[ 2], # Width of the picture | ||
| 294 | height => $item[-1]->[ 3], # Height of the picture | ||
| 295 | angle => $item[-1]->[ 4], # Angle of the picture | ||
| 296 | mirrored => $item[-1]->[ 5], # Mirrored or normal picture | ||
| 297 | embedded => $item[-1]->[ 6], # Embedded or link to the picture file | ||
| 298 | ) }; | ||
| 299 | } | ||
| 300 | /\\n/ | ||
| 301 | String[1] | ||
| 302 | { $return->{filename} = $item[-1]; } | ||
| 303 | /\\n/ | ||
| 304 | Embedded_Picture[$return->{embedded}] | ||
| 305 | { | ||
| 306 | if (defined($item[-1])) { | ||
| 307 | $return->{embedded_picture_data} = $item[-1]; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | | <error?> <reject> | ||
| 311 | |||
| 312 | Embedded_Picture: | ||
| 313 | <reject: !($arg[0] == 0)> | ||
| 314 | | { | ||
| 315 | $text =~ s/(?<data>([^\\n]*\\n)*)\\.\\n//m; | ||
| 316 | $return = $+{data}; | ||
| 317 | } | ||
| 318 | |||
| 319 | Box: | ||
| 320 | /^B\\s+/ <commit> | ||
| 321 | Int(16 /\\s+/) | ||
| 322 | { | ||
| 323 | $return = { ( | ||
| 324 | type => 'B', | ||
| 325 | x => $item[-1]->[ 0], # Lower left hand X coordinate | ||
| 326 | y => $item[-1]->[ 1], # Lower left hand Y coordinate | ||
| 327 | span => $item[-1]->[ 2], # Width of the box (x direction) | ||
| 328 | height => $item[-1]->[ 3], # Height of the box (y direction) | ||
| 329 | color => $item[-1]->[ 4], # Color index | ||
| 330 | width => $item[-1]->[ 5], # Width of lines | ||
| 331 | capstyle => $item[-1]->[ 6], # Lines cap style | ||
| 332 | dashstyle => $item[-1]->[ 7], # Type of dash style | ||
| 333 | dashlength => $item[-1]->[ 8], # Length of dash | ||
| 334 | dashspace => $item[-1]->[ 9], # Space inbetween dashes | ||
| 335 | filltype => $item[-1]->[10], # Type of fill | ||
| 336 | fillwidth => $item[-1]->[11], # Width of the fill lines | ||
| 337 | angle1 => $item[-1]->[12], # First angle of fill | ||
| 338 | pitch1 => $item[-1]->[13], # First pitch/spacing of fill | ||
| 339 | angle2 => $item[-1]->[14], # Second angle of fill | ||
| 340 | pitch2 => $item[-1]->[15], # Second pitch/spacing of fill | ||
| 341 | ) }; | ||
| 342 | } | ||
| 343 | /\\n/ | ||
| 344 | Attributes_Section | ||
| 345 | { | ||
| 346 | if ( @{$item[-1]} ) { | ||
| 347 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 348 | } else { 1 } | ||
| 349 | } | ||
| 350 | | <error?> <reject> | ||
| 351 | |||
| 352 | Circle: | ||
| 353 | /^V\\s+/ <commit> | ||
| 354 | Int(15 /\\s+/) | ||
| 355 | { | ||
| 356 | $return = { ( | ||
| 357 | type => 'V', | ||
| 358 | x => $item[-1]->[ 0], # Center X coordinate | ||
| 359 | y => $item[-1]->[ 1], # Center Y coordinate | ||
| 360 | radius => $item[-1]->[ 2], # Radius of the circle | ||
| 361 | color => $item[-1]->[ 3], # Color index | ||
| 362 | width => $item[-1]->[ 4], # Width of circle | ||
| 363 | capstyle => $item[-1]->[ 5], # 0 unused | ||
| 364 | dashstyle => $item[-1]->[ 6], # Type of dash style | ||
| 365 | dashlength => $item[-1]->[ 7], # Length of dash | ||
| 366 | dashspace => $item[-1]->[ 8], # Space inbetween dashes | ||
| 367 | filltype => $item[-1]->[ 9], # Type of fill | ||
| 368 | fillwidth => $item[-1]->[10], # Width of the fill lines | ||
| 369 | angle1 => $item[-1]->[11], # First angle of fill | ||
| 370 | pitch1 => $item[-1]->[12], # First pitch/spacing of fill | ||
| 371 | angle2 => $item[-1]->[13], # Second angle of fill | ||
| 372 | pitch2 => $item[-1]->[14], # Second pitch/spacing of fill | ||
| 373 | ) }; | ||
| 374 | } | ||
| 375 | /\\n/ | ||
| 376 | Attributes_Section | ||
| 377 | { | ||
| 378 | if ( @{$item[-1]} ) { | ||
| 379 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 380 | } else { 1 } | ||
| 381 | } | ||
| 382 | | <error?> <reject> | ||
| 383 | |||
| 384 | Arc: | ||
| 385 | /^A\\s+/ <commit> | ||
| 386 | Int(11 /\\s+/) | ||
| 387 | { | ||
| 388 | $return = { ( | ||
| 389 | type => 'A', | ||
| 390 | x => $item[-1]->[ 0], # Center X coordinate | ||
| 391 | y => $item[-1]->[ 1], # Center X coordinate | ||
| 392 | radius => $item[-1]->[ 2], # Radius of the arc | ||
| 393 | startangle => $item[-1]->[ 3], # Starting angle of the arc | ||
| 394 | sweepangle => $item[-1]->[ 4], # Amount the arc sweeps | ||
| 395 | color => $item[-1]->[ 5], # Color index | ||
| 396 | width => $item[-1]->[ 6], # Width of line | ||
| 397 | capstyle => $item[-1]->[ 7], # Line cap style | ||
| 398 | dashstyle => $item[-1]->[ 8], # Type of dash style | ||
| 399 | dashlength => $item[-1]->[ 9], # Length of dash | ||
| 400 | dashspace => $item[-1]->[10], # Space inbetween dashes | ||
| 401 | ) }; | ||
| 402 | } | ||
| 403 | /\\n/ | ||
| 404 | Attributes_Section | ||
| 405 | { | ||
| 406 | if ( @{$item[-1]} ) { | ||
| 407 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 408 | } else { 1 } | ||
| 409 | } | ||
| 410 | | <error?> <reject> | ||
| 411 | |||
| 412 | Net: | ||
| 413 | /^N\\s+/ <commit> | ||
| 414 | Int(5 /\\s+/) | ||
| 415 | { | ||
| 416 | $return = { ( | ||
| 417 | type => 'N', | ||
| 418 | x1 => $item[-1]->[ 0], # First X coordinate | ||
| 419 | y1 => $item[-1]->[ 1], # First Y coordinate | ||
| 420 | x2 => $item[-1]->[ 2], # Second X coordinate | ||
| 421 | y2 => $item[-1]->[ 3], # Second Y coordinate | ||
| 422 | color => $item[-1]->[ 4], # Color index | ||
| 423 | ) }; | ||
| 424 | } | ||
| 425 | /\\n/ | ||
| 426 | Attributes_Section | ||
| 427 | { | ||
| 428 | if ( @{$item[-1]} ) { | ||
| 429 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 430 | } else { 1 } | ||
| 431 | } | ||
| 432 | | <error?> <reject> | ||
| 433 | |||
| 434 | Bus: | ||
| 435 | /^U\\s+/ <commit> | ||
| 436 | Int(6 /\\s+/) | ||
| 437 | { | ||
| 438 | $return = { ( | ||
| 439 | type => 'U', | ||
| 440 | x1 => $item[-1]->[ 0], # First X coordinate | ||
| 441 | y1 => $item[-1]->[ 1], # First Y coordinate | ||
| 442 | x2 => $item[-1]->[ 2], # Second X coordinate | ||
| 443 | y2 => $item[-1]->[ 3], # Second Y coordinate | ||
| 444 | color => $item[-1]->[ 4], # Color index | ||
| 445 | ripperdir => $item[-1]->[ 5], # Direction of bus rippers | ||
| 446 | ) }; | ||
| 447 | } | ||
| 448 | /\\n/ | ||
| 449 | Attributes_Section | ||
| 450 | { | ||
| 451 | if ( @{$item[-1]} ) { | ||
| 452 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 453 | } else { 1 } | ||
| 454 | } | ||
| 455 | | <error?> <reject> | ||
| 456 | |||
| 457 | Pin: | ||
| 458 | /^P\\s+/ <commit> | ||
| 459 | Int(7 /\\s+/) | ||
| 460 | { | ||
| 461 | $return = { ( | ||
| 462 | type => 'P', | ||
| 463 | x1 => $item[-1]->[ 0], # First X coordinate | ||
| 464 | y1 => $item[-1]->[ 1], # First Y coordinate | ||
| 465 | x2 => $item[-1]->[ 2], # Second X coordinate | ||
| 466 | y2 => $item[-1]->[ 3], # Second Y coordinate | ||
| 467 | color => $item[-1]->[ 4], # Color index | ||
| 468 | pintype => $item[-1]->[ 5], # Type of pin | ||
| 469 | whichend => $item[-1]->[ 6], # Specifies the active end | ||
| 470 | ) }; | ||
| 471 | } | ||
| 472 | /\\n/ | ||
| 473 | Attributes_Section | ||
| 474 | { | ||
| 475 | if ( @{$item[-1]} ) { | ||
| 476 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 477 | } else { 1 } | ||
| 478 | } | ||
| 479 | | <error?> <reject> | ||
| 480 | |||
| 481 | Component: | ||
| 482 | /^C\\s+/ <commit> | ||
| 483 | Int(5 /\\s+/) | ||
| 484 | { | ||
| 485 | $return = { ( | ||
| 486 | type => 'C', | ||
| 487 | x => $item[-1]->[ 0], # Origin X coordinate | ||
| 488 | y => $item[-1]->[ 1], # Origin Y coordinate | ||
| 489 | selectable => $item[-1]->[ 2], # Selectable flag | ||
| 490 | angle => $item[-1]->[ 3], # Angle of the component | ||
| 491 | mirror => $item[-1]->[ 4], # Mirror around Y axis | ||
| 492 | ) }; | ||
| 493 | } | ||
| 494 | /\\s+/ String[1] | ||
| 495 | { | ||
| 496 | $return->{basename} = $item[-1]; | ||
| 497 | } | ||
| 498 | /\\n/ | ||
| 499 | Embedded_Component_Section | ||
| 500 | { | ||
| 501 | if ( @{$item[-1]} ) { | ||
| 502 | $return->{Embedded} = [ @{$item[-1]} ]; | ||
| 503 | } else { 1 } | ||
| 504 | } | ||
| 505 | Attributes_Section | ||
| 506 | { | ||
| 507 | if ( @{$item[-1]} ) { | ||
| 508 | $return->{Attributes} = [ @{$item[-1]} ]; | ||
| 509 | } else { 1 } | ||
| 510 | } | ||
| 511 | | <error?> <reject> | ||
| 512 | |||
| 513 | Embedded_Component_Section: | ||
| 514 | /\\[\\n/ <commit> | ||
| 515 | Object(s) | ||
| 516 | { | ||
| 517 | if ( @{$item[-1]} ) { | ||
| 518 | $return = [ @{$item[-1]} ]; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | /\\]\\n/ | ||
| 522 | | <error?> <reject> | ||
| 523 | | { [] } | ||
| 524 | |||
| 525 | Int: /[-+]?[0-9]+/m | ||
| 526 | |||
| 527 | }; | ||
| 528 | |||
| 529 | my $parser = new Parse::RecDescent($grammar); | ||
| 530 | |||
| 531 | my @files = (); | ||
| 532 | |||
| 533 | writeMsg (1, "Parsing schematic files ...\n"); | ||
| 534 | foreach my $fileName (@schFiles) { | ||
| 535 | writeMsg (2, "Parsing schematic file $fileName ...\n"); | ||
| 536 | open(SCHFILE,"<$fileName") or die "Can't open $fileName to read\n"; | ||
| 537 | undef $/; | ||
| 538 | my $text = <SCHFILE>; | ||
| 539 | my %fileHash = (); | ||
| 540 | $fileHash{fileName} = $fileName; | ||
| 541 | $fileHash{objects} = ($parser->File($text) or die "Couldn't complete parsing!\n"); | ||
| 542 | push(@files, { %fileHash } ); | ||
| 543 | } | ||
| 544 | |||
| 545 | if (0) { | ||
| 546 | $Data::Dumper::Purity = 1; | ||
| 547 | my $result = Data::Dumper->new( [ \@files ], ["files"] ); | ||
| 548 | writeMsg(1, $result->Dump); | ||
| 549 | } | ||
| 550 | |||
| 551 | [ @files ]; | ||
| 552 | |||
| 553 | } # end readSchFiles | ||
| 554 | |||
| 555 | |||
| 556 | #============================================================================== | ||
| 557 | # Write files by running through the @files array | ||
| 558 | #============================================================================== | ||
| 559 | sub writeSchFiles { | ||
| 560 | use strict; | ||
| 561 | my @files = @{shift @_}; | ||
| 562 | my $newData = ""; | ||
| 563 | |||
| 564 | writeMsg(1, "Writing schematic files ...\n"); | ||
| 565 | for (my $file_idx = 0; $file_idx < @files; $file_idx++) { | ||
| 566 | my $fileName = $files[$file_idx]->{fileName}; | ||
| 567 | writeMsg(2, "Collecting data for schematic file $fileName ...\n"); | ||
| 568 | $newData = printObjects( \@{$files[$file_idx]->{objects}} ); | ||
| 569 | if (0) { | ||
| 570 | writeMsg(1, $newData."\n"); | ||
| 571 | } | ||
| 572 | writeMsg(2, "Writing schematic file $fileName ...\n"); | ||
| 573 | open(SCHFILE,">$fileName") or die "Can't open $fileName to write\n"; | ||
| 574 | print SCHFILE $newData; | ||
| 575 | } | ||
| 576 | } # end writeSchFiles | ||
| 577 | |||
| 578 | |||
| 579 | sub printAttributes { | ||
| 580 | use strict; | ||
| 581 | my @attributes = @{shift @_} if (ref($_[0]) eq "ARRAY") or return ""; | ||
| 582 | my $return = ""; | ||
| 583 | |||
| 584 | if (@attributes) { | ||
| 585 | $return .= "{\n"; | ||
| 586 | foreach my $attribute (@attributes) { | ||
| 587 | given ($attribute->{type}) { | ||
| 588 | when ('T') { | ||
| 589 | $return .= join(" ", | ||
| 590 | $attribute->{type}, | ||
| 591 | $attribute->{x}, | ||
| 592 | $attribute->{y}, | ||
| 593 | $attribute->{color}, | ||
| 594 | $attribute->{size}, | ||
| 595 | $attribute->{visibility}, | ||
| 596 | $attribute->{show_name_value}, | ||
| 597 | $attribute->{angle}, | ||
| 598 | $attribute->{alignment}, | ||
| 599 | $attribute->{num_lines}, | ||
| 600 | )."\n";; | ||
| 601 | $return .= $attribute->{name}."=".$attribute->{value}."\n"; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | } | ||
| 605 | $return .= "}\n"; | ||
| 606 | } | ||
| 607 | $return; | ||
| 608 | } # end printAttributes | ||
| 609 | |||
| 610 | |||
| 611 | sub printEmbedded_Components { | ||
| 612 | use strict; | ||
| 613 | my @embeddeds = @{shift @_} if (ref($_[0]) eq "ARRAY") or return ""; | ||
| 614 | my $return = ""; | ||
| 615 | |||
| 616 | if (@embeddeds) { | ||
| 617 | $return .= "[\n"; | ||
| 618 | $return .= printObjects(\@embeddeds); | ||
| 619 | $return .= "]\n"; | ||
| 620 | } | ||
| 621 | $return; | ||
| 622 | } # printEmbedded_Components | ||
| 623 | |||
| 624 | |||
| 625 | sub printObjects { | ||
| 626 | use strict; | ||
| 627 | my @objects = @{shift @_}; | ||
| 628 | my $return = ""; | ||
| 629 | |||
| 630 | # Go through each object | ||
| 631 | foreach my $object (@objects) { | ||
| 632 | if (0) { | ||
| 633 | $Data::Dumper::Purity = 1; | ||
| 634 | my $result = Data::Dumper->new( [ \%{$object} ], ["object"] ); | ||
| 635 | writeMsg(1, "Processing ".$result->Dump." ...\n"); | ||
| 636 | } | ||
| 637 | |||
| 638 | given ($object->{type}) { | ||
| 639 | |||
| 640 | when ('v') { | ||
| 641 | $return .= join(" ", | ||
| 642 | $object->{type}, | ||
| 643 | $object->{version}, | ||
| 644 | $object->{fileformat_version}, | ||
| 645 | )."\n"; | ||
| 646 | } # end when ('v') | ||
| 647 | |||
| 648 | when ('L') { | ||
| 649 | $return .= join(" ", | ||
| 650 | $object->{type}, | ||
| 651 | $object->{x1}, | ||
| 652 | $object->{y1}, | ||
| 653 | $object->{x2}, | ||
| 654 | $object->{y2}, | ||
| 655 | $object->{color}, | ||
| 656 | $object->{width}, | ||
| 657 | $object->{capstyle}, | ||
| 658 | $object->{dashstyle}, | ||
| 659 | $object->{dashlength}, | ||
| 660 | $object->{dashspace}, | ||
| 661 | )."\n"; | ||
| 662 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 663 | } # end when ('L') | ||
| 664 | |||
| 665 | when ('T') { | ||
| 666 | $return .= join(" ", | ||
| 667 | $object->{type}, | ||
| 668 | $object->{x}, | ||
| 669 | $object->{y}, | ||
| 670 | $object->{color}, | ||
| 671 | $object->{size}, | ||
| 672 | $object->{visibility}, | ||
| 673 | $object->{show_name_value}, | ||
| 674 | $object->{angle}, | ||
| 675 | $object->{alignment}, | ||
| 676 | $object->{num_lines}, | ||
| 677 | )."\n"; | ||
| 678 | if ($object->{string}) { | ||
| 679 | $return .= $object->{string}."\n"; | ||
| 680 | } elsif (($object->{name}) && ($object->{value})) { | ||
| 681 | $return .= $object->{name}."=".$object->{value}."\n"; | ||
| 682 | } | ||
| 683 | } # end when ('T') | ||
| 684 | |||
| 685 | when ('G') { | ||
| 686 | $return .= join(" ", | ||
| 687 | $object->{type}, | ||
| 688 | $object->{x}, | ||
| 689 | $object->{y}, | ||
| 690 | $object->{width}, | ||
| 691 | $object->{height}, | ||
| 692 | $object->{angle}, | ||
| 693 | $object->{mirrored}, | ||
| 694 | $object->{embedded}, | ||
| 695 | )."\n"; | ||
| 696 | $return .= $object->{filename}."\n" if (defined($object->{filename})); | ||
| 697 | $return .= $object->{embedded_picture_data}."\n.\n" if (defined($object->{embedded_picture_data})); | ||
| 698 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 699 | } # end when ('G') | ||
| 700 | |||
| 701 | |||
| 702 | when ('B') { | ||
| 703 | $return .= join(" ", | ||
| 704 | $object->{type}, | ||
| 705 | $object->{x}, | ||
| 706 | $object->{y}, | ||
| 707 | $object->{span}, | ||
| 708 | $object->{height}, | ||
| 709 | $object->{color}, | ||
| 710 | $object->{width}, | ||
| 711 | $object->{capstyle}, | ||
| 712 | $object->{dashstyle}, | ||
| 713 | $object->{dashlength}, | ||
| 714 | $object->{dashspace}, | ||
| 715 | $object->{filltype}, | ||
| 716 | $object->{fillwidth}, | ||
| 717 | $object->{angle1}, | ||
| 718 | $object->{pitch1}, | ||
| 719 | $object->{angle2}, | ||
| 720 | $object->{pitch2}, | ||
| 721 | )."\n"; | ||
| 722 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 723 | } # end when ('B') | ||
| 724 | |||
| 725 | when ('V') { | ||
| 726 | $return .= join(" ", | ||
| 727 | $object->{type}, | ||
| 728 | $object->{x}, | ||
| 729 | $object->{y}, | ||
| 730 | $object->{radius}, | ||
| 731 | $object->{color}, | ||
| 732 | $object->{width}, | ||
| 733 | $object->{capstyle}, | ||
| 734 | $object->{dashstyle}, | ||
| 735 | $object->{dashlength}, | ||
| 736 | $object->{dashspace}, | ||
| 737 | $object->{filltype}, | ||
| 738 | $object->{fillwidth}, | ||
| 739 | $object->{angle1}, | ||
| 740 | $object->{pitch1}, | ||
| 741 | $object->{angle2}, | ||
| 742 | $object->{pitch2}, | ||
| 743 | )."\n"; | ||
| 744 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 745 | } # end when ('V') | ||
| 746 | |||
| 747 | when ('A') { | ||
| 748 | $return .= join(" ", | ||
| 749 | $object->{type}, | ||
| 750 | $object->{x}, | ||
| 751 | $object->{y}, | ||
| 752 | $object->{radius}, | ||
| 753 | $object->{startangle}, | ||
| 754 | $object->{sweepangle}, | ||
| 755 | $object->{color}, | ||
| 756 | $object->{width}, | ||
| 757 | $object->{capstyle}, | ||
| 758 | $object->{dashstyle}, | ||
| 759 | $object->{dashlength}, | ||
| 760 | $object->{dashspace}, | ||
| 761 | )."\n"; | ||
| 762 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 763 | } # end when ('A') | ||
| 764 | |||
| 765 | when ('N') { | ||
| 766 | $return .= join(" ", | ||
| 767 | $object->{type}, | ||
| 768 | $object->{x1}, | ||
| 769 | $object->{y1}, | ||
| 770 | $object->{x2}, | ||
| 771 | $object->{y2}, | ||
| 772 | $object->{color}, | ||
| 773 | )."\n"; | ||
| 774 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 775 | } # end when ('N') | ||
| 776 | |||
| 777 | when ('U') { | ||
| 778 | $return .= join(" ", | ||
| 779 | $object->{type}, | ||
| 780 | $object->{x1}, | ||
| 781 | $object->{y1}, | ||
| 782 | $object->{x2}, | ||
| 783 | $object->{y2}, | ||
| 784 | $object->{color}, | ||
| 785 | $object->{ripperdir}, | ||
| 786 | )."\n"; | ||
| 787 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 788 | } # end when ('P') | ||
| 789 | |||
| 790 | when ('P') { | ||
| 791 | $return .= join(" ", | ||
| 792 | $object->{type}, | ||
| 793 | $object->{x1}, | ||
| 794 | $object->{y1}, | ||
| 795 | $object->{x2}, | ||
| 796 | $object->{y2}, | ||
| 797 | $object->{color}, | ||
| 798 | $object->{pintype}, | ||
| 799 | $object->{whichend}, | ||
| 800 | )."\n"; | ||
| 801 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 802 | } # end when ('P') | ||
| 803 | |||
| 804 | when ('C') { | ||
| 805 | $return .= join(" ", | ||
| 806 | $object->{type}, | ||
| 807 | $object->{x}, | ||
| 808 | $object->{y}, | ||
| 809 | $object->{selectable}, | ||
| 810 | $object->{angle}, | ||
| 811 | $object->{mirror}, | ||
| 812 | $object->{basename}, | ||
| 813 | )."\n"; | ||
| 814 | $return .= printEmbedded_Components(\@{$object->{Embedded}}); | ||
| 815 | $return .= printAttributes(\@{$object->{Attributes}}); | ||
| 816 | } # end when ('C') | ||
| 817 | |||
| 818 | default { | ||
| 819 | $Data::Dumper::Purity = 1; | ||
| 820 | my $result = Data::Dumper->new( [ \%{$object} ], ["object"] ); | ||
| 821 | die "Object type '".$object->{type}."' not recognized: ".$result->Dump."\n"; | ||
| 822 | } | ||
| 823 | |||
| 824 | } # end given my $object_type | ||
| 825 | } # end foreach my $object | ||
| 826 | $return | ||
| 827 | } # end printObjects | ||
| 828 | |||
| 829 | 1; |