xref: /sqlite-3.40.0/tool/split-sqlite3c.tcl (revision 2b0ea020)
11d21021fSdrh#!/usr/bin/tclsh
21d21021fSdrh#
31d21021fSdrh# This script splits the sqlite3.c amalgamated source code files into
41d21021fSdrh# several smaller files such that no single files is more than a fixed
51d21021fSdrh# number of lines in length (32k or 64k).  Each of the split out files
61d21021fSdrh# is #include-ed by the master file.
71d21021fSdrh#
81d21021fSdrh# Splitting files up this way allows them to be used with older compilers
91d21021fSdrh# that cannot handle really long source files.
101d21021fSdrh#
111d21021fSdrhset MAX 32768    ;# Maximum number of lines per file.
121d21021fSdrh
131d21021fSdrhset BEGIN {^/\*+ Begin file ([a-zA-Z0-9_.]+) \*+/}
141d21021fSdrhset END   {^/\*+ End of %s \*+/}
151d21021fSdrh
161d21021fSdrhset in [open sqlite3.c]
171d21021fSdrhset out1 [open sqlite3-all.c w]
18f7fc4c22Smistachkinfconfigure $out1 -translation lf
191d21021fSdrh
201d21021fSdrh# Copy the header from sqlite3.c into sqlite3-all.c
211d21021fSdrh#
221d21021fSdrhwhile {[gets $in line]} {
231d21021fSdrh  if {[regexp $BEGIN $line]} break
241d21021fSdrh  puts $out1 $line
251d21021fSdrh}
261d21021fSdrh
271d21021fSdrh# Gather the complete content of a file into memory.  Store the
281d21021fSdrh# content in $bufout.  Store the number of lines is $nout
291d21021fSdrh#
301d21021fSdrhproc gather_one_file {firstline bufout nout} {
311d21021fSdrh  regexp $::BEGIN $firstline all filename
321d21021fSdrh  set end [format $::END $filename]
331d21021fSdrh  upvar $bufout buf $nout n
341d21021fSdrh  set buf $firstline\n
351d21021fSdrh  global in
361d21021fSdrh  set n 0
371d21021fSdrh  while {[gets $in line]>=0} {
381d21021fSdrh    incr n
391d21021fSdrh    append buf $line\n
401d21021fSdrh    if {[regexp $end $line]} break
411d21021fSdrh  }
421d21021fSdrh}
431d21021fSdrh
441d21021fSdrh# Write a big chunk of text in to an auxiliary file "sqlite3-NNN.c".
451d21021fSdrh# Also add an appropriate #include to sqlite3-all.c
461d21021fSdrh#
471d21021fSdrhset filecnt 0
481d21021fSdrhproc write_one_file {content} {
491d21021fSdrh  global filecnt
501d21021fSdrh  incr filecnt
511d21021fSdrh  set out [open sqlite3-$filecnt.c w]
52f7fc4c22Smistachkin  fconfigure $out -translation lf
531d21021fSdrh  puts -nonewline $out $content
541d21021fSdrh  close $out
551d21021fSdrh  puts $::out1 "#include \"sqlite3-$filecnt.c\""
561d21021fSdrh}
571d21021fSdrh
581d21021fSdrh# Continue reading input.  Store chunks in separate files and add
591d21021fSdrh# the #includes to the main sqlite3-all.c file as necessary to reference
601d21021fSdrh# the extra chunks.
611d21021fSdrh#
621d21021fSdrhset all {}
631d21021fSdrhset N 0
641d21021fSdrhwhile {[regexp $BEGIN $line]} {
651d21021fSdrh  set buf {}
661d21021fSdrh  set n 0
671d21021fSdrh  gather_one_file $line buf n
681d21021fSdrh  if {$n+$N>=$MAX} {
691d21021fSdrh    write_one_file $all
701d21021fSdrh    set all {}
711d21021fSdrh    set N 0
721d21021fSdrh  }
731d21021fSdrh  append all $buf
741d21021fSdrh  incr N $n
751d21021fSdrh  while {[gets $in line]>=0} {
761d21021fSdrh    if {[regexp $BEGIN $line]} break
77*2b0ea020Sdrh    if {$N>0} {
78*2b0ea020Sdrh      write_one_file $all
79*2b0ea020Sdrh      set N 0
80*2b0ea020Sdrh      set all {}
81*2b0ea020Sdrh    }
821d21021fSdrh    puts $out1 $line
831d21021fSdrh  }
841d21021fSdrh}
851d21021fSdrhif {$N>0} {
861d21021fSdrh  write_one_file $all
871d21021fSdrh}
881d21021fSdrhclose $out1
891d21021fSdrhclose $in
90