Example #1
0
void JKG_Bans_SaveBans()
{
	cJSONStream *stream;
	const char *buffer;
	fileHandle_t f;
	banentry_t *entry;
	unsigned int curr = time(NULL);
	
	stream = cJSON_Stream_New(8, 1, 0, 0);
	cJSON_Stream_BeginObject(stream, NULL); // Root object
	cJSON_Stream_WriteInteger(stream, "nextid", nextBanId);
	cJSON_Stream_BeginArray(stream, "bans"); // Start bans array
	for(entry = bans; entry; entry = entry->next) {
		if (entry->expireTime && curr >= entry->expireTime) {
			continue;	// Don't save expired bans
		}
		cJSON_Stream_BeginObject(stream, NULL); // Ban entry
		cJSON_Stream_WriteInteger(stream, "id", entry->id );
		cJSON_Stream_WriteInteger(stream, "mask", entry->mask );
		cJSON_Stream_WriteInteger(stream, "expire", entry->expireTime);
		cJSON_Stream_WriteString(stream, "reason", entry->banreason);
		
		cJSON_Stream_BeginArray(stream, "ip"); // IP
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[0]);
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[1]);
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[2]);
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[3]);
		cJSON_Stream_EndBlock(stream);

		cJSON_Stream_EndBlock(stream); // Ban entry
	}
	cJSON_Stream_EndBlock(stream);	// Bans array
	cJSON_Stream_EndBlock(stream);	// Root object
	buffer = cJSON_Stream_Finalize(stream);

	trap_FS_FOpenFile(g_banfile.string, &f, FS_WRITE);
	trap_FS_Write(buffer, strlen(buffer), f);
	trap_FS_FCloseFile(f);

	free((void *)buffer);
}
static int GLua_JSON_BeginArray( lua_State *L )
{
	const char *arg = luaL_checkstring(L, 1);
	if( !arg ) return 0;
	bool firstArgNull = false;

	if( Q_stricmp(arg, "0") == 0 )
	{
		firstArgNull = true;
	}

	cJSON_Stream_BeginArray( fJSON.streamHandle, firstArgNull ? NULL : arg );
	return 0;
}