nis-util  1.0.D108
lib/input/string.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/string.h>
00020 
00021 #include <lib/input/null.h>
00022 #include <lib/input/string.h>
00023 
00024 
00025 input_string::~input_string()
00026 {
00027 }
00028 
00029 
00030 input_string::input_string(const rcstring &a_filename, const rcstring &a_text) :
00031     filename(a_filename),
00032     line_number(1),
00033     text(a_text),
00034     tpos(0)
00035 {
00036 }
00037 
00038 
00039 input::pointer
00040 input_string::create(const rcstring &a_filename, const rcstring &a_text)
00041 {
00042     if (a_text.empty())
00043         return input_null::create();
00044     return pointer(new input_string(a_filename, a_text));
00045 }
00046 
00047 
00048 input_string::pointer
00049 input_string::create(const rcstring &a_filename, const char *a_text)
00050 {
00051     if (!a_text || !*a_text)
00052         return input_null::create();
00053     return pointer(new input_string(a_filename, a_text));
00054 }
00055 
00056 
00057 input_string::pointer
00058 input_string::create_env(const rcstring &environment_variable_name)
00059 {
00060     if (environment_variable_name.empty())
00061         return input_null::create();
00062     const char *a_text = getenv(environment_variable_name.c_str());
00063     if (!a_text || !*a_text)
00064         return input_null::create();
00065     rcstring a_filename = "$" + environment_variable_name;
00066     return create(a_filename, a_text);
00067 }
00068 
00069 
00070 size_t
00071 input_string::read_inner(source_location &data_locn, void *data,
00072     size_t data_size)
00073 {
00074     data_locn = source_location(filename, line_number);
00075     if (tpos >= text.size())
00076         return 0;
00077     size_t nbytes = text.size() - tpos;
00078     if (nbytes > data_size)
00079         nbytes = data_size;
00080     memcpy(data, text.c_str() + tpos, nbytes);
00081     tpos += nbytes;
00082     line_number += count_newlines(data, nbytes);
00083     return nbytes;
00084 }
00085 
00086 
00087 // vim: set ts=8 sw=4 et :