Код
C, который не зависит от реализации fd_set
:
void Fdset_Add(fd_set *Out, fd_set const *In, int InNfds)
{
for(i = 0; i < InNfds; i++)
{
if(i < InNfds && FD_ISSET(i, In))
FD_SET(i, Out);
}
}
int Fdset_Merge(fd_set *Out, fd_set const *In1, int NFds1, fd_set const *In2, int NFds2)
{
FD_ZERO(Out);
Fdset_Add(Out, In1, Nfds1);
Fdset_Add(Out, In2, Nfds2);
return Nfds1 > Nfds2 ? Nfds1 : Nfds2;
}
int Fdset_Filter(fd_set const *Result, int ResultNfds, fd_set *ToFilter, int NfdsToFilter)
{
int i;
int Retval;
Retval = 0;
for(i = 0; i < ResultNfds; i++)
{
if(i < NfdsToFilter && FD_ISSET(i, ToFilter))
{
if(! FD_ISSET(i, Result))
FD_CLR(i, ToFilter);
else
Retval++;
}
}
return Retval;
}
void Fdset_Split(fd_set const *Result, int ResultNfds, fd_set *In1, int Nfds1, int *Count1, fd_set *In2, int Nfds2, int *Count2)
{
*Count1 = Fdset_Filter(Result, ResultNfds, In1, Nfds1);
*Count2 = Fdset_Filter(Result, ResultNfds, In1, Nfds2);
}