conv_functions.cc 2.31 KB
Newer Older
Jean-Benoist Leger's avatar
Jean-Benoist Leger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include "conv_functions.h"

void build_MDB_val_from_node_info(
        MDB_val* mdb_value,
        double & lon, 
        double & lat, 
        elevation_value & elevation, 
        std::list<neighbor_t> & neighbors
        )
{
    // Return a pointer (don't forget to free it with free()) to a
    // build_node_info with allocation of the two internal structure in the same
    // allocation. Design to be used to build a MDB_val
    char* pc;
    node_info_t ni;
    size_t len;
    len =   sizeof(node_info_fixed_t)
            +neighbors.size()*sizeof(neightbor_t);
    pc = (char*)malloc(len);
    if(!pc)
    {
        fprintf(stderr,"Memory allocation failed\n");
        abort();
    }
    
    ni.fixed = (node_info_fixed_t*)pc;
    
    if(neighbors.size()>0)
        ni.neighbors = (neighbor_t*)(pc+sizeof(node_info_fixed_t));
    else
        ni.neighbors = (neighbor_t*)NULL;

    ni.fixed->lon = lon;
    ni.fixed->lat = lat;
    ni.fixed->elevation = elevation;
    ni.fixed->neighbors_number = neighbors.size();

    unsigned int i=0;
    for(
            std::list<neighbor_t>::iterator neighbors_it = neighbors.begin();
            neighbors_it != neighbors.end();
            neighbors_it++
       )
    {
        ni.neighbors[i].from = (*neighbors_it).from;
        ni.neighbors[i].to = (*neighbors_it).to;
        ni.neighbors[i].way_kind = (*neighbors_it).way_kind;
        i++;
    }

    mdb_value->mv_size = len;
    mdb_value->mv_data = (void*)pc;
}

void free_MDB_val_from_node_info(
        MDB_val* mdb_value
        )
{
    free(mdb_value->mv_data);
    mdb_value->mv_data = (void*)NULL;
    mdb_value->mv_size = 0;
}

void build_node_info_from_MDB_val(
        node_info_t* pni,
        MDB_val* mdb_value
        )
{
    if(mdb_value->mv_size<sizeof(node_info_fixed_t))
    {
        fprintf(stderr,"Error, database node info to short\n");
        abort();
    }
    pni->fixed = (node_info_fixed_t*)(mdb_value->mv_data);
    if(mdb_value->mv_size!=sizeof(node_info_fixed_t)+(pni->fixed->neighbors_number)*sizeof(neighbor_t))
    {
        fprintf(stderr,"Error, database node info size mismatch\n");
        abort();
    }
    if(pni->fixed->neighbors_number>0)
        pni->neighbors = (neighbor_t*)(((char*)mdb_value->mv_data)+sizeof(node_info_fixed_t));
    else
        pni->neighbors = (neighbor_t*)NULL;
}