split-sqlite3c.tcl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. # Copy the header from sqlite3.c into sqlite3-all.c
  17. #
  18. while {[gets $in line]} {
  19. if {[regexp $BEGIN $line]} break
  20. puts $out1 $line
  21. }
  22. # Gather the complete content of a file into memory. Store the
  23. # content in $bufout. Store the number of lines is $nout
  24. #
  25. proc gather_one_file {firstline bufout nout} {
  26. regexp $::BEGIN $firstline all filename
  27. set end [format $::END $filename]
  28. upvar $bufout buf $nout n
  29. set buf $firstline\n
  30. global in
  31. set n 0
  32. while {[gets $in line]>=0} {
  33. incr n
  34. append buf $line\n
  35. if {[regexp $end $line]} break
  36. }
  37. }
  38. # Write a big chunk of text in to an auxiliary file "sqlite3-NNN.c".
  39. # Also add an appropriate #include to sqlite3-all.c
  40. #
  41. set filecnt 0
  42. proc write_one_file {content} {
  43. global filecnt
  44. incr filecnt
  45. set out [open sqlite3-$filecnt.c w]
  46. puts -nonewline $out $content
  47. close $out
  48. puts $::out1 "#include \"sqlite3-$filecnt.c\""
  49. }
  50. # Continue reading input. Store chunks in separate files and add
  51. # the #includes to the main sqlite3-all.c file as necessary to reference
  52. # the extra chunks.
  53. #
  54. set all {}
  55. set N 0
  56. while {[regexp $BEGIN $line]} {
  57. set buf {}
  58. set n 0
  59. gather_one_file $line buf n
  60. if {$n+$N>=$MAX} {
  61. write_one_file $all
  62. set all {}
  63. set N 0
  64. }
  65. append all $buf
  66. incr N $n
  67. while {[gets $in line]>=0} {
  68. if {[regexp $BEGIN $line]} break
  69. puts $out1 $line
  70. }
  71. }
  72. if {$N>0} {
  73. write_one_file $all
  74. }
  75. close $out1
  76. close $in