Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function build_index($source_file = 'source.txt', $index_file = 'index.dat')
- {
- $offset = 0;
- $lenght = 0;
- $index_fh = fopen($index_file, 'w');
- $source_fh = fopen($source_file, 'r');
- while (($buff = fgets($source_fh)) !== false)
- {
- $length = strlen($buff);
- // pack data and write to index.
- // L unsigned long (always 32 bit, machine byte order)
- // http://www.php.net/manual/en/function.pack.php
- fwrite($index_fh, pack('LL', $offset, $length));
- $offset += $length;
- }
- fclose($source_fh);
- fclose($index_fh);
- }
- // Размер придётся захардкодить или добавить подсчёт
- function get_data($source_file = 'source.txt', $index_file = 'index.dat', $string_count)
- {
- // 2 * 4 byte
- $index_data_size = 8;
- $n = rand(0, $string_count - 1);
- // get offset:lenght pair from index
- $index_fh = fopen($index_file, 'r');
- fseek($index_fh, $n * $index_data_size);
- $raw = unpack('L2', fread($index_fh, $index_data_size));
- fclose($index_fh);
- // print_r($raw);
- $offset = $raw[1];
- $lenght = $raw[2];
- // get string from source
- $source_fh = fopen($source_file, 'r');
- fseek($source_fh, $offset);
- $data = trim(fread($source_fh, $lenght));
- fclose($source_fh);
- return $data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement