/* * AOPPA -- A program for extracting the AnarchyOnline resource database * MMAP input plugin. * * Copyright (C) 2003 Oskari Saarenmaa . * All rights reserved. * * $Id: input-mmap.cc,v 1.3 2003/12/16 06:55:41 pickett Exp $ * * 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 * */ #include #include #include #include #include #include #include "aoppa.h" #include "misc.h" #include "parser.h" int parse_file(int patch, int type, const char *fn) { FILE *fp; int cnt_all = 0, cnt_new = 0; struct stat st; time_t tstart, tend, telap; size_t size; void *mp; unsigned char *data; fp = fopen(fn, "r"); if(fp == NULL) { debugf(DEBUG_SERIOUS, "Failed to open %s for reading (%s)\n", fn, strerror(errno)); exit(1); } fstat(fileno(fp), &st); size = st.st_size; mp = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(fp), 0); data = (unsigned char*)mp; tstart = time(NULL); debugf(1, "Reading records from %s (%d, %d)\n", fn, patch, type); while((size_t)data-(size_t)mp < size) { size_t len; int aoid; if(memcmp(data, "AOID: ", 6) != 0) break; aoid = strtol((char*)(data+=6), (char**)&data, 10); len = strtoul((char*)(data+=5), (char**)&data, 10); data += 12; /* skip ahead to our data chunk */ cnt_new += handle_item(patch, type, aoid, len, data); data += len+12; cnt_all ++; } debugf(1, "Ok, read total %d records, %d were new / modified\n", cnt_all, cnt_new); munmap(mp, size); fclose(fp); tend = time(NULL); telap = tend-tstart; if(telap < 1) telap = 1; debugf(1, "%d seconds elapsed, %d (%d/s) records processed\n", telap, cnt_all, cnt_all/telap); return cnt_new; } aoppa_plugin_t aoppa_plugin = { AOPPA_API_VERSION, AOPPA_INPUT, NULL, NULL, NULL, NULL, parse_file, "MMAP input plugin" };