split-sqlite3c.tcl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/tclsh
  2. #
  3. # This script splits the sqlite3.c amalgamated source code files into
  4. # several smaller files such that no single files is more than a fixed
  5. # number of lines in length (32k or 64k). Each of the split out files
  6. # is #include-ed by the master file.
  7. #
  8. # Splitting files up this way allows them to be used with older compilers
  9. # that cannot handle really long source files.
  10. #
  11. set MAX 32768 ;# Maximum number of lines per file.
  12. set BEGIN {^/\*+ Begin file ([a-zA-Z0-9_.]+) \*+/}
  13. set END {^/\*+ End of %s \*+/}
  14. set in [open sqlite3.c]
  15. set out1 [open sqlite3-all.c w]
  16. fconfigure $out1 -translation lf
  17. # Copy the header from sqlite3.c into sqlite3-all.c
  18. #
  19. while {[gets $in line]} {
  20. if {[regexp $BEGIN $line]} break
  21. puts $out1 $line
  22. }
  23. # Gather the complete content of a file into memory. Store the
  24. # content in $bufout. Store the number of lines is $nout
  25. #
  26. proc gather_one_file {firstline bufout nout} {
  27. regexp $::BEGIN $firstline all filename
  28. set end [format $::END $filename]
  29. upvar $bufout buf $nout n
  30. set buf $firstline\n
  31. global in
  32. set n 0
  33. while {[gets $in line]>=0} {
  34. incr n
  35. append buf $line\n
  36. if {[regexp $end $line]} break
  37. }
  38. }
  39. # Write a big chunk of text in to an auxiliary file "sqlite3-NNN.c".
  40. # Also add an appropriate #include to sqlite3-all.c
  41. #
  42. set filecnt 0
  43. proc write_one_file {content} {
  44. global filecnt
  45. incr filecnt
  46. set label $filecnt
  47. if {$filecnt>9} {
  48. set label [string index ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop \
  49. [expr {$filecnt-10}]]
  50. } else {
  51. set label $filecnt
  52. }
  53. set out [open sqlite3-$label.c w]
  54. fconfigure $out -translation lf
  55. puts -nonewline $out $content
  56. close $out
  57. puts $::out1 "#include \"sqlite3-$filecnt.c\""
  58. }
  59. # Continue reading input. Store chunks in separate files and add
  60. # the #includes to the main sqlite3-all.c file as necessary to reference
  61. # the extra chunks.
  62. #
  63. set all {}
  64. set N 0
  65. while {[regexp $BEGIN $line]} {
  66. set buf {}
  67. set n 0
  68. gather_one_file $line buf n
  69. if {$n+$N>=$MAX} {
  70. write_one_file $all
  71. set all {}
  72. set N 0
  73. }
  74. append all $buf
  75. incr N $n
  76. while {[gets $in line]>=0} {
  77. if {[regexp $BEGIN $line]} break
  78. if {$N>0} {
  79. write_one_file $all
  80. set N 0
  81. set all {}
  82. }
  83. puts $out1 $line
  84. }
  85. }
  86. if {$N>0} {
  87. write_one_file $all
  88. }
  89. close $out1
  90. close $in