nis-util  1.0.D108
lib/input/remove_escaped_newlines.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/input/remove_escaped_newlines.h>
00020 
00021 
00022 input_remove_escaped_newlines::~input_remove_escaped_newlines()
00023 {
00024 }
00025 
00026 
00027 input_remove_escaped_newlines::input_remove_escaped_newlines(
00028     const input::pointer &a_deeper
00029 ) :
00030     deeper(a_deeper)
00031 {
00032 }
00033 
00034 
00035 input_remove_escaped_newlines::pointer
00036 input_remove_escaped_newlines::create(const input::pointer &a_deeper)
00037 {
00038     return pointer(new input_remove_escaped_newlines(a_deeper));
00039 }
00040 
00041 
00042 size_t
00043 input_remove_escaped_newlines::read_inner(source_location &data_locn,
00044     void *vdata, size_t data_size)
00045 {
00046     data_locn = deeper->get_source_location();
00047     unsigned char *data_begin = (unsigned char *)vdata;
00048     unsigned char *data = data_begin;
00049     unsigned char *data_end = data + data_size;
00050     while (data < data_end)
00051     {
00052         int c = deeper->get();
00053         if (c == input::eof)
00054             break;
00055         if (c == '\\')
00056         {
00057             int c2 = deeper->get();
00058             if (c2 == '\n')
00059             {
00060                 // The line number count has discontinuities for each
00061                 // escaped newline, so stop here.
00062                 break;
00063             }
00064             deeper->unget(c2);
00065         }
00066         *data++ = c;
00067         if (c == '\n')
00068             break;
00069     }
00070     return (data - data_begin);
00071 }
00072 
00073 
00074 // vim: set ts=8 sw=4 et :