コード例 #1
0
int BookReader::open(const char *pathName) {
    if (book_file.is_open()) return 0;
    book_file.open(pathName, ios_base::in | ios_base::binary);
    if (!book_file.is_open()) {
        cerr <<"failed to open " << pathName << endl;
        return -1;
    } else {
        // read the header
        book_file.read((char*)&hdr,sizeof(book::BookHeader));
        if (book_file.bad()) {
            cerr <<"failed to read header" << endl;
            return -1;
        }
        // correct header for endian-ness
        hdr.num_index_pages = swapEndian16((byte*)&hdr.num_index_pages);
        // verify book version is correct
        if (hdr.version != book::BOOK_VERSION) {
            cerr << "expected book version " << book::BOOK_VERSION << ", got " << (unsigned)hdr.version << endl;
			close();
            return -1;
        } else {
            return 0;
        }
    }
}
コード例 #2
0
int BookReader::lookup(const Board &board, vector<book::DataEntry> &results) {
   // fetch the index page
   if (!is_open()) return -1;
   int probe = (int)(board.hashCode() % hdr.num_index_pages);
   // seek to index
   book_file.seekg((std::ios::off_type)(sizeof(book::BookHeader)+probe*sizeof(book::IndexPage)), std::ios_base::beg);
   if (book_file.fail()) return -1;
   book::IndexPage index;
   book_file.read((char*)&index,sizeof(book::IndexPage));
   if (book_file.fail()) return -1;
   // correct for endianness
   index.next_free = swapEndian32((byte*)&index.next_free);
   book::BookLocation loc(0,book::INVALID_INDEX);
   for (unsigned i = 0; i < index.next_free; i++) {
      // correct for endianness
      uint64_t indexHashCode = (uint64_t)(swapEndian64((byte*)&index.index[i].hashCode));
      if (indexHashCode == board.hashCode()) {
         // correct for endianness
         index.index[i].page = (uint16_t)(swapEndian16((byte*)&index.index[i].page));
         index.index[i].index = (uint16_t)(swapEndian16((byte*)&index.index[i].index));
         loc = index.index[i];
         break;
      }
   }
   if (!loc.isValid()) {
       // no book moves found
       return 0;
   }
   book_file.seekg(sizeof(book::BookHeader)+
                   hdr.num_index_pages*sizeof(book::IndexPage)+
                   loc.page*sizeof(book::DataPage));
   if (book_file.fail()) return -1;
   book::DataPage data;
   book_file.read((char*)&data,sizeof(book::DataPage));
   if (book_file.fail()) return -1;
   while(loc.index != book::NO_NEXT) {
       ASSERT(loc.index < book::DATA_PAGE_SIZE);
       book::DataEntry &bookEntry = data.data[loc.index];
       bookEntry.next = swapEndian16((byte*)&bookEntry.next);
       bookEntry.weight = swapEndian16((byte*)&bookEntry.weight);
       bookEntry.count = swapEndian32((byte*)&bookEntry.count);
       results.push_back(bookEntry);
       loc.index = bookEntry.next;
   }
   return (int)results.size();
}
コード例 #3
0
ファイル: bookwrit.cpp プロジェクト: raimarHD/lcec
int BookWriter::write(const char* pathName) {
   ofstream book_file(pathName, ios::out | ios::trunc | ios::binary);
   book::BookHeader header;
   header.version = book::BOOK_VERSION;
   uint16_t pages = (uint16_t)index_pages;
   header.num_index_pages = (uint16_t)swapEndian16((byte*)&pages);
   book_file.write((char*)&header, sizeof(book::BookHeader));
   if (book_file.fail()) return -1;

   book::IndexPage empty;
   for (int i = 0; i < index_pages; i++) {
      book::IndexPage *ip = (index[i] == NULL) ? &empty : index[i];
      // correct for endianness before disk write
      for (unsigned j = 0; j < ip->next_free; j++) {
         ip->index[j].page = (uint16_t)swapEndian16((byte*)&ip->index[j].page);
         ip->index[j].index = (uint16_t)swapEndian16((byte*)&ip->index[j].index);
         ip->index[j].hashCode = (uint64_t)swapEndian64((byte*)&ip->index[j].hashCode);
      }
      ip->next_free = (uint32_t)swapEndian32((byte*)&ip->next_free);
      book_file.write((char*)ip, sizeof(book::IndexPage));
      if (book_file.fail()) return -1;
   }
   for (unsigned i = 0; i < data.size(); i++) {
      book::DataPage *dp = data[i];
      // correct for endianness before disk write
      dp->free_list = (uint32_t)swapEndian32((byte*)&dp->free_list);
      dp->num_free = (uint32_t)swapEndian32((byte*)&dp->num_free);
      for (int j = 0; j < book::DATA_PAGE_SIZE; j++) {
         dp->data[j].next = (uint16_t)swapEndian16((byte*)&dp->data[j].next);
         dp->data[j].weight = (uint16_t)swapEndian16((byte*)&dp->data[j].weight);
         dp->data[j].count = (uint32_t)swapEndian32((byte*)&dp->data[j].count);
      }
      book_file.write((char*)dp, sizeof(book::DataPage));
      if (book_file.fail()) return -1;
   }
   book_file.close();
   return 0;
}
コード例 #4
0
ファイル: cmtpacket.cpp プロジェクト: chen0510566/mrpt
//////////////////////////////////////////////////////////////////////////////////////////
// Return the Raw Data component of a data item.
CmtRawData Packet::getRawData(const uint16_t index) const
{
	CmtRawData buffer;
	if (containsRawData(index))
	{
		const uint8_t* tmp = m_msg.getDataBuffer(m_infoList[index].m_rawData);
		const uint16_t* sh = (const uint16_t*) tmp;
		uint16_t* bare = (uint16_t*) &buffer;

		for (uint16_t i=0;i<10;++i, ++sh, ++bare)
			*bare = swapEndian16(*sh);// m_msg.getDataShort(m_infoList[index].m_rawData + (2*i));
	}
	return buffer;
}