MCPcopy Create free account
hub / github.com/arun11299/cpp-subprocess / read_all

Function read_all

cpp-subprocess/subprocess.hpp:712–744  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

710 */
711
712 static inline int read_all(FILE* fp, std::vector<char>& buf)
713 {
714 auto buffer = buf.data();
715 int total_bytes_read = 0;
716 int fill_sz = buf.size();
717
718 while (1) {
719 const int rd_bytes = read_atmost_n(fp, buffer, fill_sz);
720
721 if (rd_bytes == -1) { // Read finished
722 if (total_bytes_read == 0) return -1;
723 break;
724
725 } else if (rd_bytes == fill_sz) { // Buffer full
726 const auto orig_sz = buf.size();
727 const auto new_sz = orig_sz * 2;
728 buf.resize(new_sz);
729 fill_sz = new_sz - orig_sz;
730
731 //update the buffer pointer
732 buffer = buf.data();
733 total_bytes_read += rd_bytes;
734 buffer += total_bytes_read;
735
736 } else { // Partial data ? Continue reading
737 total_bytes_read += rd_bytes;
738 fill_sz -= rd_bytes;
739 break;
740 }
741 }
742 buf.erase(buf.begin()+total_bytes_read, buf.end()); // remove extra nulls
743 return total_bytes_read;
744 }
745
746#ifndef __USING_WINDOWS__
747 /*!

Callers 3

test_read_allFunction · 0.85
communicateMethod · 0.85
communicate_threadedMethod · 0.85

Calls 1

read_atmost_nFunction · 0.85

Tested by 1

test_read_allFunction · 0.68