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