#ifndef PPSTREAM_H
#define PPSTREAM_H
#include <fstream>

class cnewline;

class newline_base {
public: 
    friend class newline;
    std::streampos nlpos_;
    newline_base  (std::ofstream& f): nlpos_ (f.tellp()) {}
    newline_base  (void): nlpos_ (0) {}
    void sync (std::ofstream& f) { nlpos_ = f.tellp(); }
};

class newline {
public:
    newline_base& base_;
    int indent_;
    int linelen_;

    explicit newline (newline_base& b, int d=0, int l=80): 
        base_(b), indent_(d), linelen_ (l) {} 
    newline (const newline& nl, int d):
        base_(nl.base_), indent_ (nl.indent_+d), linelen_ (nl.linelen_) {}
    newline (const newline& nl):
        base_(nl.base_), indent_ (nl.indent_), linelen_ (nl.linelen_) {}

    cnewline operator () (const char*);
    newline& operator += (int d) { indent_ += d; return *this; }
    newline& operator -= (int d) { indent_ -= d; return *this; }
    newline  operator +  (int d);
    newline  operator -  (int d);
};

class cnewline {
public:
    const char* sep_;
    newline& nl_;
    explicit cnewline (const char* c, newline& nl): sep_(c), nl_(nl) {}
};

std::ostream& operator << (std::ostream&, newline&);
std::ostream& operator << (std::ostream&, cnewline);
#endif
