/[xlog]/xlog/src/logfile/flog.c
ViewVC logotype

Diff of /xlog/src/logfile/flog.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by pa4tu, Sat Oct 19 17:51:39 2002 UTC revision 1.2 by pa4tu, Thu Nov 21 19:57:46 2002 UTC
# Line 28  Line 28 
28  #include "types.h"  #include "types.h"
29    
30    
31  static gint flog_open(LOGDB*);  static gint flog_open (LOGDB *);
32  static void flog_close(LOGDB*);  static void flog_close (LOGDB *);
33  static gint flog_create(LOGDB*);  static gint flog_create (LOGDB *);
34  static gint flog_qso_append(LOGDB*, const qso_t*);  static gint flog_qso_append (LOGDB *, const qso_t *);
35  static gint flog_qso_foreach(LOGDB*, gint (*fn)(LOGDB*, qso_t*, gpointer arg), gpointer arg);  static gint flog_qso_foreach (LOGDB *,
36                                  gint (*fn) (LOGDB *, qso_t *, gpointer arg),
37                                  gpointer arg);
38    
39    
40  const struct log_ops flog_ops = {  const struct log_ops flog_ops = {
41  open:                   flog_open,    open:flog_open,
42  close:                  flog_close,    close:flog_close,
43  create:                 flog_create,    create:flog_create,
44  qso_append:             flog_qso_append,    qso_append:flog_qso_append,
45  qso_foreach:    flog_qso_foreach,    qso_foreach:flog_qso_foreach,
46  type:                   TYPE_FLOG,    type:TYPE_FLOG,
47  name:                   "Flog",    name:"Flog",
48  extension:              ".xlog",    extension:".xlog",
49  };  };
50    
51  /* open for read */  /* open for read */
52  gint flog_open(LOGDB *handle)  gint
53    flog_open (LOGDB * handle)
54  {  {
55                  FILE *fp;    FILE *fp;
56                  gint column_nr = 0;    gint column_nr = 0;
57                  gchar *p, *field_name;    gchar *p, *field_name;
58                  gint field_width = 0;    gint field_width = 0;
59                  gint in_space_state = 0;    gint in_space_state = 0;
60                  gchar header_line[FLOG_MAX_LINE_SIZE];    gchar header_line[FLOG_MAX_LINE_SIZE];
61    
62                  fp = fopen(handle->path, "r");    fp = fopen (handle->path, "r");
63                  if (!fp)    if (!fp)
64                          return -1;      return -1;
65                  handle->priv = (gpointer)fp;    handle->priv = (gpointer) fp;
66    
67    
68                  /* read header line    /* read header line
69                   * TODO: handle error cases     * TODO: handle error cases
70                   */     */
71                  fgets(header_line, FLOG_MAX_LINE_SIZE, fp);    fgets (header_line, FLOG_MAX_LINE_SIZE, fp);
72                  field_name = header_line;    field_name = header_line;
73    
74                  for (p = header_line; *p; p++) {    for (p = header_line; *p; p++)
75                          field_width++;      {
76                          if (in_space_state) {        field_width++;
77                                          if (*p != ' ') {        if (in_space_state)
78                                                          /* start of a new column */          {
79                                                          in_space_state = 0;            if (*p != ' ')
80                {
81                                                          /*                /* start of a new column */
82                                                           * add previous column,                in_space_state = 0;
83                                                           * now we know the column width  
84                                                           */                /*
85                                                          handle->column_fields[column_nr] =                 * add previous column,
86                                                                          parse_field_name(field_name);                 * now we know the column width
87                                                          handle->column_widths[column_nr] = field_width;                 */
88                                                          column_nr++;                handle->column_fields[column_nr] =
89                    parse_field_name (field_name);
90                                                          field_width = 1;                handle->column_widths[column_nr] = field_width;
91                                                          field_name = p;                column_nr++;
92                                                  }  
93                          } else  if (*p == ' ') {                field_width = 1;
94                                                  /* end of field name */                field_name = p;
95                                                  in_space_state = 1;              }
96                                                  *p = '\0';          }
97                          }        else if (*p == ' ')
98                          /* last field */          {
99                          if (*p == '\n') {            /* end of field name */
100                                          *p = '\0';            in_space_state = 1;
101                          }            *p = '\0';
102                  }          }
103                  handle->column_fields[column_nr] = parse_field_name(field_name);        /* last field */
104                  handle->column_widths[column_nr] = MAX_VARIABLE_LEN;        if (*p == '\n')
105                  handle->column_nr = column_nr+1;          {
106              *p = '\0';
107            }
108        }
109      handle->column_fields[column_nr] = parse_field_name (field_name);
110      handle->column_widths[column_nr] = MAX_VARIABLE_LEN;
111      handle->column_nr = column_nr + 1;
112    
113                  return 0;    return 0;
114  }  }
115    
116  /* create and open for read */  /* create and open for read */
117  gint flog_create(LOGDB *handle)  gint
118    flog_create (LOGDB * handle)
119  {  {
120                  FILE *fp;    FILE *fp;
121                  gint i;    gint i;
122    
123                  fp = fopen(handle->path, "w");    fp = fopen (handle->path, "w");
124                  if (!fp)    if (!fp)
125                                  return -1;      return -1;
126    
127                  handle->priv = (gpointer)fp;    handle->priv = (gpointer) fp;
128    
129                  /* write header line */    /* write header line */
130                  for (i = 0; i<handle->column_nr-1; i++) {    for (i = 0; i < handle->column_nr - 1; i++)
131                          fprintf(fp, "%-*s", handle->column_widths[i],      {
132                                                          strfield(handle->column_fields[i]));        fprintf (fp, "%-*s", handle->column_widths[i],
133                  }                 strfield (handle->column_fields[i]));
134                  /* last field can be variable, omit trailing spaces */      }
135                  fprintf(fp, "%s\n",    /* last field can be variable, omit trailing spaces */
136                                                  strfield(handle->column_fields[handle->column_nr-1]));    fprintf (fp, "%s\n",
137               strfield (handle->column_fields[handle->column_nr - 1]));
138    
139                  return 0;    return 0;
140  }  }
141    
142  void flog_close(LOGDB *handle)  void
143    flog_close (LOGDB * handle)
144  {  {
145                  FILE *fp = (FILE*)handle->priv;    FILE *fp = (FILE *) handle->priv;
146                  fclose(fp);    fclose (fp);
147  }  }
148    
149  gint flog_qso_append(LOGDB *handle, const qso_t *q)  gint
150    flog_qso_append (LOGDB * handle, const qso_t * q)
151  {  {
152                  FILE *fp = (gpointer)handle->priv;    FILE *fp = (gpointer) handle->priv;
153                  gint i;    gint i;
154    
155                  for (i = 0; i < handle->column_nr-1; i++) {    for (i = 0; i < handle->column_nr - 1; i++)
156                          gint field = handle->column_fields[i];      {
157                          fprintf(fp, "%-*s", handle->column_widths[i],        gint field = handle->column_fields[i];
158                                                          q[field]);        fprintf (fp, "%-*s", handle->column_widths[i], q[field]);
159                  }      }
160                  /* last field is variable length, omit trailing spaces */    /* last field is variable length, omit trailing spaces */
161                  fprintf(fp, "%s\n", q[handle->column_fields[handle->column_nr-1]]);    fprintf (fp, "%s\n", q[handle->column_fields[handle->column_nr - 1]]);
162    
163                  return 0;    return 0;
164  }  }
165    
166  gint flog_qso_foreach(LOGDB *handle, gint (*fn)(LOGDB*, qso_t*, gpointer arg), gpointer arg)  gint
167    flog_qso_foreach (LOGDB * handle, gint (*fn) (LOGDB *, qso_t *, gpointer arg),
168                      gpointer arg)
169  {  {
170                  FILE *fp = (gpointer)handle->priv;    FILE *fp = (gpointer) handle->priv;
171                  gint i, width, ret;    gint i, width, ret;
172                  qso_t q[QSO_FIELDS];    qso_t q[QSO_FIELDS];
173                  gchar buffer[FLOG_MAX_LINE_SIZE];    gchar buffer[FLOG_MAX_LINE_SIZE];
174    
175    
176                  while (!feof(fp)) {    while (!feof (fp))
177                          memset(q, 0, sizeof(q));      {
178          memset (q, 0, sizeof (q));
179                          for (i = 0; i < handle->column_nr-1; i++)  
180                          {        for (i = 0; i < handle->column_nr - 1; i++)
181                                  width = handle->column_widths[i];          {
182                                  fgets(buffer, width, fp);            width = handle->column_widths[i];
183                                  if (strlen(buffer) != width-1 && i < handle->column_nr-2)            fgets (buffer, width, fp);
184                                          return 0;       /* broken file */            if (strlen (buffer) != width - 1 && i < handle->column_nr - 2)
185                                  q[handle->column_fields[i]] = g_strdup(g_strstrip(buffer));              return 0;           /* broken file */
186                          }            q[handle->column_fields[i]] = g_strdup (g_strstrip (buffer));
187                          /* last field */          }
188                          fgets(buffer, MAX_VARIABLE_LEN, fp);        /* last field */
189                          width = strlen(buffer);        fgets (buffer, MAX_VARIABLE_LEN, fp);
190                          /* chop off EOL */        width = strlen (buffer);
191                          if (width > 0 && buffer[width-1] == '\n')        /* chop off EOL */
192                                          buffer[width-1] = '\0';        if (width > 0 && buffer[width - 1] == '\n')
193                          q[handle->column_fields[handle->column_nr-1]] =          buffer[width - 1] = '\0';
194                                          g_strdup(g_strstrip(buffer));        q[handle->column_fields[handle->column_nr - 1]] =
195            g_strdup (g_strstrip (buffer));
196                          /*  
197                           * populate fields not present in log file        /*
198                           */         * populate fields not present in log file
199                          for (i = 0; i < QSO_FIELDS; i++)         */
200                                          if (!q[i])        for (i = 0; i < QSO_FIELDS; i++)
201                                                          q[i] = g_strdup("");          if (!q[i])
202              q[i] = g_strdup ("");
203                          ret = (*fn)(handle, q, arg);  
204                          if (ret)        ret = (*fn) (handle, q, arg);
205                                          return ret;        if (ret)
206                  }          return ret;
207                  return 0;      }
208      return 0;
209  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26