int add_functions(t_function *functions, t_list **bytes_end) { t_function *tmp_func; t_line *tmp_line; t_instruct *tmp_i; t_bytes_n_labels *bytes_n_labels; bytes_n_labels = get_bytes_n_labels(bytes_end); tmp_func = functions; while (tmp_func) { tmp_func->bytes_written = 0; tmp_line = tmp_func->lines; while (tmp_line) { tmp_i = tmp_line->content; if (!(write_opcode(tmp_i->opcode, &(bytes_n_labels->bytes_end)))) return (big_error()); tmp_func->bytes_written += 1; if (!(write_param_byte_if_nec(tmp_i, &(bytes_n_labels->bytes_end), tmp_func)) || !(write_params(tmp_i, tmp_func, functions, bytes_n_labels))) return (big_error()); tmp_line = tmp_line->next; } tmp_func = tmp_func->next; } return (resolve_unresolved_labels(bytes_n_labels->labels_to_resolve)); }
void write(BitmapType const& bitmap, std::string const& filename){ std::ofstream os( filename.c_str(), std::ios_base::out | std::ios_base::binary ); if(!os.is_open()){ throw big_error("Can't open file: " + filename); } try{ write(bitmap, os); }catch(big_error const& error){ throw big_error(std::string(error.what()) + ": " + filename); } }
void read(BitmapType& bitmap, std::string const& filename){ std::ifstream is( filename.c_str(), std::ios_base::in | std::ios_base::binary ); if(!is.is_open()){ throw big_error("Can't open file: " + filename); } try{ read(bitmap, is); }catch(big_error const& error){ throw big_error(std::string(error.what()) + ": " + filename); } }
void read(BitmapType& bitmap, std::istream& is){ using value_type = std::remove_cv_t< std::remove_pointer_t< decltype(bitmap.data()) > >; header header = read_header(is); if(header.type != big::type_v< value_type >){ throw big_error("Type in file is not compatible"); } // The last 4 bits in the type get the size of a single value if((header.type & 0x000F) != sizeof(value_type)){ throw big_error("Size type in file is not compatible"); } bitmap.resize(header.width, header.height); read_data(bitmap, is); }
void write(BitmapType const& bitmap, std::ostream& os){ using value_type = std::remove_cv_t< std::remove_pointer_t< decltype(bitmap.data()) > >; // big header informations std::uint16_t width = static_cast< std::uint16_t >(bitmap.width()); std::uint16_t height = static_cast< std::uint16_t >(bitmap.height()); std::uint16_t type = big::type_v< value_type >; std::uint32_t placeholder = 0; // 4 bytes in header are reserved // write the file header os.write(reinterpret_cast< char const* >(&width), 2); os.write(reinterpret_cast< char const* >(&height), 2); os.write(reinterpret_cast< char const* >(&type), 2); os.write(reinterpret_cast< char const* >(&placeholder), 4); if(!os.good()){ throw big_error("Can't write big header"); } auto bytes = bitmap.width() * bitmap.height() * sizeof(value_type); os.write( reinterpret_cast< char const* >( std::numeric_limits< value_type >::has_quiet_NaN ? convert_nan_to_undef(bitmap).data() : bitmap.data() ), bytes ); if(!os.good()){ throw big_error("Can't write big content"); } }
inline header read_header(std::istream& is){ header header; // big header informations // read the file header (10 Byte) is.read(reinterpret_cast< char* >(&header.width), 2); is.read(reinterpret_cast< char* >(&header.height), 2); is.read(reinterpret_cast< char* >(&header.type), 2); is.read(reinterpret_cast< char* >(&header.placeholder), 4); if(!is.good()){ throw big_error("Can't read big header"); } return header; }
void read_data(BitmapType& bitmap, std::istream& is){ using value_type = std::remove_cv_t< std::remove_pointer_t< decltype(bitmap.data()) > >; is.read( reinterpret_cast< char* >(bitmap.data()), bitmap.width() * bitmap.height() * sizeof(value_type) ); if(!is.good()){ throw big_error("Can't read big content"); } if(std::numeric_limits< value_type >::has_quiet_NaN){ bitmap = convert_undef_to_nan(std::move(bitmap)); } }