Logo Search packages:      
Sourcecode: dballe version File versions

dba_err dba_record_set_from_string ( dba_record  rec,
const char *  str 
)

Set a value in the record according to an assignment encoded in a string.

String can use keywords, aliases and varcodes. Examples: ana_id=3, name=Bologna, B12012=32.4

Parameters:
rec The record where the value is to be set.
str The string containing the assignment.
Returns:
The error indicator for the function.

Definition at line 354 of file dba_record.c.

References dba_error_consistency, dba_error_notfound, dba_record_keyword_byname_len(), DBA_RUN_OR_RETURN, DBA_STRING_TO_VAR, dba_varcode_alias_resolve_substring(), and _dba_varinfo::is_string.

{
      /* Split the input as name=val */
      const char* s;
      const char* val;
      dba_varcode varcode;
      dba_varinfo info;
      dba_keyword param;
      
      if ((s = strchr(str, '=')) == NULL)
            return dba_error_consistency("there should be an = between the name and the value in '%s'", str);

//          name = strndup(queryparm, s - queryparm);
      val = s + 1;

      /* First see if it's an alias or a variable */
      if ((varcode = dba_varcode_alias_resolve_substring(str, s - str)) != 0 || str[0] == 'B')
      {
            if (varcode == 0)
                  varcode = DBA_STRING_TO_VAR(str + 1);

            /* Query informations about the parameter */
            DBA_RUN_OR_RETURN(dba_varinfo_query_local(varcode, &info));

            if (info->is_string)
                  DBA_RUN_OR_RETURN(dba_record_var_setc(rec, varcode, val));
            else
                  DBA_RUN_OR_RETURN(dba_record_var_setd(rec, varcode, strtod(val, 0)));
      } else {
            /* Else handle a normal keyword */

            param = dba_record_keyword_byname_len(str, s - str);

            if (param == DBA_KEY_ERROR)
                  return dba_error_notfound("looking for misspelled keyword \"%.*s\"", s - str, str);

            /* Query informations about the parameter */
            DBA_RUN_OR_RETURN(dba_record_keyword_info(param, &info));

            if (info->is_string)
                  DBA_RUN_OR_RETURN(dba_record_key_setc(rec, param, val));
            else
                  DBA_RUN_OR_RETURN(dba_record_key_setd(rec, param, strtod(val, 0)));
      }

      return dba_error_ok();
}


Generated by  Doxygen 1.6.0   Back to index