http://qs321.pair.com?node_id=387827
Category: Text Processing
Author/Contact Info Mike Irwin <rhythmicus@pobox.com>
Description:

A simple script to create 'linked' text files for viewing on an iPod. To those who don't know, the iPod imposes a 4k limit on text files viewed on-screen. The script will take files that exceed 4k and create new ones with a filename in the form of file.1, file.2, etc. The width of the numbered extension will grow if needed (ie. file.001 .. file.100). The files are also linked for easy navigation.

DO NOT USE ON ORIGINALS unless you want to piece the file back together.

#!/usr/bin/perl -w

# Copyright (C) 2004 Mike Irwin <rhythmicus@pobox.com>

# This program is free software; you can redistribute it and/or modify
+ it under
# the same terms as Perl itself.

# This program comes with ABSOLUTELY NO WARRANTY.

# See `perldoc perlartistic'.

use strict;
use POSIX qw(floor);

use constant SPLIT_SIZE => 3996;

foreach (@ARGV) {
    die "$_: $!\n" unless -r;

    my $size = -s _;
    next unless $size > 4096;

    my $last_size = $size % SPLIT_SIZE;
    my $num_files = floor( $size / SPLIT_SIZE );
    $num_files++ if $last_size;

    my ( $file, $read, $j, $data, $link );
    my $width = length($num_files);

    open OF, "+<$_" or die "$_: $!\n";

    for ( my $i = $num_files ; $i > 0 ; $i-- ) {
        $file = sprintf( "%s.%0*d", $_, $width, $i );
        $read = 0;

        open NF, ">$file" or die "$file: $!\n";
        seek OF,
          -( ( $i == $num_files and $last_size ) ? $last_size : SPLIT_
+SIZE ), 2;

        while ( $j = read OF, $data, SPLIT_SIZE ) {
            print NF $data;
            $read += $j;
        }

        print NF "...\n\n<a href=\"$link\">Next Page</a>"
          if ( $i < $num_files );

        close NF;
        truncate OF, $size -= $read;
        $link = $file;
    }

    close OF;
    unlink or warn "$_: $!\n";
}
Replies are listed 'Best First'.
Re: ipodsplit
by radiantmatrix (Parson) on Sep 08, 2004 at 14:19 UTC
    Interesting tool. What might make it even more interesting is to add a command-line option to split for either iPod or Palm Memo format. I don't (and probably won't) own an iPod; but I do use my Palm to read text notes.

    I know that there are Palm apps that read large text files, but sometimes having them in memo form to beam to others would be great.

    --
    $me = rand($hacker{perl});