nis-util  1.0.D108
lib/output/file.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or (at
00008 // your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License along
00016 // with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include <lib/ac/assert.h>
00020 #include <libexplain/fclose.h>
00021 #include <libexplain/fflush.h>
00022 #include <libexplain/fopen.h>
00023 #include <libexplain/putc.h>
00024 
00025 #include <lib/output/file.h>
00026 
00027 
00028 output_file::~output_file()
00029 {
00030     assert(fp);
00031     if (fp == stdout)
00032         explain_fflush_or_die(fp);
00033     else
00034         explain_fclose_or_die(fp);
00035     fp = 0;
00036 }
00037 
00038 
00039 static bool
00040 isdflt(const rcstring &filename)
00041 {
00042     return (filename.empty() || filename == "-");
00043 }
00044 
00045 
00046 output_file::output_file(const rcstring &filename) :
00047     fp(isdflt(filename) ? stdout : explain_fopen_or_die(filename.c_str(), "w")),
00048     column(0)
00049 {
00050 }
00051 
00052 
00053 output_file::pointer
00054 output_file::create(const rcstring &filename)
00055 {
00056     return pointer(new output_file(filename));
00057 }
00058 
00059 
00060 void
00061 output_file::put(char c)
00062 {
00063     explain_putc_or_die(c, fp);
00064     switch (c)
00065     {
00066     case '\n':
00067         column = 0;
00068         break;
00069 
00070     case '\t':
00071         column = (column + 8) & ~7;
00072         break;
00073 
00074     default:
00075         ++column;
00076         break;
00077     }
00078 }
00079 
00080 
00081 int
00082 output_file::get_column(void)
00083     const
00084 {
00085     return column;
00086 }
00087 
00088 
00089 int
00090 output_file::get_line_width(void)
00091     const
00092 {
00093     return 80;
00094 }
00095 
00096 
00097 // vim: set ts=8 sw=4 et :