1 // This file is part of the brlaser printer driver. 2 // 3 // Copyright 2013 Peter De Wachter 4 // 5 // brlaser is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 2 of the License, or 8 // (at your option) any later version. 9 // 10 // brlaser is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with brlaser. If not, see <http://www.gnu.org/licenses/>. 17 18 #ifndef BLOCK_H 19 #define BLOCK_H 20 21 #include <assert.h> 22 #include <stdio.h> 23 #include <vector> 24 25 class block { 26 public: block()27 block(): line_bytes_(0) { 28 lines_.reserve(max_lines_per_block_); 29 } 30 empty()31 bool empty() const { 32 return line_bytes_ == 0; 33 } 34 add_line(std::vector<uint8_t> && line)35 void add_line(std::vector<uint8_t> &&line) { 36 assert(!line.empty()); 37 assert(line_fits(line.size())); 38 line_bytes_ += line.size(); 39 lines_.emplace_back(line); 40 } 41 line_fits(unsigned size)42 bool line_fits(unsigned size) { 43 return lines_.size() != max_lines_per_block_ 44 && line_bytes_ + size < max_block_size_; 45 } 46 flush(FILE * f)47 void flush(FILE *f) { 48 if (!empty()) { 49 fprintf(f, "%dw%c%c", 50 line_bytes_ + 2, 0, 51 static_cast<int>(lines_.size())); 52 for (auto &line : lines_) { 53 fwrite(line.data(), 1, line.size(), f); 54 } 55 line_bytes_ = 0; 56 lines_.clear(); 57 } 58 } 59 60 private: 61 static const unsigned max_block_size_ = 16350; 62 static const unsigned max_lines_per_block_ = 64; 63 64 std::vector<std::vector<uint8_t>> lines_; 65 int line_bytes_; 66 }; 67 68 #endif // BLOCK_H 69