3 * Copyright 2008-2011 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 #include "LdifWriter.h"
8 #include "StringList.h"
9 #include "LDAPAttribute.h"
14 LdifWriter::LdifWriter( std::ostream& output, int version ) :
15 m_ldifstream(output), m_version(version), m_addSeparator(false)
21 m_ldifstream << "version: " << version << std::endl;
22 m_addSeparator = true;
24 std::ostringstream err;
25 err << "Unsuported LDIF Version";
26 throw( std::runtime_error(err.str()) );
32 void LdifWriter::writeRecord(const LDAPEntry& le)
34 std::ostringstream line;
38 m_ldifstream << std::endl;
40 m_addSeparator = true;
43 line << "dn: " << le.getDN();
44 this->breakline( line.str(), m_ldifstream );
46 const LDAPAttributeList *al = le.getAttributes();
47 LDAPAttributeList::const_iterator i = al->begin();
48 for ( ; i != al->end(); i++ )
50 StringList values = i->getValues();
51 StringList::const_iterator j = values.begin();
52 for( ; j != values.end(); j++)
54 // clear output stream
56 line << i->getName() << ": " << *j;
57 this->breakline( line.str(), m_ldifstream );
62 void LdifWriter::writeIncludeRecord( const std::string& target )
64 DEBUG(LDAP_DEBUG_TRACE, "writeIncludeRecord: " << target << std::endl);
65 std::string scheme = target.substr( 0, sizeof("file:")-1 );
69 std::ostringstream err;
70 err << "\"include\" not allowed in LDIF version 1.";
71 throw( std::runtime_error(err.str()) );
76 m_ldifstream << std::endl;
78 m_addSeparator = true;
81 m_ldifstream << "include: ";
82 if ( scheme != "file:" )
84 m_ldifstream << "file://";
87 m_ldifstream << target << std::endl;
90 void LdifWriter::breakline( const std::string &line, std::ostream &out )
92 std::string::size_type pos = 0;
93 std::string::size_type linelength = 76;
96 if ( line.length() >= linelength )
98 while ( pos < line.length() )
104 out << line.substr(pos, linelength) << std::endl;
109 linelength--; //account for the leading space
113 out << line << std::endl;