void mgh::write(string fname, const raw & data){ FILE * src = fopen(fname.c_str(), "wb"); if(src == NULL)throw "Failed to open file"; int bytes_written = fwrite(data.c_str(), 1, data.size(), src); fclose(src); if(bytes_written != data.size())throw "Could not write all the data"; };
message_21::message_21(const raw & bits) : message(ID) , name("@@@@@@@@@@@@@@@@@@@@") { if ((bits.size() < SIZE_BITS_MIN) || (bits.size() > SIZE_BITS_MAX)) throw std::invalid_argument{"invalid number of bits in message_21"}; read_data(bits); }
message_03::message_03(const raw & bits) : message_01(ID) { if (bits.size() != SIZE_BITS) throw std::invalid_argument{"invalid number of bits in ais/message_03"}; read_data(bits); }
std::unique_ptr<message> make_message(const raw & data) throw(std::invalid_argument) { if (data.size() < 1) throw std::invalid_argument{"raw data of insufficient size"}; message_id type = static_cast<message_id>(data[0]); return instantiate_message(type)(data); }
void message_21::read_data(const raw & bits) { get(bits, repeat_indicator); get(bits, mmsi); get(bits, aid_type); get(bits, name); get(bits, position_accuracy); get(bits, longitude_minutes); get(bits, latitude_minutes); get(bits, to_bow); get(bits, to_stern); get(bits, to_port); get(bits, to_starboard); get(bits, epfd_fix); get(bits, utc_second); get(bits, off_position); get(bits, regional); get(bits, raim); get(bits, virtual_aid_flag); get(bits, assigned); auto rest = bits.size() - SIZE_BITS_MIN; if (rest > 0) { rest = std::min(rest, SIZE_BITS_MAX - SIZE_BITS_MIN); name_extension = read_string(bits, 272, rest / 6); } }
message_18::message_18(const raw & bits) : message_18(ID) { if (bits.size() != SIZE_BITS) throw std::invalid_argument{"invalid number of bits in message_18"}; read_data(bits); }
/// Checks the specified sizes for the raw data. In case of error, an /// exception is thrown (std::invalid_argument). /// /// This check must be done for every message. void message::check_size(const raw & data, size_t size) { assert(size > 2); if (data.size() != size) throw std::invalid_argument{"invalid number of bytes in message"}; if ((data[1] & 0x0f) != static_cast<uint8_t>(size - 3)) throw std::invalid_argument{"invalid size specified in message"}; }
std::unique_ptr<message> message_10::parse(const raw & bits) { if (bits.size() != SIZE_BITS) throw std::invalid_argument{"invalid number of bits in message_10::parse"}; std::unique_ptr<message> result = utils::make_unique<message_10>(); message_10 & msg = static_cast<message_10 &>(*result); msg.read_data(bits); return result; }
//writing the results back void memcpy_reverse_w(raw & data, const char * input, int size, int &pos){ if(data.size() < pos+size)throw "Not enough bytes to write;"; for(int i = 0; i < size; i++){data[pos+i] = input[size-i-1];}; pos+=size; };
//raw memory copy from a string void memcpy_reverse(const raw & data, char * output, int size, int &pos){ if(data.size() < pos+size)throw "Not enough bytes to read;"; for(int i = 0; i < size; i++){output[size-i-1]=data[pos+i];}; pos+=size; };