iree/base/internal/flags.c
static iree_status_t iree_flag_parse(iree_flag_t* flag,
                                     iree_string_view_t value) {
  IREE_TRACE_ZONE_BEGIN(z0);
  IREE_TRACE_ZONE_APPEND_TEXT(z0, flag->name.data, flag->name.size);
  IREE_TRACE_ZONE_APPEND_TEXT(z0, value.data, value.size);

  // Insert NUL on the flag value. This is safe as the value is either coming
  // from C argv memory which is mutable or a flagfile that we loaded into
  // memory ourselves.
  char* str_value = (char*)value.data;
  if (value.size > 0) {
    str_value[value.size] = 0;
  }

  iree_status_t status = iree_ok_status();
  switch (flag->type) {
    case IREE_FLAG_TYPE_callback:
      status = flag->parse_callback(flag->name, flag->storage, value);
      break;
    case IREE_FLAG_TYPE_bool:
      if (value.size == 0 || strcmp(str_value, "true") == 0 ||
          strcmp(str_value, "1") == 0) {
        *(bool*)flag->storage = true;
      } else {
        *(bool*)flag->storage = false;
      }
      break;
    case IREE_FLAG_TYPE_int32_t:
      *(int32_t*)flag->storage = value.size ? atoi(str_value) : 0;
      break;
    case IREE_FLAG_TYPE_int64_t:
      *(int64_t*)flag->storage = value.size ? atoll(str_value) : 0;
      break;
    case IREE_FLAG_TYPE_float:
      *(float*)flag->storage = value.size ? (float)atof(str_value) : 0.0f;
      break;
    case IREE_FLAG_TYPE_double:
      *(double*)flag->storage = value.size ? atof(str_value) : 0.0;
      break;
    case IREE_FLAG_TYPE_string: {
      iree_host_size_t str_length = value.size;
      if (str_length > 2) {
        // Strip double quotes: "foo" -> foo.
        // This may not be worth the complexity.
        if (str_value[0] == '"' && str_value[str_length - 1] == '"') {
          str_value[str_length - 1] = 0;
          ++str_value;
          str_length = str_length - 2;
        }
      }
      *(const char**)flag->storage = str_value;
      break;
    }
    default:
      status = iree_make_status(IREE_STATUS_FAILED_PRECONDITION,
                                "invalid flag type %u", flag->type);
      break;
  }
  IREE_TRACE_ZONE_END(z0);
  return status;
}
// Parses a flag value from the given string and stores it.
Parses a flag value from the given string and stores it.
Parses a flag value from the given string and stores it .
static iree_status_t iree_flag_parse(iree_flag_t* flag,
                                     iree_string_view_t value) {
  IREE_TRACE_ZONE_BEGIN(z0);
  IREE_TRACE_ZONE_APPEND_TEXT(z0, flag->name.data, flag->name.size);
  IREE_TRACE_ZONE_APPEND_TEXT(z0, value.data, value.size);
  char* str_value = (char*)value.data;
  if (value.size > 0) {
    str_value[value.size] = 0;
  }
  iree_status_t status = iree_ok_status();
  switch (flag->type) {
    case IREE_FLAG_TYPE_callback:
      status = flag->parse_callback(flag->name, flag->storage, value);
      break;
    case IREE_FLAG_TYPE_bool:
      if (value.size == 0 || strcmp(str_value, "true") == 0 ||
          strcmp(str_value, "1") == 0) {
        *(bool*)flag->storage = true;
      } else {
        *(bool*)flag->storage = false;
      }
      break;
    case IREE_FLAG_TYPE_int32_t:
      *(int32_t*)flag->storage = value.size ? atoi(str_value) : 0;
      break;
    case IREE_FLAG_TYPE_int64_t:
      *(int64_t*)flag->storage = value.size ? atoll(str_value) : 0;
      break;
    case IREE_FLAG_TYPE_float:
      *(float*)flag->storage = value.size ? (float)atof(str_value) : 0.0f;
      break;
    case IREE_FLAG_TYPE_double:
      *(double*)flag->storage = value.size ? atof(str_value) : 0.0;
      break;
    case IREE_FLAG_TYPE_string: {
      iree_host_size_t str_length = value.size;
      if (str_length > 2) {
        if (str_value[0] == '"' && str_value[str_length - 1] == '"') {
          str_value[str_length - 1] = 0;
          ++str_value;
          str_length = str_length - 2;
        }
      }
      *(const char**)flag->storage = str_value;
      break;
    }
    default:
      status = iree_make_status(IREE_STATUS_FAILED_PRECONDITION,
                                "invalid flag type %u", flag->type);
      break;
  }
  IREE_TRACE_ZONE_END(z0);
  return status;
}
static iree_status_t iree_flag_parse ( iree_flag_t * flag , iree_string_view_t value ) { IREE_TRACE_ZONE_BEGIN ( z0 ) ; IREE_TRACE_ZONE_APPEND_TEXT ( z0 , flag -> name . data , flag -> name . size ) ; IREE_TRACE_ZONE_APPEND_TEXT ( z0 , value . data , value . size ) ; char * str_value = ( char * ) value . data ; if ( value . size > 0 ) { str_value [ value . size ] = 0 ; } iree_status_t status = iree_ok_status ( ) ; switch ( flag -> type ) { case IREE_FLAG_TYPE_callback : status = flag -> parse_callback ( flag -> name , flag -> storage , value ) ; break ; case IREE_FLAG_TYPE_bool : if ( value . size == 0 || strcmp ( str_value , " " ) == 0 || strcmp ( str_value , " " ) == 0 ) { * ( bool * ) flag -> storage = true ; } else { * ( bool * ) flag -> storage = false ; } break ; case IREE_FLAG_TYPE_int32_t : * ( int32_t * ) flag -> storage = value . size ? atoi ( str_value ) : 0 ; break ; case IREE_FLAG_TYPE_int64_t : * ( int64_t * ) flag -> storage = value . size ? atoll ( str_value ) : 0 ; break ; case IREE_FLAG_TYPE_float : * ( float * ) flag -> storage = value . size ? ( float ) atof ( str_value ) : 0.0f ; break ; case IREE_FLAG_TYPE_double : * ( double * ) flag -> storage = value . size ? atof ( str_value ) : 0.0 ; break ; case IREE_FLAG_TYPE_string : { iree_host_size_t str_length = value . size ; if ( str_length > 2 ) { if ( str_value [ 0 ] == ' ' && str_value [ str_length - 1 ] == ' ' ) { str_value [ str_length - 1 ] = 0 ; ++ str_value ; str_length = str_length - 2 ; } } * ( const char * * ) flag -> storage = str_value ; break ; } default : status = iree_make_status ( IREE_STATUS_FAILED_PRECONDITION , " " , flag -> type ) ; break ; } IREE_TRACE_ZONE_END ( z0 ) ; return status ; }
Parses a flag value from the given string and stores it.
Parses a flag value from the given string and stores it .
// Insert NUL on the flag value. This is safe as the value is either coming // from C argv memory which is mutable or a flagfile that we loaded into // memory ourselves. // Strip double quotes: "foo" -> foo. // This may not be worth the complexity.