字符串解析

#ifdef MIXER_CONTROL_SETTING_SUPPORT
static void set_mixer_control_value(const char *control, char **values, unsigned int num_values)
{
    struct mixer *mixer;
    struct mixer_ctl *ctl;
    enum mixer_ctl_type type;
    unsigned int i, num_ctl_values;

    mixer = mixer_open(0);
    if (!mixer) {
         ALOGV("%s: mixer open fail", __func__);
         return;
    }

    ctl = mixer_get_ctl_by_name(mixer, control);
    if (!ctl) {
        ALOGV("%s: Could not get ctl for mixer cmd - %s",__func__, control);
        return;
    }

    type = mixer_ctl_get_type(ctl);
    num_ctl_values = mixer_ctl_get_num_values(ctl);

    if (isdigit(values[0][0])) {
        if (num_values == 1) {
            /* Set all values the same */
            int value = atoi(values[0]);

            for (i = 0; i < num_ctl_values; i++) {
                if (mixer_ctl_set_value(ctl, i, value)) {
                    ALOGV("%s: Error: invalid value\n",__func__);
                    return;
                }
            }
        } else {
            /* Set multiple values */
            if (num_values > num_ctl_values) {
                ALOGV("%s: Error: %d values given, but control only takes %d\n",
                       __func__,num_values, num_ctl_values);
                return;
            }
            for (i = 0; i < num_values; i++) {
                if (mixer_ctl_set_value(ctl, i, atoi(values[i]))) {
                    ALOGV("%s: Error: invalid value for index %d\n",__func__,i);
                    return;
                }
            }
        }
    } else {
        if (type == MIXER_CTL_TYPE_ENUM) {
            if (num_values != 1) {
                ALOGV("%s: Enclose strings in quotes and try again\n",__func__);
                return;
            }
            if (mixer_ctl_set_enum_by_string(ctl, values[0]))
                ALOGV("%s: Error: invalid enum value\n",__func__);
        } else {
            ALOGV("%s: Error: only enum types can be set with strings\n",__func__);
        }
    }

    return;
}


static int get_mixer_control_value(const char *control, int *values, int *len)
{
    struct mixer *mixer;
    struct mixer_ctl *ctl;
    enum mixer_ctl_type type;
    unsigned int i,num_values;
    int ret=0;

    mixer = mixer_open(0);
    if (!mixer) {
         ALOGV("%s: mixer open fail", __func__);
         return -EINVAL;
    }

    ctl = mixer_get_ctl_by_name(mixer, control);
    if (!ctl) {
        ALOGV("%s: Could not get ctl for mixer cmd - %s",__func__, control);
        return -EINVAL;
    }
    num_values = mixer_ctl_get_num_values(ctl);
    *len = num_values;
    for(i=0; i<num_values; i++) {
        values[i] = mixer_ctl_get_value(ctl, i);
    }
    mixer_close(mixer);

    return ret;
}

int parse_mixer_parameters_and_get_value(const char * control ,char *value)
{
    #define MIXER_KEY_PREFIX "MIXER:"
    char *btr;
    char *tmpbtr;
    char *kvpair;
    char *mixer_key;
    int ret = 0;
    int num_values;
    char mtr[256]={0};
    int i;

    ALOGV("%s: enter: %s", __func__, control);

    mixer_key = strstr(control, MIXER_KEY_PREFIX);
    if(!mixer_key)
       return -EINVAL;

    btr = strdup(control);
    kvpair = strtok_r(btr, ";", &tmpbtr);

    while (kvpair && *kvpair) {
       int val[256]={0};
       char ptr[256]={0};
       char str[256]={0};
       char wtr[256]={0};
       char *key;
       char *endptr;

       strcpy(str, kvpair);
       strcat(str, "=");
       get_mixer_control_value(kvpair + strlen(MIXER_KEY_PREFIX), val, &num_values);
       if (num_values >1) {
           for(i=0; i<num_values; i++) {
               memset(ptr, 0, sizeof(ptr));
               sprintf(ptr, "%d", val[i]);
               strcat(str,ptr);
               strcat(str, ",");
           }
           memset(wtr, 0, sizeof(wtr));
           memcpy(wtr, str, strlen(str)-1);
       } else {
           memset(ptr, 0, sizeof(ptr));
           sprintf(ptr, "%d", val[0]);
           strcat(str,ptr);
           memset(wtr, 0, sizeof(wtr));
           memcpy(wtr, str, strlen(str));
       }
       strcat(mtr, wtr);
       strcat(mtr, ";");

       num_values = 0;

next_pair:
       kvpair = strtok_r(NULL, ";", &tmpbtr);

    }
    memcpy(value,mtr, strlen(mtr)-1);

    free(btr);
    return ret;

}

int parse_mixer_parameters_and_set_value(const char* control)
{
    #define MIXER_KEY_PREFIX "MIXER:"
    char *str;
    char *tmpstr;
    char *kvpair;
    int ret = 0;

    ALOGV("%s: enter: %s", __func__, control);

    str = strdup(control);
    if (!str)
       goto end;
    kvpair = strtok_r(str, ";", &tmpstr);
    while (kvpair && *kvpair) {
       char *eq = strchr(kvpair, '=');
       char *value;
       char *key;
       char *endptr;
       char *mixer_key;
       char *val[256];
       int num_values = 0;

       if (eq == kvpair)
       goto next_pair;
       if (eq) {
           key = strndup(kvpair, eq - kvpair);
           if (*(++eq)) {
               value = strdup(eq);
               char *buf = value;
               while((val[num_values]=strtok(buf,","))!=NULL) {
                 num_values++;
                 buf=NULL;
              }
           } else {
               value = strdup("");
           }
       } else {
           key = strdup(kvpair);
           value = strdup("");
       }

       mixer_key = strstr(key, MIXER_KEY_PREFIX);
       if (mixer_key && (*value != '\0')) {
           set_mixer_control_value(mixer_key + strlen(MIXER_KEY_PREFIX), val, num_values);
       } else {
           ALOGV("%s: Failed to Parse mixer parameters", __func__);
           ret = -EINVAL;
       }
       free(key);
       free(value);

next_pair:
       kvpair = strtok_r(NULL, ";", &tmpstr);
    }
    free(str);
end:
    return ret;
}
#endif

你可能感兴趣的:(字符串解析)