dumpsplit 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/perl
  2. #
  3. # Created by Zachary
  4. # Updated by Brandon to include both split and join
  5. # Updated by Brandon for Perl5 and the new database format
  6. #
  7. ## require perl 5 or better, sorry guys, upgrade time.
  8. if ($] < 5) {
  9. die("This program requires perl 5.002 or greater.\n");
  10. }
  11. require 5.002;
  12. $parents = "";
  13. $buffer = "";
  14. select(STDOUT); $| = 1;
  15. sub frobfile {
  16. local($file) = @_;
  17. if ($file =~ /[^a-z0-9_]/) {
  18. print "\nInvalid object name \"\$$file\"";
  19. $file =~ s/[^a-z0-9_]//g;
  20. print ", stripping to \"\$$file\".\n";
  21. }
  22. return $file;
  23. }
  24. sub saveobj {
  25. local($object) = @_;
  26. local($objname);
  27. ($object =~ /^new object [\$#]([^:; ]+)/) ||
  28. ($object =~ /^object [\$#]([^:; ]+)/);
  29. $objname = $1;
  30. local($file) = &frobfile($objname);
  31. local($tmp) = "$file.cdc";
  32. local($i) = 0;
  33. while (-e $tmp) {
  34. print "\nFile \"$tmp\" exists, renaming to ";
  35. $i++;
  36. $tmp = "$file-${i}.cdc";
  37. print "\"$tmp\".\n";
  38. }
  39. $file = $tmp;
  40. print ".";
  41. open(FILE, ">$srcdir/$file") || die("\nUnable to write to \"$srcdir\".\n");
  42. print INDEX "$file\n";
  43. print FILE $object;
  44. print FILE $buffer;
  45. $buffer = "";
  46. $parents = "";
  47. close(FILE);
  48. }
  49. sub split {
  50. $reading_obj = 0;
  51. while (<DUMP>) {
  52. if (/^new object / || /^object /) {
  53. ($reading_obj) && &saveobj($reading_obj);
  54. $reading_obj = $_;
  55. } else {
  56. $buffer .= "$_";
  57. }
  58. }
  59. &saveobj($reading_obj);
  60. }
  61. sub unsplit {
  62. local($name);
  63. while (<INDEX>) {
  64. chomp;
  65. print STDERR ".";
  66. $file = "$srcdir/$_";
  67. if (-e $file) {
  68. open(FILE, $file);
  69. while (<FILE>) {
  70. print DUMP;
  71. }
  72. close(FILE);
  73. } else {
  74. print STDERR "\nAck, unable to find file \"$file\".\n";
  75. }
  76. }
  77. }
  78. $USAGE = <<END;
  79. Usage: dumpsplit <option> [<dump> <srcdir>]
  80. dump defaults to "textdump"
  81. srcdir defaults to "src"
  82. Options:
  83. -s -- split
  84. -j -- join db (unsplit)
  85. END
  86. $_ = $ARGV[0];
  87. shift;
  88. if ($ARGV[0]) { $textdump = $ARGV[0]; } else { $textdump = "textdump"; }
  89. if ($ARGV[1]) { $srcdir = $ARGV[1]; } else { $srcdir = "src"; }
  90. $index = "$srcdir/INDEX";
  91. if (!(-d $srcdir)) {
  92. mkdir($srcdir, 0755) ||
  93. (print "Unable to make directory \"$srcdir\"." && exit);
  94. }
  95. if (/^-s/) {
  96. open(DUMP, "$textdump") ||
  97. (print "${USAGE}Unable to open textdump \"$textdump\".\n" && exit);
  98. open(INDEX, ">$index") ||
  99. (print "${USAGE}Unable to open name file \"$index\".\n" && exit);
  100. print "Splitting db";
  101. &split();
  102. } elsif (/^-[uj]/) {
  103. open(DUMP, ">$textdump") ||
  104. (print "${USAGE}Unable to open textdump \"$textdump\".\n" && exit);
  105. open(INDEX, "$index") ||
  106. (print "${USAGE}Unable to open name file \"$index\".\n" && exit);
  107. print "Joining db";
  108. &unsplit();
  109. } else {
  110. print $USAGE;
  111. exit(0);
  112. }
  113. print "\n";
  114. close(DUMP);
  115. close(INDEX);