Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Book as variable #14

Open
wants to merge 1 commit into from

2 participants

@garu

This patch is meant to address (though not entirely) issue #6. It provides a new Data() method for EBook::MOBI::MobiPerl::Palm::PDB, that returns the binary data itself instead of writing it to a file.

As a result, Write() becomes a simple convenience wrapper for Data(), writing to a file. There should be no backward compatibility issues, but it will let you provide front-end API calls in EBook::MOBI that will return the data instead of writing it to a file, which could be useful for web services and whatnot.

WARNING: There is a tradeoff in that it will need to store the entire PDB in memory before writing it to disk. If this is a problem then I suggest not taking this patch.

WARNING #2: Except for "perl -c", this was all blind-coding. I'm having trouble installing some of the prereqs on my system so I can't run the test suite.

Hope this helps. Cheers!

@garu garu new Data() method returns the binary instead of writing to file
This patch turns Write() into a simple convenience wrapper for Data(),
writing to a file. There should be no backward compatibility issues,
but it will let you provide front-end API calls that will return
the data instead of writing it to a file, which could be useful for
web services and whatnot.

WARNING: There is a tradeoff in that it will need to store the entire
PDB in memory before writing it to disk. If this is a problem then I
suggest not taking this patch.
1feaed6
@garu

My suggestion:

since EBook::MOBI already provides this:

$mobi->pack( $mhtml, $filename, $author, $title);

we could make things a bit different and also let the user also provide an open filehandle or a scalar ref. This would allow something like:

my $data;
$mobi->pack( $mhtml, \$data, $author, $title);

It could make things cleaner and prevent the memory tradeoff entirely.

If you like it, I can submit yet another patch (but in this case you should probably NOT accept this pull request)

@borisdaeppen

The second approach seems the better one?
I let the decision to you.

Which prereqs are disturbing youand why?
I have so many which are nor really needed, so maybe I can remove it!
One heavy prereq is GD... is it that what makes you trouble?

@borisdaeppen

Please feel free to make a full patch, including what ever it needs, so that things will work. I just get confused with to much half-working patches, because I'm working at the "input_plugin" branch right now :-)

@borisdaeppen

BTW:

The dependency to GD is disturbing anyway! I also have problems installing GD on one of my Debian machines!

I use GD in EBook::MOBI::Picture to resize images... is there a smaller module that does this without this problems?
For testing purposes it might help if you delete "t/23_picture.t" --- not, the rest will not compile because you don't have the lib... whatever.

I'm happy if GD can be removed from the module, but I think it would be great if images get resized automatically if they are to big as it is now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 15, 2012
  1. @garu

    new Data() method returns the binary instead of writing to file

    garu authored
    This patch turns Write() into a simple convenience wrapper for Data(),
    writing to a file. There should be no backward compatibility issues,
    but it will let you provide front-end API calls that will return
    the data instead of writing it to a file, which could be useful for
    web services and whatnot.
    
    WARNING: There is a tradeoff in that it will need to store the entire
    PDB in memory before writing it to disk. If this is a problem then I
    suggest not taking this patch.
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 19 deletions.
  1. +39 −19 lib/EBook/MOBI/MobiPerl/Palm/PDB.pm
View
58 lib/EBook/MOBI/MobiPerl/Palm/PDB.pm
@@ -925,15 +925,15 @@ sub _load_resources
}
}
-=head2 Write
+=head2 Data
- $pdb->Write("filename");
+ my $bindata = $pdb->Data();
Invokes methods in helper classes to get the application-specific
parts of the database, then writes the database to the file
I<filename>.
-Write() uses the following helper methods:
+Data() uses the following helper methods:
=over
@@ -959,15 +959,11 @@ See also L</HELPER CLASSES>.
=cut
#' <-- For Emacs
-sub Write
+sub Data
{
my $self = shift;
- my $fname = shift; # Output file name
my @record_data;
-
- # Open file
- open my $OFILE, '>', $fname or die "Can't write to \"$fname\": $!\n";
- binmode $OFILE; # Write as binary file under MS-DOS
+ my $full_data;
# Get AppInfo block
my $appinfo_block = $self->PackAppInfoBlock;
@@ -1122,13 +1118,13 @@ sub Write
$self->{uniqueIDseed};
;
- print $OFILE "$header";
+ $full_data = $header;
# Write index header
my $index_header;
$index_header = pack "N n", 0, ($#record_data+1);
- print $OFILE "$index_header";
+ $full_data .= $index_header;
# Write index
my $rec_offset; # Offset of next record/resource
@@ -1162,7 +1158,7 @@ sub Write
$type,
$id,
$rec_offset;
- print $OFILE "$index_data";
+ $full_data .= $index_data;
$rec_offset += length($data);
}
@@ -1192,7 +1188,7 @@ sub Write
($id >> 16) & 0xff,
($id >> 8) & 0xff,
$id & 0xff;
- print $OFILE "$index_data";
+ $full_data .= $index_data;
$rec_offset += length($data);
}
@@ -1201,16 +1197,16 @@ sub Write
# Write the two NULs
if (length($self->{"2NULs"}) == 2)
{
- print $OFILE $self->{"2NULs"};
+ $full_data .= $self->{"2NULs"};
} else {
- print $OFILE "\0\0";
+ $full_data .= "\0\0";
}
# Write AppInfo block
- print $OFILE $appinfo_block unless $appinfo_offset == 0;
+ $full_data .= $appinfo_block unless $appinfo_offset == 0;
# Write sort block
- print $OFILE $sort_block unless $sort_offset == 0;
+ $full_data .= $sort_block unless $sort_offset == 0;
# Write record/resource list
my $record;
@@ -1231,10 +1227,34 @@ sub Write
($attributes, $id, $data) = @{$record};
}
- print $OFILE $data;
+ $full_data .= $data;
}
- close $OFILE;
+ return $full_data;
+}
+
+=head2 Write
+
+ $pdb->Write("filename");
+
+Convenience wrapper around L</Data>, writing the database to the
+file I<filename>.
+
+=cut
+
+sub Write {
+ my $self = shift;
+ my $fname = shift; # Output file name
+
+ my $data = $self->Data;
+
+ # Open file
+ open my $OFILE, '>', $fname or die "Can't write to \"$fname\": $!\n";
+ binmode $OFILE; # Write as binary file under MS-DOS
+
+ print $OFILE $data;
+
+ close $OFILE;
}
=head2 new_Record
Something went wrong with that request. Please try again.