Definition at line 584 of file IOBuffer.h. 00585 { 00586 // read all contents of file and put in the IOBuffer 00587 FILE* file = fopen(filename.c_str(), "r"); 00588 if (!file) 00589 { 00590 std::cout << "[CreateIOBufferFromFile] failed to open " << filename 00591 << std::endl; 00592 return 0; 00593 } 00594 00595 int blockSize = 0x1000; 00596 std::deque<unsigned char*> bufferList; 00597 int read; 00598 do 00599 { 00600 unsigned char* buffer = new unsigned char[blockSize]; 00601 read = fread(buffer,1,blockSize,file); 00602 bufferList.push_back(buffer); 00603 } 00604 while (read == blockSize); 00605 fclose(file); 00606 00607 // create total buffer 00608 int total = ((bufferList.size()-1)*blockSize) + read; 00609 unsigned char* buffer = new unsigned char[total]; 00610 int i; 00611 for(i=0 ; i<bufferList.size() - 1 ; i++) 00612 { 00613 memcpy(buffer + (i*blockSize), bufferList[i], blockSize); 00614 delete [] bufferList[i]; 00615 bufferList[i] = 0; 00616 } 00617 memcpy(buffer + (i*blockSize), bufferList[i], read); // last block only #read bytes 00618 delete [] bufferList[i]; 00619 bufferList[i] = 0; 00620 00621 return new IOBuffer(total, buffer); 00622 }
|