#include "Riostream.h"
#include "TVirtualPS.h"
TVirtualPS *gVirtualPS = 0;
const Int_t kMaxBuffer = 250;
ClassImp(TVirtualPS)
TVirtualPS::TVirtualPS()
{
fStream = 0;
fNByte = 0;
fSizBuffer = kMaxBuffer;
fBuffer = new char[fSizBuffer+1];
fLenBuffer = 0;
fPrinted = kFALSE;
fImplicitCREsc = 0;
}
TVirtualPS::TVirtualPS(const char *name, Int_t)
: TNamed(name,"Postscript interface")
{
fStream = 0;
fNByte = 0;
fSizBuffer = kMaxBuffer;
fBuffer = new char[fSizBuffer+1];
fLenBuffer = 0;
fPrinted = kFALSE;
fImplicitCREsc = 0;
}
TVirtualPS::~TVirtualPS()
{
if (fBuffer) delete [] fBuffer;
}
void TVirtualPS::PrintStr(const char *str)
{
if (!str || !str[0])
return;
Int_t len = strlen(str);
while (len) {
if (str[0] == '@') {
if (fLenBuffer) {
fStream->write(fBuffer, fLenBuffer);
fNByte += fLenBuffer;
fLenBuffer = 0;
fStream->write("\n", 1);
fNByte++;
fPrinted = kTRUE;
}
len--;
str++;
} else {
Int_t lenText = len;
if (str[len-1] == '@') lenText--;
PrintFast(lenText, str);
len -= lenText;
str += lenText;
}
}
}
void TVirtualPS::PrintFast(Int_t len, const char *str)
{
if (!len || !str) return;
while ((len + fLenBuffer) > kMaxBuffer) {
Int_t nWrite = kMaxBuffer;
if (fImplicitCREsc) {
if (fLenBuffer > 0) nWrite = fLenBuffer;
} else {
if ((len + fLenBuffer) > nWrite) {
while ((nWrite >= fLenBuffer) && (str[nWrite - fLenBuffer] != ' ')) nWrite--;
if (nWrite < fLenBuffer) {
while ((nWrite >= 0) && (fBuffer[nWrite] != ' ')) nWrite--;
}
if (nWrite <= 0) {
nWrite = kMaxBuffer;
}
}
}
if (nWrite >= fLenBuffer) {
if (fLenBuffer > 0) {
fStream->write(fBuffer, fLenBuffer);
fNByte += fLenBuffer;
nWrite -= fLenBuffer;
fLenBuffer = 0;
}
if (nWrite > 0) {
fStream->write(str, nWrite);
len -= nWrite;
str += nWrite;
fNByte += nWrite;
}
} else {
if (nWrite > 0) {
fStream->write(fBuffer, nWrite);
fNByte += nWrite;
memmove(fBuffer, fBuffer + nWrite, fLenBuffer - nWrite);
fBuffer[fLenBuffer - nWrite] = 0;
fLenBuffer -= nWrite;
}
}
if (fImplicitCREsc) {
Int_t crlen = strlen(fImplicitCREsc);
fStream->write(fImplicitCREsc, crlen);
fNByte += crlen;
}
fStream->write("\n",1);
fNByte++;
}
if (len > 0) {
strlcpy(fBuffer + fLenBuffer, str, len+1);
fLenBuffer += len;
fBuffer[fLenBuffer] = 0;
}
fPrinted = kTRUE;
}
void TVirtualPS::WriteInteger(Int_t n, Bool_t space )
{
char str[15];
if (space) {
snprintf(str,15," %d", n);
} else {
snprintf(str,15,"%d", n);
}
PrintStr(str);
}
void TVirtualPS::WriteReal(Float_t z)
{
char str[15];
snprintf(str,15," %g", z);
PrintStr(str);
}