Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include "bin_manip.h"
  2.  
  3. write_le_int32::write_le_int32(const int32_t& n) : _n(n){}
  4.  
  5. std::basic_ostream<char> & operator<<(std::basic_ostream<char> & ofs, const write_le_int32 & wlei32) {
  6. return wlei32(ofs);
  7. }
  8.  
  9. std::basic_ostream<char> & write_le_int32::operator()(std::basic_ostream<char> & ofs) const {
  10. ofs.write((char*) & _n, sizeof(_n));
  11. return ofs;
  12. }
  13.  
  14. read_le_int32::read_le_int32(std::int32_t & n) : _n(n) {}
  15.  
  16. std::basic_istream<char> & operator>>(std::basic_istream<char> & ifs, const read_le_int32 rlei32) {
  17. return rlei32(ifs);
  18. }
  19.  
  20. std::basic_istream<char> & read_le_int32::operator()(std::basic_istream<char> & ifs) const {
  21. ifs.read((char*) & _n, sizeof(_n));
  22. return ifs;
  23. }
  24.  
  25. read_bool::read_bool(bool & b) : _b(b) {}
  26.  
  27. std::basic_istream<char> & operator>>(std::basic_istream<char> & ifs, const read_bool & rb) {
  28. return rb(ifs);
  29. }
  30.  
  31. std::basic_istream<char> & read_bool::operator()(std::basic_istream<char> & ifs) const {
  32. ifs.read((char*) & _b, sizeof(_b));
  33. return ifs;
  34. }
  35.  
  36. write_bool::write_bool(const bool & b) : _b(b) {}
  37.  
  38. std::basic_ostream<char>& operator<<(std::basic_ostream<char> & ofs, const write_bool & wb) {
  39. return wb(ofs);
  40. }
  41.  
  42. std::basic_ostream<char> & write_bool::operator()(std::basic_ostream<char> & ofs) const {
  43. ofs.write((char*) & _b, sizeof(_b));
  44. return ofs;
  45. }
  46.  
  47. write_c_str::write_c_str(char * const str) : _str(str) {}
  48.  
  49. std::basic_ostream<char>& operator<<(std::basic_ostream<char> & ofs, const write_c_str & wstr) {
  50. return wstr(ofs);
  51. }
  52.  
  53. std::basic_ostream<char> & write_c_str::operator()(std::basic_ostream<char> & ofs) const {
  54. int32_t index = 0;
  55. while (_str[index] != '\0') {
  56. ofs.write((char*) & _str[index], sizeof(char));
  57. index++;
  58. }
  59. ofs.write((char*) & _str[index], sizeof(char));
  60. return ofs;
  61. }
  62.  
  63. read_c_str::read_c_str(char * str, size_t size) : _str(str), _size(size) {}
  64.  
  65. std::basic_istream<char>& operator>>(std::basic_istream<char> & ifs, const read_c_str & rs) {
  66. return rs(ifs);
  67. }
  68.  
  69. std::basic_istream<char> & read_c_str::operator()(std::basic_istream<char> & ifs) const {
  70. size_t index = 0;
  71. char last_char = 'a';
  72. while (index < _size && last_char != '\0') {
  73. ifs.read((char*) & last_char, sizeof(char));
  74. _str[index++] = last_char;
  75. }
  76. if (index == _size && _str[index] != '\0') {
  77. ifs.setstate(std::ios::failbit);
  78. }
  79. return ifs;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement