[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] hwo to write resource ?



Laurent Bardi writes:
> but the TextToRes (writing the resource ) NOT!

Appended are two subroutines I use for reading and writing resources (to
SoundEdit16 files.) I pass in three arguments: the type of resource, the ID of
the resource I want, and a template I use to pack/unpack the data to/from an
array I can handle. The code and method are surely non-optimal, and I'd love
to see what the real/proper solutions are, but these work fine for me.

Oh, to answer your question, I think what you are missing is the
"WriteResource $newHandle;" command.

-dan

---------------------------------------------------------------------------

So, for example, I might say:

$INFO_TYPE = 'INFO';
$INFO_ID = 1000;
$INFO_TEMPLATE = "L16 S2 L S"; #16 longs, 2 shorts, a long, a short

@theInfo = &readResource($INFO_TYPE,$INFO_ID,$INFO_TEMPLATE);
...
&writeResource($INFO_TYPE,$INFO_ID,$INFO_TEMPLATE,@theInfo);

sub writeResource {
  my($type,$index,$template)=splice(@_,0,3);
  my($myString);
  my($array_ref);
  
  for $array_ref (@_) {
    $myString .= pack($template, @$array_ref);
  }

  my($tempHandle) = Get1Resource($type, $index);
  my($id, $type, $name) = GetResInfo($tempHandle);
  RemoveResource $tempHandle;
  ReleaseResource $tempHandle;
  
  my($newHandle) = new Handle($myString) || die "bad handle";
  AddResource $newHandle, $type, $index, $name;
  
  if ( ChangedResource($newHandle) ) {
    WriteResource $newHandle;
  } else {
    die "Failed to changedResource";
  }
  dispose $newHandle;
}

sub readResource {
  my($type,$index,$template)=@_;
  undef my(@theReturn);
  
  if (!defined(my($tempHandle) = Get1Resource($type, $index))) {
    die "error reading resource $type # $index\n";
  }

  my($id, $type, $name) = GetResInfo($tempHandle);
  my($myString) = $tempHandle->get;
  ReleaseResource $tempHandle;
  
  while (length($myString) >0) {
    @_ = unpack $template." a*", $myString;
    $myString = pop @_;
    push @theReturn,[@_];
  }
  return @theReturn;
}