ldifio.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       ldifio.cc
00003 ///             Storage, parser, and builder classes for ldif operations.
00004 ///
00005 
00006 /*
00007     Copyright (C) 2010-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "ldifio.h"
00023 
00024 namespace Barry {
00025 
00026 LdifStore::LdifStore(const std::string &filename)
00027         : m_ifs( new std::ifstream(filename.c_str()) )
00028         , m_is(*m_ifs)
00029         , m_os(*m_ofs)  // yes, this is a reference to a null ptr
00030                         // but will never be used (see below as well)
00031         , m_end_of_file(false)
00032         , m_ldif("")
00033 {
00034 }
00035 
00036 LdifStore::LdifStore(std::istream &is)
00037         : m_is(is)
00038         , m_os(*m_ofs)
00039         , m_end_of_file(false)
00040         , m_ldif("")
00041 {
00042 }
00043 
00044 // output constructors
00045 LdifStore::LdifStore(const std::string &filename,
00046                 const std::string &baseDN,
00047                 const std::string &dnattr)
00048         : m_ofs( new std::ofstream(filename.c_str()) )
00049         , m_is(*m_ifs)
00050         , m_os(*m_ofs)
00051         , m_end_of_file(false)
00052         , m_ldif(baseDN)
00053 {
00054         m_ldif.SetDNAttr(dnattr);
00055 }
00056 
00057 LdifStore::LdifStore(std::ostream &os,
00058                 const std::string &baseDN,
00059                 const std::string &dnattr)
00060         : m_is(*m_ifs)
00061         , m_os(os)
00062         , m_end_of_file(false)
00063         , m_ldif(baseDN)
00064 {
00065         m_ldif.SetDNAttr(dnattr);
00066 }
00067 
00068 // storage operator
00069 void LdifStore::operator() (const Contact &rec)
00070 {
00071         m_ldif.DumpLdif(m_os, rec);
00072 }
00073 
00074 // retrieval operator
00075 bool LdifStore::operator() (Contact &rec, const Barry::Builder &builder)
00076 {
00077         if( m_end_of_file )
00078                 return false;
00079 
00080         // there may be LDIF records in the input that generate
00081         // invalid Contact records, but valid Contact records
00082         // may come after.. so keep processing until end of stream
00083         while( m_is ) {
00084                 if( m_ldif.ReadLdif(m_is, rec) )
00085                         return true;
00086         }
00087 
00088         m_end_of_file = true;
00089         return false;
00090 }
00091 
00092 } // namespace Barry
00093