SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Serial.h"
  2.  
  3. namespace serial {
  4.    
  5.     std::size_t OBinaryFile::write(const std::byte* data, std::size_t size){
  6.         return fwrite(data, sizeof(std::byte), size, m_file);
  7.     }
  8.    
  9.     OBinaryFile::OBinaryFile(const std::string& filename, Mode mode){
  10.         if(mode==Truncate){    
  11.             m_file= fopen(filename.data(),"wb");
  12.         }else if(mode==Append){
  13.             m_file= fopen(filename.data(),"a");
  14.         }
  15.     }
  16.     OBinaryFile::~OBinaryFile(){
  17.       fclose(m_file);
  18.     }
  19.    
  20.  
  21.     OBinaryFile& operator<<(OBinaryFile& file, std::byte x){
  22.         file.write(&x,1);
  23.         return file;
  24.     }
  25.  
  26.     OBinaryFile& operator<<(OBinaryFile& file, uint8_t x){
  27.         std::byte a = { static_cast<std::byte>(x) };
  28.         file.write(&a, 1);
  29.         return file;
  30.     }
  31.  
  32.  
  33.     OBinaryFile& operator<<(OBinaryFile& file, int8_t x){
  34.         std::byte a = { static_cast<std::byte>(x) };
  35.         file.write(&a, 1);
  36.         return file;
  37.     }
  38.    
  39.     OBinaryFile& operator<<(OBinaryFile& file, uint16_t x){
  40.         constexpr std::size_t size=sizeof(uint16_t);
  41.         std::byte array[size];
  42.         array[0]=static_cast<std::byte>(x>>8);
  43.         array[1]=static_cast<std::byte>(x&0xff);
  44.         file.write(array,size);
  45.         return file;
  46.     }
  47.  
  48.     OBinaryFile& operator<<(OBinaryFile& file, int16_t x){
  49.         constexpr std::size_t size=sizeof(uint16_t);
  50.         std::byte array[size];
  51.         array[0]=static_cast<std::byte>(x>>8);
  52.         array[1]=static_cast<std::byte>(x&0xff);
  53.         file.write(array,size);
  54.         return file;
  55.     }  
  56.  
  57.     OBinaryFile& operator<<(OBinaryFile& file, uint32_t x){
  58.         constexpr std::size_t size=sizeof(uint32_t);
  59.         std::byte array[size];
  60.         array[0]=static_cast<std::byte>((x>>24)&0xff);
  61.         array[1]=static_cast<std::byte>((x>>16)&0xff);
  62.         array[2]=static_cast<std::byte>((x>>8)&0xff);
  63.         array[3]=static_cast<std::byte>(x&0xff);
  64.         file.write(array,size);
  65.         return file;
  66.     }
  67.  
  68.     OBinaryFile& operator<<(OBinaryFile& file, int32_t x){
  69.         constexpr std::size_t size=sizeof(int32_t);
  70.         std::byte array[size];
  71.         array[0]=static_cast<std::byte>((x>>24)&0xff);
  72.         array[1]=static_cast<std::byte>((x>>16)&0xff);
  73.         array[2]=static_cast<std::byte>((x>>8)&0xff);
  74.         array[3]=static_cast<std::byte>(x&0xff);
  75.         file.write(array,size);
  76.         return file;
  77.     }
  78.  
  79.     OBinaryFile& operator<<(OBinaryFile& file, uint64_t x){
  80.         constexpr std::size_t size=sizeof(uint64_t);
  81.         std::byte array[size];
  82.         array[0]=static_cast<std::byte>((x>>56)&0xff);
  83.         array[1]=static_cast<std::byte>((x>>48)&0xff);
  84.         array[2]=static_cast<std::byte>((x>>40)&0xff);
  85.         array[3]=static_cast<std::byte>((x>>32)&0xff);
  86.         array[4]=static_cast<std::byte>((x>>24)&0xff);
  87.         array[5]=static_cast<std::byte>((x>>16)&0xff);
  88.         array[6]=static_cast<std::byte>((x>>8)&0xff);
  89.         array[7]=static_cast<std::byte>(x&0xff);
  90.         file.write(array,size);
  91.         return file;
  92.     }
  93.     OBinaryFile& operator<<(OBinaryFile& file, int64_t x){
  94.         constexpr std::size_t size=sizeof(int64_t);
  95.         std::byte array[size];
  96.         array[0]=static_cast<std::byte>((x>>56)&0xff);
  97.         array[1]=static_cast<std::byte>((x>>48)&0xff);
  98.         array[2]=static_cast<std::byte>((x>>40)&0xff);
  99.         array[3]=static_cast<std::byte>((x>>32)&0xff);
  100.         array[4]=static_cast<std::byte>((x>>24)&0xff);
  101.         array[5]=static_cast<std::byte>((x>>16)&0xff);
  102.         array[6]=static_cast<std::byte>((x>>8)&0xff);
  103.         array[7]=static_cast<std::byte>(x&0xff);
  104.         file.write(array,size);
  105.         return file;
  106.     }
  107.     OBinaryFile& operator<<(OBinaryFile& file, char x){
  108.         std::byte charToSend=static_cast<std::byte>(x);
  109.         file.write(&charToSend,1);
  110.         return file;
  111.     }
  112.  
  113.     OBinaryFile& operator<<(OBinaryFile& file, float x){
  114.         constexpr std::size_t size=sizeof(float);
  115.         uint32_t tmp;
  116.         memcpy(&tmp,&x,size);
  117.         std::byte array[size];
  118.         array[0]=static_cast<std::byte>((tmp>>24)&0xff);
  119.         array[1]=static_cast<std::byte>((tmp>>16)&0xff);
  120.         array[2]=static_cast<std::byte>((tmp>>8)&0xff);
  121.         array[3]=static_cast<std::byte>(tmp&0xff);
  122.         file.write(array,size);
  123.         return file;
  124.     }
  125.    
  126.     OBinaryFile& operator<<(OBinaryFile& file, double x){
  127.         constexpr std::size_t size=sizeof(double);
  128.         uint64_t tmp;
  129.         memcpy(&tmp,&x,size);
  130.         std::byte array[size];
  131.         array[0]=static_cast<std::byte>((tmp>>56)&0xff);
  132.         array[1]=static_cast<std::byte>((tmp>>48)&0xff);
  133.         array[2]=static_cast<std::byte>((tmp>>40)&0xff);
  134.         array[3]=static_cast<std::byte>((tmp>>32)&0xff);
  135.         array[4]=static_cast<std::byte>((tmp>>24)&0xff);
  136.         array[5]=static_cast<std::byte>((tmp>>16)&0xff);
  137.         array[6]=static_cast<std::byte>((tmp>>8)&0xff);
  138.         array[7]=static_cast<std::byte>(tmp&0xff);
  139.         file.write(array,size);
  140.         return file;
  141.     }
  142.  
  143.     OBinaryFile& operator<<(OBinaryFile& file, bool x){
  144.         std::byte valeurVerite{0};
  145.         if(x==true){
  146.             valeurVerite=static_cast<std::byte>(1);
  147.         }
  148.         else if(x==false){
  149.             valeurVerite=static_cast<std::byte>(0);
  150.         }
  151.         file.write(&valeurVerite,1);    
  152.         return file;
  153.     }
  154.  
  155.     OBinaryFile& operator<<(OBinaryFile& file, const std::string& x){
  156.         //Send the size of string.
  157.         int8_t size = x.size();
  158.         file << size;
  159.  
  160.         //Write all character in string.
  161.         for(int8_t i = 0; i < size; ++i){
  162.             file << x[i];
  163.         }
  164.  
  165.         return file;
  166.     }
  167.  
  168.     std::size_t IBinaryFile::read(std::byte* data, std::size_t size){
  169.         return fread(data, sizeof(std::byte), size, m_file);
  170.     }
  171.  
  172.     IBinaryFile::IBinaryFile(const std::string& filename){
  173.       m_file = fopen(filename.data(), "rb");
  174.     }
  175.     IBinaryFile::~IBinaryFile(){
  176.       fclose(m_file);
  177.     }
  178.     IBinaryFile& operator>>(IBinaryFile& file, int8_t& x){                                                                              
  179.         std::byte read;
  180.         file.read(&read, 1);
  181.         x = static_cast<int8_t>(read);
  182.         return file;
  183.     }
  184.     IBinaryFile& operator>>(IBinaryFile& file, uint8_t& x){
  185.         std::byte read;
  186.         file.read(&read, 1);
  187.         x = static_cast<uint8_t>(read);
  188.         return file;
  189.     }
  190.     IBinaryFile& operator>>(IBinaryFile& file, int16_t& x){
  191.         constexpr std::size_t size=sizeof(int16_t);
  192.         std::byte read[size];
  193.         file.read(read,size);
  194.         x=(static_cast<int16_t>(read[0]) << 8)|
  195.            static_cast<int16_t>(read[1]);
  196.         return file;
  197.     }
  198.     IBinaryFile& operator>>(IBinaryFile& file, uint16_t& x){
  199.         constexpr std::size_t=sizeof(uint16_t);
  200.         std::byte read[size];
  201.         file.read(read,size);
  202.         x=(static_cast<uint16_t>(read[0]) << 8)|
  203.            static_cast<uint16_t>(read[1]);
  204.         return file;
  205.     }
  206.     IBinaryFile& operator>>(IBinaryFile& file, int32_t& x){
  207.         constexpr std::size_t size=sizeof(int32_t);
  208.         std::byte read[size];
  209.         file.read(read,size);
  210.         x =  static_cast<int32_t>(read[3])      |
  211.             (static_cast<int32_t>(read[2])<<8)  |
  212.             (static_cast<int32_t>(read[1])<<16) |
  213.             (static_cast<int32_t>(read[0])<<24);
  214.         return file;
  215.     }
  216.  
  217.     IBinaryFile& operator>>(IBinaryFile& file, uint32_t& x){
  218.         constexpr std::size_t size=sizeof(uint32_t);
  219.         std::byte read[size];
  220.         file.read(read,size);
  221.         x =  static_cast<uint32_t>(read[3])      |
  222.             (static_cast<uint32_t>(read[2])<<8)  |
  223.             (static_cast<uint32_t>(read[1])<<16) |
  224.             (static_cast<uint32_t>(read[0])<<24);
  225.         return file;
  226.     }
  227.  
  228.     IBinaryFile& operator>>(IBinaryFile& file, int64_t& x){
  229.         constexpr std::size_t size=sizeof(int64_t);
  230.         std::byte read[size];
  231.         file.read(read,size);
  232.         x =  static_cast<int64_t>(read[7])      |  
  233.             (static_cast<int64_t>(read[6])<<8)  |
  234.             (static_cast<int64_t>(read[5])<<16) |
  235.             (static_cast<int64_t>(read[4])<<24) |
  236.             (static_cast<int64_t>(read[3])<<32) |
  237.             (static_cast<int64_t>(read[2])<<40) |
  238.             (static_cast<int64_t>(read[1])<<48) |
  239.             (static_cast<int64_t>(read[0])<<56);
  240.         return file;
  241.     }
  242.     IBinaryFile& operator>>(IBinaryFile& file, uint64_t& x){
  243.         constexpr std::size_t size=sizeof(uint64_t);
  244.         std::byte read[size];
  245.         file.read(read,size);
  246.         x =  static_cast<uint64_t>(read[7])      |
  247.             (static_cast<uint64_t>(read[6])<<8)  |
  248.             (static_cast<uint64_t>(read[5])<<16) |
  249.             (static_cast<uint64_t>(read[4])<<24) |
  250.             (static_cast<uint64_t>(read[3])<<32) |
  251.             (static_cast<uint64_t>(read[2])<<40) |
  252.             (static_cast<uint64_t>(read[1])<<48) |
  253.             (static_cast<uint64_t>(read[0])<<56);
  254.         return file;
  255.     }
  256.     IBinaryFile& operator>>(IBinaryFile& file, char& x){
  257.         constexpr std::size_t size=sizeof(char);
  258.         std::byte read;
  259.         file.read(&read,size);
  260.         x=static_cast<char>(read);
  261.         return file;
  262.     }
  263.     IBinaryFile& operator>>(IBinaryFile& file, float& x){
  264.         constexpr std::size_t size=sizeof(float);
  265.         std::byte read[size];
  266.         file.read(read,size);
  267.         uint32_t tmp;
  268.         tmp =    static_cast<uint32_t>(read[3])      |
  269.                 (static_cast<uint32_t>(read[2])<<8)  |
  270.                 (static_cast<uint32_t>(read[1])<<16) |
  271.                 (static_cast<uint32_t>(read[0])<<24);
  272.         memcpy(&x,&tmp,size);
  273.         return file;
  274.     }
  275.     IBinaryFile& operator>>(IBinaryFile& file, double& x){
  276.         constexpr std::size_t size=sizeof(double);
  277.         std::byte read[size];
  278.         file.read(read,size);
  279.         uint64_t tmp;
  280.         tmp =    static_cast<uint64_t>(read[7])      |
  281.                 (static_cast<uint64_t>(read[6])<<8)  |
  282.                 (static_cast<uint64_t>(read[5])<<16) |
  283.                 (static_cast<uint64_t>(read[4])<<24) |
  284.                 (static_cast<uint64_t>(read[3])<<32) |
  285.                 (static_cast<uint64_t>(read[2])<<40) |
  286.                 (static_cast<uint64_t>(read[1])<<48) |
  287.                 (static_cast<uint64_t>(read[0])<<56);
  288.         memcpy(&x,&tmp,size);
  289.         return file;
  290.     }
  291.     IBinaryFile& operator>>(IBinaryFile& file, bool& x){
  292.         std::byte read;
  293.         file.read(&read,1);
  294.         if(static_cast<int>(read)>0){
  295.             x=true;
  296.         }
  297.         else{
  298.             x=false;
  299.         }
  300.         return file;
  301.     }
  302.     IBinaryFile& operator>>(IBinaryFile& file, std::string& x){
  303.         x.clear(); //Clean the old string.
  304.  
  305.         //Recv the size of string.
  306.         int8_t size;
  307.         file >> size;
  308.  
  309.         //Write all character in string.
  310.         for(int8_t i = 0; i < size; ++i){
  311.             char car;
  312.             file >> car;
  313.             x.push_back(car);
  314.         }
  315.        
  316.         return file;
  317.     }
  318. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top