/* This file is part of Lcrzo.
*/

/* This file is part of Lcrzo.
*/

#ifdef LCRZODEF_SYSTEM_Unix
  /* this function is not needed*/

#elif defined LCRZODEF_SYSTEM_Windows


/*-------------------------------------------------------------*/
int lcrzo_priv_conf_packetgetadapternames_add(lcrzo_list *plist,
                                              lcrzo_constdevice device);
int lcrzo_priv_conf_packetgetadapternames_add(lcrzo_list *plist,
                                              lcrzo_constdevice device)
{ return(lcrzo_datalist_add_pos(plist, -1, 1, (lcrzo_constdata)device, strlen(device)+1));
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_packetgetadapternames_get(lcrzo_list *plist,
                                              lcrzo_int32 pos,
                                              lcrzo_device device);
int lcrzo_priv_conf_packetgetadapternames_get(lcrzo_list *plist,
                                              lcrzo_int32 pos,
                                              lcrzo_device device)
{ lcrzo_data data;
  int ret;

  lcrzo_er(lcrzo_datalist_valuem_pos(plist, pos, NULL, &data,NULL));
  ret=lcrzo_device_init((lcrzo_conststring)data, device);
  lcrzo_data_free(data);
  return(ret);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_packetgetadapternames(lcrzo_list *plist);
int lcrzo_priv_conf_packetgetadapternames(lcrzo_list *plist)
{ lcrzo_data data;
  lcrzo_uint32 datasizeu;
  lcrzo_int32 datasize;
  lcrzo_device device;
  BOOL err;
  lcrzo_bool isunicode;
  int start, end, ret;
  
  datasize=10000;
  datasizeu=datasize;
  lcrzo_er(lcrzo_data_alloc(datasize, &data));
  err=lcrzo_priv_cs_PacketGetAdapterNames((lcrzo_string)data, &datasizeu);
  if (!err)
  { lcrzo_data_free(data);
    return(LCRZO_ERR_FUPACKETGETADAPTERNAMES);
  }
  if (datasizeu>9999)
  { lcrzo_data_free(data);
    return(LCRZO_ERR_FUPACKETGETADAPTERNAMES);
  }
  datasize=datasizeu;

  /* empty list */
  if ( datasize<2 )
  { lcrzo_data_free(data);
    ret=lcrzo_datalist_init(plist);
    return(ret);
  }

  /* simple check if data is unicode */
  isunicode=LCRZO_FALSE;
  if ( data[1]==0 ) isunicode=LCRZO_TRUE;

  /* loop for each adapter name */
  start=0;
  ret=LCRZO_ERR_OK;
  ret=lcrzo_datalist_init(plist);
  while(ret==LCRZO_ERR_OK)
  { /* find the end of adapter name */
    end=start;
    if (isunicode)
    { while( !(data[end]==0 && data[end+1]==0) && end<datasize ) end+=2;
    }
    else
    { while( data[end]!=0 && end<datasize ) end++;
    }
    /* end of list */
    if ( end==start || end==datasize ) break;
    /* add the device */
    if (isunicode)
    { ret=lcrzo_priv_device_init_unicode(data+start, device);
    }
    else
    { ret=lcrzo_priv_device_init_ascii((lcrzo_string)(data+start), device);
    }    
    if (ret==LCRZO_ERR_OK)
    { ret=lcrzo_priv_conf_packetgetadapternames_add(plist, device);
    }
    /* skip this device */
    if (isunicode) start=end+2;
    else start=end+1;
  }

  lcrzo_data_free(data);
  if (ret!=LCRZO_ERR_OK)
  { lcrzo_list_close(plist);
  }
  return(ret);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_packetgetadapternames_debug_fprint(LCRZOFILE *pf);
int lcrzo_priv_conf_packetgetadapternames_debug_fprint(LCRZOFILE *pf)
{ lcrzo_list list;
  lcrzo_device device;
  lcrzo_int32 i, count;

  fprintf(pf, "\n\nList of adapters from PacketGetAdapterNames\n");
  lcrzo_er(lcrzo_priv_conf_packetgetadapternames(&list));
  lcrzo_er(lcrzo_list_count(&list, &count));
  for ( i=1 ; i<=count ; i++ )
  { lcrzo_er(lcrzo_priv_conf_packetgetadapternames_get(&list, i, device));
    lcrzo_device_fprint(pf, " dev=", device, "\n");
  }
  lcrzo_er(lcrzo_list_close(&list));
  return(LCRZO_ERR_OK);
}


#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif
