Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Public Member Functions | Private Member Functions | Private Attributes
CsvIm Class Reference

#include <csvim.h>

List of all members.

Public Member Functions

 CsvIm (const QString &fname, const QString &enc, gtWriter *w, const QString &fdelim=",", const QString &vdelim="\"", bool header=false, bool usevdelim=true)
 ~CsvIm ()
void setFieldDelimiter (const QString &fdelim)
void setValueDelimiter (const QString &vdelim)
void write ()

Private Member Functions

void loadFile ()
void parseLine (const QString &line, bool isHeader)
QString toUnicode (const QString &text)
void setupPStyles ()
void setupTabulators ()

Private Attributes

QString fieldDelimiter
QString valueDelimiter
bool hasHeader
bool useVDelim
QString filename
QString encoding
gtWriter * writer
QString header
QString data
int rowNumber
int colIndex
int colCount
gtParagraphStyle * pstyleData
gtParagraphStyle * pstyleHeader

Detailed Description

Definition at line 27 of file csvim.h.


Constructor & Destructor Documentation

CsvIm::CsvIm ( const QString &  fname,
const QString &  enc,
gtWriter *  w,
const QString &  fdelim = ",",
const QString &  vdelim = "\"",
bool  header = false,
bool  usevdelim = true 
)

Definition at line 35 of file csvim.cpp.

{
       fieldDelimiter = fdelim;
       valueDelimiter = vdelim;
       hasHeader = header;
       useVDelim = usevdelim;
       filename = fname;
       encoding = enc;
       writer = w;
       header = "";
       data = "";
       rowNumber = 0;
       colIndex = 0;
       colCount = 0;
       setupPStyles();
       loadFile();
       setupTabulators();
}

Here is the call graph for this function:

Definition at line 230 of file csvim.cpp.

{
       delete pstyleData;
       delete pstyleHeader;
}

Member Function Documentation

void CsvIm::loadFile ( ) [private]

Definition at line 89 of file csvim.cpp.

{
       QString text = "";
       QFile f(filename);
       QFileInfo fi(f);
       if (!fi.exists())
              return;
       uint posi;
       QByteArray bb(f.size());
       if (f.open(IO_ReadOnly))
       {
              f.readBlock(bb.data(), f.size());
              f.close();
              for (posi = 0; posi < bb.size(); ++posi)
                     text += QChar(bb[posi]);
       }
       text = toUnicode(text);
       QStringList lines = QStringList::split("\n", text);
       uint i;
       if (hasHeader)
       {
              colIndex = 0;
              parseLine(lines[0], true);
              header += "\n";
              colCount = colIndex;
              i = 1;
              ++rowNumber;
       }
       else
              i = 0;
       for (uint i2 = i; i2 < lines.size(); ++i2)
       {
              colIndex = 0;
              parseLine(lines[i2], false);
              data += "\n";
              ++rowNumber;
              if (colCount < colIndex)
                     colCount = colIndex;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void CsvIm::parseLine ( const QString &  line,
bool  isHeader 
) [private]

Definition at line 130 of file csvim.cpp.

{
       if ((line.find(valueDelimiter) < 0) || (!useVDelim))
       {
              QStringList l = QStringList::split(fieldDelimiter, line);
              for (uint i = 0; i < l.size(); ++i)
              {
                     ++colIndex;
                     QString tmp = l[i].stripWhiteSpace();
                     if (isHeader)
                            header += "\t" + tmp;
                     else
                            data += "\t" + tmp;
              }
              return; // line done
       }
       
       int vdIndexStart = line.find(valueDelimiter);
       int vdIndexEnd   = line.find(valueDelimiter, vdIndexStart + 1);
       if (vdIndexEnd < 0)
       {
              if (isHeader)
                     header += "\t" + line;
              else
                     data += "\t" + line;
              return; // error in line, no closing valuedelimiter could be found    
       }
       
       int fdIndex = line.find(fieldDelimiter, vdIndexEnd + 1);
       QString tmpCol = "";
       
       if (fdIndex < 0)
       {
              if (vdIndexEnd < 0)
              {
                     if (isHeader)
                            header += "\t" + line;
                     else
                            data += "\t" + line;
              }
              else 
              {
                     tmpCol = line.mid(vdIndexStart + 1, (vdIndexEnd - 1) - vdIndexStart);
                     if (isHeader)
                            header += "\t" + tmpCol;
                     else
                            data += "\t" + tmpCol;
              }
              ++colIndex;
              return; // no more field delimiters left
       }
       
       if (fdIndex < vdIndexStart)
       {
              tmpCol = line.mid(0, fdIndex);
              if (isHeader)
                     header += "\t" + tmpCol;
              else
                     data += "\t" + tmpCol;
              ++colIndex;
              parseLine(line.mid(fdIndex + 1, line.length() - (fdIndex + 1)), isHeader);
       }
       else if (fdIndex > vdIndexEnd)
       {
              tmpCol = line.mid(vdIndexStart + 1, (vdIndexEnd - 1) - vdIndexStart);
              if (isHeader)
                     header += "\t" + tmpCol;
              else
                     data += "\t" + tmpCol;
              ++colIndex;
              parseLine(line.mid(vdIndexEnd + 1, line.length() - (vdIndexEnd + 1)), isHeader);
       }
}

Here is the caller graph for this function:

void CsvIm::setFieldDelimiter ( const QString &  fdelim)

Definition at line 73 of file csvim.cpp.

{
       fieldDelimiter = fdelim;
}
void CsvIm::setupPStyles ( ) [private]

Definition at line 55 of file csvim.cpp.

{
       pstyleData = new gtParagraphStyle(*(writer->getDefaultStyle()));
       pstyleData->setName(writer->getFrameName() + "-" + QObject::tr("CSV_data"));
       if (hasHeader)
       {
              pstyleHeader = new gtParagraphStyle(*pstyleData);
              pstyleHeader->setName(writer->getFrameName() + "-" + QObject::tr("CSV_header"));
              pstyleHeader->setSpaceBelow(7.0);
              int size = pstyleData->getFont()->getSize();
              size += 10;
              pstyleHeader->getFont()->setSize(size);
              pstyleHeader->getFont()->setWeight(BOLD);
       }
       else
              pstyleHeader = NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void CsvIm::setupTabulators ( ) [private]

Definition at line 205 of file csvim.cpp.

{
       double frameWidth = writer->getFrameWidth();
       double addition = frameWidth / (colCount + 1);
       double curValue = addition / 2;
       for (int i = 0; i < colCount; ++i)
       {
              pstyleData->setTabValue(curValue);
              if (pstyleHeader)
                     pstyleHeader->setTabValue(curValue, CENTER_T);
              curValue += addition;
       }
}

Here is the caller graph for this function:

void CsvIm::setValueDelimiter ( const QString &  vdelim)

Definition at line 78 of file csvim.cpp.

{
       valueDelimiter = vdelim;
}
QString CsvIm::toUnicode ( const QString &  text) [private]

Definition at line 219 of file csvim.cpp.

{
       QTextCodec *codec;
       if (encoding.isEmpty())
              codec = QTextCodec::codecForLocale();
       else
              codec = QTextCodec::codecForName(encoding);
       QString dec = codec->toUnicode(text);
       return dec;
}

Here is the caller graph for this function:

void CsvIm::write ( )

Definition at line 83 of file csvim.cpp.

{
       writer->append(header, pstyleHeader);
       writer->append(data, pstyleData);
}

Here is the caller graph for this function:


Member Data Documentation

int CsvIm::colCount [private]

Definition at line 48 of file csvim.h.

int CsvIm::colIndex [private]

Definition at line 47 of file csvim.h.

QString CsvIm::data [private]

Definition at line 45 of file csvim.h.

QString CsvIm::encoding [private]

Definition at line 42 of file csvim.h.

QString CsvIm::fieldDelimiter [private]

Definition at line 37 of file csvim.h.

QString CsvIm::filename [private]

Definition at line 41 of file csvim.h.

Definition at line 39 of file csvim.h.

QString CsvIm::header [private]

Definition at line 44 of file csvim.h.

gtParagraphStyle* CsvIm::pstyleData [private]

Definition at line 49 of file csvim.h.

gtParagraphStyle* CsvIm::pstyleHeader [private]

Definition at line 50 of file csvim.h.

int CsvIm::rowNumber [private]

Definition at line 46 of file csvim.h.

Definition at line 40 of file csvim.h.

QString CsvIm::valueDelimiter [private]

Definition at line 38 of file csvim.h.

gtWriter* CsvIm::writer [private]

Definition at line 43 of file csvim.h.


The documentation for this class was generated from the following files: