/* * Gnome Set game - records.c * Copyright 2007 Kirill Gorelov * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "records.h" #define MAX_RECORD_TABLES 10 typedef struct game_record_t { time_t t; int nsets; gchar *name; } game_record; typedef struct record_table_t { int size; game_record *tbl; } record_table; record_table rtbl_handles[MAX_RECORD_TABLES]; void records_table_init() { int i; for (i=0; i < MAX_RECORD_TABLES; i++) { rtbl_handles[i].size = 0; rtbl_handles[i].tbl = NULL; } } int records_table_new(int size) { int i = 0; // find an empty slot while ((i < MAX_RECORD_TABLES) && (rtbl_handles[i].tbl != NULL)) { i++; } if (i>=MAX_RECORD_TABLES) return -1; rtbl_handles[i].tbl = malloc(sizeof(game_record)*size); if (!rtbl_handles[i].tbl) return -1; rtbl_handles[i].size = size; memset(rtbl_handles[i].tbl, 0, sizeof(game_record)*size); return i; } int records_check_record(int handle, time_t t, int nsets) { if (t>0 && nsets>0 && rtbl_handles[handle].tbl) { int size = records_table_size(handle); int i=0; // find an appropriate position while (i < size) { if (!rtbl_handles[handle].tbl[i].name) break; else if (t < rtbl_handles[handle].tbl[i].t) break; else if ((t == rtbl_handles[handle].tbl[i].t) && (nsets > rtbl_handles[handle].tbl[i].nsets)) break; i++; } // ok, it's a new record, insert it into the table if (i < size) return i; } return -1; } int records_commit_record(int handle, time_t t, int nsets, const gchar *name) { int pos; if (name && ((pos = records_check_record(handle, t, nsets)) >= 0)) { int size = records_table_size(handle); int i; // free memory taken by the element being removed if (rtbl_handles[handle].tbl[size-1].name) g_free(rtbl_handles[handle].tbl[size-1].name); // move down items for (i = size - 1; i > pos; i--) rtbl_handles[handle].tbl[i] = rtbl_handles[handle].tbl[i-1]; rtbl_handles[handle].tbl[pos].t = t; rtbl_handles[handle].tbl[pos].nsets = nsets; rtbl_handles[handle].tbl[pos].name = g_strdup(name); return 1; } return 0; } int records_table_size(int handle) { if (rtbl_handles[handle].tbl) return rtbl_handles[handle].size; return 0; } int records_table_get(int handle, int pos, time_t *t, int* nsets, gchar **name) { if (rtbl_handles[handle].tbl) { if (pos >= rtbl_handles[handle].size) return 0; *t = rtbl_handles[handle].tbl[pos].t; *nsets = rtbl_handles[handle].tbl[pos].nsets; *name = g_strdup(rtbl_handles[handle].tbl[pos].name); return 1; } return 0; } void records_table_free(int handle) { if (rtbl_handles[handle].tbl) { int i; for (i=0; i < rtbl_handles[handle].size; i++) { if (rtbl_handles[handle].tbl[i].name) g_free(rtbl_handles[handle].tbl[i].name); } rtbl_handles[handle].size = 0; free(rtbl_handles[handle].tbl); } } void records_table_destoy() { // release all record table }