Erbureth

writer.h

Jan 30th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.58 KB | None | 0 0
  1. // -*- C++ -*- (c) 2010-2011 Jan Kriho <[email protected]>
  2.  
  3. #ifndef DIVINE_WRITER_H
  4. #define DIVINE_WRITER_H
  5.  
  6. #include <divine/pma/format.h>
  7. #include <wibble/test.h>
  8. #include <fstream>
  9. #include <list>
  10. #include <vector>
  11. #include <string.h>
  12.  
  13.  
  14. namespace divine {
  15. namespace pma {
  16.    
  17. template< typename N >
  18. struct Writer {
  19.     std::fstream *outfile;
  20.     std::string outfilename;
  21.     bool initialized;
  22.    
  23.     template< typename C >
  24.     void storeTable( tableDescriptor &tableDesc,
  25.                      std::vector< nodeIndexItem > &nodes,
  26.                      std::vector< C > &successorTable,
  27.                      std::vector< N > &blobs,
  28.                      std::vector< std::string > &descs,
  29.                      bool seekp = true
  30.                    )
  31.     {
  32.         if (seekp) {
  33.             outfile->seekp(tableDesc.offset, std::ios::beg);
  34.         }
  35.         storeNodeIndex( tableDesc, nodes );
  36.        
  37.         for (int64_t i = 0; i < tableDesc.nodeCount; i++) {
  38.             storeNodeSuccessors( nodes[i], successorTable[i] );
  39.         }
  40.        
  41.         for (int64_t i = 0; i < tableDesc.nodeCount; i++) {
  42.             storeNode( nodes[i], blobs[i], descs[i] );
  43.             if (!(i & ~((int64_t)0xFF))) { outfile->flush(); }
  44.         }
  45.         outfile->flush();
  46.     }
  47.     template < typename C >
  48.     void storeNodeSuccessors( nodeIndexItem &node,
  49.                               C &successors,
  50.                               int64_t offset_to = 0,
  51.                               bool seekp = false )
  52.     {
  53.         if ( seekp ) {
  54.             outfile->seekp(offset_to, std::ios::beg);
  55.         }
  56.         for (typename C::iterator it = successors.begin(); it != successors.end(); it++) {
  57.             storeInitial(*it, 0, false);
  58.         }
  59.         /*while ( !successors.empty() ) {
  60.             storeInitial( successors.front(), 0, false);
  61.             successors.pop_front();
  62.         }*/
  63.     }
  64.    
  65.     void storeNode( nodeIndexItem &node,
  66.                     N &blob,
  67.                     std::string &desc,
  68.                     int64_t offset_to = 0,
  69.                     bool seekp = false )
  70.     {
  71.         char * array = new char[node.nodeSize + node.descSize];
  72.         int64_t offset = 0;
  73.        
  74.         assert( blob.valid() );
  75.         assert( blob.size() == node.nodeSize );
  76.         memcpy( array + offset, blob.data(), node.nodeSize );
  77.         offset += node.nodeSize;
  78.         memcpy( array + offset, desc.c_str(), node.descSize );
  79.         offset += node.descSize;
  80.        
  81.         outfile->write(array, offset);
  82.        
  83.         delete[] array;
  84.     }
  85.    
  86.     void storeNodeIndex( tableDescriptor &tableDesc,
  87.                          std::vector< nodeIndexItem > &nodes,
  88.                          int64_t offset_to = 0,
  89.                          bool seekp = false )
  90.     {
  91.         if ( seekp ) {
  92.             outfile->seekp(offset_to, std::ios::beg);
  93.         }
  94.         for ( int i = 0; i < tableDesc.nodeCount; i++ ) {
  95.             storeNodeIndexItem( nodes[i] );
  96.         }
  97.         outfile->flush();
  98.     }
  99.    
  100.    
  101.    
  102.     void storeNodeIndexItem( nodeIndexItem &node, int64_t offset_to = 0, bool seekp = false ) {
  103.         if ( seekp ) {
  104.             outfile->seekp( offset_to, std::ios::beg);
  105.         }
  106.         char array[NODE_INDEX_ITEM_SIZE];
  107.         int offset = 0;
  108.        
  109.         (*(uint64_t *)((array) + offset)) = htole64(node.succsOffset);
  110.                 offset += sizeof(node.succsOffset);
  111.         (*(uint64_t *)((array) + offset)) = htole64(node.dataOffset);
  112.                 offset += sizeof(node.dataOffset);
  113.         (*(uint32_t *)((array) + offset)) = htole32(node.nodeSize);
  114.                 offset += sizeof(node.nodeSize);
  115.         (*(uint32_t *)((array) + offset)) = htole32(node.descSize);
  116.                 offset += sizeof(node.descSize);
  117.         (*(uint16_t *)((array) + offset)) = htole16(node.successors);
  118.                 offset += sizeof(node.successors);
  119.         (*(uint16_t *)((array) + offset)) = htole16(node.flags);
  120.                 offset += sizeof(node.flags);
  121.        
  122.        
  123.         assert(offset == NODE_INDEX_ITEM_SIZE);
  124.        
  125.         outfile->write( array, offset );
  126.     }
  127.    
  128.     void storeTableDescriptors ( std::vector< tableDescriptor > &tables, fileHeader &header ) {
  129.         outfile->seekp(TOTAL_HEADER_SIZE + header.initials * NODE_DESCRIPTOR_SIZE);
  130.         if (header.vertexDistribution == None ) {
  131.             storeTableDesc( tables[0], header, 0, false);
  132.         }
  133.         else if (header.vertexDistribution == Hash ) {
  134.             for (int i = 0; i < header.mpiWorkers * header.localWorkers; i++) {
  135.                 storeTableDesc( tables[i], header, i, false);
  136.             }
  137.         }
  138.         outfile->flush();
  139.     }
  140.    
  141.     void storeTableDesc( N & table, fileHeader &header, int id, bool seekp = true ) {
  142.         if ( seekp ) {
  143.             outfile->seekp(TOTAL_HEADER_SIZE +
  144.                            header.initials * NODE_DESCRIPTOR_SIZE +
  145.                            id * TABLE_DESCRIPTOR_SIZE);
  146.         }
  147.         outfile->write(table.data(), TABLE_DESCRIPTOR_SIZE);
  148.     }
  149.    
  150.     void storeTableDesc( tableDescriptor &table, fileHeader &header, int id, bool seekp = true ) {
  151.        
  152.         N array(TABLE_DESCRIPTOR_SIZE);
  153.         int offset = 0;
  154.        
  155.         offset = table.template write<N>(array, offset);
  156.        
  157.         storeTableDesc(array, header, id, seekp);
  158.     }
  159.    
  160.     void storeInitials( std::vector< nodeDescriptor > &initials, fileHeader &header ) {
  161.         outfile->seekp(TOTAL_HEADER_SIZE, std::ios::beg);
  162.         for (int i = 0; i < header.initials; i++) {
  163.             storeInitial(initials[i], i, false);
  164.         }
  165.         outfile->flush();
  166.     }
  167.    
  168.     void store(N &data, size_t size = 0) {
  169.         assert(data.data() != 0);
  170.         assert_leq(1, data.size());
  171.         if (size == 0) {
  172.             size = data.size();
  173.         }
  174.         outfile->write(data.data(), size);
  175.     }
  176.    
  177.     void storeInitial(N &initial, int id, bool seekp = true) {
  178.         if ( seekp ) {
  179.             outfile->seekp(TOTAL_HEADER_SIZE + NODE_DESCRIPTOR_SIZE * id, std::ios::beg);
  180.         }
  181.         outfile->write(initial.data(), NODE_DESCRIPTOR_SIZE);
  182.     }
  183.    
  184.     void storeInitial( nodeDescriptor &initial, int id, bool seekp = true ) {
  185.        
  186.         N array(NODE_DESCRIPTOR_SIZE);
  187.         int offset = 0;
  188.         offset = initial.template write<N>(array, offset);
  189.         storeInitial(array, id, seekp);
  190.         array.free();
  191.     }
  192.    
  193.     void storeHeader(N &header) {
  194.         outfile->seekp(0, std::ios::beg);
  195.        
  196.         outfile->write(header.data(), FILE_HEADER_SIZE + ARCHITECTURE_INFO_SIZE);
  197.         outfile->flush();
  198.     }
  199.    
  200.     void storeHeader(fileHeader &header, architectureInfo &archinfo) {
  201.         outfile->seekp(0, std::ios::beg);
  202.         N array(FILE_HEADER_SIZE + ARCHITECTURE_INFO_SIZE);
  203.         int offset = 0;
  204.        
  205.         offset = header.template write<N>(array, offset);
  206.         offset = archinfo.template write<N>(array, offset);
  207.         storeHeader(array);
  208.         //array.free();
  209.        
  210.         return;
  211.     }
  212.    
  213.     void init() {
  214.         this->outfile = new std::fstream(this->outfilename.c_str(),
  215.                                          std::ios::out | std::ios::binary |
  216.                                          std::ios::trunc);
  217.         this->initialized = true;
  218.     }
  219.    
  220.    
  221.    
  222.     Writer()
  223.         : initialized(false)
  224.     {
  225.        
  226.     }
  227.     ~Writer() {
  228.         if (initialized) {
  229.             outfile->flush();
  230.             outfile->close();
  231.             delete outfile;
  232.         }
  233.     }
  234. };
  235.  
  236. }    
  237. }
Advertisement
Add Comment
Please, Sign In to add comment