#include "indent.h"

newline_base::newline_base (std::ostream& f) {
    std::ofstream* ff = dynamic_cast <std::ofstream*>(&f);
    if (ff) nlpos_ = ff->tellp();
}

void newline_base::sync (std::ostream& f) {
    std::ofstream* ff = dynamic_cast <std::ofstream*>(&f);
    if (ff) nlpos_ = ff->tellp();
}

cnewline newline :: operator () (const char* c) {
    return cnewline (c, *this);
}
newline  newline :: operator +  (int d) { 
    newline nn (*this, d);
    return nn; 
}
newline  newline :: operator -  (int d) { 
    newline nn (*this, -d); 
    return nn; 
}
std::ostream& operator << (std::ostream& f, newline& nl) {
    int i;
    f << "\n";
    std::ofstream* ff = dynamic_cast <std::ofstream*>(&f);
    if (ff) nl.base_.nlpos_ = ff->tellp();
    for (i=0; i < nl.indent_/8; i++) f << "\t";
    for (i=0; i < nl.indent_%8; i++)  f << " ";
    return f;
}
std::ostream& operator << (std::ostream& f, cnewline cnl) {
    f << cnl.sep_;
    std::ofstream* ff = dynamic_cast<std::ofstream*>(&f);
    if (ff && ff->tellp() - cnl.nl_.base_.nlpos_ > cnl.nl_.linelen_) {
	f << cnl.nl_;
    }
    return f;
}
