libexplain  1.4.D001
libexplain/output/file.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as
00008  * published by the Free Software Foundation; either version 3 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/stdio.h>
00021 
00022 #include <libexplain/output.h>
00023 
00024 
00025 typedef struct explain_output_file_t explain_output_file_t;
00026 struct explain_output_file_t
00027 {
00028     explain_output_t inherited;
00029     FILE            *fp;
00030 };
00031 
00032 
00033 static void
00034 destructor(explain_output_t *op)
00035 {
00036     explain_output_file_t *p;
00037 
00038     p = (explain_output_file_t *)op;
00039     fclose(p->fp);
00040 }
00041 
00042 
00043 static void
00044 message(explain_output_t *op, const char *text)
00045 {
00046     explain_output_file_t *p;
00047 
00048     p = (explain_output_file_t *)op;
00049     fprintf(p->fp, "%s\n", text);
00050 }
00051 
00052 
00053 static const explain_output_vtable_t vtable =
00054 {
00055     destructor,
00056     message,
00057     0, /* exit */
00058     sizeof(explain_output_file_t)
00059 };
00060 
00061 
00062 explain_output_t *
00063 explain_output_file_new(const char *filename, int append)
00064 {
00065     explain_output_t *result;
00066 
00067     result = explain_output_new(&vtable);
00068     if (result)
00069     {
00070         const char      *flags;
00071         explain_output_file_t *p;
00072 
00073         p = (explain_output_file_t *)result;
00074         flags = (append ? "wa" : "w");
00075         p->fp = fopen(filename, flags);
00076         if (!p->fp)
00077             p->fp = stderr;
00078 #ifdef HAVE_SETLINEBUF
00079         setlinebuf(p->fp);
00080 #endif
00081     }
00082     return result;
00083 }
00084 
00085 
00086 /* vim: set ts=8 sw=4 et : */