views:

166

answers:

2

I got a problem implementing a PHP programm in C++. It is about the PHP/Perl function unpack. I don't know how to do the follwing in C++ (no problem in reading a file... but how do i unpack("C*") the read contents).

<?php
$file = fopen("bitmaskt.dat", "rb");
//create the data stream
$matrix_x           = unpack("C*", fread($file, 286));
$matrix_y           = unpack("C*", fread($file, 286));
$mask_data          = unpack("C*", fread($file, 286));
$reed_ecc_codewords = ord(fread($file, 1));
$reed_blockorder    = unpack("C*", fread($file, 128));
fclose($file);
?>

Currently, I'm very hopeless solving this problem on my own - I'm searching for days, all I found are questions... Is there any free unpack() c++ implementation out there? :-(

+1  A: 

I don't know about any general implementation of unpack for C++, but that doesn't seem to be the thing you need anyway.

if matrix_x is defined somewhere as unsigned char matrix_x[286] and you have an opened input stream inFile then what you need to do is inFile.get(matrix_x, 286). This reads 286 bytes from the input and places them in the array pointed to by matrix_x.

cube
Hi cube,thx for your answer, but then there is binary "crap" located in `matrix_x` instead of the number as I would get them in PHP and Perl through `unpack("C*")`. "bitmaskt.dat" was created with pack("C*") in Perl.I tried a little bit, typecasteing every element of `matrix_x` to `long int` seems to have the same effect as PHP's unpack("C*") - can anyone confirm this?
Georg
I don't quite understand how you casted the numbers. Could you show that part of the C++ code?
cube
+2  A: 

Perl's documentation for pack covers the templates used for pack and unpack.

Say you generated bitmaskt.dat with

#! /usr/bin/perl

use warnings;
use strict;

open my $fh, ">", "bitmaskt.dat" or die "$0: open: $!";

my @data = (42) x 286;

print $fh pack("C*" => @data);
print $fh pack("C*" => @data);
print $fh pack("C*" => @data);
print $fh pack("C"  => 7);
print $fh pack("C*" => (1) x 128);

close $fh or warn "$0: close";

You might read it with

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

typedef unsigned char datum_t;
typedef std::vector<datum_t> buf_t;

std::istream &read_data(std::istream &in, buf_t &buf, size_t n)
{
  std::istreambuf_iterator<char> it(in.rdbuf()), eos;
  while (it != eos && n-- != 0)
    buf.push_back(static_cast<datum_t>(*it++));

  return in;
}

For example:

int main()
{
  std::ifstream bm("bitmaskt.dat", std::ifstream::binary | std::ifstream::in);

  struct {
    buf_t buf;
    size_t len;
    std::string name;
  } sections[] = {
    { buf_t(), 286, "matrix_x" },
    { buf_t(), 286, "matrix_y" },
    { buf_t(), 286, "mask_data" },
    { buf_t(),   1, "reed_ecc_codewords" },
    { buf_t(), 128, "reed_blockorder" },
  };
  const int n = sizeof(sections) / sizeof(sections[0]);

  for (int i = 0; n - i > 0; i++) {
    if (!read_data(bm, sections[i].buf, sections[i].len)) {
      std::cerr << "Read " << sections[i].name << " failed" << std::endl;
      return 1;
    }
  }

  const int codeword = 3;
  std::cout << (unsigned int) sections[codeword].buf[0] << '\n';

  return 0;
}

Output:

7
Greg Bacon