R1KO
fuck society
- Сообщения
- 9,457
- Реакции
- 7,786
- Команда форума
- #1
Может кому нужно будет функции для преобразования строки с айпи в int (4 байта) и обратно.
Можно было сделать на стандартных ф-ях см но мне захотелось сделать так.
Можно было сделать на стандартных ф-ях см но мне захотелось сделать так.
PHP:
#pragma semicolon 1
#include <sourcemod>
#pragma newdecls required
public void OnClientPostAdminCheck(int iClient)
{
char szIp[16];
GetClientIP(iClient, szIp, sizeof(szIp));
LogMessage("Original IP: %s", szIp);
int iLongIP = IP2L(szIp);
LogMessage("Long IP: %i", iLongIP);
L2IP(iLongIP, szIp, sizeof(szIp));
LogMessage("Result IP: %s", szIp);
}
stock int IP2L(const char[] szIp)
{
char szParts[4][4];
if(ExplodeStringByChar(szIp, '.', szParts, 4, 4) == 4)
{
return (((StringToInt(szParts[0])*256+StringToInt(szParts[1]))*256+StringToInt(szParts[2]))*256+StringToInt(szParts[3]));
}
return 0;
}
stock void L2IP(int iIp, char[] szBuffer, int iMaxLen)
{
FormatEx(szBuffer, iMaxLen, "%d.%d.%d.%d", (iIp >> 24) & 0x000000FF, (iIp >> 16) & 0x000000FF, (iIp >> 8) & 0x000000FF, iIp & 0x000000FF);
}
stock int ExplodeStringByChar(const char[] szText, int iChar, char[][] szBuffers, int iMaxStrings, int iMaxStringLength, bool bCopyRemainder = false)
{
int reloc_idx, idx, total;
while ((idx = FindCharEx(szText[reloc_idx], iChar)) != -1)
{
strcopy(szBuffers[total], idx+1, szText[reloc_idx]);
szBuffers[total][idx] = 0; // Вроде можно без этого
reloc_idx += idx+1;
if (++total == iMaxStrings)
{
if (bCopyRemainder)
{
strcopy(szBuffers[total-1], iMaxStringLength, szText[reloc_idx-idx]);
}
return total;
}
}
strcopy(szBuffers[total++], iMaxStringLength, szText[reloc_idx]);
return total;
}
stock int FindCharEx(const char[] szBuffer, char iChar)
{
int len = strlen(szBuffer), i;
for (i = 0; i < len; ++i)
{
if (szBuffer[i] == iChar)
return i;
}
return -1;
}