| 1 |
#!/usr/local/bin/perl |
| 2 |
# $Id: acecsplit,v 1.1 1995/02/02 22:21:42 rd Exp $ |
| 3 |
# File: acecsplit |
| 4 |
# Author: Detlef Wolf ([email protected]) |
| 5 |
# Copyright (C) European Data Resource, 1994 |
| 6 |
#-------------------------------------------------------------------- |
| 7 |
# This file is part of the IGD project |
| 8 |
# |
| 9 |
# Description: split a .ace file into parts according to mapping file |
| 10 |
# usage: acecsplit [-c <configfile>] [-p <prefix>] [<filename>...] |
| 11 |
# Inputs: .ace data from files or stdin |
| 12 |
# if option -c is given, then |
| 13 |
# split according to config file, |
| 14 |
# else split by class name |
| 15 |
# Output: .ace data is distributed into files according to config |
| 16 |
# if <prefix> is given then all output files start with it, |
| 17 |
# default for <prefix> is "split." |
| 18 |
# Precondition: |
| 19 |
# Postcondition: |
| 20 |
# HISTORY: |
| 21 |
# Created: Fri Nov 11 09:49:27 1994 (dok256) |
| 22 |
#-------------------------------------------------------------------- |
| 23 |
|
| 24 |
|
| 25 |
# ------------------ evaluate command line options |
| 26 |
if ($ARGV[0] =~ /^-h(elp)?/) { |
| 27 |
print |
| 28 |
"usage: acecsplit [-h] [-c <configfile>] [-p <prefix>] [<filename>...] |
| 29 |
purpose: split .ace files into parts |
| 30 |
<prefix> is the string prepended to all output file names; default \"split.\" |
| 31 |
modes of operation: |
| 32 |
- automatic |
| 33 |
put all objects of each class into a file with the classes name |
| 34 |
- configured (option -c) |
| 35 |
a mapping file controls witch class goes to which file; |
| 36 |
Format of the lines in the mapping file: |
| 37 |
<class_name> <split_file_name> |
| 38 |
A special class name is \"default\", where all non-mapped classes go |
| 39 |
"; |
| 40 |
exit; |
| 41 |
}; |
| 42 |
|
| 43 |
if ($ARGV[0] eq "-c") { |
| 44 |
shift; |
| 45 |
$configfile = $ARGV[0]; |
| 46 |
shift; |
| 47 |
$byname=0; |
| 48 |
} else {$byname=1}; |
| 49 |
|
| 50 |
#$byname && die "class name split no yet implemented; use -c option\n"; |
| 51 |
|
| 52 |
$prefix="split."; |
| 53 |
if ($ARGV[0] eq "-p") { |
| 54 |
shift; |
| 55 |
$prefix = $ARGV[0]; |
| 56 |
shift; |
| 57 |
}; |
| 58 |
|
| 59 |
if (! $byname) { |
| 60 |
# ------------------ parse the config file |
| 61 |
open(L,"<$configfile") || die "couldn't open $configfile"; |
| 62 |
while (<L>) { |
| 63 |
/^\s*#/ && next; |
| 64 |
/^\s*(\w+)\s+(\w+)\b/ || next; |
| 65 |
$class2db{$1}=$2; |
| 66 |
$db2class{$2}= $db2class{$2} . " " . $1; |
| 67 |
print "mapping class $1 onto output $2\n"; |
| 68 |
}; |
| 69 |
close(L); |
| 70 |
if (! $class2db{"default"}) {die "$configfile must contain a default clause"}; |
| 71 |
|
| 72 |
# ---------------------- open the output channels for write |
| 73 |
|
| 74 |
foreach (keys %db2class) { |
| 75 |
&openoutput($_); |
| 76 |
}; |
| 77 |
|
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
# --------------------- parse the .ace file |
| 82 |
# set an intitial target file name |
| 83 |
if ($byname) { |
| 84 |
# $targetdb="unclassified"; |
| 85 |
# $db2class{$targetdb}= "(lines before first object)" |
| 86 |
# &openoutput($targetdb); |
| 87 |
$targetdb=""; # ignore the lines before the first object |
| 88 |
} else { |
| 89 |
$targetdb=$class2db{"default"}; |
| 90 |
} |
| 91 |
|
| 92 |
$inobject=0; |
| 93 |
while (<>) { |
| 94 |
m|^//| && next; # skip comments |
| 95 |
if (/^(\w+)\s*:?\s+"?([^"]+)"?/ && !$inobject) { # a new object starts |
| 96 |
$class=$1; |
| 97 |
$object=$2; |
| 98 |
$inobject=1; |
| 99 |
|
| 100 |
$targetdb=$class2db{$class}; # determine output destination |
| 101 |
if (! $targetdb) { # a new/unknown destination |
| 102 |
if ($byname) { # to a new file or to default? |
| 103 |
$class2db{$class}=$class; # open a new file |
| 104 |
$db2class{$class}= $db2class{$class} . " " . $class; |
| 105 |
&openoutput($class); |
| 106 |
$targetdb=$class; |
| 107 |
} |
| 108 |
else { |
| 109 |
$targetdb=$class2db{"default"} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ($class eq "LongText") { |
| 114 |
print $targetdb $_; |
| 115 |
while (<>) { |
| 116 |
print $targetdb $_; |
| 117 |
/^\*\*\*LongTextEnd\*\*\*$/ && last; |
| 118 |
}; |
| 119 |
print $targetdb "\n"; |
| 120 |
$inobject=0; |
| 121 |
next; |
| 122 |
} |
| 123 |
}; |
| 124 |
if (/^\s*$/ && $inobject) { # an object ends |
| 125 |
$inobject=0; |
| 126 |
} |
| 127 |
print $targetdb $_; |
| 128 |
|
| 129 |
}; # loop over the .ace lines |
| 130 |
|
| 131 |
# -------------------- subroutines ---------------- |
| 132 |
sub openoutput { |
| 133 |
# input: $_[0] -- name of output channel |
| 134 |
local($handle,$file); |
| 135 |
$handle=$_[0]; |
| 136 |
$file="$prefix$_[0].ace"; |
| 137 |
open($handle,">$file") || die "file $file not writeable"; |
| 138 |
chop ($date=`date`); |
| 139 |
print $handle "// file $file created by acecsplit on $date |
| 140 |
// for class(es) $db2class{$_[0]}\n\n"; |
| 141 |
print "opening $file\n"; |
| 142 |
}; |
| 143 |
|
| 144 |
|