Esempio n. 1
0
 T readFromLittleEndian(zim::ifstream& in, const char* errorMsg)
 {
   char buffer[sizeof(T)];
   in.read(buffer, sizeof(T));
   if (!in)
     throw ZimFileFormatError(errorMsg);
   return fromLittleEndian<T>(reinterpret_cast<const T*>(buffer + 0));
 }
Esempio n. 2
0
  /* This return the number of char read */
  offset_type ClusterImpl::read_header(std::istream& in)
  {
    log_debug1("read_header");
    // read first offset, which specifies, how many offsets we need to read
    size_type offset;
    in.read(reinterpret_cast<char*>(&offset), sizeof(offset));
    if (in.fail())
    {
      std::cerr << "fail at read offset" << std::endl;
      throw ZimFileFormatError("fail at read first offset");
    }

    offset = fromLittleEndian(&offset);

    size_type n = offset / 4;
    size_type a = offset;

    log_debug1("first offset is " << offset << " n=" << n << " a=" << a);

    // read offsets
    offsets.clear();
    offsets.reserve(n);
    offsets.push_back(0);
    while (--n)
    {
      in.read(reinterpret_cast<char*>(&offset), sizeof(offset));
      if (in.fail())
      {
        log_debug("fail at " << n);
        throw ZimFileFormatError("fail at read offset");
      }
      offset = fromLittleEndian(&offset);
      log_debug1("offset=" << offset << '(' << offset-a << ')');
      offsets.push_back(offset - a);
    }
    return a;
  }