Hi,
does somebody knows how to read and write the ban files ? A tutorial would be great
does somebody knows how to read and write the ban files ? A tutorial would be great
struct demo_ban
{
in_addr IP_address;
__int64 Time;
};
//----
struct name_ban
{
char username [24];
__int64 Time;
int BanHours;
};
//----
struct File
{
char Id [6];
byte Zero [1];
byte Version [1];
int Demobans;
int Namebans;
};
//----
#include <stdio.h>
#include <stdlib.h>
// cross platform loveliness
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
// __int64 is a compiler specific thing. long long should be around 8 bytes, but we
// can't guarantee it :( Ideally this should be declared as a compiler argument.
#ifndef __int64
#define __int64 long long
#endif
#ifndef LFS_BAN_VER
#define LFS_BAN_VER 246
#endif
struct ban_core
{
char id[7]; // null terminated string LFSBAN\0
char version;
int num_demo;
int num_name;
};
struct ban_demo
{
in_addr ip_address;
__int64 time;
};
struct ban_name
{
char username[24];
__int64 time;
int banhours;
};
int main()
{
FILE *fio = NULL;
long fsize;
struct ban_core core;
// lets set core to all \0's
memset(core, 0, sizeof(core));
fio = fopen("bans.ban", "rb");
if (fio != NULL)
{
fseek(fio , 0 , SEEK_END);
fsize = ftell(fio);
rewind(fio);
// minimum size for a .ban
if (fsize >= 16)
{
// using your example we want to only read the first 12 bytes
fread((void *)&core, 12, 1, fio);
// we've now filled the ban_core struct with the first 12 bytes of data.
// Hopefully it's right
// You should check the .ban version
if (core.version != LFS_BAN_VER)
{
// not the revision we were expecting. Abort.
fclose(fio);
return 1;
}
// Check the num_demo bans is > 0
if (core.num_demo > 0)
{
// Lets pretend we don't care about the bans, and fast forward to the
// number of named bans
// otherwise you'd read in all the demo bans one at a time
fseek(fio, sizeof(struct ban_demo) * core.num_demo, SEEK_CUR);
}
// Read the next 4 bytes (should be the number of named bans)
// note: I've not used sizeof(int) instead, because in most places where you'd care,
// int should be 4 bytes, but I dont want to risk it
fread((void *)&core.num_name, 4, 1, fio);
// Check the num_name bans is > 0
if (core.num_name > 0)
{
// again, you'd read in the named bans here, but I'm just seeking
fseek(fio, sizeof(struct ban_name) * core.num_name, SEEK_CUR);
}
// in theory we should be at the end of the file
if (ftell(fio) != fsize)
{
// Something went horribly wrong; some how we aren't at the end of the file
//... oo er - throw an error
fclose(fio);
return 2;
}
}
fclose(fio);
}
return 0;
}
struct core bancore;
FILE *file = NULL;
long fsize;
memset(&bancore, 0, sizeof(core));
file = fopen("data\\misc\\bans.ban", "rb");
if (file != NULL)
{
fseek(file , 0 , SEEK_END);
fsize = ftell(file);
rewind(file);
// minimum size for a ban
if (fsize >= 16)
{
// read the first 12 bytes
fread((void *)&bancore, 12, 1, file);
if (bancore.Demobans > 0)
{
fseek(file, sizeof(struct demo_ban) * bancore.Demobans, SEEK_CUR);
}
char buffer0[24];
fread((void *)&bancore.Namebans, 4, 1, file);
strcpy(buffer0,file->curp);
Memo2->Lines->Add(buffer0);
// Check the num_name bans is > 0
if (bancore.Namebans > 0)
{
fseek(file, sizeof(struct name_ban) * bancore.Namebans, SEEK_CUR);
}
}
}
fclose(file);