1 # ======================================================================
3 # moveUpOrAddAttribute.pl
5 # Copyright 2001 Sony Online Entertainment Inc.
8 # PURPOSE (object template .tpf file utility):
10 # If a given attribute exists for a specified class, move the attribute
11 # from the old class into the new class. The new class must be higher
12 # up (more general) in the class hierarchy. If the given attribute
13 # doesn't exist, add (or replace) it with the given default value in the
14 # new class section. If the new class section doesn't exist, create
15 # the new class section.
19 # perl moveUpOrAddAttribute.pl <tpf filename> <old class name> <new class name> <attribute name> <default value>
21 # ======================================================================
23 # check for proper argument count
24 if (scalar(@ARGV) != 5)
26 die "syntax: perl moveUpOrAddAttribute.pl <tpf filename> <old class name> <new class name> <attribute name> <default value>\n";
35 # open source tpf file
36 $sourcePathname = $ARGV[0];
37 open (SOURCE
, $sourcePathname) or die "failed to open source filename for reading [$sourcePathname]\n";
39 # open temp work filename
40 $destPathname = $sourcePathname . '.tmp';
41 open (DEST
, '>' . $destPathname) or die "failed to open dest filename for writing [$destPathname]\n";
44 $oldClassName = $ARGV[1];
45 $newClassName = $ARGV[2];
46 $attributeName = $ARGV[3];
48 $currentState = $SCAN_OUTSIDE;
49 $writeValue = $ARGV[4];
51 # process each line from source file
56 if ($currentState == $SCAN_OUTSIDE)
58 # check for new states
59 if (/^\@class $oldClassName/)
61 $currentState = $SCAN_OLD_CLASS;
63 elsif (/^\@class $newClassName/)
65 $currentState = $SCAN_NEW_CLASS;
74 if ($currentState == $SCAN_OLD_CLASS)
76 # check for new states
77 if (/^$attributeName\s=\s(.*)$/)
79 # found the old attribute, save its setting.
80 # do not emit the attribute in the new file (we're moving it)
85 # for all other input, we do copy the input
86 if (/^\@class\s(.*)\s$/)
88 # we're in the old class, moving to a new class
89 if ($1 eq $newClassName)
91 $currentState = $SCAN_NEW_CLASS;
95 $currentState = $SCAN_OUTSIDE;
105 if ($currentState == $SCAN_NEW_CLASS)
107 if (/^$attributeName\s=/)
109 # found the attribute in the new class, replace it.
110 # do not emit the attribute in the new file (we're moving it)
112 print DEST
"$attributeName = $writeValue\n";
114 # we're done, copy the rest
115 $currentState = $COPY_REMAINDER;
119 # for all other input, we do copy the input
123 # we're moving to a new class, need to inject the attribute since it hasn't appeared yet.
124 print DEST
"$attributeName = $writeValue\n\n";
126 # we're done, copy the rest
127 $currentState = $COPY_REMAINDER;
137 if ($currentState == $COPY_REMAINDER)
145 die "unknown state $currentState\n";
149 # let all states deal with the end-of-source-file event
153 if ($currentState == $SCAN_OUTSIDE)
155 # print missing class and attribute
156 print DEST
"\n\@class $newClassName\n$attributeName = $writeValue\n";
160 if ($currentState == $SCAN_OLD_CLASS)
162 # print missing class and attribute
163 print DEST
"\n\@class $newClassName\n$attributeName = $writeValue\n";
167 if ($currentState == $SCAN_NEW_CLASS)
169 # print missing attribute
170 print DEST
"$attributeName = $writeValue\n";
174 if ($currentState == $COPY_REMAINDER)
180 die "unknown state $currentState\n";
184 # close the source and dest file
188 # rename dest filename to source filename, clobbering the original source
189 rename $destPathname, $sourcePathname