Erbureth

reader.h

Jan 30th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.88 KB | None | 0 0
  1. // -*- C++ -*- (c) 2010-2011 Jan Kriho <[email protected]>
  2.  
  3. #ifndef DIVINE_READER_H
  4. #define DIVINE_READER_H
  5.  
  6.  
  7. #include <wibble/test.h>
  8. #include <fstream>
  9. #include <list>
  10. #include <vector>
  11. #include <string.h>
  12. #include <divine/pma/format.h>
  13.  
  14. namespace divine {
  15. namespace pma {
  16.    
  17.    
  18. template< typename N >
  19. struct Reader {
  20.     std::fstream *infile;
  21.     fileHeader header;
  22.     architectureInfo archinfo;
  23.    
  24.     std::string getStateDesc(tableDescriptor td, nodeIndexItem node, bool seekg = true) {
  25.         if (seekg) {
  26.             infile->seekg(td.offset + td.nodeCount*NODE_INDEX_ITEM_SIZE + node.dataOffset + td.succsCount*NODE_DESCRIPTOR_SIZE + node.nodeSize);
  27.         }
  28.         std::string stateDesc;
  29.         stateDesc.resize(node.descSize, '\0');
  30.         char * array;
  31.         array = new char[node.descSize];
  32.         infile->read(array, node.descSize - 1);
  33.         array[node.descSize - 1] = '\0';
  34.         stateDesc.insert(0, array, node.descSize);
  35.         return stateDesc;
  36.     }
  37.    
  38.     std::vector< nodeDescriptor > getSuccessors(tableDescriptor td, nodeIndexItem node, bool seekg = true) {
  39.         if (seekg) {
  40.             infile->seekg(td.offset + td.nodeCount*NODE_INDEX_ITEM_SIZE + node.succsOffset);
  41.         }
  42.        
  43.         std::vector< nodeDescriptor > successors;
  44.         successors.resize(node.successors);
  45.         if (node.successors > 0) {
  46.             N * array = new N (node.successors * NODE_DESCRIPTOR_SIZE);
  47.             infile->read(array->data(), node.successors * NODE_DESCRIPTOR_SIZE);
  48.        
  49.             int offset = 0;
  50.        
  51.             for (int i = 0; i < node.successors; i++) {
  52.                 offset = successors[i].template read(*array, offset);
  53.             }
  54.        
  55.             array->free();
  56.        
  57.             delete array;
  58.         }
  59.        
  60.         return successors;
  61.     }
  62.    
  63.     nodeDescriptor getSucc() { //private use only
  64.         return getInitial(0, false); // use existing infrastructure w/o seeking
  65.     }
  66.    
  67.     std::vector< nodeIndexItem > getNodeIndex(tableDescriptor td, bool seekg = true) {
  68.         if (seekg) {
  69.             std::cerr << "Seeking to " << td.offset << std::endl;
  70.             infile->seekg(td.offset);
  71.         }
  72.         std::vector< nodeIndexItem > ni;
  73.         ni.resize(td.nodeCount);
  74.         for (int i = 0; i < td.nodeCount; i++) {
  75.             ni[i] = getNodeIndexItem();
  76.         }
  77.         return ni;
  78.     }
  79.    
  80.     nodeIndexItem getNodeIndexItem(int64_t offset_to = 0, bool seekg = false) {
  81.         if (seekg) {
  82.             infile->seekg(offset_to);
  83.         }
  84.         N array(NODE_INDEX_ITEM_SIZE);
  85.         int offset = 0;
  86.        
  87.         infile->read(array.data(), NODE_INDEX_ITEM_SIZE);
  88.        
  89.         nodeIndexItem nii;
  90.        
  91.         nii.template read< N >(array, 0);
  92.        
  93.         array.free();
  94.        
  95.         return nii;
  96.     }
  97.    
  98.     std::vector< tableDescriptor > getTableDescriptors(bool seekg = true) {
  99.         if( seekg ) {
  100.             infile->seekg(TOTAL_HEADER_SIZE + header.initials * NODE_DESCRIPTOR_SIZE);
  101.         }
  102.         std::vector< tableDescriptor > tds;
  103.         tds.resize(getTableCount());
  104.         for (int i = 0; i < getTableCount(); i++) {
  105.             tds[i] = getTableDesc();
  106.         }
  107.         return tds;
  108.     }
  109.    
  110.     tableDescriptor getTableDesc(int id = 0, bool seekg = false) {
  111.         if (seekg) {
  112.             infile->seekg(TOTAL_HEADER_SIZE +
  113.                            header.initials * NODE_DESCRIPTOR_SIZE +
  114.                            id * TABLE_DESCRIPTOR_SIZE);
  115.         }
  116.         N array(TABLE_DESCRIPTOR_SIZE);
  117.         int offset = 0;
  118.        
  119.         infile->read(array.data(), TABLE_DESCRIPTOR_SIZE);
  120.        
  121.         tableDescriptor td;
  122.        
  123.         td.template read<N>(array, 0);
  124.         /*
  125.         td.offset = le64toh((*(int64_t *)(array + offset)));
  126.                 offset += sizeof(td.offset);
  127.         td.nodeCount = le64toh((*(int64_t *)(array + offset)));
  128.                 offset += sizeof(td.offset);
  129.         td.succsCount = le64toh((*(int64_t *)(array + offset)));
  130.                 offset += sizeof(td.succsCount);
  131.         td.dataSize = le64toh((*(int64_t *)(array + offset)));
  132.                 offset += sizeof(td.offset);
  133.        
  134.         assert(offset == TABLE_DESCRIPTOR_SIZE);
  135.         */
  136.         return td;
  137.     }
  138.    
  139.     int getTableCount() {
  140.         switch (header.vertexDistribution) {
  141.             case None:
  142.                 return 1;
  143.                 break;
  144.             case Hash:
  145.                 return header.mpiWorkers * header.localWorkers;
  146.         }
  147.     }
  148.    
  149.     std::vector< nodeDescriptor > getInitials(bool seekg = true) {
  150.         std::vector< nodeDescriptor > initials;
  151.         initials.resize( header.initials );
  152.         for (int i = 0; i < header.initials; i++) {
  153.             initials[i] = getInitial(i, (i == 0 ? seekg : false) );
  154.         }
  155.         return initials;
  156.     }
  157.    
  158.     nodeDescriptor getInitial(int id, bool seekg = true) {
  159.         if ( seekg ) {
  160.             infile->seekg(TOTAL_HEADER_SIZE + NODE_DESCRIPTOR_SIZE * id, std::ios::beg);
  161.         }
  162.         N array(NODE_DESCRIPTOR_SIZE);
  163.         int offset = 0;
  164.        
  165.         infile->read(array.data(), NODE_DESCRIPTOR_SIZE);
  166.        
  167.         nodeDescriptor initial;
  168.        
  169.         initial.template read<N>(array, 0);
  170.         /*
  171.         initial.nodeNo = le64toh((*(int64_t *)((array) + offset)));
  172.                 offset += sizeof(initial.nodeNo);
  173.         initial.table = le32toh((*(int32_t *)((array) + offset)));
  174.                 offset += sizeof(initial.table);
  175.                
  176.         assert(offset == NODE_DESCRIPTOR_SIZE);*/
  177.        
  178.         return initial;
  179.        
  180.     }
  181.    
  182.     fileHeader getHeader() {
  183.         return header;
  184.     }
  185.    
  186.     architectureInfo getArchInfo() {
  187.         return archinfo;
  188.     }
  189.    
  190.     bool readHeader() {
  191.         infile->seekg(0, std::ios::beg);
  192.         N array(TOTAL_HEADER_SIZE);
  193.         int offset = 0;
  194.        
  195.         /*int count = infile->gcount();
  196.         int pos = infile->tellg();
  197.         */
  198.         infile->read(array.data(), TOTAL_HEADER_SIZE);
  199.         /*
  200.         count = infile->gcount();
  201.         pos = infile->tellg();
  202.         */
  203.        
  204.         offset = header.template read<N>(array, offset);
  205.         offset = archinfo.template read<N>(array, offset);
  206.        
  207.         assert(offset == TOTAL_HEADER_SIZE);
  208.        
  209.         if (header.header == 666) {
  210.             return true;
  211.         }
  212.         else {
  213.             return false;
  214.         }
  215.     }
  216.    
  217.    
  218.     Reader( std::string &filename ) {
  219.         infile = new std::fstream(filename.c_str(), std::ios::in | std::ios::binary );
  220.         if (!readHeader()) {
  221.             std::cerr << "Not a valid state space dump!" << std::endl;
  222.             exit( 1 );
  223.         }
  224.     }
  225.    
  226.     ~Reader() {
  227.         if (infile) {
  228.  //           delete infile;
  229.         }
  230.     }
  231.    
  232. };
  233.    
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment