nis-util  1.0.D108
lib/input/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/fcntl.h>
00020 #include <libexplain/close.h>
00021 #include <libexplain/open.h>
00022 #include <libexplain/read.h>
00023 
00024 #include <lib/input/file.h>
00025 #include <lib/input/stdin.h>
00026 
00027 
00028 input_file::~input_file()
00029 {
00030     if (fd >= 0)
00031     {
00032         if (fd != 0)
00033             explain_close_or_die(fd);
00034         fd = -1;
00035     }
00036 }
00037 
00038 
00039 input_file::input_file(const rcstring &a_filename) :
00040     filename(a_filename),
00041     line_number(1),
00042     fd(explain_open_or_die(a_filename.c_str(), O_RDONLY, 0))
00043 {
00044     assert(fd >= 0);
00045 }
00046 
00047 
00048 input::pointer
00049 input_file::create(const rcstring &filename)
00050 {
00051     if (input_stdin::candidate(filename))
00052         return input_stdin::create();
00053     return pointer(new input_file(filename));
00054 }
00055 
00056 
00057 size_t
00058 input_file::read_inner(source_location &data_locn, void *data, size_t data_size)
00059 {
00060     if (fd < 0)
00061         return 0;
00062     data_locn = source_location(filename, line_number);
00063     size_t nbytes = explain_read_or_die(fd, data, data_size);
00064     line_number += count_newlines(data, nbytes);
00065     return nbytes;
00066 }
00067 
00068 
00069 // vim: set ts=8 sw=4 et :