outputgen.cpp 3.05 KB
Newer Older
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1 2
/******************************************************************************
 *
3
 * 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
4
 *
5
 * Copyright (C) 1997-2008 by Dimitri van Heesch.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
6 7 8 9 10 11 12
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
13 14
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
15 16 17 18
 *
 */

#include <stdlib.h>
19 20

#include "qtbc.h"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
21 22
#include "outputgen.h"
#include "message.h"
23
#include "portable.h"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
24 25 26 27 28 29 30 31

OutputGenerator::OutputGenerator()
{
  //printf("OutputGenerator::OutputGenerator()\n");
  file=0;
  b.setBuffer(a);
  b.open( IO_WriteOnly );
  t.setDevice(&b);
32
  t.setEncoding(QTextStream::UnicodeUTF8);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
33
  active=TRUE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
34 35
  genStack = new QStack<bool>;
  genStack->setAutoDelete(TRUE);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
36 37 38 39 40 41
}

OutputGenerator::~OutputGenerator()
{
  //printf("OutputGenerator::~OutputGenerator()\n");
  delete file;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
42
  delete genStack;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
43 44 45 46 47
}

void OutputGenerator::startPlainFile(const char *name)
{
  //printf("startPlainFile(%s)\n",name);
48
  QCString fileName=dir+"/"+name;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  file = new QFile(fileName);
  if (!file)
  {
    err("Could not create file object for %s\n",fileName.data());
    exit(1);
  }
  if (!file->open(IO_WriteOnly))
  {
    err("Could not open file %s for writing\n",fileName.data());
    exit(1);
  }
  fs.setDevice(file);
}

void OutputGenerator::endPlainFile()
{
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  bool converted=false;
  if (!encoding.isEmpty())
  {
    QByteArray enc(a.size()*4);
    void *cd = portable_iconv_open(encoding,"UTF-8");
    if (cd!=(void *)(-1))
    {
      size_t iLeft=a.size();
      size_t oLeft=enc.size();
      const char *inputPtr = a.data();
      char *outputPtr = enc.data();
      if (!portable_iconv(cd, &inputPtr, &iLeft, &outputPtr, &oLeft))
      {
        enc.resize(enc.size()-oLeft);
        postProcess(enc);
        //printf("a.size()=%d enc.size()=%d iLeft=%d oLeft=%d enc2.size()=%d\n",
        //    a.size(),enc.size(),iLeft,oLeft,enc2.size());
        fs.writeRawBytes(enc.data(),enc.size()) ;  // write string buffer to file
        converted=TRUE;
      }
      portable_iconv_close(cd);
    }
  }
  if (!converted)
  {
    //printf("endPlainFile(%s)\n",file->name());
    fs.writeRawBytes(a.data(),a.size()) ;  // write string buffer to file
  }
  b.close();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
94 95 96 97 98 99 100 101
  delete file; 
  file=0;
  a.resize(0);
  b.setBuffer(a);
  b.open(IO_WriteOnly);
  t.setDevice(&b);
}

102
QCString OutputGenerator::getContents() const
Dimitri van Heesch's avatar
Dimitri van Heesch committed
103
{
104
  QCString s;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
105 106 107 108 109 110
  s.resize(a.size()+1);
  memcpy(s.data(),a.data(),a.size());
  s.at(a.size())='\0';
  return s;
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
111 112 113 114 115 116 117
void OutputGenerator::pushGeneratorState()
{
  genStack->push(new bool(isEnabled()));
}

void OutputGenerator::popGeneratorState()
{
118 119 120 121
  bool *lb = genStack->pop();
  ASSERT(lb!=0);
  if (lb==0) return; // for some robustness against superfluous \endhtmlonly commands.
  if (*lb) enable(); else disable();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
122
  delete lb;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
123
}
124