C实现的简单键值对

typedef struct {
    unsigned short id;
    char *str;
} value_string;

static const value_string vals_message_type[] = {
    /* MMS 1.0 */
    { PDU_M_SEND_REQ,        "m-send-req" },
    { PDU_M_SEND_CONF,        "m-send-conf" },
    { PDU_M_NOTIFICATION_IND,    "m-notification-ind" },
    { PDU_M_NOTIFYRESP_IND,    "m-notifyresp-ind" },
    { PDU_M_RETRIEVE_CONF,    "m-retrieve-conf" },
    { PDU_M_ACKNOWLEDGE_IND,    "m-acknowledge-ind" },
    { PDU_M_DELIVERY_IND,    "m-delivery-ind" },
    /* MMS 1.1 */
    { PDU_M_READ_REC_IND,    "m-read-rec-ind" },
    { PDU_M_READ_ORIG_IND,    "m-read-orig-ind" },
    { PDU_M_FORWARD_REQ,    "m-forward-req" },
    { PDU_M_FORWARD_CONF,    "m-forward-conf" },
    /* MMS 1.2 */
    { PDU_M_MBOX_STORE_REQ,    "m-mbox-store-req" },
    { PDU_M_MBOX_STORE_CONF,    "m-mbox-store-conf" },
    { PDU_M_MBOX_VIEW_REQ,    "m-mbox-view-req" },
    { PDU_M_MBOX_VIEW_CONF,    "m-mbox-view-conf" },
    { PDU_M_MBOX_UPLOAD_REQ,    "m-mbox-upload-req" },
    { PDU_M_MBOX_UPLOAD_CONF,    "m-mbox-upload-conf" },
    { PDU_M_MBOX_DELETE_REQ,    "m-mbox-delete-req" },
    { PDU_M_MBOX_DELETE_CONF,    "m-mbox-delete-conf" },
    { PDU_M_MBOX_DESCR,        "m-mbox-descr" },
    { 0x00, NULL },
};

char *Value2String(unsigned char val, const value_string *array, char *def)
{
    int i = 0;

    while (array[i].str != NULL && array[i].id != val) {
        i++;
    }
    if (array[i].str == NULL)
        return def;
    
    return array[i].str;
}

你可能感兴趣的:(C语言)