/usr/share/perl5/Palm/StdAppInfo.pm is in libpalm-perl 1:1.012-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | # Palm::StdAppInfo.pm
#
# Class for dealing with standard AppInfo blocks in PDBs.
#
# Copyright (C) 1999, 2000, Andrew Arensburger.
# You may distribute this file under the terms of the Artistic
# License, as specified in the README file.
use strict;
package Palm::StdAppInfo;
use Palm::Raw();
# Don't harass me about these variables
use vars qw( $VERSION @ISA $error );
# $error acts like $! in that it reports the error that occurred
# One liner, to allow MakeMaker to work.
$VERSION = '1.012';
@ISA = qw( Palm::Raw );
=head1 NAME
Palm::StdAppInfo - Handles standard AppInfo block (categories)
=head1 SYNOPSIS
Usually:
package MyPDBHandler;
use Palm::StdAppInfo(); # Note the parentheses
@ISA = qw( Palm::StdAppInfo );
use constant APPINFO_PADDING = 1;
sub ParseAppInfoBlock {
my $self = shift;
my $data = shift;
my $appinfo = {};
&Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
$app_specific_data = $appinfo->{other};
}
sub PackAppInfoBlock {
my $self = shift;
my $retval;
$self->{appinfo}{other} = <pack application-specific data>;
$retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
return $retval;
}
Or as a standalone C<PDB> helper class:
use Palm::StdAppInfo;
=head1 DESCRIPTION
Many Palm applications use a common format for keeping track of categories.
The C<Palm::StdAppInfo> class deals with this common format:
$pdb = new Palm::PDB;
$pdb->Load("myfile.pdb");
@categories = @{$pdb->{appinfo}{categories}};
$lastUniqueID = $pdb->{appinfo}{lastUniqueID};
$other = $pdb->{appinfo}{other};
where:
C<@categories> is an array of references-to-hash:
=over 4
=item C<$cat = $categories[0];>
=item C<$cat-E<gt>{name}>
The name of the category, a string of at most 16 characters.
=item C<$cat-E<gt>{id}>
The category ID, an integer in the range 0-255. Each category has a
unique ID. By convention, 0 is reserved for the "Unfiled" category;
IDs assigned by the Palm are in the range 1-127, and IDs assigned by
the desktop are in the range 128-255.
=item C<$cat-E<gt>{renamed}>
A boolean. This field is true iff the category has been renamed since
the last sync.
=back
C<$lastUniqueID> is (I think) the last category ID that was assigned.
C<$other> is any data that follows the category list in the AppInfo
block. If you're writing a helper class for a PDB that includes a
category list, you should parse this field to get any data that
follows the category list; you should also make sure that this field
is initialized before you call C<&Palm::StdAppInfo::pack_AppInfo>.
=head2 APPINFO_PADDING
Normally, the AppInfo block includes a byte of padding at the end, to
bring its length to an even number. However, some databases use this
byte for data.
If your database uses the padding byte for data, then your
C<&ParseAppInfoBlock> method (see L<"SYNOPSIS">) should call
C<&parse_StdAppInfo> with a true $nopadding argument.
If, for whatever reason, you wish to inherit
C<&StdAppInfo::ParseAppInfoBlock>, then add
use constant APPINFO_PADDING => 0;
to your handler package, to tell it that the padding byte is really
data.
=head1 FUNCTIONS
=cut
#'
use constant APPINFO_PADDING => 1; # Whether to add the padding byte at
# the end of the AppInfo block.
# Note that this might be considered a hack:
# this relies on the fact that 'use constant'
# defines a function with no arguments; that
# therefore this can be called as an instance
# method, with full inheritance. That is, if
# the handler class doesn't define it, Perl
# will find the constant in the parent. If
# this ever changes, the code below that uses
# $self->APPINFO_PADDING will need to be
# changed.
use constant numCategories => 16; # Number of categories in AppInfo block
use constant categoryLength => 16; # Length of category names
use constant stdAppInfoSize => # Length of a standard AppInfo block
2 +
(categoryLength * numCategories) +
numCategories +
1 + 1; # The padding byte at the end may
# be omitted
sub import
{
&Palm::PDB::RegisterPDBHandlers(__PACKAGE__,
[ "", "" ],
);
}
=head2 seed_StdAppInfo
&Palm::StdAppInfo::seed_StdAppInfo(\%appinfo);
Creates the standard fields in an existing AppInfo hash. Usually used
to ensure that a newly-created AppInfo block contains an initialized
category array:
my $appinfo = {};
&Palm::StdAppInfo::seed_StdAppInfo($appinfo);
Note: this is not a method.
=cut
# seed_StdAppInfo
# *** THIS IS NOT A METHOD ***
# Given a reference to an appinfo hash, creates all of the fields for
# a new AppInfo block.
sub seed_StdAppInfo
{
my $appinfo = shift;
my $i;
$appinfo->{categories} = []; # Create array of categories
# Initialize the categories
# Note that all of the IDs are initialized to $i. There's no
# real good reason for doing it this way, except that that's
# what the Palm appears to do with new category lists.
for ($i = 0; $i < numCategories; $i++)
{
$appinfo->{categories}[$i] = {};
$appinfo->{categories}[$i]{renamed} = 0;
$appinfo->{categories}[$i]{name} = undef;
$appinfo->{categories}[$i]{id} = $i;
}
# The only fixed category is "Unfiled". Initialize it now
$appinfo->{categories}[0]{name} = "Unfiled";
$appinfo->{categories}[0]{id} = 0;
# I'm not sure what this is, but let's initialize it.
# The Palm appears to initialize this to numCategories - 1.
$appinfo->{lastUniqueID} = numCategories - 1;
}
=head2 newStdAppInfo
$appinfo = Palm::StdAppInfo->newStdAppInfo;
Like C<seed_StdAppInfo>, but creates an AppInfo hash and returns a
reference to it.
=cut
sub newStdAppInfo
{
my $class = shift;
my $retval = {};
&seed_StdAppInfo($retval);
return $retval;
}
=head2 new
$pdb = new Palm::StdAppInfo;
Create a new PDB, initialized with nothing but a standard AppInfo block.
There are very few reasons to use this, and even fewer good ones. If
you're writing a helper class to parse some PDB format that contains a
category list, then you should make that helper class a subclass of
C<Palm::StdAppInfo>.
=cut
#'
sub new
{
my $classname = shift;
my $self = $classname->SUPER::new(@_);
# Create a generic PDB. No need to rebless it,
# though.
# Initialize the AppInfo block
$self->{appinfo} = &newStdAppInfo();
return $self;
}
=head2 parse_StdAppInfo
$len = &Palm::StdAppInfo::parse_StdAppInfo(\%appinfo, $data, $nopadding);
This function (this is not a method) is intended to be called from
within a PDB helper class's C<ParseAppInfoBlock> method.
C<parse_StdAppInfo()> parses a standard AppInfo block from the raw
data C<$data> and fills in the fields in C<%appinfo>. It returns the
number of bytes parsed.
C<$nopadding> is optional, and defaults to false. Normally, the
AppInfo block includes a padding byte at the end. If C<$nopadding> is
true, then C<&parse_StdAppInfo> assumes that the padding byte is
application data, and includes it in C<$appinfo{'other'}>, so that the
caller can parse it.
=cut
#'
# parse_StdAppInfo
# *** THIS IS NOT A METHOD ***
#
# Reads the raw data from $data, parses it as a standard AppInfo
# block, and fills in the corresponding fields in %$appinfo. Returns
# the number of bytes parsed.
sub parse_StdAppInfo
{
my $appinfo = shift; # A reference to hash, to fill in
my $data = shift; # Raw data to read
my $nopadding = shift; # Optional: no padding byte at end
my $unpackstr; # First argument to unpack()
my $renamed; # Bitmap of renamed categories
my @labels; # Array of category labels
my @uniqueIDs; # Array of category IDs
my $lastUniqueID; # Not sure what this is
return undef
if length $data < 4+(categoryLength*numCategories)+numCategories;
if (!defined($nopadding))
{
$nopadding = 0;
}
# Make sure $appinfo contains all of the requisite fields
&seed_StdAppInfo($appinfo);
# The argument to unpack() isn't hard to understand, it's just
# hard to write in a readable fashion.
$unpackstr = # Argument to unpack(), since it's hairy
"n" . # Renamed categories
("a" . categoryLength) x numCategories .
# Category labels
"C" x numCategories .
# Category IDs
"C" . # Last unique ID
"x";
# Unpack the data
($renamed,
@labels[0..(numCategories-1)],
@uniqueIDs[0..(numCategories-1)],
$lastUniqueID) =
unpack $unpackstr, $data;
# Clean this stuff up a bit
for (@labels)
{
s/\0.*$//; # Trim at NUL
}
# Now put the data into $appinfo
my $i;
for ($i = 0; $i < numCategories; $i++)
{
$appinfo->{categories}[$i]{renamed} =
($renamed & (1 << $i) ? 1 : 0);
$appinfo->{categories}[$i]{name} = $labels[$i];
$appinfo->{categories}[$i]{id} = $uniqueIDs[$i];
}
$appinfo->{lastUniqueID} = $lastUniqueID;
# There might be other stuff in the AppInfo block other than
# the standard categories. Put everything else in
# $appinfo->{other}.
$appinfo->{other} = substr($data,
stdAppInfoSize - ($nopadding ? 1 : 0));
return ($nopadding ? stdAppInfoSize - 1 : stdAppInfoSize);
}
=head2 ParseAppInfoBlock
$pdb = new Palm::StdAppInfo;
$pdb->ParseAppInfoBlock($data);
If your application's AppInfo block contains standard category support
and nothing else, you may choose to just inherit this method instead
of writing your own C<ParseAppInfoBlock> method. Otherwise, see the
example in L<"SYNOPSIS">.
=cut
#'
sub ParseAppInfoBlock
{
my $self = shift;
my $data = shift;
my $appinfo = {};
&parse_StdAppInfo($appinfo, $data, $self->APPINFO_PADDING);
return $appinfo;
}
=head2 pack_StdAppInfo
$data = &Palm::StdAppInfo::pack_StdAppInfo(\%appinfo);
This function (this is not a method) is intended to be called from
within a PDB helper class's C<PackAppInfoBlock> method.
C<pack_StdAppInfo> takes an AppInfo hash and packs it as a string of
raw data that can be written to a PDB.
Note that if you're using this inside a helper class's
C<PackAppInfoBlock> method, you should make sure that
C<$appinfo{other}> is properly initialized before you call
C<&Palm::StdAppInfo::pack_StdAppInfo>.
C<$nopadding> is optional, and defaults to false. Normally, the
AppInfo block includes a byte of padding at the end. If C<$nopadding>
is true, then C<&pack_StdAppInfo> doesn't include this byte of
padding, so that the application can use it.
=cut
#'
# pack_StdAppInfo
# *** THIS IS NOT A METHOD ***
#
# Given a reference to a hash containing an AppInfo block (such as
# that initialized by parse_StdAppInfo()), returns a packed string
# that can be written to the PDB file.
sub pack_StdAppInfo
{
my $appinfo = shift;
my $nopadding = shift;
my $retval;
my $i;
$nopadding = 0 if !defined($nopadding);
# Create the bitfield of renamed categories
my $renamed;
$renamed = 0;
for ($i = 0; $i < numCategories; $i++)
{
if ($appinfo->{categories}[$i]{renamed})
{
$renamed |= (1 << $i);
}
}
$retval = pack("n", $renamed);
# There have to be exactly 16 categories in the AppInfo block,
# even though $appinfo->{categories} may have been mangled
# by a naive (or clever) user or broken program.
for ($i = 0; $i < numCategories; $i++)
{
my $name; # Category name
# This is mainly to stop Perl 5.6 from complaining if
# the category name is undefined.
if ((!defined($appinfo->{categories}[$i]{name})) ||
$appinfo->{categories}[$i]{name} eq "")
{
$name = "";
} else {
$name = $appinfo->{categories}[$i]{name};
}
$retval .= pack("a" . categoryLength, $name);
}
# Ditto for category IDs
for ($i = 0; $i < numCategories; $i++)
{
$retval .= pack("C", $appinfo->{categories}[$i]{id});
}
# Last unique ID, and alignment padding
$retval .= pack("Cx", $appinfo->{lastUniqueID});
$retval .= $appinfo->{other} if defined($appinfo->{other});
return $retval;
}
=head2 PackAppInfoBlock
$pdb = new Palm::StdAppInfo;
$data = $pdb->PackAppInfoBlock();
If your application's AppInfo block contains standard category support
and nothing else, you may choose to just inherit this method instead
of writing your own C<PackAppInfoBlock> method. Otherwise, see the
example in L<"SYNOPSIS">.
=cut
#'
sub PackAppInfoBlock
{
my $self = shift;
return &pack_StdAppInfo($self->{appinfo}, $self->{APPINFO_PADDING});
}
=head2 addCategory
$pdb->addCategory($name [, $id [, $renamed]]);
Adds a category to $pdb.
The $name argument specifies the new category's name.
The optional $id argument specifies the new category's numeric ID; if
omitted or undefined, &addCategory will pick one.
The optional $renamed argument is a boolean value indicating whether
the new category should be marked as having been modified. This
defaults to true since, conceptually, &addCategory doesn't really add
a category: it finds one whose name happens to be empty, and renames
it.
Returns a true value if successful, false otherwise. In case of
failure, &addCategory sets $Palm::StdAppInfo::error to an error
message.
=cut
#'
# XXX - When choosing a new category ID, should pick them from the
# range 128-255.
sub addCategory
{
my $self = shift; # PDB
my $name = shift; # Category name
my $id = shift; # Category ID (optional)
my $renamed = $#_ >= 0 ? $_[0] : 1;
# Flag: was the category renamed (optional)
# This initialization may look weird,
# but it's this way so that it'll
# default to true if omitted.
my $categories = $self->{appinfo}{categories};
my $i;
my %used; # Category IDs in use
# Collect all the IDs in the current list
for (@{$categories})
{
next if !defined($_->{name}) || $_->{name} eq "";
$used{$_->{id}} = 1;
}
if (defined($id))
{
# Sanity check: make sure this ID isn't already in use
if (defined($used{$id}))
{
$error = "Category ID already in use";
return undef;
}
} else {
# Find an unused category number, if none was specified
for ($id = 128; $id < 256; $id++)
{
last if !defined($used{$id});
}
}
# Go through the list of categories, looking for an unused slot
for ($i = 0; $i < numCategories; $i++)
{
# Ignore named categories
next unless !defined($categories->[$i]{name}) or
$categories->[$i]{name} eq "";
# Found an empty slot
$categories->[$i]{name} = $name;
$categories->[$i]{id} = $id;
$categories->[$i]{renamed} = $renamed;
return 1;
}
# If we get this far, there are no empty category slots
$error = "No unused categories";
return undef;
}
=head2 deleteCategory
$pdb->deleteCategory($name);
Deletes the category with name $name. Actually, though, it doesn't
delete the category: it just changes its name to the empty string, and
marks the category as renamed.
=cut
#'
sub deleteCategory
{
my $self = shift;
my $name = shift; # Category name
for (@{$self->{appinfo}{categories}})
{
# Find the category named $name
next if $_->{name} ne $name;
# Erase this category
$_->{name} = "";
# You'd think it would make sense to set the "renamed"
# field here, but the Palm doesn't do that.
}
}
=head2 renameCategory
$pdb->renameCategory($oldname, $newname);
Renames the category named $oldname to $newname.
If successful, returns a true value. If there is no category named
$oldname, returns a false value and sets $Palm::StdAppInfo::error to
an error message.
=cut
#'
# XXX - This doesn't behave the same way as the Palm: the Palm also
# picks a new category ID.
sub renameCategory
{
my $self = shift;
my $oldname = shift;
my $newname = shift;
for (@{$self->{appinfo}{categories}})
{
# Look for a category named $oldname
next if !defined($_->{name}) || $_->{name} ne $oldname;
# Found it. Rename it and mark it as renamed.
$_->{name} = $newname;
$_->{renamed} = 1;
return 1;
}
$error = "No such category";
return undef;
}
1;
__END__
=head1 SOURCE CONTROL
The source is in Github:
http://github.com/briandfoy/p5-Palm/tree/master
=head1 AUTHOR
Alessandro Zummo, C<< <a.zummo@towertech.it> >>
Currently maintained by brian d foy, C<< <bdfoy@cpan.org> >>
=head1 SEE ALSO
Palm::PDB(3)
=cut
|