Advertisement
Guest User

Untitled

a guest
Jan 14th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #ifndef LineBuffer_h__
  2. #define LineBuffer_h__
  3.  
  4. #include "common_header.h"
  5.  
  6. namespace FileMerger
  7. {
  8. struct StringView
  9. {
  10. int size;
  11. char* str;
  12.  
  13. StringView( char* s, int sz ) : str( s ), size( sz )
  14. {
  15. DEBUG_ASSERT( s );
  16. DEBUG_ASSERT( sz > 0 );
  17. }
  18. };
  19.  
  20. class LineBuffer : public std::vector< StringView >
  21. {
  22. public:
  23.  
  24. typedef vector< string > StringList;
  25.  
  26. static float rangeRandom( float min, float max )
  27. {
  28. return ((float)rand()/(float)RAND_MAX)*(max-min) + min;
  29. }
  30.  
  31. LineBuffer() :
  32. mTotalSize( 0 )
  33. {
  34. mLineBuf.reserve( 1024*1024 );
  35. }
  36.  
  37. ~LineBuffer()
  38. {
  39.  
  40. }
  41.  
  42. void addFile( const string& path )
  43. {
  44. FILE* file = fopen( path.c_str(), "r" );
  45.  
  46. if( !file )
  47. {
  48. cout << "Error: cannot find file \"" << path << "\"" << endl;
  49. return;
  50. }
  51.  
  52. //create a new file buffer
  53. fseek( file, 0, SEEK_END);
  54.  
  55. mLoadedFiles.push_back( "" );
  56. mLoadedFiles.back().resize( ftell( file ) );
  57.  
  58. fseek( file, 0, SEEK_SET );
  59.  
  60. //load the file into the buffer
  61. char* buf = (char*)mLoadedFiles.back().c_str();
  62. char* endPtr = buf + mLoadedFiles.back().size();
  63. fread( buf, 1, mLoadedFiles.back().size(), file );
  64.  
  65. fclose( file );
  66.  
  67. char lastChar = 0;
  68. size_t lineSize = 0;
  69. while( buf < endPtr )
  70. {
  71. //find next line start
  72. for( ; buf < endPtr && *buf == ' ' && *buf == '\n'; ++buf );
  73. char* start = buf;
  74.  
  75. //find next line end
  76. for( ; buf < endPtr && *buf != '\n'; ++buf );
  77. char* end = buf++;
  78.  
  79. if( start == end )
  80. continue;
  81.  
  82. //save the slice
  83. *end = 0;
  84.  
  85. size_t size = end - start;
  86. push_back( StringView( start, size ) );
  87. mTotalSize += size;
  88. }
  89. }
  90.  
  91. ///shuffles n loaded lines selected at random
  92. void shuffle()
  93. {
  94. for( int i = 0; i < size(); ++i )
  95. {
  96. int first = randomIndex();
  97. int second = randomIndex();
  98.  
  99. if( first == second )
  100. continue;
  101.  
  102. auto temp = at(first);
  103. at(first) = at( second );
  104. at( second ) = temp;
  105. }
  106. }
  107.  
  108. inline int randomIndex()
  109. {
  110. return rangeRandom( 0, (int)size()-1 );
  111. }
  112.  
  113. inline size_t getTotalSize() const
  114. {
  115. return mTotalSize;
  116. }
  117.  
  118. protected:
  119.  
  120. size_t mTotalSize;
  121. string mLineBuf;
  122. StringList mLoadedFiles;
  123.  
  124. private:
  125. };
  126. }
  127.  
  128. #endif // LineBuffer_h__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement