#!/usr/bin/perl # # genEncodeVarWithCoderPB.pl # v1.1 # This simple perl script generate accessor methods definitions of an # Objective-C class. The generated code uses the "always retain" form (see # below). # # - Typical installation on Mac OS X: # Install this script in ~/Library/Scripts. # - Typical usage on Mac OS X: # Copy in the clipboard some text containing the member variable declarations # for which you want the accessors. Select this script from the script menu at # the top right of the screen (search the internet for how to install the script # menu). Then simply do a "paste" somewhere in your code or text file and you # will see the generated code. If you don't want to use the Script menu, # you can invoke the script from the terminal. # # Works with every type of variable; e.g., for the following variables: # id name; # float expectedRaise; # # the generated code is: # - (id)name { return name; } # - (void)setName:(id)_t_m_p_ # { # [_t_m_p_ retain]; # [name release]; # name = _t_m_p_; # } # - (float)expectedRaise { return expectedRaise; } # - (void)setExpectedRaise:(float)_t_m_p_ { expectedRaise = _t_m_p_; } # # Requirements: # - each member variable declaration must be correct Objective-C syntax; # - each member variable should occupy one and only one line; # - class names can begin with lowercase as well as uppercase (or _) chars; # - variable names can begin with lowercase as well as uppercase (or _) chars; # - empty lines between member variables are ok; # - IBOutlet and IBAction macros are not handled. # # Known bugs: # - for unknown reasons, activating this script from the Script menu doesn't # seem to work on Tiger. It does work on Panther, as well as activating it # from the command line. # # History of Changes # v1.1 # - Removed evil \r characters from the input string. # v1.0 # - Initial version. # # Created by Ettore Pasquini on 11/18/04. # Copyright 2004, 2005 Cubelogic. # This code is released under the GNU GPL without any warranty, without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License at http://gnu.org for more details. # Writing these notes took me much more than writing the script. # Tested on Mac OS X 10.3.6. # Report bugs to support@cubelogic.org $input = `/usr/bin/pbpaste`; # remove evil \r characters $_ = $input; s#\015\n#\n#g; # DOS lines s#\015#\n#g; # Mac lines $input = $_; @inputlist = split /\n/, $input; $output = ""; foreach $line (@inputlist) { if ($line =~ /^\s*id\s+/) { # it's an `id' object $line1 = $line; $line2 = $line; $line1 =~ s%^\s*(id)\s+(\w+);.*%- ($1)$2 { return $2; }%; $line2 =~ s%^\s*(id)\s+(\w+);.*%- (void)set\u$2:($1)_t_m_p_\n{\n\t[_t_m_p_ retain];\n\t[$2 release];\n\t$2 = _t_m_p_;\n}%; } elsif ($line =~ /^\s*\w+\s*\*\s*\w+/) { # it's an object $line1 = $line; $line2 = $line; $line1 =~ s%^\s*(\w+)\s*\*\s*(\w+);.*%- ($1 *)$2 { return $2; }%; $line2 =~ s%^\s*(\w+)\s*\*\s*(\w+);.*%- (void)set\u$2:($1 *)_t_m_p_\n{\n\t[_t_m_p_ retain];\n\t[$2 release];\n\t$2 = _t_m_p_;\n}%; } else { # it's a int, or float, or other primitive type $line1 = $line; $line2 = $line; $line1 =~ s%^\s*(\w+)\s*(\w+);.*%- ($1)$2 { return $2; }%; $line2 =~ s%^\s*(\w+)\s*(\w+);.*%- (void)set\u$2:($1)_t_m_p_ { $2 = _t_m_p_; }%; } $output .= "$line1\n$line2\n"; } # open pasteboard as output file open(PBCOPY, "|-", '/usr/bin/pbcopy') or die "Unable to open pbcopy"; print PBCOPY $output; close PBCOPY;