summaryrefslogtreecommitdiffstats
path: root/contrib/ldapc++/src/LdifWriter.cpp
blob: 586cf3ba25fe1895b563f4dfb1060f1a7d7a8021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// $OpenLDAP$
/*
 * Copyright 2008-2024 The OpenLDAP Foundation, All Rights Reserved.
 * 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;
            err << "Unsupported LDIF Version";
            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;
    }
}