Parent Directory
|
Revision Log
Revision 10 - (view) (download) (as text)
| 1 : | sam | 10 | #!/usr/local/bin/perl |
| 2 : | sam | 2 | |
| 3 : | gage | 8 | use lib '.'; use webworkInit; # WeBWorKInitLine |
| 4 : | sam | 2 | use capa2PG; |
| 5 : | use Benchmark; | ||
| 6 : | |||
| 7 : | |||
| 8 : | |||
| 9 : | # Called by | ||
| 10 : | # convert_capa_2_PG.pl indirectory outdirectory | ||
| 11 : | |||
| 12 : | unless (defined($ARGV[0]) ) { | ||
| 13 : | print qq! | ||
| 14 : | Useage: | ||
| 15 : | convert_capa_2_PG.pl in_directory_path out_directory_path | ||
| 16 : | |||
| 17 : | and will convert all *.txt files in the in directory from CAPA to PG files, | ||
| 18 : | placing them in *.pg files in the out directory. | ||
| 19 : | Files named prob1a.txt prob1b.txt will be concatenated | ||
| 20 : | and placed into the file prob1.pg. | ||
| 21 : | |||
| 22 : | For example: | ||
| 23 : | |||
| 24 : | convert_capa_2.PG.pl type1 type1pg | ||
| 25 : | |||
| 26 : | If out_directory_path is omitted then the out_directory_path is obtained by adding pg to the | ||
| 27 : | in_directory | ||
| 28 : | \n\n | ||
| 29 : | !; | ||
| 30 : | exit; | ||
| 31 : | } | ||
| 32 : | |||
| 33 : | |||
| 34 : | $directory = $ARGV[0]; | ||
| 35 : | $indirectory = $directory; # no final slash | ||
| 36 : | $outdirectory = "${directory}pg"; | ||
| 37 : | $outdirectory = $ARGV[1] if defined($ARGV[1]); | ||
| 38 : | |||
| 39 : | print "Converting all *.txt files in the directory | ||
| 40 : | $directory | ||
| 41 : | to the PG language and placing them in | ||
| 42 : | *.pg files in the directory | ||
| 43 : | $outdirectory. | ||
| 44 : | Proceed?>"; | ||
| 45 : | my $response = <STDIN>; | ||
| 46 : | exit unless $response =~ /y/i ; | ||
| 47 : | |||
| 48 : | |||
| 49 : | |||
| 50 : | # $outdirectory = "${directory}pg"; # no final slash | ||
| 51 : | opendir(DIRHANDLE, "$indirectory" ) || die "Can't open $indirectory $!"; | ||
| 52 : | |||
| 53 : | @allfiles = sort grep /\.txt/, readdir DIRHANDLE; | ||
| 54 : | closedir DIRHANDLE; | ||
| 55 : | |||
| 56 : | $first_file = shift(@allfiles); | ||
| 57 : | |||
| 58 : | |||
| 59 : | |||
| 60 : | |||
| 61 : | my @all_lines = slurp("$indirectory/$first_file"); | ||
| 62 : | |||
| 63 : | while (1) { | ||
| 64 : | my $file = shift @allfiles; | ||
| 65 : | |||
| 66 : | if ( root($first_file) eq root($file) ) { | ||
| 67 : | # $out_line .= "$directory/$file "; | ||
| 68 : | push(@all_lines, slurp("$directory/$file") ); | ||
| 69 : | } else { | ||
| 70 : | # we're done time to ship the file | ||
| 71 : | my $newName = root($first_file); | ||
| 72 : | #$newName =~ s/prob/capa/; | ||
| 73 : | |||
| 74 : | print STDERR "Creating $outdirectory/$newName.pg\n"; | ||
| 75 : | open(FILE_OUT, ">$outdirectory/$newName.pg") || print STDERR "Unable to output to $outdirectory/$newName.pg\n"; | ||
| 76 : | |||
| 77 : | ## begin Timing code | ||
| 78 : | my $beginTime = new Benchmark; | ||
| 79 : | ## end Timing code | ||
| 80 : | print FILE_OUT parse_CAPA_file(\@all_lines); | ||
| 81 : | ## begin Timing code | ||
| 82 : | my $endTime = new Benchmark; | ||
| 83 : | print FILE_OUT "\n#################################################\n## Processing time = ", timestr( timediff($endTime,$beginTime) ), | ||
| 84 : | "\n#################################################\n"; | ||
| 85 : | ## end Timing code | ||
| 86 : | |||
| 87 : | close(FILE_OUT); | ||
| 88 : | # print $out_line; | ||
| 89 : | # system( $out_line); | ||
| 90 : | # and prepare the next | ||
| 91 : | $first_file = $file; | ||
| 92 : | #$out_line = "cat $directory/$first_file "; | ||
| 93 : | @all_lines = slurp("$directory/$first_file"); | ||
| 94 : | last unless defined($first_file); | ||
| 95 : | } | ||
| 96 : | } | ||
| 97 : | |||
| 98 : | sub slurp { | ||
| 99 : | my $path = shift; | ||
| 100 : | my @lines = (); | ||
| 101 : | if (open(FILE, "<$path") ) { | ||
| 102 : | print STDERR "Reading $path\n"; | ||
| 103 : | @lines = <FILE>; | ||
| 104 : | } else { | ||
| 105 : | print STDERR "Can't open $path for reading. Skipping."; | ||
| 106 : | } | ||
| 107 : | @lines; | ||
| 108 : | } | ||
| 109 : | |||
| 110 : | |||
| 111 : | |||
| 112 : | |||
| 113 : | sub root { | ||
| 114 : | my $in = shift; | ||
| 115 : | $in =~ s/[abcdefg]*\.txt$//; | ||
| 116 : | $in; | ||
| 117 : | } |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |