static int _wemq_proxy_load_item()

in eventmesh-sdks/eventmesh-sdk-c/src/rmb_access_config.c [122:222]


static int _wemq_proxy_load_item (StWemqProxy * pproxy, const char *pstr,
                                  size_t len)
{
  char weight[64] = "";
  char idc[32] = "";
  char port[64] = "";
  int index1 = -1;              // ':'位置
  int index2 = -1;              // '#'位置
  int index3 = -1;              // '|' 位置
  int begin = 0;
  int size = 0;
  int i = 0;

  if (pstr == NULL || pproxy == NULL || len == 0)
  {
    return -1;
  }

  for (i = 0; i < (int) len; i++)
  {
    if (pstr[i] == ':')
    {
      if (index1 != -1)
      {
        return -2;
      }
      index1 = i;
    }
    else if (pstr[i] == '#')
    {
      if (index2 != -1)
      {
        return -2;
      }
      index2 = i;
    }
    else if (pstr[i] == '|')
    {
      if (index3 != -1)
      {
        return -2;
      }
      index3 = i;
    }
  }

  // 确认格式是否正确
  if (index1 == -1 || index2 == -1 || index1 >= index2 || index2 >= index3)
  {
    return -2;
  }

  // IP
  if (index1 > 100 || index1 == 0)
  {
    return -3;
  }
  memcpy (pproxy->host, pstr, index1);
  pproxy->host[index1] = '\0';

  // port
  begin = index1 + 1;
  size = index2 - begin;
  if (size > 64 || size == 0)
  {
    return -3;
  }
  memcpy (port, &pstr[begin], size);
  port[size] = '\0';

  // weight
  begin = index2 + 1;
  size = index3 - begin;
  if (size > 64 || size == 0)
  {
    return -3;
  }
  memcpy (weight, &pstr[begin], size);
  weight[size] = '\0';

  // idc
  begin = index3 + 1;
  size = len - begin;
  if (size > 64 || size == 0)
  {
    return -3;
  }
  memcpy (pproxy->idc, &pstr[begin], size);
  pproxy->idc[size] = '\0';

  pproxy->port = atoi (port);
  pproxy->weight = atoi (weight);

  if (pproxy->port == 0 || pproxy->weight == 0)
  {
    return -4;
  }

  pproxy->flag = 0;
  return 0;
}