/* * xlog - GTK+ logging program for amateur radio operators * Copyright (C) 2001-2003 Joop Stakenborg * * This program is free oftware; 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 Library 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. */ /* * trlog.c - support for TRLOG(TLF) flat file format */ #include #include #include #include #include "logfile.h" #include "types.h" /* * file fields */ const gint trlog_fields[] = { BAND, MODE, DATE, GMT, RST /* exch */, CALL, RST, MYRST, MYRST /* exch */ }; const gint trlog_widths[] = { 2, 3, 9, 5, 5, 14, 4, 4, 4 }; const gint trlog_field_nr = 9; static gint trlog_open (LOGDB *); static void trlog_close (LOGDB *); static gint trlog_create (LOGDB *); static gint trlog_qso_append (LOGDB *, const qso_t *); static gint trlog_qso_foreach (LOGDB *, gint (*fn) (LOGDB *, qso_t *, gpointer arg), gpointer arg); const struct log_ops trlog_ops = { .open = trlog_open, .close = trlog_close, .create = trlog_create, .qso_append = trlog_qso_append, .qso_foreach = trlog_qso_foreach, .type = TYPE_TRLOG, .name = "TRLog", .extension = ".log", }; /* * open for read */ gint trlog_open (LOGDB * handle) { FILE *fp; fp = fopen (handle->path, "r"); if (!fp) return -1; handle->priv = (gpointer) fp; handle->column_nr = trlog_field_nr; memcpy (handle->column_fields, trlog_fields, sizeof (trlog_fields)); memcpy (handle->column_widths, trlog_widths, sizeof (trlog_widths)); return 0; } /* * open for write */ gint trlog_create (LOGDB * handle) { FILE *fp; fp = fopen (handle->path, "w"); if (!fp) return -1; handle->priv = (gpointer) fp; return 0; } void trlog_close (LOGDB * handle) { FILE *fp = (FILE *) handle->priv; fclose (fp); } /* * The save method is unsupported and fully broken, * esp. the exchanges */ gint trlog_qso_append (LOGDB * handle, const qso_t * q) { FILE *fp = (FILE *) handle->priv; gchar *exch = "", *rxexch = ""; gchar gmt[6]; if (q[GMT][2] == ':') strcpy(gmt, q[GMT]); else { gmt[0] = q[GMT][0]; gmt[1] = q[GMT][1]; gmt[2] = ':'; gmt[3] = q[GMT][2]; gmt[4] = q[GMT][3]; gmt[5] = '\0'; } // 10SSB 30-Mar-03 16:51 0059 P3A 59 59 3264 3 fprintf (fp, " %2s%3s %9s %5s %-4s %-15s%-5s%-5s%-7s\n", q[BAND], q[MODE], q[DATE], gmt, exch, q[CALL], q[RST], q[MYRST], rxexch); return 0; } #define MAXROWLEN 100 gint trlog_qso_foreach (LOGDB * handle, gint (*fn) (LOGDB *, qso_t *, gpointer arg), gpointer arg) { FILE *fp = (FILE *) handle->priv; gint i, ret; qso_t q[QSO_FIELDS]; gchar *field, *end, buffer[MAXROWLEN]; const gint *widths = trlog_widths; while (!feof (fp)) { if (!fgets (buffer, MAXROWLEN - 1, fp)) break; memset (q, 0, sizeof (q)); field = buffer; /* comment line */ if (buffer[0] == ';') continue; /* insert a space between band and mode * FIXME: VHF and upper bands? */ memmove(buffer, buffer+1, 2); buffer[2] = ' '; for (i = 0; i < trlog_field_nr; i++) { // 10SSB 30-Mar-03 16:51 0059 P3A 59 59 3264 3 end = field + widths[i]; *end = '\0'; /* remove the ':' in the GMT field */ if (trlog_fields[i] == GMT && field[2] == ':') { memmove(field+2, field+3, 2); buffer[4] = ' '; } /* fifth and nineth field are EXCH, MYEXCH */ if (!(i == 4 || i == 8)) q[trlog_fields[i]] = g_strdup (g_strstrip (field)); field = end + 1; } ret = (*fn) (handle, q, arg); if (ret) return ret; } return 0; }