]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LdifWriter.cpp
a05df229a6ce2ee7ac2f4fe1d5db51b173b50234
[openldap] / contrib / ldapc++ / src / LdifWriter.cpp
1 // $OpenLDAP$
2 /*
3  * Copyright 2008-2013 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "LdifWriter.h"
8 #include "StringList.h"
9 #include "LDAPAttribute.h"
10 #include "debug.h"
11 #include <sstream>
12 #include <stdexcept>
13
14 LdifWriter::LdifWriter( std::ostream& output, int version ) :
15         m_ldifstream(output), m_version(version), m_addSeparator(false)
16 {
17     if ( version )
18     {
19         if ( version == 1 )
20         {
21             m_ldifstream << "version: " << version << std::endl;
22             m_addSeparator = true;
23         } else {
24             std::ostringstream err;
25             err << "Unsuported LDIF Version";
26             throw( std::runtime_error(err.str()) );
27         }
28     }
29     
30 }
31
32 void LdifWriter::writeRecord(const LDAPEntry& le)
33 {
34     std::ostringstream line;
35
36     if ( m_addSeparator )
37     {
38         m_ldifstream << std::endl;
39     } else {
40         m_addSeparator = true;
41     }
42
43     line << "dn: " << le.getDN();
44     this->breakline( line.str(), m_ldifstream );
45
46     const LDAPAttributeList *al = le.getAttributes();
47     LDAPAttributeList::const_iterator i = al->begin();
48     for ( ; i != al->end(); i++ )
49     {
50         StringList values = i->getValues();
51         StringList::const_iterator j = values.begin();
52         for( ; j != values.end(); j++)
53         {
54             // clear output stream
55             line.str("");
56             line << i->getName() << ": " << *j;
57             this->breakline( line.str(), m_ldifstream );
58         }
59     }
60 }
61
62 void LdifWriter::writeIncludeRecord( const std::string& target )
63 {
64     DEBUG(LDAP_DEBUG_TRACE, "writeIncludeRecord: " << target << std::endl);
65     std::string scheme = target.substr( 0, sizeof("file:")-1 );
66     
67     if ( m_version == 1 )
68     {
69         std::ostringstream err;
70         err << "\"include\" not allowed in LDIF version 1.";
71         throw( std::runtime_error(err.str()) );
72     }
73     
74     if ( m_addSeparator )
75     {
76         m_ldifstream << std::endl;
77     } else {
78         m_addSeparator = true;
79     }
80
81     m_ldifstream << "include: ";
82     if ( scheme != "file:" )
83     {
84         m_ldifstream << "file://";
85     }
86
87     m_ldifstream << target << std::endl;
88 }
89
90 void LdifWriter::breakline( const std::string &line, std::ostream &out )
91 {
92     std::string::size_type pos = 0;
93     std::string::size_type linelength = 76;
94     bool first = true;
95     
96     if ( line.length() >= linelength )
97     {
98         while ( pos < line.length() )
99         {
100             if (! first )
101             {
102                 out << " ";
103             }
104             out << line.substr(pos, linelength) << std::endl;
105             pos += linelength;
106             if ( first )
107             {
108                 first = false;
109                 linelength--; //account for the leading space
110             }
111         }
112     } else {
113         out << line << std::endl;
114     }
115 }
116