openldap/contrib/ldapc++/src/LdifWriter.cpp

117 lines
2.8 KiB
C++
Raw Normal View History

2008-03-28 07:05:10 -04:00
// $OpenLDAP$
2008-03-27 13:02:37 -04:00
/*
2021-01-11 14:25:53 -05:00
* Copyright 2008-2021 The OpenLDAP Foundation, All Rights Reserved.
2008-03-27 13:02:37 -04:00
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "LdifWriter.h"
#include "StringList.h"
#include "LDAPAttribute.h"
#include "debug.h"
#include <sstream>
#include <stdexcept>
LdifWriter::LdifWriter( std::ostream& output, int version ) :
m_ldifstream(output), m_version(version), m_addSeparator(false)
{
if ( version )
{
if ( version == 1 )
{
m_ldifstream << "version: " << version << std::endl;
m_addSeparator = true;
} else {
std::ostringstream err;
ITS#8605 - spelling fixes * javascript * kernel * ldap * length * macros * maintained * manager * matching * maximum * mechanism * memory * method * mimic * minimum * modifiable * modifiers * modifying * multiple * necessary * normalized * objectclass * occurrence * occurring * offered * operation * original * overridden * parameter * permanent * preemptively * printable * protocol * provider * really * redistribution * referenced * refresh * regardless * registered * request * reserved * resource * response * sanity * separated * setconcurrency * should * specially * specifies * structure * structures * subordinates * substitution * succeed * successful * successfully * sudoers * sufficient * superiors * supported * synchronization * terminated * they're * through * traffic * transparent * unsigned * unsupported * version * absence * achieves * adamson * additional * address * against * appropriate * architecture * associated * async * attribute * authentication * authorized * auxiliary * available * begin * beginning * buffered * canonical * certificate * charray * check * class * compatibility * compilation * component * configurable * configuration * configure * conjunction * constraints * constructor * contained * containing * continued * control * convenience * correspond * credentials * cyrillic * database * definitions * deloldrdn * dereferencing * destroy * distinguish * documentation * emmanuel * enabled * entry * enumerated * everything * exhaustive * existence * existing * explicitly * extract * fallthru * fashion * february * finally * function * generically * groupname * happened * implementation * including * initialization * initializes * insensitive * instantiated * instantiation * integral * internal * iterate
2017-02-26 02:49:31 -05:00
err << "Unsupported LDIF Version";
2008-03-27 13:02:37 -04:00
throw( std::runtime_error(err.str()) );
}
}
}
void LdifWriter::writeRecord(const LDAPEntry& le)
{
std::ostringstream line;
if ( m_addSeparator )
{
m_ldifstream << std::endl;
} else {
m_addSeparator = true;
}
line << "dn: " << le.getDN();
this->breakline( line.str(), m_ldifstream );
const LDAPAttributeList *al = le.getAttributes();
LDAPAttributeList::const_iterator i = al->begin();
for ( ; i != al->end(); i++ )
{
StringList values = i->getValues();
StringList::const_iterator j = values.begin();
for( ; j != values.end(); j++)
{
// clear output stream
line.str("");
line << i->getName() << ": " << *j;
this->breakline( line.str(), m_ldifstream );
}
}
}
void LdifWriter::writeIncludeRecord( const std::string& target )
{
DEBUG(LDAP_DEBUG_TRACE, "writeIncludeRecord: " << target << std::endl);
std::string scheme = target.substr( 0, sizeof("file:")-1 );
if ( m_version == 1 )
{
std::ostringstream err;
err << "\"include\" not allowed in LDIF version 1.";
throw( std::runtime_error(err.str()) );
}
if ( m_addSeparator )
{
m_ldifstream << std::endl;
} else {
m_addSeparator = true;
}
m_ldifstream << "include: ";
if ( scheme != "file:" )
{
m_ldifstream << "file://";
}
m_ldifstream << target << std::endl;
}
void LdifWriter::breakline( const std::string &line, std::ostream &out )
{
std::string::size_type pos = 0;
std::string::size_type linelength = 76;
bool first = true;
if ( line.length() >= linelength )
{
while ( pos < line.length() )
{
if (! first )
{
out << " ";
}
out << line.substr(pos, linelength) << std::endl;
pos += linelength;
if ( first )
{
first = false;
linelength--; //account for the leading space
}
}
} else {
out << line << std::endl;
}
}