unsigned long Serialization::unserialize(const std::vector<char> &buf, Transformable &tr_data, unsigned long start_loc) { // delete all existing items tr_data.clear(); unsigned long names_arg_sz; unsigned long n_rec; unsigned long bytes_read; size_t i_start = 0; // get size of names record i_start = start_loc; w_memcpy_s(&names_arg_sz, sizeof(names_arg_sz), buf.data()+i_start, sizeof(names_arg_sz)); i_start += sizeof(names_arg_sz); unique_ptr<char[]> names_buf(new char[names_arg_sz]); w_memcpy_s(names_buf.get(), sizeof(char)*names_arg_sz, buf.data()+i_start, sizeof(char)*names_arg_sz); i_start += sizeof(char)*names_arg_sz; w_memcpy_s(&n_rec, sizeof(n_rec), buf.data()+i_start, sizeof(n_rec)); i_start += sizeof(n_rec); // build transformable data set double *value_ptr = (double *) ( buf.data()+i_start); vector<string> names_vec; tokenize(strip_cp(string(names_buf.get(), names_arg_sz)), names_vec); assert(names_vec.size() == n_rec); for (unsigned long i=0; i< n_rec; ++i) { tr_data.insert(names_vec[i], *(value_ptr+i)); } bytes_read = i_start + n_rec * sizeof(double); return bytes_read; }
unsigned long Serialization::unserialize(const vector<char> &ser_data, Transformable &items, const vector<string> &names_vec, unsigned long start_loc) { unsigned long total_bytes_read = 0; size_t vec_size = (ser_data.size() - start_loc) / sizeof(double); assert(vec_size >= names_vec.size()); items.clear(); double *value = 0; size_t iloc = start_loc; size_t n_read = 0; size_t n_val = names_vec.size(); for (n_read=0; n_read<n_val; ++n_read) { value = (double*)&ser_data[iloc]; items.insert(names_vec[n_read], *value); iloc += sizeof(double); } total_bytes_read = n_read * sizeof(double); return total_bytes_read; }
void read_data_file(const string &filename, Transformable &data) { ifstream fin; fin.open(filename); string line; string name; double value; data.clear(); while (getline(fin, line)) { vector<string> tokens; pest_utils::strip_ip(line); if (!line.empty() && line[0] != '#') { pest_utils::tokenize(line, tokens); name = pest_utils::upper_cp(tokens[0]); pest_utils::convert_ip(tokens[1], value); data.insert(name, value); } } fin.close(); }